blob: f7376ec3074601189a67d04921144e4a7857b71b [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"
Devang Patel83489bb2009-01-13 00:35:13 +000019#include "llvm/CodeGen/DwarfWriter.h"
20#include "llvm/Analysis/DebugInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000021#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng61bbbab2007-08-16 23:50:06 +000022#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000023#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000024#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000026#include "llvm/Target/TargetOptions.h"
Dan Gohman707e0182008-04-12 04:36:06 +000027#include "llvm/Target/TargetSubtarget.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000028#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000029#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000030#include "llvm/DerivedTypes.h"
Devang Patel83489bb2009-01-13 00:35:13 +000031#include "llvm/GlobalVariable.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000032#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000033#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000034#include "llvm/Support/MathExtras.h"
Chris Lattner79715142007-02-03 01:12:36 +000035#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000036#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000037#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000038#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000039using namespace llvm;
40
41//===----------------------------------------------------------------------===//
42/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
43/// hacks on it until the target machine can handle it. This involves
44/// eliminating value sizes the machine cannot handle (promoting small sizes to
45/// large sizes or splitting up large values into small values) as well as
46/// eliminating operations the machine cannot handle.
47///
48/// This code also does a small amount of optimization and recognition of idioms
49/// as part of its processing. For example, if a target does not support a
50/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
51/// will attempt merge setcc and brc instructions into brcc's.
52///
53namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000054class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000055 TargetLowering &TLI;
56 SelectionDAG &DAG;
Duncan Sandsb6862bb2008-12-14 09:43:15 +000057 bool TypesNeedLegalizing;
Chris Lattner3e928bb2005-01-07 07:47:09 +000058
Chris Lattner6831a812006-02-13 09:18:02 +000059 // Libcall insertion helpers.
60
61 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
62 /// legalized. We use this to ensure that calls are properly serialized
63 /// against each other, including inserted libcalls.
Dan Gohman475871a2008-07-27 21:46:04 +000064 SDValue LastCALLSEQ_END;
Chris Lattner6831a812006-02-13 09:18:02 +000065
66 /// IsLegalizingCall - This member is used *only* for purposes of providing
67 /// helpful assertions that a libcall isn't created while another call is
68 /// being legalized (which could lead to non-serialized call sequences).
69 bool IsLegalizingCall;
70
Chris Lattner3e928bb2005-01-07 07:47:09 +000071 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000072 Legal, // The target natively supports this operation.
73 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000074 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000075 };
Chris Lattner6831a812006-02-13 09:18:02 +000076
Chris Lattner3e928bb2005-01-07 07:47:09 +000077 /// ValueTypeActions - This is a bitvector that contains two bits for each
78 /// value type, where the two bits correspond to the LegalizeAction enum.
79 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000080 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000081
Chris Lattner3e928bb2005-01-07 07:47:09 +000082 /// LegalizedNodes - For nodes that are of legal width, and that have more
83 /// than one use, this map indicates what regularized operand to use. This
84 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000085 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000086
Chris Lattner03c85462005-01-15 05:21:40 +000087 /// PromotedNodes - For nodes that are below legal width, and that have more
88 /// than one use, this map indicates what promoted value to use. This allows
89 /// us to avoid promoting the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000090 DenseMap<SDValue, SDValue> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000091
Chris Lattnerc7029802006-03-18 01:44:44 +000092 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +000093 /// which operands are the expanded version of the input. This allows
Chris Lattnerc7029802006-03-18 01:44:44 +000094 /// us to avoid expanding the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000095 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000096
Chris Lattnerc7029802006-03-18 01:44:44 +000097 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +000098 /// which operands are the split version of the input. This allows us
Chris Lattnerc7029802006-03-18 01:44:44 +000099 /// to avoid splitting the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +0000100 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000101
Dan Gohman7f321562007-06-25 16:23:39 +0000102 /// ScalarizedNodes - For nodes that need to be converted from vector types to
103 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000104 /// processed to the result.
Dan Gohman475871a2008-07-27 21:46:04 +0000105 std::map<SDValue, SDValue> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000106
Mon P Wangf007a8b2008-11-06 05:31:54 +0000107 /// WidenNodes - For nodes that need to be widened from one vector type to
108 /// another, this contains the mapping of those that we have already widen.
109 /// This allows us to avoid widening more than once.
Mon P Wang0c397192008-10-30 08:01:45 +0000110 std::map<SDValue, SDValue> WidenNodes;
111
Dan Gohman475871a2008-07-27 21:46:04 +0000112 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000113 LegalizedNodes.insert(std::make_pair(From, To));
114 // If someone requests legalization of the new node, return itself.
115 if (From != To)
116 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000117 }
Dan Gohman475871a2008-07-27 21:46:04 +0000118 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman6b345ee2008-07-07 17:46:23 +0000119 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Chris Lattner03c85462005-01-15 05:21:40 +0000120 assert(isNew && "Got into the map somehow?");
Evan Cheng24ac4082008-11-24 07:09:49 +0000121 isNew = isNew;
Chris Lattner69a889e2005-12-20 00:53:54 +0000122 // If someone requests legalization of the new node, return itself.
123 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000124 }
Mon P Wangf007a8b2008-11-06 05:31:54 +0000125 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang0c397192008-10-30 08:01:45 +0000126 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
127 assert(isNew && "Got into the map somehow?");
Evan Cheng24ac4082008-11-24 07:09:49 +0000128 isNew = isNew;
Mon P Wang0c397192008-10-30 08:01:45 +0000129 // If someone requests legalization of the new node, return itself.
130 LegalizedNodes.insert(std::make_pair(To, To));
131 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000132
Chris Lattner3e928bb2005-01-07 07:47:09 +0000133public:
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000134 explicit SelectionDAGLegalize(SelectionDAG &DAG, bool TypesNeedLegalizing);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000135
Chris Lattner3e928bb2005-01-07 07:47:09 +0000136 /// getTypeAction - Return how we should legalize values of this type, either
137 /// it is already legal or we need to expand it into multiple registers of
138 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000139 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000140 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000141 }
142
143 /// isTypeLegal - Return true if this type is legal on this target.
144 ///
Duncan Sands83ec4b62008-06-06 12:08:01 +0000145 bool isTypeLegal(MVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000146 return getTypeAction(VT) == Legal;
147 }
148
Chris Lattner3e928bb2005-01-07 07:47:09 +0000149 void LegalizeDAG();
150
Chris Lattner456a93a2006-01-28 07:39:30 +0000151private:
Dan Gohman7f321562007-06-25 16:23:39 +0000152 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000153 /// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000154 void HandleOp(SDValue Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000155
156 /// LegalizeOp - We know that the specified value has a legal type.
157 /// Recursively ensure that the operands have legal types, then return the
158 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000159 SDValue LegalizeOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000160
Dan Gohman82669522007-10-11 23:57:53 +0000161 /// UnrollVectorOp - We know that the given vector has a legal type, however
162 /// the operation it performs is not legal and is an operation that we have
163 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
164 /// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000165 SDValue UnrollVectorOp(SDValue O);
Nate Begeman68679912008-04-25 18:07:40 +0000166
167 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
168 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
169 /// is necessary to spill the vector being inserted into to memory, perform
170 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000171 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
172 SDValue Idx);
Dan Gohman82669522007-10-11 23:57:53 +0000173
Chris Lattnerc7029802006-03-18 01:44:44 +0000174 /// PromoteOp - Given an operation that produces a value in an invalid type,
175 /// promote it to compute the value into a larger type. The produced value
176 /// will have the correct bits for the low portion of the register, but no
177 /// guarantee is made about the top bits: it may be zero, sign-extended, or
178 /// garbage.
Dan Gohman475871a2008-07-27 21:46:04 +0000179 SDValue PromoteOp(SDValue O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000180
Dan Gohman475871a2008-07-27 21:46:04 +0000181 /// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattnerc7029802006-03-18 01:44:44 +0000182 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman929d3eb2008-10-01 15:07:49 +0000183 /// the LegalizedNodes map is filled in for any results that are not expanded,
Chris Lattnerc7029802006-03-18 01:44:44 +0000184 /// the ExpandedNodes map is filled in for any results that are expanded, and
185 /// the Lo/Hi values are returned. This applies to integer types and Vector
186 /// types.
Dan Gohman475871a2008-07-27 21:46:04 +0000187 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000188
Mon P Wangf007a8b2008-11-06 05:31:54 +0000189 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
190 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
191 /// for the existing elements but no guarantee is made about the new elements
192 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
193 /// when we have an instruction operating on an illegal vector type and we
194 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang0c397192008-10-30 08:01:45 +0000195 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
196
Dan Gohman7f321562007-06-25 16:23:39 +0000197 /// SplitVectorOp - Given an operand of vector type, break it down into
198 /// two smaller values.
Dan Gohman475871a2008-07-27 21:46:04 +0000199 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000200
Dan Gohman89b20c02007-06-27 14:06:22 +0000201 /// ScalarizeVectorOp - Given an operand of single-element vector type
202 /// (e.g. v1f32), convert it into the equivalent operation that returns a
203 /// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +0000204 SDValue ScalarizeVectorOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000205
Mon P Wangf007a8b2008-11-06 05:31:54 +0000206 /// Useful 16 element vector type that is used to pass operands for widening.
Mon P Wang0c397192008-10-30 08:01:45 +0000207 typedef SmallVector<SDValue, 16> SDValueVector;
208
209 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
210 /// the LdChain contains a single load and false if it contains a token
211 /// factor for multiple loads. It takes
212 /// Result: location to return the result
213 /// LdChain: location to return the load chain
214 /// Op: load operation to widen
215 /// NVT: widen vector result type we want for the load
216 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
217 SDValue Op, MVT NVT);
218
219 /// Helper genWidenVectorLoads - Helper function to generate a set of
220 /// loads to load a vector with a resulting wider type. It takes
221 /// LdChain: list of chains for the load we have generated
222 /// Chain: incoming chain for the ld vector
223 /// BasePtr: base pointer to load from
224 /// SV: memory disambiguation source value
225 /// SVOffset: memory disambiugation offset
226 /// Alignment: alignment of the memory
227 /// isVolatile: volatile load
228 /// LdWidth: width of memory that we want to load
229 /// ResType: the wider result result type for the resulting loaded vector
230 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
231 SDValue BasePtr, const Value *SV,
232 int SVOffset, unsigned Alignment,
233 bool isVolatile, unsigned LdWidth,
234 MVT ResType);
235
236 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
237 /// location. It takes
238 /// ST: store node that we want to replace
239 /// Chain: incoming store chain
240 /// BasePtr: base address of where we want to store into
241 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
242 SDValue BasePtr);
243
244 /// Helper genWidenVectorStores - Helper function to generate a set of
245 /// stores to store a widen vector into non widen memory
246 // It takes
247 // StChain: list of chains for the stores we have generated
248 // Chain: incoming chain for the ld vector
249 // BasePtr: base pointer to load from
250 // SV: memory disambiguation source value
251 // SVOffset: memory disambiugation offset
252 // Alignment: alignment of the memory
253 // isVolatile: volatile lod
254 // ValOp: value to store
255 // StWidth: width of memory that we want to store
256 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
257 SDValue BasePtr, const Value *SV,
258 int SVOffset, unsigned Alignment,
259 bool isVolatile, SDValue ValOp,
260 unsigned StWidth);
261
Duncan Sandsd038e042008-07-21 10:20:31 +0000262 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Chris Lattner4352cc92006-04-04 17:23:26 +0000263 /// specified mask and type. Targets can specify exactly which masks they
264 /// support and the code generator is tasked with not creating illegal masks.
265 ///
266 /// Note that this will also return true for shuffles that are promoted to a
267 /// different type.
268 ///
269 /// If this is a legal shuffle, this method returns the (possibly promoted)
270 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman475871a2008-07-27 21:46:04 +0000271 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Chris Lattner4352cc92006-04-04 17:23:26 +0000272
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000273 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000274 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000275
Dan Gohman475871a2008-07-27 21:46:04 +0000276 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Evan Cheng7f042682008-10-15 02:05:31 +0000277 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC);
278 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC) {
279 LegalizeSetCCOperands(LHS, RHS, CC);
280 LegalizeSetCCCondCode(VT, LHS, RHS, CC);
281 }
Nate Begeman750ac1b2006-02-01 07:19:44 +0000282
Dan Gohman475871a2008-07-27 21:46:04 +0000283 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
284 SDValue &Hi);
Dale Johannesenaf435272009-02-02 19:03:57 +0000285 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source, DebugLoc dl);
Chris Lattnercad063f2005-07-16 00:19:57 +0000286
Dan Gohman475871a2008-07-27 21:46:04 +0000287 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
288 SDValue ExpandBUILD_VECTOR(SDNode *Node);
289 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dale Johannesenaf435272009-02-02 19:03:57 +0000290 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy,
291 SDValue Op, DebugLoc dl);
292 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT,
293 DebugLoc dl);
294 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned,
295 DebugLoc dl);
296 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned,
297 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000298
Dan Gohman475871a2008-07-27 21:46:04 +0000299 SDValue ExpandBSWAP(SDValue Op);
300 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
301 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
302 SDValue &Lo, SDValue &Hi);
303 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
304 SDValue &Lo, SDValue &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000305
Dan Gohman475871a2008-07-27 21:46:04 +0000306 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
307 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000308};
309}
310
Chris Lattner4352cc92006-04-04 17:23:26 +0000311/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
312/// specified mask and type. Targets can specify exactly which masks they
313/// support and the code generator is tasked with not creating illegal masks.
314///
315/// Note that this will also return true for shuffles that are promoted to a
316/// different type.
Dan Gohman475871a2008-07-27 21:46:04 +0000317SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Chris Lattner4352cc92006-04-04 17:23:26 +0000318 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
319 default: return 0;
320 case TargetLowering::Legal:
321 case TargetLowering::Custom:
322 break;
323 case TargetLowering::Promote: {
324 // If this is promoted to a different type, convert the shuffle mask and
325 // ask if it is legal in the promoted type!
Duncan Sands83ec4b62008-06-06 12:08:01 +0000326 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd038e042008-07-21 10:20:31 +0000327 MVT EltVT = NVT.getVectorElementType();
Chris Lattner4352cc92006-04-04 17:23:26 +0000328
329 // If we changed # elements, change the shuffle mask.
330 unsigned NumEltsGrowth =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000331 NVT.getVectorNumElements() / VT.getVectorNumElements();
Chris Lattner4352cc92006-04-04 17:23:26 +0000332 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
333 if (NumEltsGrowth > 1) {
334 // Renumber the elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000335 SmallVector<SDValue, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000336 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000337 SDValue InOp = Mask.getOperand(i);
Chris Lattner4352cc92006-04-04 17:23:26 +0000338 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
339 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd038e042008-07-21 10:20:31 +0000340 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000341 else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000342 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd038e042008-07-21 10:20:31 +0000343 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000344 }
345 }
346 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000347 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000348 }
349 VT = NVT;
350 break;
351 }
352 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000353 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Chris Lattner4352cc92006-04-04 17:23:26 +0000354}
355
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000356SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag, bool types)
357 : TLI(dag.getTargetLoweringInfo()), DAG(dag), TypesNeedLegalizing(types),
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000358 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000359 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000360 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000361}
362
Chris Lattner3e928bb2005-01-07 07:47:09 +0000363void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000364 LastCALLSEQ_END = DAG.getEntryNode();
365 IsLegalizingCall = false;
366
Chris Lattnerab510a72005-10-02 17:49:46 +0000367 // The legalize process is inherently a bottom-up recursive process (users
368 // legalize their uses before themselves). Given infinite stack space, we
369 // could just start legalizing on the root and traverse the whole graph. In
370 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000371 // blocks. To avoid this problem, compute an ordering of the nodes where each
372 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000373 DAG.AssignTopologicalOrder();
374 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
375 E = prior(DAG.allnodes_end()); I != next(E); ++I)
376 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000377
378 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000379 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000380 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
381 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000382
383 ExpandedNodes.clear();
384 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000385 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000386 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000387 ScalarizedNodes.clear();
Mon P Wang0c397192008-10-30 08:01:45 +0000388 WidenNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000389
390 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000391 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000392}
393
Chris Lattner6831a812006-02-13 09:18:02 +0000394
395/// FindCallEndFromCallStart - Given a chained node that is part of a call
396/// sequence, find the CALLSEQ_END node that terminates the call sequence.
397static SDNode *FindCallEndFromCallStart(SDNode *Node) {
398 if (Node->getOpcode() == ISD::CALLSEQ_END)
399 return Node;
400 if (Node->use_empty())
401 return 0; // No CallSeqEnd
402
403 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000404 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000405 if (TheChain.getValueType() != MVT::Other) {
406 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000407 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000408 if (TheChain.getValueType() != MVT::Other) {
409 // Otherwise, hunt for it.
410 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
411 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000412 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000413 break;
414 }
415
416 // Otherwise, we walked into a node without a chain.
417 if (TheChain.getValueType() != MVT::Other)
418 return 0;
419 }
420 }
421
422 for (SDNode::use_iterator UI = Node->use_begin(),
423 E = Node->use_end(); UI != E; ++UI) {
424
425 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000426 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000427 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
428 if (User->getOperand(i) == TheChain)
429 if (SDNode *Result = FindCallEndFromCallStart(User))
430 return Result;
431 }
432 return 0;
433}
434
435/// FindCallStartFromCallEnd - Given a chained node that is part of a call
436/// sequence, find the CALLSEQ_START node that initiates the call sequence.
437static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
438 assert(Node && "Didn't find callseq_start for a call??");
439 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
440
441 assert(Node->getOperand(0).getValueType() == MVT::Other &&
442 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000443 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000444}
445
446/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
447/// see if any uses can reach Dest. If no dest operands can get to dest,
448/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000449///
450/// Keep track of the nodes we fine that actually do lead to Dest in
451/// NodesLeadingTo. This avoids retraversing them exponential number of times.
452///
453bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000454 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000455 if (N == Dest) return true; // N certainly leads to Dest :)
456
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000457 // If we've already processed this node and it does lead to Dest, there is no
458 // need to reprocess it.
459 if (NodesLeadingTo.count(N)) return true;
460
Chris Lattner6831a812006-02-13 09:18:02 +0000461 // If the first result of this node has been already legalized, then it cannot
462 // reach N.
463 switch (getTypeAction(N->getValueType(0))) {
464 case Legal:
Dan Gohman475871a2008-07-27 21:46:04 +0000465 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000466 break;
467 case Promote:
Dan Gohman475871a2008-07-27 21:46:04 +0000468 if (PromotedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000469 break;
470 case Expand:
Dan Gohman475871a2008-07-27 21:46:04 +0000471 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000472 break;
473 }
474
475 // Okay, this node has not already been legalized. Check and legalize all
476 // operands. If none lead to Dest, then we can legalize this node.
477 bool OperandsLeadToDest = false;
478 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
479 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000480 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000481
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000482 if (OperandsLeadToDest) {
483 NodesLeadingTo.insert(N);
484 return true;
485 }
Chris Lattner6831a812006-02-13 09:18:02 +0000486
487 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000488 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000489 return false;
490}
491
Mon P Wang0c397192008-10-30 08:01:45 +0000492/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000493/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000494void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000495 MVT VT = Op.getValueType();
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000496 // If the type legalizer was run then we should never see any illegal result
497 // types here except for target constants (the type legalizer does not touch
Mon P Wang87c8a8f2008-12-18 20:03:17 +0000498 // those) or for build vector used as a mask for a vector shuffle.
499 // FIXME: We can removed the BUILD_VECTOR case when we fix PR2957.
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000500 assert((TypesNeedLegalizing || getTypeAction(VT) == Legal ||
Mon P Wang87c8a8f2008-12-18 20:03:17 +0000501 Op.getOpcode() == ISD::TargetConstant ||
502 Op.getOpcode() == ISD::BUILD_VECTOR) &&
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000503 "Illegal type introduced after type legalization?");
Dan Gohman7f321562007-06-25 16:23:39 +0000504 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000505 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000506 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang0c397192008-10-30 08:01:45 +0000507 case Promote:
508 if (!VT.isVector()) {
509 (void)PromoteOp(Op);
510 break;
511 }
512 else {
513 // See if we can widen otherwise use Expand to either scalarize or split
514 MVT WidenVT = TLI.getWidenVectorType(VT);
515 if (WidenVT != MVT::Other) {
516 (void) WidenVectorOp(Op, WidenVT);
517 break;
518 }
519 // else fall thru to expand since we can't widen the vector
520 }
Chris Lattnerc7029802006-03-18 01:44:44 +0000521 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000522 if (!VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000523 // If this is an illegal scalar, expand it into its two component
524 // pieces.
Dan Gohman475871a2008-07-27 21:46:04 +0000525 SDValue X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000526 if (Op.getOpcode() == ISD::TargetConstant)
527 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000528 ExpandOp(Op, X, Y);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000529 } else if (VT.getVectorNumElements() == 1) {
Dan Gohman7f321562007-06-25 16:23:39 +0000530 // If this is an illegal single element vector, convert it to a
531 // scalar operation.
532 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000533 } else {
Mon P Wang0c397192008-10-30 08:01:45 +0000534 // This is an illegal multiple element vector.
Dan Gohman7f321562007-06-25 16:23:39 +0000535 // Split it in half and legalize both parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000536 SDValue X, Y;
Dan Gohman7f321562007-06-25 16:23:39 +0000537 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000538 }
539 break;
540 }
541}
Chris Lattner6831a812006-02-13 09:18:02 +0000542
Evan Cheng9f877882006-12-13 20:57:08 +0000543/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
544/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000545static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000546 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000547 bool Extend = false;
548
549 // If a FP immediate is precise when represented as a float and if the
550 // target can do an extending load from float to double, we put it into
551 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000552 // double. This shrinks FP constants and canonicalizes them for targets where
553 // an FP extending load is the same cost as a normal load (such as on the x87
554 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000555 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000556 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000557 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000558 if (VT!=MVT::f64 && VT!=MVT::f32)
559 assert(0 && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000560 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000561 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000562 }
563
Duncan Sands83ec4b62008-06-06 12:08:01 +0000564 MVT OrigVT = VT;
565 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000566 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000567 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000568 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
569 // Only do this if the target has a native EXTLOAD instruction from
570 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000571 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000572 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000573 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000574 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
575 VT = SVT;
576 Extend = true;
577 }
Evan Cheng00495212006-12-12 21:32:44 +0000578 }
579
Dan Gohman475871a2008-07-27 21:46:04 +0000580 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +0000581 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000582 if (Extend)
583 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000584 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000585 0, VT, false, Alignment);
Evan Chengef120572008-03-04 08:05:30 +0000586 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000587 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000588}
589
Chris Lattner6831a812006-02-13 09:18:02 +0000590
Evan Cheng912095b2007-01-04 21:56:39 +0000591/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
592/// operations.
593static
Dan Gohman475871a2008-07-27 21:46:04 +0000594SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
Dan Gohman0d137d72009-01-15 16:43:02 +0000595 SelectionDAG &DAG,
596 const TargetLowering &TLI) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000597 MVT VT = Node->getValueType(0);
598 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000599 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
600 "fcopysign expansion only supported for f32 and f64");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000601 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000602
Evan Cheng912095b2007-01-04 21:56:39 +0000603 // First get the sign bit of second operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000604 SDValue Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000605 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
606 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000607 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman475871a2008-07-27 21:46:04 +0000608 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000609 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000610 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000611 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Evan Cheng912095b2007-01-04 21:56:39 +0000612 if (SizeDiff > 0) {
613 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
614 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
615 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnerc563e1d2008-07-10 23:46:13 +0000616 } else if (SizeDiff < 0) {
617 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
618 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
619 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
620 }
Evan Cheng068c5f42007-01-05 21:31:51 +0000621
622 // Clear the sign bit of first operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000623 SDValue Mask2 = (VT == MVT::f64)
Evan Cheng068c5f42007-01-05 21:31:51 +0000624 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
625 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
626 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman475871a2008-07-27 21:46:04 +0000627 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000628 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
629
630 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000631 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
632 return Result;
633}
634
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000635/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
636static
Dan Gohman475871a2008-07-27 21:46:04 +0000637SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000638 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000639 SDValue Chain = ST->getChain();
640 SDValue Ptr = ST->getBasePtr();
641 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000642 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000643 int Alignment = ST->getAlignment();
644 int SVOffset = ST->getSrcValueOffset();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000645 if (ST->getMemoryVT().isFloatingPoint() ||
646 ST->getMemoryVT().isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000647 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
648 if (TLI.isTypeLegal(intVT)) {
649 // Expand to a bitconvert of the value to the integer type of the
650 // same size, then a (misaligned) int store.
651 // FIXME: Does not handle truncating floating point stores!
652 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
653 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
654 SVOffset, ST->isVolatile(), Alignment);
655 } else {
656 // Do a (aligned) store to a stack slot, then copy from the stack slot
657 // to the final destination using (unaligned) integer loads and stores.
658 MVT StoredVT = ST->getMemoryVT();
659 MVT RegVT =
660 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
661 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
662 unsigned RegBytes = RegVT.getSizeInBits() / 8;
663 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000664
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000665 // Make sure the stack slot is also aligned for the register type.
666 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
667
668 // Perform the original store, only redirected to the stack slot.
Duncan Sands05e11fa2008-12-12 21:47:02 +0000669 SDValue Store = DAG.getTruncStore(Chain, Val, StackPtr, NULL, 0,StoredVT);
670 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
671 SmallVector<SDValue, 8> Stores;
672 unsigned Offset = 0;
673
674 // Do all but one copies using the full register width.
675 for (unsigned i = 1; i < NumRegs; i++) {
676 // Load one integer register's worth from the stack slot.
677 SDValue Load = DAG.getLoad(RegVT, Store, StackPtr, NULL, 0);
678 // Store it to the final location. Remember the store.
679 Stores.push_back(DAG.getStore(Load.getValue(1), Load, Ptr,
680 ST->getSrcValue(), SVOffset + Offset,
681 ST->isVolatile(),
682 MinAlign(ST->getAlignment(), Offset)));
683 // Increment the pointers.
684 Offset += RegBytes;
685 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
686 Increment);
687 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
688 }
689
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000690 // The last store may be partial. Do a truncating store. On big-endian
691 // machines this requires an extending load from the stack slot to ensure
692 // that the bits are in the right place.
693 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000694
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000695 // Load from the stack slot.
696 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Store, StackPtr,
697 NULL, 0, MemVT);
698
Duncan Sands05e11fa2008-12-12 21:47:02 +0000699 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, Ptr,
700 ST->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000701 MemVT, ST->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000702 MinAlign(ST->getAlignment(), Offset)));
703 // The order of the stores doesn't matter - say it with a TokenFactor.
704 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
705 Stores.size());
706 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000707 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000708 assert(ST->getMemoryVT().isInteger() &&
709 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000710 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000711 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000712 MVT NewStoredVT =
713 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
714 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000715 int IncrementSize = NumBits / 8;
716
717 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000718 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
719 SDValue Lo = Val;
720 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000721
722 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000723 SDValue Store1, Store2;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000724 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
725 ST->getSrcValue(), SVOffset, NewStoredVT,
726 ST->isVolatile(), Alignment);
727 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
728 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000729 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000730 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
731 ST->getSrcValue(), SVOffset + IncrementSize,
732 NewStoredVT, ST->isVolatile(), Alignment);
733
734 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
735}
736
737/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
738static
Dan Gohman475871a2008-07-27 21:46:04 +0000739SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000740 const TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000741 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000742 SDValue Chain = LD->getChain();
743 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000744 MVT VT = LD->getValueType(0);
745 MVT LoadedVT = LD->getMemoryVT();
746 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000747 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
748 if (TLI.isTypeLegal(intVT)) {
749 // Expand to a (misaligned) integer load of the same size,
750 // then bitconvert to floating point or vector.
751 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
752 SVOffset, LD->isVolatile(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000753 LD->getAlignment());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000754 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
755 if (VT.isFloatingPoint() && LoadedVT != VT)
756 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000757
Duncan Sands05e11fa2008-12-12 21:47:02 +0000758 SDValue Ops[] = { Result, Chain };
759 return DAG.getMergeValues(Ops, 2);
760 } else {
761 // Copy the value to a (aligned) stack slot using (unaligned) integer
762 // loads and stores, then do a (aligned) load from the stack slot.
763 MVT RegVT = TLI.getRegisterType(intVT);
764 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
765 unsigned RegBytes = RegVT.getSizeInBits() / 8;
766 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
767
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000768 // Make sure the stack slot is also aligned for the register type.
769 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
770
Duncan Sands05e11fa2008-12-12 21:47:02 +0000771 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
772 SmallVector<SDValue, 8> Stores;
773 SDValue StackPtr = StackBase;
774 unsigned Offset = 0;
775
776 // Do all but one copies using the full register width.
777 for (unsigned i = 1; i < NumRegs; i++) {
778 // Load one integer register's worth from the original location.
779 SDValue Load = DAG.getLoad(RegVT, Chain, Ptr, LD->getSrcValue(),
780 SVOffset + Offset, LD->isVolatile(),
781 MinAlign(LD->getAlignment(), Offset));
782 // Follow the load with a store to the stack slot. Remember the store.
783 Stores.push_back(DAG.getStore(Load.getValue(1), Load, StackPtr,
784 NULL, 0));
785 // Increment the pointers.
786 Offset += RegBytes;
787 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
788 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
789 Increment);
790 }
791
792 // The last copy may be partial. Do an extending load.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000793 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000794 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Chain, Ptr,
795 LD->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000796 MemVT, LD->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000797 MinAlign(LD->getAlignment(), Offset));
798 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000799 // On big-endian machines this requires a truncating store to ensure
800 // that the bits end up in the right place.
801 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, StackPtr,
802 NULL, 0, MemVT));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000803
804 // The order of the stores doesn't matter - say it with a TokenFactor.
805 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
806 Stores.size());
807
808 // Finally, perform the original load only redirected to the stack slot.
809 Load = DAG.getExtLoad(LD->getExtensionType(), VT, TF, StackBase,
810 NULL, 0, LoadedVT);
811
812 // Callers expect a MERGE_VALUES node.
813 SDValue Ops[] = { Load, TF };
814 return DAG.getMergeValues(Ops, 2);
815 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000816 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000817 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000818 "Unaligned load of unsupported type.");
819
Dale Johannesen8155d642008-02-27 22:36:00 +0000820 // Compute the new VT that is half the size of the old one. This is an
821 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000822 unsigned NumBits = LoadedVT.getSizeInBits();
823 MVT NewLoadedVT;
824 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000825 NumBits >>= 1;
826
827 unsigned Alignment = LD->getAlignment();
828 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000829 ISD::LoadExtType HiExtType = LD->getExtensionType();
830
831 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
832 if (HiExtType == ISD::NON_EXTLOAD)
833 HiExtType = ISD::ZEXTLOAD;
834
835 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000836 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000837 if (TLI.isLittleEndian()) {
838 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
839 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
840 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
841 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
842 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
843 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000844 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000845 } else {
846 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
847 NewLoadedVT,LD->isVolatile(), Alignment);
848 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
849 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
850 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
851 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000852 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000853 }
854
855 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000856 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
857 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000858 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
859
Dan Gohman475871a2008-07-27 21:46:04 +0000860 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000861 Hi.getValue(1));
862
Dan Gohman475871a2008-07-27 21:46:04 +0000863 SDValue Ops[] = { Result, TF };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000864 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000865}
Evan Cheng912095b2007-01-04 21:56:39 +0000866
Dan Gohman82669522007-10-11 23:57:53 +0000867/// UnrollVectorOp - We know that the given vector has a legal type, however
868/// the operation it performs is not legal and is an operation that we have
869/// no way of lowering. "Unroll" the vector, splitting out the scalars and
870/// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000871SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000872 MVT VT = Op.getValueType();
Dan Gohman82669522007-10-11 23:57:53 +0000873 assert(isTypeLegal(VT) &&
874 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000875 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman82669522007-10-11 23:57:53 +0000876 "Can't unroll a vector with multiple results!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000877 unsigned NE = VT.getVectorNumElements();
878 MVT EltVT = VT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000879
Dan Gohman475871a2008-07-27 21:46:04 +0000880 SmallVector<SDValue, 8> Scalars;
881 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman82669522007-10-11 23:57:53 +0000882 for (unsigned i = 0; i != NE; ++i) {
883 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +0000884 SDValue Operand = Op.getOperand(j);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000885 MVT OperandVT = Operand.getValueType();
886 if (OperandVT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +0000887 // A vector operand; extract a single element.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000888 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000889 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
890 OperandEltVT,
891 Operand,
892 DAG.getConstant(i, MVT::i32));
893 } else {
894 // A scalar operand; just use it as is.
895 Operands[j] = Operand;
896 }
897 }
Mon P Wange9f10152008-12-09 05:46:39 +0000898
899 switch (Op.getOpcode()) {
900 default:
901 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
902 &Operands[0], Operands.size()));
903 break;
904 case ISD::SHL:
905 case ISD::SRA:
906 case ISD::SRL:
Duncan Sands92abc622009-01-31 15:50:11 +0000907 case ISD::ROTL:
908 case ISD::ROTR:
Mon P Wange9f10152008-12-09 05:46:39 +0000909 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT, Operands[0],
Duncan Sands92abc622009-01-31 15:50:11 +0000910 DAG.getShiftAmountOperand(Operands[1])));
Mon P Wange9f10152008-12-09 05:46:39 +0000911 break;
912 }
Dan Gohman82669522007-10-11 23:57:53 +0000913 }
914
915 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
916}
917
Duncan Sands007f9842008-01-10 10:28:30 +0000918/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000919static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000920 RTLIB::Libcall Call_F32,
921 RTLIB::Libcall Call_F64,
922 RTLIB::Libcall Call_F80,
923 RTLIB::Libcall Call_PPCF128) {
924 return
925 VT == MVT::f32 ? Call_F32 :
926 VT == MVT::f64 ? Call_F64 :
927 VT == MVT::f80 ? Call_F80 :
928 VT == MVT::ppcf128 ? Call_PPCF128 :
929 RTLIB::UNKNOWN_LIBCALL;
930}
931
Nate Begeman68679912008-04-25 18:07:40 +0000932/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
933/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
934/// is necessary to spill the vector being inserted into to memory, perform
935/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000936SDValue SelectionDAGLegalize::
937PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
938 SDValue Tmp1 = Vec;
939 SDValue Tmp2 = Val;
940 SDValue Tmp3 = Idx;
Nate Begeman68679912008-04-25 18:07:40 +0000941
942 // If the target doesn't support this, we have to spill the input vector
943 // to a temporary stack slot, update the element, then reload it. This is
944 // badness. We could also load the value into a vector register (either
945 // with a "move to register" or "extload into register" instruction, then
946 // permute it into place, if the idx is a constant and if the idx is
947 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000948 MVT VT = Tmp1.getValueType();
949 MVT EltVT = VT.getVectorElementType();
950 MVT IdxVT = Tmp3.getValueType();
951 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000952 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000953
Gabor Greifba36cb52008-08-28 21:40:38 +0000954 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000955
956 // Store the vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000957 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000958 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000959
960 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000961 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman68679912008-04-25 18:07:40 +0000962 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
963 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000964 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman68679912008-04-25 18:07:40 +0000965 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman475871a2008-07-27 21:46:04 +0000966 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000967 // Store the scalar value.
968 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000969 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000970 // Load the updated vector.
Dan Gohmana54cf172008-07-11 22:44:52 +0000971 return DAG.getLoad(VT, Ch, StackPtr,
972 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000973}
974
Mon P Wange9f10152008-12-09 05:46:39 +0000975
Dan Gohmana3466152007-07-13 20:14:11 +0000976/// LegalizeOp - We know that the specified value has a legal type, and
977/// that its operands are legal. Now ensure that the operation itself
978/// is legal, recursively ensuring that the operands' operations remain
979/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000980SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000981 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
982 return Op;
983
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000984 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000985 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000986 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000987 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000988
Chris Lattner3e928bb2005-01-07 07:47:09 +0000989 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000990 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000991 if (Node->getNumValues() > 1) {
992 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000993 if (getTypeAction(Node->getValueType(i)) != Legal) {
994 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000995 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000996 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000997 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000998 }
999 }
1000
Chris Lattner45982da2005-05-12 16:53:42 +00001001 // Note that LegalizeOp may be reentered even from single-use nodes, which
1002 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00001003 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001004 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001005
Dan Gohman475871a2008-07-27 21:46:04 +00001006 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
1007 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +00001008 bool isCustom = false;
1009
Chris Lattner3e928bb2005-01-07 07:47:09 +00001010 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +00001011 case ISD::FrameIndex:
1012 case ISD::EntryToken:
1013 case ISD::Register:
1014 case ISD::BasicBlock:
1015 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +00001016 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +00001017 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +00001018 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +00001019 case ISD::TargetConstantPool:
1020 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001021 case ISD::TargetGlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001022 case ISD::TargetExternalSymbol:
Chris Lattner948c1b12006-01-28 08:31:04 +00001023 case ISD::VALUETYPE:
1024 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +00001025 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +00001026 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +00001027 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +00001028 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00001029 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +00001030 "This must be legal!");
1031 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001032 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001033 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1034 // If this is a target node, legalize it by legalizing the operands then
1035 // passing it through.
Dan Gohman475871a2008-07-27 21:46:04 +00001036 SmallVector<SDValue, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001037 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001038 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001039
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001040 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001041
1042 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1043 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001044 return Result.getValue(Op.getResNo());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001045 }
1046 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001047#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00001048 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001049#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00001050 assert(0 && "Do not know how to legalize this operator!");
1051 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +00001052 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001053 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001054 case ISD::GlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001055 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +00001056 case ISD::ConstantPool:
1057 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001058 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1059 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +00001060 case TargetLowering::Custom:
1061 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001062 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner948c1b12006-01-28 08:31:04 +00001063 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001064 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001065 break;
1066 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001067 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +00001068 case ISD::FRAMEADDR:
1069 case ISD::RETURNADDR:
1070 // The only option for these nodes is to custom lower them. If the target
1071 // does not custom lower them, then return zero.
1072 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001073 if (Tmp1.getNode())
Nate Begemanbcc5f362007-01-29 22:58:52 +00001074 Result = Tmp1;
1075 else
1076 Result = DAG.getConstant(0, TLI.getPointerTy());
1077 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001078 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001079 MVT VT = Node->getValueType(0);
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001080 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1081 default: assert(0 && "This action is not supported yet!");
1082 case TargetLowering::Custom:
1083 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001084 if (Result.getNode()) break;
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001085 // Fall Thru
1086 case TargetLowering::Legal:
1087 Result = DAG.getConstant(0, VT);
1088 break;
1089 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001090 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001091 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001092 case ISD::EXCEPTIONADDR: {
1093 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001094 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001095 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1096 default: assert(0 && "This action is not supported yet!");
1097 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +00001098 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +00001099 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001100 }
1101 break;
1102 case TargetLowering::Custom:
1103 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001104 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001105 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001106 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001107 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands4bdcb612008-07-02 17:40:58 +00001108 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001109 break;
1110 }
1111 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001112 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001113 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001114
Gabor Greifba36cb52008-08-28 21:40:38 +00001115 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001116 "Cannot return more than two values!");
1117
1118 // Since we produced two values, make sure to remember that we
1119 // legalized both of them.
1120 Tmp1 = LegalizeOp(Result);
1121 Tmp2 = LegalizeOp(Result.getValue(1));
1122 AddLegalizedOperand(Op.getValue(0), Tmp1);
1123 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001124 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +00001125 case ISD::EHSELECTION: {
1126 Tmp1 = LegalizeOp(Node->getOperand(0));
1127 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001128 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +00001129 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1130 default: assert(0 && "This action is not supported yet!");
1131 case TargetLowering::Expand: {
1132 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +00001133 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +00001134 }
1135 break;
1136 case TargetLowering::Custom:
1137 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001138 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +00001139 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001140 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001141 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands4bdcb612008-07-02 17:40:58 +00001142 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +00001143 break;
1144 }
1145 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001146 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001147 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001148
Gabor Greifba36cb52008-08-28 21:40:38 +00001149 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001150 "Cannot return more than two values!");
1151
1152 // Since we produced two values, make sure to remember that we
1153 // legalized both of them.
1154 Tmp1 = LegalizeOp(Result);
1155 Tmp2 = LegalizeOp(Result.getValue(1));
1156 AddLegalizedOperand(Op.getValue(0), Tmp1);
1157 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001158 return Op.getResNo() ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001159 case ISD::EH_RETURN: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001160 MVT VT = Node->getValueType(0);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001161 // The only "good" option for this node is to custom lower it.
1162 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1163 default: assert(0 && "This action is not supported at all!");
1164 case TargetLowering::Custom:
1165 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001166 if (Result.getNode()) break;
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001167 // Fall Thru
1168 case TargetLowering::Legal:
1169 // Target does not know, how to lower this, lower to noop
1170 Result = LegalizeOp(Node->getOperand(0));
1171 break;
1172 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001173 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001174 break;
Chris Lattner08951a32005-09-02 01:15:01 +00001175 case ISD::AssertSext:
1176 case ISD::AssertZext:
1177 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001178 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +00001179 break;
Chris Lattner308575b2005-11-20 22:56:56 +00001180 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001181 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif99a6cb92008-08-26 22:36:50 +00001182 Result = Node->getOperand(Op.getResNo());
Chris Lattner456a93a2006-01-28 07:39:30 +00001183 break;
Chris Lattner69a52152005-01-14 22:38:01 +00001184 case ISD::CopyFromReg:
1185 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001186 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001187 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001188 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +00001189 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001190 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001191 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001192 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001193 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1194 } else {
1195 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1196 }
Chris Lattner7310fb12005-12-18 15:27:43 +00001197 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1198 }
Chris Lattner13c184d2005-01-28 06:27:38 +00001199 // Since CopyFromReg produces two values, make sure to remember that we
1200 // legalized both of them.
1201 AddLegalizedOperand(Op.getValue(0), Result);
1202 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001203 return Result.getValue(Op.getResNo());
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001204 case ISD::UNDEF: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001205 MVT VT = Op.getValueType();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001206 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +00001207 default: assert(0 && "This action is not supported yet!");
1208 case TargetLowering::Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001209 if (VT.isInteger())
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001210 Result = DAG.getConstant(0, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001211 else if (VT.isFloatingPoint())
1212 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf41db212007-09-26 17:26:49 +00001213 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001214 else
1215 assert(0 && "Unknown value type!");
1216 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001217 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001218 break;
1219 }
1220 break;
1221 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001222
Chris Lattner48b61a72006-03-28 00:40:33 +00001223 case ISD::INTRINSIC_W_CHAIN:
1224 case ISD::INTRINSIC_WO_CHAIN:
1225 case ISD::INTRINSIC_VOID: {
Dan Gohman475871a2008-07-27 21:46:04 +00001226 SmallVector<SDValue, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001227 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1228 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001229 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001230
1231 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001232 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001233 TargetLowering::Custom) {
1234 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001235 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001236 }
1237
Gabor Greifba36cb52008-08-28 21:40:38 +00001238 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001239
1240 // Must have return value and chain result.
Gabor Greifba36cb52008-08-28 21:40:38 +00001241 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattner13fc2f12006-03-27 20:28:29 +00001242 "Cannot return more than two values!");
1243
1244 // Since loads produce two values, make sure to remember that we
1245 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001246 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1247 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001248 return Result.getValue(Op.getResNo());
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001249 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001250
Dan Gohman7f460202008-06-30 20:59:49 +00001251 case ISD::DBG_STOPPOINT:
1252 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +00001253 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1254
Dan Gohman7f460202008-06-30 20:59:49 +00001255 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +00001256 case TargetLowering::Promote:
1257 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001258 case TargetLowering::Expand: {
Devang Patel83489bb2009-01-13 00:35:13 +00001259 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf560ffa2009-01-28 17:46:25 +00001260 bool useDEBUG_LOC = TLI.isOperationLegalOrCustom(ISD::DEBUG_LOC,
1261 MVT::Other);
1262 bool useLABEL = TLI.isOperationLegalOrCustom(ISD::DBG_LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001263
Dan Gohman7f460202008-06-30 20:59:49 +00001264 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Devang Patel83489bb2009-01-13 00:35:13 +00001265 GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
1266 if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
1267 DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
1268 unsigned SrcFile = DW->RecordSource(CU.getDirectory(),
1269 CU.getFilename());
1270
Dan Gohman7f460202008-06-30 20:59:49 +00001271 unsigned Line = DSP->getLine();
1272 unsigned Col = DSP->getColumn();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001273
1274 if (useDEBUG_LOC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001275 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Cheng71e86852008-07-08 20:06:39 +00001276 DAG.getConstant(Col, MVT::i32),
1277 DAG.getConstant(SrcFile, MVT::i32) };
1278 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001279 } else {
Devang Patel83489bb2009-01-13 00:35:13 +00001280 unsigned ID = DW->RecordSourceLine(Line, Col, SrcFile);
Dan Gohman44066042008-07-01 00:05:16 +00001281 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001282 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001283 } else {
1284 Result = Tmp1; // chain
1285 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001286 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001287 }
Evan Cheng71e86852008-07-08 20:06:39 +00001288 case TargetLowering::Legal: {
1289 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1290 if (Action == Legal && Tmp1 == Node->getOperand(0))
1291 break;
1292
Dan Gohman475871a2008-07-27 21:46:04 +00001293 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001294 Ops.push_back(Tmp1);
1295 if (Action == Legal) {
1296 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1297 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1298 } else {
1299 // Otherwise promote them.
1300 Ops.push_back(PromoteOp(Node->getOperand(1)));
1301 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner36ce6912005-11-29 06:21:05 +00001302 }
Evan Cheng71e86852008-07-08 20:06:39 +00001303 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1304 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1305 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001306 break;
1307 }
Evan Cheng71e86852008-07-08 20:06:39 +00001308 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001309 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001310
1311 case ISD::DECLARE:
1312 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1313 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1314 default: assert(0 && "This action is not supported yet!");
1315 case TargetLowering::Legal:
1316 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1317 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1318 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1319 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1320 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001321 case TargetLowering::Expand:
1322 Result = LegalizeOp(Node->getOperand(0));
1323 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001324 }
1325 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001326
1327 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001328 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001329 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001330 default: assert(0 && "This action is not supported yet!");
Evan Cheng71e86852008-07-08 20:06:39 +00001331 case TargetLowering::Legal: {
1332 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001333 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Cheng71e86852008-07-08 20:06:39 +00001334 if (Action == Legal && Tmp1 == Node->getOperand(0))
1335 break;
1336 if (Action == Legal) {
1337 Tmp2 = Node->getOperand(1);
1338 Tmp3 = Node->getOperand(2);
1339 Tmp4 = Node->getOperand(3);
1340 } else {
1341 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1342 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1343 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1344 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001345 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001346 break;
1347 }
Evan Cheng71e86852008-07-08 20:06:39 +00001348 }
Jim Laskeyabf6d172006-01-05 01:25:28 +00001349 break;
1350
Dan Gohman44066042008-07-01 00:05:16 +00001351 case ISD::DBG_LABEL:
1352 case ISD::EH_LABEL:
1353 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1354 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001355 default: assert(0 && "This action is not supported yet!");
1356 case TargetLowering::Legal:
1357 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohman44066042008-07-01 00:05:16 +00001358 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001359 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001360 case TargetLowering::Expand:
1361 Result = LegalizeOp(Node->getOperand(0));
1362 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001363 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001364 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001365
Evan Cheng27b7db52008-03-08 00:58:38 +00001366 case ISD::PREFETCH:
1367 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1368 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1369 default: assert(0 && "This action is not supported yet!");
1370 case TargetLowering::Legal:
1371 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1372 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1373 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1374 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1375 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1376 break;
1377 case TargetLowering::Expand:
1378 // It's a noop.
1379 Result = LegalizeOp(Node->getOperand(0));
1380 break;
1381 }
1382 break;
1383
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001384 case ISD::MEMBARRIER: {
1385 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001386 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1387 default: assert(0 && "This action is not supported yet!");
1388 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001389 SDValue Ops[6];
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001390 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001391 for (int x = 1; x < 6; ++x) {
1392 Ops[x] = Node->getOperand(x);
1393 if (!isTypeLegal(Ops[x].getValueType()))
1394 Ops[x] = PromoteOp(Ops[x]);
1395 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001396 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1397 break;
1398 }
1399 case TargetLowering::Expand:
1400 //There is no libgcc call for this op
1401 Result = Node->getOperand(0); // Noop
1402 break;
1403 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001404 break;
1405 }
1406
Dan Gohman0b1d4a72008-12-23 21:37:04 +00001407 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang63307c32008-05-05 19:05:59 +00001408 unsigned int num_operands = 4;
1409 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001410 SDValue Ops[4];
Mon P Wang63307c32008-05-05 19:05:59 +00001411 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001412 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang63307c32008-05-05 19:05:59 +00001413 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1414
1415 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1416 default: assert(0 && "This action is not supported yet!");
1417 case TargetLowering::Custom:
1418 Result = TLI.LowerOperation(Result, DAG);
1419 break;
1420 case TargetLowering::Legal:
1421 break;
1422 }
Dan Gohman475871a2008-07-27 21:46:04 +00001423 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1424 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001425 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001426 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00001427 case ISD::ATOMIC_LOAD_ADD:
1428 case ISD::ATOMIC_LOAD_SUB:
1429 case ISD::ATOMIC_LOAD_AND:
1430 case ISD::ATOMIC_LOAD_OR:
1431 case ISD::ATOMIC_LOAD_XOR:
1432 case ISD::ATOMIC_LOAD_NAND:
1433 case ISD::ATOMIC_LOAD_MIN:
1434 case ISD::ATOMIC_LOAD_MAX:
1435 case ISD::ATOMIC_LOAD_UMIN:
1436 case ISD::ATOMIC_LOAD_UMAX:
1437 case ISD::ATOMIC_SWAP: {
Mon P Wang63307c32008-05-05 19:05:59 +00001438 unsigned int num_operands = 3;
1439 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001440 SDValue Ops[3];
Mon P Wang63307c32008-05-05 19:05:59 +00001441 for (unsigned int x = 0; x < num_operands; ++x)
1442 Ops[x] = LegalizeOp(Node->getOperand(x));
1443 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands126d9072008-07-04 11:47:58 +00001444
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001445 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001446 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001447 case TargetLowering::Custom:
1448 Result = TLI.LowerOperation(Result, DAG);
1449 break;
1450 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001451 break;
1452 }
Dan Gohman475871a2008-07-27 21:46:04 +00001453 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1454 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001455 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001456 }
Scott Michelc1513d22007-08-08 23:23:31 +00001457 case ISD::Constant: {
1458 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1459 unsigned opAction =
1460 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1461
Chris Lattner3e928bb2005-01-07 07:47:09 +00001462 // We know we don't need to expand constants here, constants only have one
1463 // value and we check that it is fine above.
1464
Scott Michelc1513d22007-08-08 23:23:31 +00001465 if (opAction == TargetLowering::Custom) {
1466 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001467 if (Tmp1.getNode())
Scott Michelc1513d22007-08-08 23:23:31 +00001468 Result = Tmp1;
1469 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001470 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001471 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001472 case ISD::ConstantFP: {
1473 // Spill FP immediates to the constant pool if the target cannot directly
1474 // codegen them. Targets often have some immediate values that can be
1475 // efficiently generated into an FP register without a load. We explicitly
1476 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001477 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1478
Chris Lattner3181a772006-01-29 06:26:56 +00001479 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1480 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001481 case TargetLowering::Legal:
1482 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001483 case TargetLowering::Custom:
1484 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001485 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001486 Result = Tmp3;
1487 break;
1488 }
1489 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001490 case TargetLowering::Expand: {
1491 // Check to see if this FP immediate is already legal.
1492 bool isLegal = false;
1493 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1494 E = TLI.legal_fpimm_end(); I != E; ++I) {
1495 if (CFP->isExactlyValue(*I)) {
1496 isLegal = true;
1497 break;
1498 }
1499 }
1500 // If this is a legal constant, turn it into a TargetConstantFP node.
1501 if (isLegal)
1502 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001503 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001504 }
Nate Begemane1795842008-02-14 08:57:00 +00001505 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001506 break;
1507 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001508 case ISD::TokenFactor:
1509 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001510 Tmp1 = LegalizeOp(Node->getOperand(0));
1511 Tmp2 = LegalizeOp(Node->getOperand(1));
1512 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1513 } else if (Node->getNumOperands() == 3) {
1514 Tmp1 = LegalizeOp(Node->getOperand(0));
1515 Tmp2 = LegalizeOp(Node->getOperand(1));
1516 Tmp3 = LegalizeOp(Node->getOperand(2));
1517 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001518 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001519 SmallVector<SDValue, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001520 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001521 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1522 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001523 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001524 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001525 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001526
1527 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001528 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001529 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001530 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001531 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001532 // A call within a calling sequence must be legalized to something
1533 // other than the normal CALLSEQ_END. Violating this gets Legalize
1534 // into an infinite loop.
1535 assert ((!IsLegalizingCall ||
1536 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001537 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001538 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001539
1540 // The number of incoming and outgoing values should match; unless the final
1541 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001542 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1543 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1544 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001545 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001546 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001547
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001548 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001549 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001550 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1551 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001552 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001553 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001554 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001555 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001556 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001557 }
1558 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001559 case ISD::EXTRACT_SUBREG: {
1560 Tmp1 = LegalizeOp(Node->getOperand(0));
1561 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1562 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001563 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001564 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1565 }
1566 break;
1567 case ISD::INSERT_SUBREG: {
1568 Tmp1 = LegalizeOp(Node->getOperand(0));
1569 Tmp2 = LegalizeOp(Node->getOperand(1));
1570 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1571 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001572 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001573 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1574 }
1575 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001576 case ISD::BUILD_VECTOR:
1577 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001578 default: assert(0 && "This action is not supported yet!");
1579 case TargetLowering::Custom:
1580 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001581 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001582 Result = Tmp3;
1583 break;
1584 }
1585 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001586 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001587 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001588 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001589 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001590 break;
1591 case ISD::INSERT_VECTOR_ELT:
1592 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001593 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001594
1595 // The type of the value to insert may not be legal, even though the vector
1596 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1597 // here.
1598 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1599 default: assert(0 && "Cannot expand insert element operand");
1600 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1601 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang0c397192008-10-30 08:01:45 +00001602 case Expand:
1603 // FIXME: An alternative would be to check to see if the target is not
1604 // going to custom lower this operation, we could bitcast to half elt
1605 // width and perform two inserts at that width, if that is legal.
1606 Tmp2 = Node->getOperand(1);
1607 break;
Nate Begeman0325d902008-02-13 06:43:04 +00001608 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001609 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1610
1611 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1612 Node->getValueType(0))) {
1613 default: assert(0 && "This action is not supported yet!");
1614 case TargetLowering::Legal:
1615 break;
1616 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001617 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001618 if (Tmp4.getNode()) {
Nate Begeman2281a992008-01-05 20:47:37 +00001619 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001620 break;
1621 }
1622 // FALLTHROUGH
Mon P Wang0c397192008-10-30 08:01:45 +00001623 case TargetLowering::Promote:
1624 // Fall thru for vector case
Chris Lattner2332b9f2006-03-19 01:17:20 +00001625 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001626 // If the insert index is a constant, codegen this as a scalar_to_vector,
1627 // then a shuffle that inserts it into the right position in the vector.
1628 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001629 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1630 // match the element type of the vector being created.
1631 if (Tmp2.getValueType() ==
Duncan Sands83ec4b62008-06-06 12:08:01 +00001632 Op.getValueType().getVectorElementType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001633 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman0325d902008-02-13 06:43:04 +00001634 Tmp1.getValueType(), Tmp2);
1635
Duncan Sands83ec4b62008-06-06 12:08:01 +00001636 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1637 MVT ShufMaskVT =
1638 MVT::getIntVectorWithNumElements(NumElts);
1639 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman0325d902008-02-13 06:43:04 +00001640
1641 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1642 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1643 // elt 0 of the RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00001644 SmallVector<SDValue, 8> ShufOps;
Nate Begeman0325d902008-02-13 06:43:04 +00001645 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001646 if (i != InsertPos->getZExtValue())
Nate Begeman0325d902008-02-13 06:43:04 +00001647 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1648 else
1649 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1650 }
Dan Gohman475871a2008-07-27 21:46:04 +00001651 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman0325d902008-02-13 06:43:04 +00001652 &ShufOps[0], ShufOps.size());
1653
1654 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1655 Tmp1, ScVec, ShufMask);
1656 Result = LegalizeOp(Result);
1657 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001658 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001659 }
Nate Begeman68679912008-04-25 18:07:40 +00001660 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001661 break;
1662 }
1663 }
1664 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001665 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001666 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1667 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1668 break;
1669 }
1670
Chris Lattnerce872152006-03-19 06:31:19 +00001671 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1672 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1673 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1674 Node->getValueType(0))) {
1675 default: assert(0 && "This action is not supported yet!");
1676 case TargetLowering::Legal:
1677 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001678 case TargetLowering::Custom:
1679 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001680 if (Tmp3.getNode()) {
Chris Lattner4d3abee2006-03-19 06:47:21 +00001681 Result = Tmp3;
1682 break;
1683 }
1684 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001685 case TargetLowering::Expand:
1686 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001687 break;
1688 }
Chris Lattnerce872152006-03-19 06:31:19 +00001689 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001690 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001691 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1692 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1693 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1694
1695 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001696 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1697 default: assert(0 && "Unknown operation action!");
1698 case TargetLowering::Legal:
1699 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1700 "vector shuffle should not be created if not legal!");
1701 break;
1702 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001703 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001704 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001705 Result = Tmp3;
1706 break;
1707 }
1708 // FALLTHROUGH
1709 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001710 MVT VT = Node->getValueType(0);
1711 MVT EltVT = VT.getVectorElementType();
1712 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001713 SDValue Mask = Node->getOperand(2);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001714 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001715 SmallVector<SDValue,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001716 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001717 SDValue Arg = Mask.getOperand(i);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001718 if (Arg.getOpcode() == ISD::UNDEF) {
1719 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1720 } else {
1721 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001722 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng18dd6d02006-04-05 06:07:11 +00001723 if (Idx < NumElems)
1724 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1725 DAG.getConstant(Idx, PtrVT)));
1726 else
1727 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1728 DAG.getConstant(Idx - NumElems, PtrVT)));
1729 }
1730 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001731 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001732 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001733 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001734 case TargetLowering::Promote: {
1735 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001736 MVT OVT = Node->getValueType(0);
1737 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001738
1739 // Cast the two input vectors.
1740 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1741 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1742
1743 // Convert the shuffle mask to the right # elements.
Dan Gohman475871a2008-07-27 21:46:04 +00001744 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001745 assert(Tmp3.getNode() && "Shuffle not legal?");
Chris Lattner4352cc92006-04-04 17:23:26 +00001746 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1747 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1748 break;
1749 }
Chris Lattner87100e02006-03-20 01:52:29 +00001750 }
1751 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001752
1753 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001754 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001755 Tmp2 = LegalizeOp(Node->getOperand(1));
1756 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001757 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001758 break;
1759
Dan Gohman7f321562007-06-25 16:23:39 +00001760 case ISD::EXTRACT_SUBVECTOR:
1761 Tmp1 = Node->getOperand(0);
1762 Tmp2 = LegalizeOp(Node->getOperand(1));
1763 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1764 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001765 break;
1766
Mon P Wang0c397192008-10-30 08:01:45 +00001767 case ISD::CONCAT_VECTORS: {
1768 // Use extract/insert/build vector for now. We might try to be
1769 // more clever later.
1770 MVT PtrVT = TLI.getPointerTy();
1771 SmallVector<SDValue, 8> Ops;
1772 unsigned NumOperands = Node->getNumOperands();
1773 for (unsigned i=0; i < NumOperands; ++i) {
1774 SDValue SubOp = Node->getOperand(i);
1775 MVT VVT = SubOp.getNode()->getValueType(0);
1776 MVT EltVT = VVT.getVectorElementType();
1777 unsigned NumSubElem = VVT.getVectorNumElements();
1778 for (unsigned j=0; j < NumSubElem; ++j) {
1779 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1780 DAG.getConstant(j, PtrVT)));
1781 }
1782 }
1783 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1784 &Ops[0], Ops.size()));
1785 }
1786
Chris Lattner6831a812006-02-13 09:18:02 +00001787 case ISD::CALLSEQ_START: {
1788 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1789
1790 // Recursively Legalize all of the inputs of the call end that do not lead
1791 // to this call start. This ensures that any libcalls that need be inserted
1792 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001793 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001794 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001795 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001796 NodesLeadingTo);
1797 }
Chris Lattner6831a812006-02-13 09:18:02 +00001798
1799 // Now that we legalized all of the inputs (which may have inserted
1800 // libcalls) create the new CALLSEQ_START node.
1801 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1802
1803 // Merge in the last call, to ensure that this call start after the last
1804 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001805 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001806 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1807 Tmp1 = LegalizeOp(Tmp1);
1808 }
Chris Lattner6831a812006-02-13 09:18:02 +00001809
1810 // Do not try to legalize the target-specific arguments (#1+).
1811 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001812 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001813 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001814 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001815 }
1816
1817 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001818 AddLegalizedOperand(Op.getValue(0), Result);
1819 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1820 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1821
Chris Lattner6831a812006-02-13 09:18:02 +00001822 // Now that the callseq_start and all of the non-call nodes above this call
1823 // sequence have been legalized, legalize the call itself. During this
1824 // process, no libcalls can/will be inserted, guaranteeing that no calls
1825 // can overlap.
1826 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001827 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001828 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001829 IsLegalizingCall = true;
1830
1831 // Legalize the call, starting from the CALLSEQ_END.
1832 LegalizeOp(LastCALLSEQ_END);
1833 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1834 return Result;
1835 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001836 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001837 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1838 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001839 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001840 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1841 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001842 assert(I != LegalizedNodes.end() &&
1843 "Legalizing the call start should have legalized this node!");
1844 return I->second;
1845 }
1846
1847 // Otherwise, the call start has been legalized and everything is going
1848 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001849 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001850 // Do not try to legalize the target-specific arguments (#1+), except for
1851 // an optional flag input.
1852 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1853 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001854 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001855 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001856 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001857 }
1858 } else {
1859 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1860 if (Tmp1 != Node->getOperand(0) ||
1861 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001862 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001863 Ops[0] = Tmp1;
1864 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001865 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001866 }
Chris Lattner6a542892006-01-24 05:48:21 +00001867 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001868 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001869 // This finishes up call legalization.
1870 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001871
1872 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001873 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001874 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001875 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001876 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001877 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001878 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001879 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1880 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1881 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001882 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001883
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001884 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001885 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001886 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001887 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001888 case TargetLowering::Expand: {
1889 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1890 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1891 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001892 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001893
1894 // Chain the dynamic stack allocation so that it doesn't modify the stack
1895 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001896 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001897
Dan Gohman475871a2008-07-27 21:46:04 +00001898 SDValue Size = Tmp2.getOperand(1);
1899 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001900 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001901 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001902 unsigned StackAlign =
1903 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1904 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001905 SP = DAG.getNode(ISD::AND, VT, SP,
1906 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001907 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001908 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1909
Chris Lattnere563bbc2008-10-11 22:08:30 +00001910 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1911 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001912
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001913 Tmp1 = LegalizeOp(Tmp1);
1914 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001915 break;
1916 }
1917 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001918 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001919 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001920 Tmp1 = LegalizeOp(Tmp3);
1921 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001922 }
Chris Lattner903d2782006-01-15 08:54:32 +00001923 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001924 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001925 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001926 }
Chris Lattner903d2782006-01-15 08:54:32 +00001927 // Since this op produce two values, make sure to remember that we
1928 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001929 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1930 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001931 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001932 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001933 case ISD::INLINEASM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001934 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001935 bool Changed = false;
1936 // Legalize all of the operands of the inline asm, in case they are nodes
1937 // that need to be expanded or something. Note we skip the asm string and
1938 // all of the TargetConstant flags.
Dan Gohman475871a2008-07-27 21:46:04 +00001939 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001940 Changed = Op != Ops[0];
1941 Ops[0] = Op;
1942
1943 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1944 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001945 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Chris Lattner25a022c2006-07-11 01:40:09 +00001946 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman475871a2008-07-27 21:46:04 +00001947 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001948 if (Op != Ops[i]) {
1949 Changed = true;
1950 Ops[i] = Op;
1951 }
1952 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001953 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001954
1955 if (HasInFlag) {
1956 Op = LegalizeOp(Ops.back());
1957 Changed |= Op != Ops.back();
1958 Ops.back() = Op;
1959 }
1960
1961 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001962 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001963
1964 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman475871a2008-07-27 21:46:04 +00001965 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1966 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001967 return Result.getValue(Op.getResNo());
Chris Lattner25a022c2006-07-11 01:40:09 +00001968 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001969 case ISD::BR:
1970 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001971 // Ensure that libcalls are emitted before a branch.
1972 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1973 Tmp1 = LegalizeOp(Tmp1);
1974 LastCALLSEQ_END = DAG.getEntryNode();
1975
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001976 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001977 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001978 case ISD::BRIND:
1979 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1980 // Ensure that libcalls are emitted before a branch.
1981 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1982 Tmp1 = LegalizeOp(Tmp1);
1983 LastCALLSEQ_END = DAG.getEntryNode();
1984
1985 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1986 default: assert(0 && "Indirect target must be legal type (pointer)!");
1987 case Legal:
1988 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1989 break;
1990 }
1991 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1992 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001993 case ISD::BR_JT:
1994 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1995 // Ensure that libcalls are emitted before a branch.
1996 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1997 Tmp1 = LegalizeOp(Tmp1);
1998 LastCALLSEQ_END = DAG.getEntryNode();
1999
2000 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
2001 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2002
2003 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
2004 default: assert(0 && "This action is not supported yet!");
2005 case TargetLowering::Legal: break;
2006 case TargetLowering::Custom:
2007 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002008 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00002009 break;
2010 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002011 SDValue Chain = Result.getOperand(0);
2012 SDValue Table = Result.getOperand(1);
2013 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002014
Duncan Sands83ec4b62008-06-06 12:08:01 +00002015 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002016 MachineFunction &MF = DAG.getMachineFunction();
2017 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00002018 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman475871a2008-07-27 21:46:04 +00002019 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002020
Duncan Sands712f7b32008-12-12 08:13:38 +00002021 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
2022 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, Chain, Addr,
2023 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00002024 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002025 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00002026 // For PIC, the sequence is:
2027 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00002028 // RelocBase can be JumpTable, GOT or some sort of global base.
Evan Chengcc415862007-11-09 01:32:10 +00002029 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
2030 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00002031 }
Evan Chengcc415862007-11-09 01:32:10 +00002032 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002033 }
2034 }
2035 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002036 case ISD::BRCOND:
2037 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002038 // Ensure that libcalls are emitted before a return.
2039 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2040 Tmp1 = LegalizeOp(Tmp1);
2041 LastCALLSEQ_END = DAG.getEntryNode();
2042
Chris Lattner47e92232005-01-18 19:27:06 +00002043 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2044 case Expand: assert(0 && "It's impossible to expand bools");
2045 case Legal:
2046 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2047 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002048 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00002049 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00002050
2051 // The top bits of the promoted condition are not necessarily zero, ensure
2052 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002053 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002054 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002055 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerf9908172006-11-27 04:39:56 +00002056 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002057 break;
2058 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002059 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002060
2061 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002062 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00002063
2064 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
2065 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002066 case TargetLowering::Legal: break;
2067 case TargetLowering::Custom:
2068 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002069 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002070 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002071 case TargetLowering::Expand:
2072 // Expand brcond's setcc into its constituent parts and create a BR_CC
2073 // Node.
2074 if (Tmp2.getOpcode() == ISD::SETCC) {
2075 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
2076 Tmp2.getOperand(0), Tmp2.getOperand(1),
2077 Node->getOperand(2));
2078 } else {
2079 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
2080 DAG.getCondCode(ISD::SETNE), Tmp2,
2081 DAG.getConstant(0, Tmp2.getValueType()),
2082 Node->getOperand(2));
2083 }
2084 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002085 }
2086 break;
2087 case ISD::BR_CC:
2088 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002089 // Ensure that libcalls are emitted before a branch.
2090 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2091 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002092 Tmp2 = Node->getOperand(2); // LHS
2093 Tmp3 = Node->getOperand(3); // RHS
2094 Tmp4 = Node->getOperand(1); // CC
2095
Duncan Sands5480c042009-01-01 15:52:00 +00002096 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()), Tmp2, Tmp3,Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00002097 LastCALLSEQ_END = DAG.getEntryNode();
2098
Evan Cheng7f042682008-10-15 02:05:31 +00002099 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002100 // the LHS is a legal SETCC itself. In this case, we need to compare
2101 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002102 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002103 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2104 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00002105 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00002106
2107 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2108 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002109
Chris Lattner181b7a32005-12-17 23:46:46 +00002110 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2111 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002112 case TargetLowering::Legal: break;
2113 case TargetLowering::Custom:
2114 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002115 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00002116 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002117 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002118 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002119 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00002120 LoadSDNode *LD = cast<LoadSDNode>(Node);
2121 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2122 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002123
Evan Cheng466685d2006-10-09 20:57:25 +00002124 ISD::LoadExtType ExtType = LD->getExtensionType();
2125 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002126 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00002127 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2128 Tmp3 = Result.getValue(0);
2129 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002130
Evan Cheng466685d2006-10-09 20:57:25 +00002131 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2132 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002133 case TargetLowering::Legal:
2134 // If this is an unaligned load and the target doesn't support it,
2135 // expand it.
2136 if (!TLI.allowsUnalignedMemoryAccesses()) {
2137 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002138 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002139 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002140 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002141 TLI);
2142 Tmp3 = Result.getOperand(0);
2143 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00002144 Tmp3 = LegalizeOp(Tmp3);
2145 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002146 }
2147 }
2148 break;
Evan Cheng466685d2006-10-09 20:57:25 +00002149 case TargetLowering::Custom:
2150 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002151 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002152 Tmp3 = LegalizeOp(Tmp1);
2153 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00002154 }
Evan Cheng466685d2006-10-09 20:57:25 +00002155 break;
2156 case TargetLowering::Promote: {
2157 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002158 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00002159 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002160 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002161
2162 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002163 LD->getSrcValueOffset(),
2164 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00002165 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2166 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002167 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00002168 }
Evan Cheng466685d2006-10-09 20:57:25 +00002169 }
2170 // Since loads produce two values, make sure to remember that we
2171 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002172 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2173 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002174 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00002175 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002176 MVT SrcVT = LD->getMemoryVT();
2177 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002178 int SVOffset = LD->getSrcValueOffset();
2179 unsigned Alignment = LD->getAlignment();
2180 bool isVolatile = LD->isVolatile();
2181
Duncan Sands83ec4b62008-06-06 12:08:01 +00002182 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002183 // Some targets pretend to have an i1 loading operation, and actually
2184 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2185 // bits are guaranteed to be zero; it helps the optimizers understand
2186 // that these bits are zero. It is also useful for EXTLOAD, since it
2187 // tells the optimizers that those bits are undefined. It would be
2188 // nice to have an effective generic way of getting these benefits...
2189 // Until such a way is found, don't insist on promoting i1 here.
2190 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00002191 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002192 // Promote to a byte-sized load if not loading an integral number of
2193 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002194 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2195 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002196 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002197
2198 // The extra bits are guaranteed to be zero, since we stored them that
2199 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2200
2201 ISD::LoadExtType NewExtType =
2202 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2203
2204 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2205 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2206 NVT, isVolatile, Alignment);
2207
2208 Ch = Result.getValue(1); // The chain.
2209
2210 if (ExtType == ISD::SEXTLOAD)
2211 // Having the top bits zero doesn't help when sign extending.
2212 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2213 Result, DAG.getValueType(SrcVT));
2214 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2215 // All the top bits are guaranteed to be zero - inform the optimizers.
2216 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2217 DAG.getValueType(SrcVT));
2218
2219 Tmp1 = LegalizeOp(Result);
2220 Tmp2 = LegalizeOp(Ch);
2221 } else if (SrcWidth & (SrcWidth - 1)) {
2222 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002223 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002224 "Unsupported extload!");
2225 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2226 assert(RoundWidth < SrcWidth);
2227 unsigned ExtraWidth = SrcWidth - RoundWidth;
2228 assert(ExtraWidth < RoundWidth);
2229 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2230 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002231 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2232 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002233 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002234 unsigned IncrementSize;
2235
2236 if (TLI.isLittleEndian()) {
2237 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2238 // Load the bottom RoundWidth bits.
2239 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2240 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2241 Alignment);
2242
2243 // Load the remaining ExtraWidth bits.
2244 IncrementSize = RoundWidth / 8;
2245 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2246 DAG.getIntPtrConstant(IncrementSize));
2247 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2248 LD->getSrcValue(), SVOffset + IncrementSize,
2249 ExtraVT, isVolatile,
2250 MinAlign(Alignment, IncrementSize));
2251
2252 // Build a factor node to remember that this load is independent of the
2253 // other one.
2254 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2255 Hi.getValue(1));
2256
2257 // Move the top bits to the right place.
2258 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2259 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2260
2261 // Join the hi and lo parts.
2262 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002263 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002264 // Big endian - avoid unaligned loads.
2265 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2266 // Load the top RoundWidth bits.
2267 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2268 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2269 Alignment);
2270
2271 // Load the remaining ExtraWidth bits.
2272 IncrementSize = RoundWidth / 8;
2273 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2274 DAG.getIntPtrConstant(IncrementSize));
2275 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2276 LD->getSrcValue(), SVOffset + IncrementSize,
2277 ExtraVT, isVolatile,
2278 MinAlign(Alignment, IncrementSize));
2279
2280 // Build a factor node to remember that this load is independent of the
2281 // other one.
2282 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2283 Hi.getValue(1));
2284
2285 // Move the top bits to the right place.
2286 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2287 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2288
2289 // Join the hi and lo parts.
2290 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2291 }
2292
2293 Tmp1 = LegalizeOp(Result);
2294 Tmp2 = LegalizeOp(Ch);
2295 } else {
Evan Cheng03294662008-10-14 21:26:46 +00002296 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002297 default: assert(0 && "This action is not supported yet!");
2298 case TargetLowering::Custom:
2299 isCustom = true;
2300 // FALLTHROUGH
2301 case TargetLowering::Legal:
2302 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2303 Tmp1 = Result.getValue(0);
2304 Tmp2 = Result.getValue(1);
2305
2306 if (isCustom) {
2307 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002308 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002309 Tmp1 = LegalizeOp(Tmp3);
2310 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2311 }
2312 } else {
2313 // If this is an unaligned load and the target doesn't support it,
2314 // expand it.
2315 if (!TLI.allowsUnalignedMemoryAccesses()) {
2316 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002317 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002318 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002319 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002320 TLI);
2321 Tmp1 = Result.getOperand(0);
2322 Tmp2 = Result.getOperand(1);
2323 Tmp1 = LegalizeOp(Tmp1);
2324 Tmp2 = LegalizeOp(Tmp2);
2325 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002326 }
2327 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002328 break;
2329 case TargetLowering::Expand:
2330 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2331 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman475871a2008-07-27 21:46:04 +00002332 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002333 LD->getSrcValueOffset(),
2334 LD->isVolatile(), LD->getAlignment());
2335 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2336 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2337 Tmp2 = LegalizeOp(Load.getValue(1));
2338 break;
2339 }
2340 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2341 // Turn the unsupported load into an EXTLOAD followed by an explicit
2342 // zero/sign extend inreg.
2343 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2344 Tmp1, Tmp2, LD->getSrcValue(),
2345 LD->getSrcValueOffset(), SrcVT,
2346 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00002347 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002348 if (ExtType == ISD::SEXTLOAD)
2349 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2350 Result, DAG.getValueType(SrcVT));
2351 else
2352 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2353 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2354 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002355 break;
2356 }
Evan Cheng466685d2006-10-09 20:57:25 +00002357 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002358
Evan Cheng466685d2006-10-09 20:57:25 +00002359 // Since loads produce two values, make sure to remember that we legalized
2360 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002361 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2362 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002363 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002364 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002365 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002366 case ISD::EXTRACT_ELEMENT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002367 MVT OpTy = Node->getOperand(0).getValueType();
Nate Begeman5dc897b2005-10-19 00:06:56 +00002368 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002369 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002370 case Legal:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002371 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Nate Begeman5dc897b2005-10-19 00:06:56 +00002372 // 1 -> Hi
2373 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002374 DAG.getConstant(OpTy.getSizeInBits()/2,
Nate Begeman5dc897b2005-10-19 00:06:56 +00002375 TLI.getShiftAmountTy()));
2376 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2377 } else {
2378 // 0 -> Lo
2379 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2380 Node->getOperand(0));
2381 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002382 break;
2383 case Expand:
2384 // Get both the low and high parts.
2385 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002386 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Nate Begeman5dc897b2005-10-19 00:06:56 +00002387 Result = Tmp2; // 1 -> Hi
2388 else
2389 Result = Tmp1; // 0 -> Lo
2390 break;
2391 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002392 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002393 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002394
2395 case ISD::CopyToReg:
2396 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002397
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002398 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002399 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002400 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002401 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002402 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002403 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002404 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002405 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002406 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002407 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002408 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2409 Tmp3);
2410 } else {
2411 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002412 }
2413
2414 // Since this produces two values, make sure to remember that we legalized
2415 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002416 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2417 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002418 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002419 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002420 break;
2421
2422 case ISD::RET:
2423 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002424
2425 // Ensure that libcalls are emitted before a return.
2426 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2427 Tmp1 = LegalizeOp(Tmp1);
2428 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002429
Chris Lattner3e928bb2005-01-07 07:47:09 +00002430 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002431 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002432 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002433 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002434 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002435 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002436 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002437 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002438 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002439 if (!Tmp2.getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002440 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002441 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002442
2443 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002444 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002445 std::swap(Lo, Hi);
2446
Gabor Greifba36cb52008-08-28 21:40:38 +00002447 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00002448 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2449 else
2450 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002451 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002452 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00002453 SDNode *InVal = Tmp2.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002454 int InIx = Tmp2.getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002455 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2456 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002457
Dan Gohman7f321562007-06-25 16:23:39 +00002458 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002459 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002460 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002461 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002462 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002463 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002464 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002465 } else if (NumElems == 1) {
2466 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002467 Tmp2 = ScalarizeVectorOp(Tmp2);
2468 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002469 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002470
2471 // FIXME: Returns of gcc generic vectors smaller than a legal type
2472 // should be returned in integer registers!
2473
Chris Lattnerf87324e2006-04-11 01:31:51 +00002474 // The scalarized value type may not be legal, e.g. it might require
2475 // promotion or expansion. Relegalize the return.
2476 Result = LegalizeOp(Result);
2477 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002478 // FIXME: Returns of gcc generic vectors larger than a legal vector
2479 // type should be returned by reference!
Dan Gohman475871a2008-07-27 21:46:04 +00002480 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002481 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002482 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002483 Result = LegalizeOp(Result);
2484 }
2485 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002486 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002487 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002488 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002489 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002490 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002491 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002492 }
2493 break;
2494 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002495 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002496 break;
2497 default: { // ret <values>
Dan Gohman475871a2008-07-27 21:46:04 +00002498 SmallVector<SDValue, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002499 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002500 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002501 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2502 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002503 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002504 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002505 break;
2506 case Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002507 SDValue Lo, Hi;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002508 assert(!Node->getOperand(i).getValueType().isExtended() &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002509 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002510 ExpandOp(Node->getOperand(i), Lo, Hi);
2511 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002512 NewValues.push_back(Node->getOperand(i+1));
Gabor Greifba36cb52008-08-28 21:40:38 +00002513 if (Hi.getNode()) {
Evan Cheng13acce32006-12-11 19:27:14 +00002514 NewValues.push_back(Hi);
2515 NewValues.push_back(Node->getOperand(i+1));
2516 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002517 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002518 }
2519 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002520 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002521 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002522
2523 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002524 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002525 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002526 Result = DAG.getNode(ISD::RET, MVT::Other,
2527 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002528 break;
2529 }
2530 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002531
Chris Lattner6862dbc2006-01-29 21:02:23 +00002532 if (Result.getOpcode() == ISD::RET) {
2533 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2534 default: assert(0 && "This action is not supported yet!");
2535 case TargetLowering::Legal: break;
2536 case TargetLowering::Custom:
2537 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002538 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner6862dbc2006-01-29 21:02:23 +00002539 break;
2540 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002541 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002542 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002543 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002544 StoreSDNode *ST = cast<StoreSDNode>(Node);
2545 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2546 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002547 int SVOffset = ST->getSrcValueOffset();
2548 unsigned Alignment = ST->getAlignment();
2549 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002550
Evan Cheng8b2794a2006-10-13 21:14:26 +00002551 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002552 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2553 // FIXME: We shouldn't do this for TargetConstantFP's.
2554 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2555 // to phase ordering between legalized code and the dag combiner. This
2556 // probably means that we need to integrate dag combiner and legalizer
2557 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002558 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002559 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002560 if (CFP->getValueType(0) == MVT::f32 &&
2561 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002562 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00002563 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002564 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002565 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2566 SVOffset, isVolatile, Alignment);
2567 break;
2568 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002569 // If this target supports 64-bit registers, do a single 64-bit store.
2570 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00002571 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002572 zextOrTrunc(64), MVT::i64);
Chris Lattner3cb93512007-10-15 05:46:06 +00002573 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2574 SVOffset, isVolatile, Alignment);
2575 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002576 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002577 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2578 // stores. If the target supports neither 32- nor 64-bits, this
2579 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00002580 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00002581 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2582 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002583 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002584
2585 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2586 SVOffset, isVolatile, Alignment);
2587 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002588 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002589 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002590 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002591
2592 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2593 break;
2594 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002595 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002596 }
2597
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002598 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002599 case Legal: {
2600 Tmp3 = LegalizeOp(ST->getValue());
2601 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2602 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002603
Duncan Sands83ec4b62008-06-06 12:08:01 +00002604 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002605 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2606 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002607 case TargetLowering::Legal:
2608 // If this is an unaligned store and the target doesn't support it,
2609 // expand it.
2610 if (!TLI.allowsUnalignedMemoryAccesses()) {
2611 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002612 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002613 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002614 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002615 TLI);
2616 }
2617 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002618 case TargetLowering::Custom:
2619 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002620 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002621 break;
2622 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002623 assert(VT.isVector() && "Unknown legal promote case!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002624 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2625 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2626 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002627 ST->getSrcValue(), SVOffset, isVolatile,
2628 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002629 break;
2630 }
2631 break;
2632 }
2633 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002634 if (!ST->getMemoryVT().isVector()) {
2635 // Truncate the value and store the result.
2636 Tmp3 = PromoteOp(ST->getValue());
2637 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2638 SVOffset, ST->getMemoryVT(),
2639 isVolatile, Alignment);
2640 break;
2641 }
2642 // Fall thru to expand for vector
2643 case Expand: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002644 unsigned IncrementSize = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002645 SDValue Lo, Hi;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002646
2647 // If this is a vector type, then we have to calculate the increment as
2648 // the product of the element size in bytes, and the number of elements
2649 // in the high half of the vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002650 if (ST->getValue().getValueType().isVector()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002651 SDNode *InVal = ST->getValue().getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002652 int InIx = ST->getValue().getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002653 MVT InVT = InVal->getValueType(InIx);
2654 unsigned NumElems = InVT.getVectorNumElements();
2655 MVT EVT = InVT.getVectorElementType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002656
Dan Gohman7f321562007-06-25 16:23:39 +00002657 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002658 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002659 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002660 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002661 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002662 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002663 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002664 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002665 Result = LegalizeOp(Result);
2666 break;
2667 } else if (NumElems == 1) {
2668 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002669 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002670 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002671 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002672 // The scalarized value type may not be legal, e.g. it might require
2673 // promotion or expansion. Relegalize the scalar store.
2674 Result = LegalizeOp(Result);
2675 break;
2676 } else {
Mon P Wang0c397192008-10-30 08:01:45 +00002677 // Check if we have widen this node with another value
2678 std::map<SDValue, SDValue>::iterator I =
2679 WidenNodes.find(ST->getValue());
2680 if (I != WidenNodes.end()) {
2681 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2682 break;
2683 }
2684 else {
2685 SplitVectorOp(ST->getValue(), Lo, Hi);
2686 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2687 EVT.getSizeInBits()/8;
2688 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002689 }
2690 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002691 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greifba36cb52008-08-28 21:40:38 +00002692 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002693
Richard Pennington4b052dc2008-09-25 16:15:10 +00002694 if (Hi.getNode() && TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002695 std::swap(Lo, Hi);
2696 }
2697
2698 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002699 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002700
Gabor Greifba36cb52008-08-28 21:40:38 +00002701 if (Hi.getNode() == NULL) {
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002702 // Must be int <-> float one-to-one expansion.
2703 Result = Lo;
2704 break;
2705 }
2706
Evan Cheng8b2794a2006-10-13 21:14:26 +00002707 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002708 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002709 assert(isTypeLegal(Tmp2.getValueType()) &&
2710 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002711 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002712 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002713 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002714 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002715 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2716 break;
Mon P Wang0c397192008-10-30 08:01:45 +00002717 } // case Expand
Evan Cheng8b2794a2006-10-13 21:14:26 +00002718 }
2719 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002720 switch (getTypeAction(ST->getValue().getValueType())) {
2721 case Legal:
2722 Tmp3 = LegalizeOp(ST->getValue());
2723 break;
2724 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002725 if (!ST->getValue().getValueType().isVector()) {
2726 // We can promote the value, the truncstore will still take care of it.
2727 Tmp3 = PromoteOp(ST->getValue());
2728 break;
2729 }
2730 // Vector case falls through to expand
Chris Lattnerddf89562008-01-17 19:59:44 +00002731 case Expand:
2732 // Just store the low part. This may become a non-trunc store, so make
2733 // sure to use getTruncStore, not UpdateNodeOperands below.
2734 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2735 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2736 SVOffset, MVT::i8, isVolatile, Alignment);
2737 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002738
Duncan Sands83ec4b62008-06-06 12:08:01 +00002739 MVT StVT = ST->getMemoryVT();
2740 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00002741
Duncan Sands83ec4b62008-06-06 12:08:01 +00002742 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00002743 // Promote to a byte-sized store with upper bits zero if not
2744 // storing an integral number of bytes. For example, promote
2745 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002746 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands7e857202008-01-22 07:17:34 +00002747 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2748 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2749 SVOffset, NVT, isVolatile, Alignment);
2750 } else if (StWidth & (StWidth - 1)) {
2751 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002752 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00002753 "Unsupported truncstore!");
2754 unsigned RoundWidth = 1 << Log2_32(StWidth);
2755 assert(RoundWidth < StWidth);
2756 unsigned ExtraWidth = StWidth - RoundWidth;
2757 assert(ExtraWidth < RoundWidth);
2758 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2759 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002760 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2761 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002762 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00002763 unsigned IncrementSize;
2764
2765 if (TLI.isLittleEndian()) {
2766 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2767 // Store the bottom RoundWidth bits.
2768 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2769 SVOffset, RoundVT,
2770 isVolatile, Alignment);
2771
2772 // Store the remaining ExtraWidth bits.
2773 IncrementSize = RoundWidth / 8;
2774 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2775 DAG.getIntPtrConstant(IncrementSize));
2776 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2777 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2778 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2779 SVOffset + IncrementSize, ExtraVT, isVolatile,
2780 MinAlign(Alignment, IncrementSize));
2781 } else {
2782 // Big endian - avoid unaligned stores.
2783 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2784 // Store the top RoundWidth bits.
2785 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2786 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2787 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2788 RoundVT, isVolatile, Alignment);
2789
2790 // Store the remaining ExtraWidth bits.
2791 IncrementSize = RoundWidth / 8;
2792 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2793 DAG.getIntPtrConstant(IncrementSize));
2794 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2795 SVOffset + IncrementSize, ExtraVT, isVolatile,
2796 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002797 }
Duncan Sands7e857202008-01-22 07:17:34 +00002798
2799 // The order of the stores doesn't matter.
2800 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2801 } else {
2802 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2803 Tmp2 != ST->getBasePtr())
2804 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2805 ST->getOffset());
2806
2807 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2808 default: assert(0 && "This action is not supported yet!");
2809 case TargetLowering::Legal:
2810 // If this is an unaligned store and the target doesn't support it,
2811 // expand it.
2812 if (!TLI.allowsUnalignedMemoryAccesses()) {
2813 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002814 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00002815 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002816 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00002817 TLI);
2818 }
2819 break;
2820 case TargetLowering::Custom:
2821 Result = TLI.LowerOperation(Result, DAG);
2822 break;
2823 case Expand:
2824 // TRUNCSTORE:i16 i32 -> STORE i16
2825 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2826 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2827 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2828 isVolatile, Alignment);
2829 break;
2830 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002831 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002832 }
2833 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002834 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002835 case ISD::PCMARKER:
2836 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002837 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002838 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002839 case ISD::STACKSAVE:
2840 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002841 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2842 Tmp1 = Result.getValue(0);
2843 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002844
Chris Lattner140d53c2006-01-13 02:50:02 +00002845 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2846 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002847 case TargetLowering::Legal: break;
2848 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002849 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002850 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002851 Tmp1 = LegalizeOp(Tmp3);
2852 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002853 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002854 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002855 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002856 // Expand to CopyFromReg if the target set
2857 // StackPointerRegisterToSaveRestore.
2858 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002859 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002860 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002861 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002862 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002863 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2864 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002865 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002866 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002867 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002868
2869 // Since stacksave produce two values, make sure to remember that we
2870 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002871 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2872 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002873 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002874
Chris Lattner140d53c2006-01-13 02:50:02 +00002875 case ISD::STACKRESTORE:
2876 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2877 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002878 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002879
2880 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2881 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002882 case TargetLowering::Legal: break;
2883 case TargetLowering::Custom:
2884 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002885 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002886 break;
2887 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002888 // Expand to CopyToReg if the target set
2889 // StackPointerRegisterToSaveRestore.
2890 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2891 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2892 } else {
2893 Result = Tmp1;
2894 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002895 break;
2896 }
2897 break;
2898
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002899 case ISD::READCYCLECOUNTER:
2900 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002901 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002902 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2903 Node->getValueType(0))) {
2904 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002905 case TargetLowering::Legal:
2906 Tmp1 = Result.getValue(0);
2907 Tmp2 = Result.getValue(1);
2908 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002909 case TargetLowering::Custom:
2910 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002911 Tmp1 = LegalizeOp(Result.getValue(0));
2912 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002913 break;
2914 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002915
2916 // Since rdcc produce two values, make sure to remember that we legalized
2917 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002918 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2919 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002920 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002921
Chris Lattner2ee743f2005-01-14 22:08:15 +00002922 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002923 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2924 case Expand: assert(0 && "It's impossible to expand bools");
2925 case Legal:
2926 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2927 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002928 case Promote: {
Mon P Wang0c397192008-10-30 08:01:45 +00002929 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Chris Lattner47e92232005-01-18 19:27:06 +00002930 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002931 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002932 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002933 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002934 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerb6c80602006-11-28 01:03:30 +00002935 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002936 break;
2937 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002938 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002939 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002940 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002941
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002942 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002943
Nate Begemanb942a3d2005-08-23 04:29:48 +00002944 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002945 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002946 case TargetLowering::Legal: break;
2947 case TargetLowering::Custom: {
2948 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002949 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002950 break;
2951 }
Nate Begeman9373a812005-08-10 20:51:12 +00002952 case TargetLowering::Expand:
2953 if (Tmp1.getOpcode() == ISD::SETCC) {
2954 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2955 Tmp2, Tmp3,
2956 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2957 } else {
2958 Result = DAG.getSelectCC(Tmp1,
2959 DAG.getConstant(0, Tmp1.getValueType()),
2960 Tmp2, Tmp3, ISD::SETNE);
2961 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002962 break;
2963 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002964 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002965 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2966 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002967 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002968 ExtOp = ISD::BIT_CONVERT;
2969 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002970 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002971 ExtOp = ISD::ANY_EXTEND;
2972 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002973 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002974 ExtOp = ISD::FP_EXTEND;
2975 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002976 }
2977 // Promote each of the values to the new type.
2978 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2979 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2980 // Perform the larger operation, then round down.
2981 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002982 if (TruncOp != ISD::FP_ROUND)
2983 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2984 else
2985 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2986 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002987 break;
2988 }
2989 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002990 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002991 case ISD::SELECT_CC: {
2992 Tmp1 = Node->getOperand(0); // LHS
2993 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002994 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2995 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00002996 SDValue CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002997
Duncan Sands5480c042009-01-01 15:52:00 +00002998 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002999
Evan Cheng7f042682008-10-15 02:05:31 +00003000 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00003001 // the LHS is a legal SETCC itself. In this case, we need to compare
3002 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00003003 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003004 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3005 CC = DAG.getCondCode(ISD::SETNE);
3006 }
3007 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
3008
3009 // Everything is legal, see if we should expand this op or something.
3010 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
3011 default: assert(0 && "This action is not supported yet!");
3012 case TargetLowering::Legal: break;
3013 case TargetLowering::Custom:
3014 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003015 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00003016 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003017 }
3018 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00003019 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003020 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00003021 Tmp1 = Node->getOperand(0);
3022 Tmp2 = Node->getOperand(1);
3023 Tmp3 = Node->getOperand(2);
Evan Cheng7f042682008-10-15 02:05:31 +00003024 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003025
3026 // If we had to Expand the SetCC operands into a SELECT node, then it may
3027 // not always be possible to return a true LHS & RHS. In this case, just
3028 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003029 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003030 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003031 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003032 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003033
Chris Lattner73e142f2006-01-30 22:43:50 +00003034 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003035 default: assert(0 && "Cannot handle this action for SETCC yet!");
3036 case TargetLowering::Custom:
3037 isCustom = true;
3038 // FALLTHROUGH.
3039 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00003040 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00003041 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00003042 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003043 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00003044 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003045 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00003046 case TargetLowering::Promote: {
3047 // First step, figure out the appropriate operation to use.
3048 // Allow SETCC to not be supported for all legal data types
3049 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00003050 MVT NewInTy = Node->getOperand(0).getValueType();
3051 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00003052
3053 // Scan for the appropriate larger type to use.
3054 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003055 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00003056
Duncan Sands83ec4b62008-06-06 12:08:01 +00003057 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003058 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003059 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003060 "Fell off of the edge of the floating point world");
3061
3062 // If the target supports SETCC of this type, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003063 if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00003064 break;
3065 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003066 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00003067 assert(0 && "Cannot promote Legal Integer SETCC yet");
3068 else {
3069 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
3070 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
3071 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003072 Tmp1 = LegalizeOp(Tmp1);
3073 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00003074 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00003075 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00003076 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00003077 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003078 case TargetLowering::Expand:
3079 // Expand a setcc node into a select_cc of the same condition, lhs, and
3080 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003081 MVT VT = Node->getValueType(0);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003082 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3083 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00003084 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003085 break;
3086 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003087 break;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003088 case ISD::VSETCC: {
3089 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3090 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman475871a2008-07-27 21:46:04 +00003091 SDValue CC = Node->getOperand(2);
Nate Begemanb43e9c12008-05-12 19:40:03 +00003092
3093 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3094
3095 // Everything is legal, see if we should expand this op or something.
3096 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3097 default: assert(0 && "This action is not supported yet!");
3098 case TargetLowering::Legal: break;
3099 case TargetLowering::Custom:
3100 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003101 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003102 break;
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003103 case TargetLowering::Expand: {
3104 // Unroll into a nasty set of scalar code for now.
3105 MVT VT = Node->getValueType(0);
3106 unsigned NumElems = VT.getVectorNumElements();
3107 MVT EltVT = VT.getVectorElementType();
3108 MVT TmpEltVT = Tmp1.getValueType().getVectorElementType();
3109 SmallVector<SDValue, 8> Ops(NumElems);
3110 for (unsigned i = 0; i < NumElems; ++i) {
3111 SDValue In1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3112 Tmp1, DAG.getIntPtrConstant(i));
Duncan Sands5480c042009-01-01 15:52:00 +00003113 Ops[i] = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(TmpEltVT), In1,
Mon P Wang84aff842008-12-17 08:49:47 +00003114 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3115 Tmp2, DAG.getIntPtrConstant(i)),
3116 CC);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003117 Ops[i] = DAG.getNode(ISD::SELECT, EltVT, Ops[i], DAG.getConstant(
3118 APInt::getAllOnesValue(EltVT.getSizeInBits()),
3119 EltVT), DAG.getConstant(0, EltVT));
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003120 }
3121 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], NumElems);
3122 break;
3123 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00003124 }
3125 break;
3126 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00003127
Chris Lattner5b359c62005-04-02 04:00:59 +00003128 case ISD::SHL_PARTS:
3129 case ISD::SRA_PARTS:
3130 case ISD::SRL_PARTS: {
Dan Gohman475871a2008-07-27 21:46:04 +00003131 SmallVector<SDValue, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00003132 bool Changed = false;
Duncan Sands92abc622009-01-31 15:50:11 +00003133 unsigned N = Node->getNumOperands();
3134 for (unsigned i = 0; i + 1 < N; ++i) {
Chris Lattner84f67882005-01-20 18:52:28 +00003135 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3136 Changed |= Ops.back() != Node->getOperand(i);
3137 }
Duncan Sands92abc622009-01-31 15:50:11 +00003138 Ops.push_back(LegalizeOp(DAG.getShiftAmountOperand(Node->getOperand(N-1))));
3139 Changed |= Ops.back() != Node->getOperand(N-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003140 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003141 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00003142
Evan Cheng05a2d562006-01-09 18:31:59 +00003143 switch (TLI.getOperationAction(Node->getOpcode(),
3144 Node->getValueType(0))) {
3145 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003146 case TargetLowering::Legal: break;
3147 case TargetLowering::Custom:
3148 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003149 if (Tmp1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003150 SDValue Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00003151 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003152 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman475871a2008-07-27 21:46:04 +00003153 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003154 if (i == Op.getResNo())
Evan Cheng12f22742006-01-19 04:54:52 +00003155 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00003156 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003157 assert(RetVal.getNode() && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00003158 return RetVal;
3159 }
Evan Cheng05a2d562006-01-09 18:31:59 +00003160 break;
3161 }
3162
Chris Lattner2c8086f2005-04-02 05:00:07 +00003163 // Since these produce multiple values, make sure to remember that we
3164 // legalized all of them.
3165 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +00003166 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00003167 return Result.getValue(Op.getResNo());
Chris Lattner84f67882005-01-20 18:52:28 +00003168 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003169
3170 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00003171 case ISD::ADD:
3172 case ISD::SUB:
3173 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00003174 case ISD::MULHS:
3175 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003176 case ISD::UDIV:
3177 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003178 case ISD::AND:
3179 case ISD::OR:
3180 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00003181 case ISD::SHL:
3182 case ISD::SRL:
3183 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00003184 case ISD::FADD:
3185 case ISD::FSUB:
3186 case ISD::FMUL:
3187 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00003188 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003189 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands92abc622009-01-31 15:50:11 +00003190 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003191
3192 if ((Node->getOpcode() == ISD::SHL ||
3193 Node->getOpcode() == ISD::SRL ||
3194 Node->getOpcode() == ISD::SRA) &&
Duncan Sands92abc622009-01-31 15:50:11 +00003195 !Node->getValueType(0).isVector())
3196 Tmp2 = DAG.getShiftAmountOperand(Tmp2);
3197
3198 switch (getTypeAction(Tmp2.getValueType())) {
3199 case Expand: assert(0 && "Not possible");
3200 case Legal:
3201 Tmp2 = LegalizeOp(Tmp2); // Legalize the RHS.
3202 break;
3203 case Promote:
3204 Tmp2 = PromoteOp(Tmp2); // Promote the RHS.
3205 break;
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003206 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003207
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003208 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003209
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003210 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003211 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00003212 case TargetLowering::Legal: break;
3213 case TargetLowering::Custom:
3214 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003215 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003216 Result = Tmp1;
3217 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00003218 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003219 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003220 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003221 MVT VT = Op.getValueType();
Mon P Wang0c397192008-10-30 08:01:45 +00003222
Dan Gohman525178c2007-10-08 18:33:35 +00003223 // See if multiply or divide can be lowered using two-result operations.
3224 SDVTList VTs = DAG.getVTList(VT, VT);
3225 if (Node->getOpcode() == ISD::MUL) {
3226 // We just need the low half of the multiply; try both the signed
3227 // and unsigned forms. If the target supports both SMUL_LOHI and
3228 // UMUL_LOHI, form a preference by checking which forms of plain
3229 // MULH it supports.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003230 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3231 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3232 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3233 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
Dan Gohman525178c2007-10-08 18:33:35 +00003234 unsigned OpToUse = 0;
3235 if (HasSMUL_LOHI && !HasMULHS) {
3236 OpToUse = ISD::SMUL_LOHI;
3237 } else if (HasUMUL_LOHI && !HasMULHU) {
3238 OpToUse = ISD::UMUL_LOHI;
3239 } else if (HasSMUL_LOHI) {
3240 OpToUse = ISD::SMUL_LOHI;
3241 } else if (HasUMUL_LOHI) {
3242 OpToUse = ISD::UMUL_LOHI;
3243 }
3244 if (OpToUse) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003245 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003246 break;
3247 }
3248 }
3249 if (Node->getOpcode() == ISD::MULHS &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003250 TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003251 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3252 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003253 break;
3254 }
3255 if (Node->getOpcode() == ISD::MULHU &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003256 TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003257 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3258 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003259 break;
3260 }
3261 if (Node->getOpcode() == ISD::SDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003262 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003263 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3264 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003265 break;
3266 }
3267 if (Node->getOpcode() == ISD::UDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003268 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003269 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3270 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003271 break;
3272 }
Mon P Wang87c8a8f2008-12-18 20:03:17 +00003273
Dan Gohman82669522007-10-11 23:57:53 +00003274 // Check to see if we have a libcall for this operator.
3275 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3276 bool isSigned = false;
3277 switch (Node->getOpcode()) {
3278 case ISD::UDIV:
3279 case ISD::SDIV:
3280 if (VT == MVT::i32) {
3281 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang0c397192008-10-30 08:01:45 +00003282 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00003283 isSigned = Node->getOpcode() == ISD::SDIV;
3284 }
3285 break;
Chris Lattner31d71612008-10-04 21:27:46 +00003286 case ISD::MUL:
3287 if (VT == MVT::i32)
3288 LC = RTLIB::MUL_I32;
Nate Begeman9b994852009-01-24 22:12:48 +00003289 else if (VT == MVT::i64)
Scott Michel845145f2008-12-29 03:21:37 +00003290 LC = RTLIB::MUL_I64;
Chris Lattner31d71612008-10-04 21:27:46 +00003291 break;
Dan Gohman82669522007-10-11 23:57:53 +00003292 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003293 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3294 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003295 break;
Scott Micheld1e8d9c2009-01-21 04:58:48 +00003296 case ISD::FDIV:
3297 LC = GetFPLibCall(VT, RTLIB::DIV_F32, RTLIB::DIV_F64, RTLIB::DIV_F80,
3298 RTLIB::DIV_PPCF128);
3299 break;
Dan Gohman82669522007-10-11 23:57:53 +00003300 default: break;
3301 }
3302 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00003303 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003304 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003305 break;
3306 }
Mon P Wang0c397192008-10-30 08:01:45 +00003307
Duncan Sands83ec4b62008-06-06 12:08:01 +00003308 assert(Node->getValueType(0).isVector() &&
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003309 "Cannot expand this binary operator!");
3310 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003311 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003312 break;
3313 }
Evan Chengcc987612006-04-12 21:20:24 +00003314 case TargetLowering::Promote: {
3315 switch (Node->getOpcode()) {
3316 default: assert(0 && "Do not know how to promote this BinOp!");
3317 case ISD::AND:
3318 case ISD::OR:
3319 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003320 MVT OVT = Node->getValueType(0);
3321 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3322 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00003323 // Bit convert each of the values to the new type.
3324 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3325 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3326 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3327 // Bit convert the result back the original type.
3328 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3329 break;
3330 }
3331 }
3332 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003333 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003334 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003335
Dan Gohmane14ea862007-10-05 14:17:22 +00003336 case ISD::SMUL_LOHI:
3337 case ISD::UMUL_LOHI:
3338 case ISD::SDIVREM:
3339 case ISD::UDIVREM:
3340 // These nodes will only be produced by target-specific lowering, so
3341 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003342 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003343 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003344
3345 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3346 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3347 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003348 break;
3349
Chris Lattnera09f8482006-03-05 05:09:38 +00003350 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3351 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3352 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3353 case Expand: assert(0 && "Not possible");
3354 case Legal:
3355 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3356 break;
3357 case Promote:
3358 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3359 break;
3360 }
3361
3362 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3363
3364 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3365 default: assert(0 && "Operation not supported");
3366 case TargetLowering::Custom:
3367 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003368 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003369 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003370 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003371 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003372 // If this target supports fabs/fneg natively and select is cheap,
3373 // do this efficiently.
3374 if (!TLI.isSelectExpensive() &&
3375 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3376 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003377 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003378 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003379 // Get the sign bit of the RHS.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003380 MVT IVT =
Chris Lattner8f4191d2006-03-13 06:08:38 +00003381 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman475871a2008-07-27 21:46:04 +00003382 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Duncan Sands5480c042009-01-01 15:52:00 +00003383 SignBit = DAG.getSetCC(TLI.getSetCCResultType(IVT),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003384 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3385 // Get the absolute value of the result.
Dan Gohman475871a2008-07-27 21:46:04 +00003386 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Chris Lattner8f4191d2006-03-13 06:08:38 +00003387 // Select between the nabs and abs value based on the sign bit of
3388 // the input.
3389 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3390 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3391 AbsVal),
3392 AbsVal);
3393 Result = LegalizeOp(Result);
3394 break;
3395 }
3396
3397 // Otherwise, do bitwise ops!
Duncan Sands83ec4b62008-06-06 12:08:01 +00003398 MVT NVT =
Evan Cheng912095b2007-01-04 21:56:39 +00003399 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3400 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3401 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3402 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003403 break;
3404 }
Evan Cheng912095b2007-01-04 21:56:39 +00003405 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003406 break;
3407
Nate Begeman551bf3f2006-02-17 05:43:56 +00003408 case ISD::ADDC:
3409 case ISD::SUBC:
3410 Tmp1 = LegalizeOp(Node->getOperand(0));
3411 Tmp2 = LegalizeOp(Node->getOperand(1));
3412 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003413 Tmp3 = Result.getValue(0);
3414 Tmp4 = Result.getValue(1);
3415
3416 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3417 default: assert(0 && "This action is not supported yet!");
3418 case TargetLowering::Legal:
3419 break;
3420 case TargetLowering::Custom:
3421 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3422 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003423 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003424 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3425 }
3426 break;
3427 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003428 // Since this produces two values, make sure to remember that we legalized
3429 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003430 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3431 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3432 return Op.getResNo() ? Tmp4 : Tmp3;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003433
Nate Begeman551bf3f2006-02-17 05:43:56 +00003434 case ISD::ADDE:
3435 case ISD::SUBE:
3436 Tmp1 = LegalizeOp(Node->getOperand(0));
3437 Tmp2 = LegalizeOp(Node->getOperand(1));
3438 Tmp3 = LegalizeOp(Node->getOperand(2));
3439 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003440 Tmp3 = Result.getValue(0);
3441 Tmp4 = Result.getValue(1);
3442
3443 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3444 default: assert(0 && "This action is not supported yet!");
3445 case TargetLowering::Legal:
3446 break;
3447 case TargetLowering::Custom:
3448 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3449 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003450 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003451 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3452 }
3453 break;
3454 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003455 // Since this produces two values, make sure to remember that we legalized
3456 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003457 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3458 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3459 return Op.getResNo() ? Tmp4 : Tmp3;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003460
Nate Begeman419f8b62005-10-18 00:27:41 +00003461 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003462 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00003463 // TODO: handle the case where the Lo and Hi operands are not of legal type
3464 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3465 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3466 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003467 case TargetLowering::Promote:
3468 case TargetLowering::Custom:
3469 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003470 case TargetLowering::Legal:
3471 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3472 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3473 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003474 case TargetLowering::Expand:
3475 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3476 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3477 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003478 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00003479 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003480 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003481 break;
3482 }
3483 break;
3484 }
3485
Nate Begemanc105e192005-04-06 00:23:54 +00003486 case ISD::UREM:
3487 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003488 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003489 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3490 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003491
Nate Begemanc105e192005-04-06 00:23:54 +00003492 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003493 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3494 case TargetLowering::Custom:
3495 isCustom = true;
3496 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003497 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003498 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003499 if (isCustom) {
3500 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003501 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003502 }
Nate Begemanc105e192005-04-06 00:23:54 +00003503 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003504 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003505 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003506 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003507 MVT VT = Node->getValueType(0);
Dan Gohman525178c2007-10-08 18:33:35 +00003508
3509 // See if remainder can be lowered using two-result operations.
3510 SDVTList VTs = DAG.getVTList(VT, VT);
3511 if (Node->getOpcode() == ISD::SREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003512 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003513 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003514 break;
3515 }
3516 if (Node->getOpcode() == ISD::UREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003517 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003518 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003519 break;
3520 }
3521
Duncan Sands83ec4b62008-06-06 12:08:01 +00003522 if (VT.isInteger()) {
Dan Gohman525178c2007-10-08 18:33:35 +00003523 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003524 TargetLowering::Legal) {
3525 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003526 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003527 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3528 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003529 } else if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003530 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003531 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003532 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003533 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003534 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3535 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman475871a2008-07-27 21:46:04 +00003536 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003537 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003538 }
Dan Gohman0d974262007-11-06 22:11:54 +00003539 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003540 assert(VT.isFloatingPoint() &&
Dan Gohman0d974262007-11-06 22:11:54 +00003541 "remainder op must have integer or floating-point type");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003542 if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003543 Result = LegalizeOp(UnrollVectorOp(Op));
3544 } else {
3545 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003546 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3547 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003548 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003549 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman80176312007-11-05 23:35:22 +00003550 }
Nate Begemanc105e192005-04-06 00:23:54 +00003551 }
3552 break;
3553 }
Dan Gohman525178c2007-10-08 18:33:35 +00003554 }
Nate Begemanc105e192005-04-06 00:23:54 +00003555 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003556 case ISD::VAARG: {
3557 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3558 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3559
Duncan Sands83ec4b62008-06-06 12:08:01 +00003560 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003561 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3562 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003563 case TargetLowering::Custom:
3564 isCustom = true;
3565 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003566 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003567 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3568 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003569 Tmp1 = Result.getValue(1);
3570
3571 if (isCustom) {
3572 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003573 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003574 Result = LegalizeOp(Tmp2);
3575 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3576 }
3577 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003578 break;
3579 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003580 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003581 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003582 // Increment the pointer, VAList, to the next vaarg
Duncan Sands5c58a312008-11-03 11:51:11 +00003583 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00003584 DAG.getConstant(TLI.getTargetData()->
3585 getTypePaddedSize(VT.getTypeForMVT()),
3586 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00003587 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00003588 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003589 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003590 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003591 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003592 Result = LegalizeOp(Result);
3593 break;
3594 }
3595 }
3596 // Since VAARG produces two values, make sure to remember that we
3597 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003598 AddLegalizedOperand(SDValue(Node, 0), Result);
3599 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003600 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003601 }
3602
3603 case ISD::VACOPY:
3604 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3605 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3606 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3607
3608 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3609 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003610 case TargetLowering::Custom:
3611 isCustom = true;
3612 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003613 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003614 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3615 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003616 if (isCustom) {
3617 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003618 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003619 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003620 break;
3621 case TargetLowering::Expand:
3622 // This defaults to loading a pointer from the input and storing it to the
3623 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003624 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3625 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman499c1bd2008-04-17 02:09:26 +00003626 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3627 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003628 break;
3629 }
3630 break;
3631
3632 case ISD::VAEND:
3633 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3634 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3635
3636 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3637 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003638 case TargetLowering::Custom:
3639 isCustom = true;
3640 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003641 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003642 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003643 if (isCustom) {
3644 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003645 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003646 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003647 break;
3648 case TargetLowering::Expand:
3649 Result = Tmp1; // Default to a no-op, return the chain
3650 break;
3651 }
3652 break;
3653
3654 case ISD::VASTART:
3655 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3656 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3657
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003658 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3659
Nate Begemanacc398c2006-01-25 18:21:52 +00003660 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3661 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003662 case TargetLowering::Legal: break;
3663 case TargetLowering::Custom:
3664 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003665 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003666 break;
3667 }
3668 break;
3669
Nate Begeman35ef9132006-01-11 21:21:00 +00003670 case ISD::ROTL:
3671 case ISD::ROTR:
3672 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands92abc622009-01-31 15:50:11 +00003673 Tmp2 = LegalizeOp(DAG.getShiftAmountOperand(Node->getOperand(1))); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003674 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003675 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3676 default:
3677 assert(0 && "ROTL/ROTR legalize operation not supported");
3678 break;
3679 case TargetLowering::Legal:
3680 break;
3681 case TargetLowering::Custom:
3682 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003683 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelc9dc1142007-04-02 21:36:32 +00003684 break;
3685 case TargetLowering::Promote:
3686 assert(0 && "Do not know how to promote ROTL/ROTR");
3687 break;
3688 case TargetLowering::Expand:
3689 assert(0 && "Do not know how to expand ROTL/ROTR");
3690 break;
3691 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003692 break;
3693
Nate Begemand88fc032006-01-14 03:14:10 +00003694 case ISD::BSWAP:
3695 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3696 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003697 case TargetLowering::Custom:
3698 assert(0 && "Cannot custom legalize this yet!");
3699 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003700 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003701 break;
3702 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003703 MVT OVT = Tmp1.getValueType();
3704 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3705 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begemand88fc032006-01-14 03:14:10 +00003706
Chris Lattner456a93a2006-01-28 07:39:30 +00003707 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3708 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3709 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3710 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3711 break;
3712 }
3713 case TargetLowering::Expand:
3714 Result = ExpandBSWAP(Tmp1);
3715 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003716 }
3717 break;
3718
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003719 case ISD::CTPOP:
3720 case ISD::CTTZ:
3721 case ISD::CTLZ:
3722 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3723 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003724 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003725 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003726 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003727 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003728 TargetLowering::Custom) {
3729 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003730 if (Tmp1.getNode()) {
Scott Michel335f4f72007-08-02 02:22:46 +00003731 Result = Tmp1;
3732 }
Scott Michel910b66d2007-07-30 21:00:31 +00003733 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003734 break;
3735 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003736 MVT OVT = Tmp1.getValueType();
3737 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003738
3739 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003740 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3741 // Perform the larger operation, then subtract if needed.
3742 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003743 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003744 case ISD::CTPOP:
3745 Result = Tmp1;
3746 break;
3747 case ISD::CTTZ:
3748 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Duncan Sands5480c042009-01-01 15:52:00 +00003749 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003750 DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003751 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003752 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003753 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003754 break;
3755 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003756 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003757 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003758 DAG.getConstant(NVT.getSizeInBits() -
3759 OVT.getSizeInBits(), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003760 break;
3761 }
3762 break;
3763 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003764 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003765 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003766 break;
3767 }
3768 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003769
Chris Lattner2c8086f2005-04-02 05:00:07 +00003770 // Unary operators
3771 case ISD::FABS:
3772 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003773 case ISD::FSQRT:
3774 case ISD::FSIN:
3775 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003776 case ISD::FLOG:
3777 case ISD::FLOG2:
3778 case ISD::FLOG10:
3779 case ISD::FEXP:
3780 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00003781 case ISD::FTRUNC:
3782 case ISD::FFLOOR:
3783 case ISD::FCEIL:
3784 case ISD::FRINT:
3785 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003786 Tmp1 = LegalizeOp(Node->getOperand(0));
3787 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003788 case TargetLowering::Promote:
3789 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003790 isCustom = true;
3791 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003792 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003793 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003794 if (isCustom) {
3795 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003796 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00003797 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003798 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003799 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003800 switch (Node->getOpcode()) {
3801 default: assert(0 && "Unreachable!");
3802 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003803 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3804 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003805 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003806 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003807 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003808 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003809 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003810 Tmp2 = DAG.getConstantFP(0.0, VT);
Duncan Sands5480c042009-01-01 15:52:00 +00003811 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
3812 Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003813 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3814 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003815 break;
3816 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00003817 case ISD::FSQRT:
3818 case ISD::FSIN:
3819 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003820 case ISD::FLOG:
3821 case ISD::FLOG2:
3822 case ISD::FLOG10:
3823 case ISD::FEXP:
3824 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003825 case ISD::FTRUNC:
3826 case ISD::FFLOOR:
3827 case ISD::FCEIL:
3828 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00003829 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003830 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003831
3832 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003833 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003834 Result = LegalizeOp(UnrollVectorOp(Op));
3835 break;
3836 }
3837
Evan Cheng56966222007-01-12 02:11:51 +00003838 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003839 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003840 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003841 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3842 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003843 break;
3844 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003845 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3846 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003847 break;
3848 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003849 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3850 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003851 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003852 case ISD::FLOG:
3853 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3854 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3855 break;
3856 case ISD::FLOG2:
3857 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3858 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3859 break;
3860 case ISD::FLOG10:
3861 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3862 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3863 break;
3864 case ISD::FEXP:
3865 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3866 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3867 break;
3868 case ISD::FEXP2:
3869 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3870 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3871 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003872 case ISD::FTRUNC:
3873 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3874 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3875 break;
3876 case ISD::FFLOOR:
3877 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3878 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3879 break;
3880 case ISD::FCEIL:
3881 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3882 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3883 break;
3884 case ISD::FRINT:
3885 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3886 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3887 break;
3888 case ISD::FNEARBYINT:
3889 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3890 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3891 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00003892 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003893 default: assert(0 && "Unreachable!");
3894 }
Dan Gohman475871a2008-07-27 21:46:04 +00003895 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003896 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003897 break;
3898 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003899 }
3900 break;
3901 }
3902 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003903 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003904 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003905
3906 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003907 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003908 Result = LegalizeOp(UnrollVectorOp(Op));
3909 break;
3910 }
3911
3912 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003913 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3914 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003915 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003916 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003917 break;
3918 }
Chris Lattner35481892005-12-23 00:16:34 +00003919 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003920 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003921 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3922 Node->getValueType(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003923 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003924 // The input has to be a vector type, we have to either scalarize it, pack
3925 // it, or convert it based on whether the input vector type is legal.
Gabor Greifba36cb52008-08-28 21:40:38 +00003926 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00003927 int InIx = Node->getOperand(0).getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003928 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3929 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohman7f321562007-06-25 16:23:39 +00003930
3931 // Figure out if there is a simple type corresponding to this Vector
3932 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003933 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00003934 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003935 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003936 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3937 LegalizeOp(Node->getOperand(0)));
3938 break;
3939 } else if (NumElems == 1) {
3940 // Turn this into a bit convert of the scalar input.
3941 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3942 ScalarizeVectorOp(Node->getOperand(0)));
3943 break;
3944 } else {
3945 // FIXME: UNIMP! Store then reload
3946 assert(0 && "Cast from unsupported vector type not implemented yet!");
3947 }
Chris Lattner67993f72006-01-23 07:30:46 +00003948 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003949 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3950 Node->getOperand(0).getValueType())) {
3951 default: assert(0 && "Unknown operation action!");
3952 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003953 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3954 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003955 break;
3956 case TargetLowering::Legal:
3957 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003958 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003959 break;
3960 }
3961 }
3962 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003963 case ISD::CONVERT_RNDSAT: {
3964 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3965 switch (CvtCode) {
3966 default: assert(0 && "Unknown cvt code!");
3967 case ISD::CVT_SF:
3968 case ISD::CVT_UF:
Mon P Wang77cdf302008-11-10 20:54:11 +00003969 case ISD::CVT_FF:
Mon P Wang1cd46bb2008-12-09 07:27:39 +00003970 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003971 case ISD::CVT_FS:
3972 case ISD::CVT_FU:
3973 case ISD::CVT_SS:
3974 case ISD::CVT_SU:
3975 case ISD::CVT_US:
3976 case ISD::CVT_UU: {
3977 SDValue DTyOp = Node->getOperand(1);
3978 SDValue STyOp = Node->getOperand(2);
3979 SDValue RndOp = Node->getOperand(3);
3980 SDValue SatOp = Node->getOperand(4);
3981 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3982 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3983 case Legal:
3984 Tmp1 = LegalizeOp(Node->getOperand(0));
3985 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3986 RndOp, SatOp);
3987 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3988 TargetLowering::Custom) {
3989 Tmp1 = TLI.LowerOperation(Result, DAG);
3990 if (Tmp1.getNode()) Result = Tmp1;
3991 }
3992 break;
3993 case Promote:
3994 Result = PromoteOp(Node->getOperand(0));
3995 // For FP, make Op1 a i32
3996
Mon P Wang1cd46bb2008-12-09 07:27:39 +00003997 Result = DAG.getConvertRndSat(Op.getValueType(), Result,
Mon P Wang77cdf302008-11-10 20:54:11 +00003998 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3999 break;
4000 }
4001 break;
4002 }
4003 } // end switch CvtCode
4004 break;
4005 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00004006 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00004007 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004008 case ISD::UINT_TO_FP: {
4009 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman7f8613e2008-08-14 20:04:46 +00004010 Result = LegalizeINT_TO_FP(Result, isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +00004011 Node->getValueType(0), Node->getOperand(0), dl);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004012 break;
4013 }
4014 case ISD::TRUNCATE:
4015 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4016 case Legal:
4017 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel546d7b52008-12-02 19:55:08 +00004018 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
4019 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
4020 case TargetLowering::Custom:
Mon P Wangf67303d2008-12-11 00:44:22 +00004021 isCustom = true;
4022 // FALLTHROUGH
Scott Michel546d7b52008-12-02 19:55:08 +00004023 case TargetLowering::Legal:
Mon P Wangf67303d2008-12-11 00:44:22 +00004024 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4025 if (isCustom) {
4026 Tmp1 = TLI.LowerOperation(Result, DAG);
4027 if (Tmp1.getNode()) Result = Tmp1;
4028 }
4029 break;
Mon P Wang9e5ecb82008-12-12 01:25:51 +00004030 case TargetLowering::Expand:
4031 assert(Result.getValueType().isVector() && "must be vector type");
4032 // Unroll the truncate. We should do better.
4033 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Schellerb0a5cdd2008-12-02 12:12:25 +00004034 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004035 break;
4036 case Expand:
4037 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4038
4039 // Since the result is legal, we should just be able to truncate the low
4040 // part of the source.
4041 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
4042 break;
4043 case Promote:
4044 Result = PromoteOp(Node->getOperand(0));
4045 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
4046 break;
4047 }
4048 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004049
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004050 case ISD::FP_TO_SINT:
4051 case ISD::FP_TO_UINT:
4052 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4053 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00004054 Tmp1 = LegalizeOp(Node->getOperand(0));
4055
Chris Lattner1618beb2005-07-29 00:11:56 +00004056 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
4057 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004058 case TargetLowering::Custom:
4059 isCustom = true;
4060 // FALLTHROUGH
4061 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004062 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004063 if (isCustom) {
4064 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004065 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00004066 }
4067 break;
4068 case TargetLowering::Promote:
4069 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Dale Johannesenaf435272009-02-02 19:03:57 +00004070 Node->getOpcode() == ISD::FP_TO_SINT,
4071 dl);
Chris Lattner456a93a2006-01-28 07:39:30 +00004072 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00004073 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00004074 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004075 SDValue True, False;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004076 MVT VT = Node->getOperand(0).getValueType();
4077 MVT NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00004078 const uint64_t zero[] = {0, 0};
Duncan Sands83ec4b62008-06-06 12:08:01 +00004079 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
4080 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohmanc7773bf2008-02-29 01:44:25 +00004081 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00004082 Tmp2 = DAG.getConstantFP(apf, VT);
Dale Johannesenaf435272009-02-02 19:03:57 +00004083 Tmp3 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
4084 Node->getOperand(0),
Duncan Sands5480c042009-01-01 15:52:00 +00004085 Tmp2, ISD::SETLT);
Dale Johannesenaf435272009-02-02 19:03:57 +00004086 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
4087 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
4088 DAG.getNode(ISD::FSUB, dl, VT,
4089 Node->getOperand(0), Tmp2));
4090 False = DAG.getNode(ISD::XOR, dl, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00004091 DAG.getConstant(x, NVT));
Dale Johannesenaf435272009-02-02 19:03:57 +00004092 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp3, True, False);
Chris Lattner456a93a2006-01-28 07:39:30 +00004093 break;
Nate Begemand2558e32005-08-14 01:20:53 +00004094 } else {
4095 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
4096 }
4097 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00004098 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004099 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00004100 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004101 MVT VT = Op.getValueType();
4102 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004103 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004104 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004105 if (Node->getOpcode() == ISD::FP_TO_SINT) {
Dale Johannesenaf435272009-02-02 19:03:57 +00004106 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
Chris Lattner0bd48932008-01-17 07:00:52 +00004107 Node->getOperand(0), DAG.getValueType(MVT::f64));
Dale Johannesenaf435272009-02-02 19:03:57 +00004108 Result = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Result,
Chris Lattner0bd48932008-01-17 07:00:52 +00004109 DAG.getIntPtrConstant(1));
Dale Johannesenaf435272009-02-02 19:03:57 +00004110 Result = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00004111 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004112 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4113 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4114 Tmp2 = DAG.getConstantFP(apf, OVT);
4115 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4116 // FIXME: generated code sucks.
Dale Johannesenaf435272009-02-02 19:03:57 +00004117 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Node->getOperand(0),
4118 Tmp2,
4119 DAG.getNode(ISD::ADD, dl, MVT::i32,
4120 DAG.getNode(ISD::FP_TO_SINT, dl, VT,
4121 DAG.getNode(ISD::FSUB, dl, OVT,
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004122 Node->getOperand(0), Tmp2)),
4123 DAG.getConstant(0x80000000, MVT::i32)),
Dale Johannesenaf435272009-02-02 19:03:57 +00004124 DAG.getNode(ISD::FP_TO_SINT, dl, VT,
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004125 Node->getOperand(0)),
4126 DAG.getCondCode(ISD::SETGE));
4127 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004128 break;
4129 }
Dan Gohmana2e94852008-03-10 23:03:31 +00004130 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsb2ff8852008-07-17 02:36:29 +00004131 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4132 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4133 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman475871a2008-07-27 21:46:04 +00004134 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00004135 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00004136 break;
4137 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004138 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004139 Tmp1 = PromoteOp(Node->getOperand(0));
4140 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4141 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004142 break;
4143 }
4144 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004145
Chris Lattnerf2670a82008-01-16 06:57:07 +00004146 case ISD::FP_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004147 MVT DstVT = Op.getValueType();
4148 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004149 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4150 // The only other way we can lower this is to turn it into a STORE,
4151 // LOAD pair, targetting a temporary location (a stack slot).
4152 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
4153 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00004154 }
4155 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4156 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4157 case Legal:
4158 Tmp1 = LegalizeOp(Node->getOperand(0));
4159 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4160 break;
4161 case Promote:
4162 Tmp1 = PromoteOp(Node->getOperand(0));
4163 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4164 break;
4165 }
4166 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004167 }
Dale Johannesen5411a392007-08-09 01:04:01 +00004168 case ISD::FP_ROUND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004169 MVT DstVT = Op.getValueType();
4170 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004171 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4172 if (SrcVT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00004173 SDValue Lo;
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004174 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00004175 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004176 if (DstVT!=MVT::f64)
4177 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00004178 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00004179 }
Chris Lattner0bd48932008-01-17 07:00:52 +00004180 // The only other way we can lower this is to turn it into a STORE,
4181 // LOAD pair, targetting a temporary location (a stack slot).
4182 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4183 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00004184 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00004185 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4186 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4187 case Legal:
4188 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004189 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004190 break;
4191 case Promote:
4192 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004193 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4194 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004195 break;
4196 }
4197 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004198 }
Chris Lattner13c78e22005-09-02 00:18:10 +00004199 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004200 case ISD::ZERO_EXTEND:
4201 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004202 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00004203 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004204 case Legal:
4205 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel82747a52008-04-30 00:26:38 +00004206 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel0123b7d2008-02-15 23:05:48 +00004207 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4208 TargetLowering::Custom) {
Scott Michel82747a52008-04-30 00:26:38 +00004209 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004210 if (Tmp1.getNode()) Result = Tmp1;
Scott Michel0123b7d2008-02-15 23:05:48 +00004211 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004212 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004213 case Promote:
4214 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00004215 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004216 Tmp1 = PromoteOp(Node->getOperand(0));
4217 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00004218 break;
Chris Lattner1713e732005-01-16 00:38:00 +00004219 case ISD::ZERO_EXTEND:
4220 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004221 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00004222 Result = DAG.getZeroExtendInReg(Result,
4223 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00004224 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004225 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00004226 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004227 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00004228 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004229 Result,
4230 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00004231 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004232 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004233 }
4234 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00004235 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00004236 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00004237 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004238 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00004239
4240 // If this operation is not supported, convert it to a shl/shr or load/store
4241 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004242 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4243 default: assert(0 && "This action not supported for this op yet!");
4244 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004245 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004246 break;
4247 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00004248 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00004249 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00004250 // NOTE: we could fall back on load/store here too for targets without
4251 // SAR. However, it is doubtful that any exist.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004252 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4253 ExtraVT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00004254 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00004255 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4256 Node->getOperand(0), ShiftCst);
4257 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4258 Result, ShiftCst);
4259 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00004260 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00004261 // EXTLOAD pair, targetting a temporary location (a stack slot).
4262
4263 // NOTE: there is a choice here between constantly creating new stack
4264 // slots and always reusing the same one. We currently always create
4265 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00004266 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4267 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00004268 } else {
4269 assert(0 && "Unknown op");
4270 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004271 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00004272 }
Chris Lattner0f69b292005-01-15 06:18:18 +00004273 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004274 }
Duncan Sands36397f52007-07-27 12:58:54 +00004275 case ISD::TRAMPOLINE: {
Dan Gohman475871a2008-07-27 21:46:04 +00004276 SDValue Ops[6];
Duncan Sands36397f52007-07-27 12:58:54 +00004277 for (unsigned i = 0; i != 6; ++i)
4278 Ops[i] = LegalizeOp(Node->getOperand(i));
4279 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4280 // The only option for this node is to custom lower it.
4281 Result = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004282 assert(Result.getNode() && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00004283
4284 // Since trampoline produces two values, make sure to remember that we
4285 // legalized both of them.
4286 Tmp1 = LegalizeOp(Result.getValue(1));
4287 Result = LegalizeOp(Result);
Dan Gohman475871a2008-07-27 21:46:04 +00004288 AddLegalizedOperand(SDValue(Node, 0), Result);
4289 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00004290 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00004291 }
Dan Gohman9c78a392008-05-14 00:43:10 +00004292 case ISD::FLT_ROUNDS_: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004293 MVT VT = Node->getValueType(0);
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004294 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4295 default: assert(0 && "This action not supported for this op yet!");
4296 case TargetLowering::Custom:
4297 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004298 if (Result.getNode()) break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004299 // Fall Thru
4300 case TargetLowering::Legal:
4301 // If this operation is not supported, lower it to constant 1
4302 Result = DAG.getConstant(1, VT);
4303 break;
4304 }
Dan Gohman9ab9ee82008-05-12 16:07:15 +00004305 break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004306 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004307 case ISD::TRAP: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004308 MVT VT = Node->getValueType(0);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004309 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4310 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00004311 case TargetLowering::Legal:
4312 Tmp1 = LegalizeOp(Node->getOperand(0));
4313 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4314 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004315 case TargetLowering::Custom:
4316 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004317 if (Result.getNode()) break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004318 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00004319 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004320 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00004321 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004322 TargetLowering::ArgListTy Args;
Dan Gohman475871a2008-07-27 21:46:04 +00004323 std::pair<SDValue,SDValue> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00004324 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00004325 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00004326 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Dale Johannesen7d2ad622009-01-30 23:10:59 +00004327 Args, DAG, dl);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004328 Result = CallResult.second;
4329 break;
4330 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004331 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004332 }
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004333
Bill Wendling74c37652008-12-09 22:08:41 +00004334 case ISD::SADDO:
4335 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004336 MVT VT = Node->getValueType(0);
4337 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4338 default: assert(0 && "This action not supported for this op yet!");
4339 case TargetLowering::Custom:
4340 Result = TLI.LowerOperation(Op, DAG);
4341 if (Result.getNode()) break;
4342 // FALLTHROUGH
4343 case TargetLowering::Legal: {
4344 SDValue LHS = LegalizeOp(Node->getOperand(0));
4345 SDValue RHS = LegalizeOp(Node->getOperand(1));
4346
Bill Wendling74c37652008-12-09 22:08:41 +00004347 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
4348 ISD::ADD : ISD::SUB, LHS.getValueType(),
4349 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004350 MVT OType = Node->getValueType(1);
4351
Bill Wendlinga6af91a2008-11-25 08:19:22 +00004352 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004353
Bill Wendling740464e2008-11-25 19:40:17 +00004354 // LHSSign -> LHS >= 0
4355 // RHSSign -> RHS >= 0
4356 // SumSign -> Sum >= 0
4357 //
Bill Wendling74c37652008-12-09 22:08:41 +00004358 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00004359 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00004360 // Sub:
4361 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00004362 //
4363 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4364 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
Bill Wendling74c37652008-12-09 22:08:41 +00004365 SDValue SignsMatch = DAG.getSetCC(OType, LHSSign, RHSSign,
4366 Node->getOpcode() == ISD::SADDO ?
4367 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004368
Bill Wendling740464e2008-11-25 19:40:17 +00004369 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4370 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004371
Bill Wendling74c37652008-12-09 22:08:41 +00004372 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004373
4374 MVT ValueVTs[] = { LHS.getValueType(), OType };
4375 SDValue Ops[] = { Sum, Cmp };
4376
Duncan Sandsaaffa052008-12-01 11:41:29 +00004377 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4378 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004379 SDNode *RNode = Result.getNode();
4380 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4381 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4382 break;
4383 }
4384 }
4385
4386 break;
4387 }
Bill Wendling74c37652008-12-09 22:08:41 +00004388 case ISD::UADDO:
4389 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00004390 MVT VT = Node->getValueType(0);
4391 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4392 default: assert(0 && "This action not supported for this op yet!");
4393 case TargetLowering::Custom:
4394 Result = TLI.LowerOperation(Op, DAG);
4395 if (Result.getNode()) break;
4396 // FALLTHROUGH
4397 case TargetLowering::Legal: {
4398 SDValue LHS = LegalizeOp(Node->getOperand(0));
4399 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004400
Bill Wendling74c37652008-12-09 22:08:41 +00004401 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
4402 ISD::ADD : ISD::SUB, LHS.getValueType(),
4403 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004404 MVT OType = Node->getValueType(1);
Bill Wendling74c37652008-12-09 22:08:41 +00004405 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS,
4406 Node->getOpcode () == ISD::UADDO ?
4407 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004408
Bill Wendling41ea7e72008-11-24 19:21:46 +00004409 MVT ValueVTs[] = { LHS.getValueType(), OType };
4410 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004411
Duncan Sandsaaffa052008-12-01 11:41:29 +00004412 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4413 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004414 SDNode *RNode = Result.getNode();
4415 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4416 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4417 break;
4418 }
4419 }
4420
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004421 break;
4422 }
Bill Wendling74c37652008-12-09 22:08:41 +00004423 case ISD::SMULO:
4424 case ISD::UMULO: {
4425 MVT VT = Node->getValueType(0);
4426 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4427 default: assert(0 && "This action is not supported at all!");
4428 case TargetLowering::Custom:
4429 Result = TLI.LowerOperation(Op, DAG);
4430 if (Result.getNode()) break;
4431 // Fall Thru
4432 case TargetLowering::Legal:
4433 // FIXME: According to Hacker's Delight, this can be implemented in
4434 // target independent lowering, but it would be inefficient, since it
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00004435 // requires a division + a branch.
Bill Wendling74c37652008-12-09 22:08:41 +00004436 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4437 break;
4438 }
4439 break;
4440 }
4441
Chris Lattner45b8caf2005-01-15 07:15:18 +00004442 }
Chris Lattner456a93a2006-01-28 07:39:30 +00004443
Chris Lattner4ddd2832006-04-08 04:13:17 +00004444 assert(Result.getValueType() == Op.getValueType() &&
4445 "Bad legalization!");
4446
Chris Lattner456a93a2006-01-28 07:39:30 +00004447 // Make sure that the generated code is itself legal.
4448 if (Result != Op)
4449 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004450
Chris Lattner45982da2005-05-12 16:53:42 +00004451 // Note that LegalizeOp may be reentered even from single-use nodes, which
4452 // means that we always must cache transformed nodes.
4453 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004454 return Result;
4455}
4456
Chris Lattner8b6fa222005-01-15 22:16:26 +00004457/// PromoteOp - Given an operation that produces a value in an invalid type,
4458/// promote it to compute the value into a larger type. The produced value will
4459/// have the correct bits for the low portion of the register, but no guarantee
4460/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman475871a2008-07-27 21:46:04 +00004461SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004462 MVT VT = Op.getValueType();
4463 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004464 assert(getTypeAction(VT) == Promote &&
4465 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004466 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner03c85462005-01-15 05:21:40 +00004467 "Cannot promote to smaller type!");
4468
Dan Gohman475871a2008-07-27 21:46:04 +00004469 SDValue Tmp1, Tmp2, Tmp3;
4470 SDValue Result;
Gabor Greifba36cb52008-08-28 21:40:38 +00004471 SDNode *Node = Op.getNode();
Chris Lattner03c85462005-01-15 05:21:40 +00004472
Dan Gohman475871a2008-07-27 21:46:04 +00004473 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004474 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004475
Chris Lattner03c85462005-01-15 05:21:40 +00004476 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004477 case ISD::CopyFromReg:
4478 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004479 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004480#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004481 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004482#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004483 assert(0 && "Do not know how to promote this operator!");
4484 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004485 case ISD::UNDEF:
4486 Result = DAG.getNode(ISD::UNDEF, NVT);
4487 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004488 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004489 if (VT != MVT::i1)
4490 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4491 else
4492 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004493 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4494 break;
4495 case ISD::ConstantFP:
4496 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4497 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4498 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004499
Duncan Sands5480c042009-01-01 15:52:00 +00004500 case ISD::SETCC: {
4501 MVT VT0 = Node->getOperand(0).getValueType();
4502 assert(isTypeLegal(TLI.getSetCCResultType(VT0))
Nate Begeman5922f562008-03-14 00:53:31 +00004503 && "SetCC type is not legal??");
Duncan Sands5480c042009-01-01 15:52:00 +00004504 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(VT0),
Nate Begeman5922f562008-03-14 00:53:31 +00004505 Node->getOperand(0), Node->getOperand(1),
4506 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004507 break;
Duncan Sands5480c042009-01-01 15:52:00 +00004508 }
Chris Lattner03c85462005-01-15 05:21:40 +00004509 case ISD::TRUNCATE:
4510 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4511 case Legal:
4512 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004513 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner03c85462005-01-15 05:21:40 +00004514 "This truncation doesn't make sense!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004515 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Chris Lattner03c85462005-01-15 05:21:40 +00004516 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4517 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004518 case Promote:
4519 // The truncation is not required, because we don't guarantee anything
4520 // about high bits anyway.
4521 Result = PromoteOp(Node->getOperand(0));
4522 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004523 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004524 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4525 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004526 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004527 }
4528 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004529 case ISD::SIGN_EXTEND:
4530 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004531 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004532 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4533 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4534 case Legal:
4535 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004536 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004537 break;
4538 case Promote:
4539 // Promote the reg if it's smaller.
4540 Result = PromoteOp(Node->getOperand(0));
4541 // The high bits are not guaranteed to be anything. Insert an extend.
4542 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004543 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004544 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004545 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004546 Result = DAG.getZeroExtendInReg(Result,
4547 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004548 break;
4549 }
4550 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00004551 case ISD::CONVERT_RNDSAT: {
4552 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4553 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4554 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4555 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4556 "can only promote integers");
4557 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4558 Node->getOperand(1), Node->getOperand(2),
4559 Node->getOperand(3), Node->getOperand(4),
4560 CvtCode);
4561 break;
4562
4563 }
Chris Lattner35481892005-12-23 00:16:34 +00004564 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004565 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4566 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004567 Result = PromoteOp(Result);
4568 break;
4569
Chris Lattner8b6fa222005-01-15 22:16:26 +00004570 case ISD::FP_EXTEND:
4571 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4572 case ISD::FP_ROUND:
4573 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4574 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4575 case Promote: assert(0 && "Unreachable with 2 FP types!");
4576 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004577 if (Node->getConstantOperandVal(1) == 0) {
4578 // Input is legal? Do an FP_ROUND_INREG.
4579 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4580 DAG.getValueType(VT));
4581 } else {
4582 // Just remove the truncate, it isn't affecting the value.
4583 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4584 Node->getOperand(1));
4585 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004586 break;
4587 }
4588 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004589 case ISD::SINT_TO_FP:
4590 case ISD::UINT_TO_FP:
4591 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4592 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004593 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004594 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004595 break;
4596
4597 case Promote:
4598 Result = PromoteOp(Node->getOperand(0));
4599 if (Node->getOpcode() == ISD::SINT_TO_FP)
4600 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004601 Result,
4602 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004603 else
Chris Lattner23993562005-04-13 02:38:47 +00004604 Result = DAG.getZeroExtendInReg(Result,
4605 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004606 // No extra round required here.
4607 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004608 break;
4609 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004610 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00004611 Node->getOperand(0), Node->getDebugLoc());
Chris Lattner77e77a62005-01-21 06:05:23 +00004612 // Round if we cannot tolerate excess precision.
4613 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004614 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4615 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004616 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004617 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004618 break;
4619
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004620 case ISD::SIGN_EXTEND_INREG:
4621 Result = PromoteOp(Node->getOperand(0));
4622 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4623 Node->getOperand(1));
4624 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004625 case ISD::FP_TO_SINT:
4626 case ISD::FP_TO_UINT:
4627 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4628 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004629 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004630 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004631 break;
4632 case Promote:
4633 // The input result is prerounded, so we don't have to do anything
4634 // special.
4635 Tmp1 = PromoteOp(Node->getOperand(0));
4636 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004637 }
Nate Begemand2558e32005-08-14 01:20:53 +00004638 // If we're promoting a UINT to a larger size, check to see if the new node
4639 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4640 // we can use that instead. This allows us to generate better code for
4641 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4642 // legal, such as PowerPC.
4643 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004644 !TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NVT) &&
4645 (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT) ||
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004646 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004647 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4648 } else {
4649 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4650 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004651 break;
4652
Chris Lattner2c8086f2005-04-02 05:00:07 +00004653 case ISD::FABS:
4654 case ISD::FNEG:
4655 Tmp1 = PromoteOp(Node->getOperand(0));
4656 assert(Tmp1.getValueType() == NVT);
4657 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4658 // NOTE: we do not have to do any extra rounding here for
4659 // NoExcessFPPrecision, because we know the input will have the appropriate
4660 // precision, and these operations don't modify precision at all.
4661 break;
4662
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004663 case ISD::FLOG:
4664 case ISD::FLOG2:
4665 case ISD::FLOG10:
4666 case ISD::FEXP:
4667 case ISD::FEXP2:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004668 case ISD::FSQRT:
4669 case ISD::FSIN:
4670 case ISD::FCOS:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00004671 case ISD::FTRUNC:
4672 case ISD::FFLOOR:
4673 case ISD::FCEIL:
4674 case ISD::FRINT:
4675 case ISD::FNEARBYINT:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004676 Tmp1 = PromoteOp(Node->getOperand(0));
4677 assert(Tmp1.getValueType() == NVT);
4678 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004679 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004680 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4681 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004682 break;
4683
Evan Cheng9d24ac52008-09-09 23:35:53 +00004684 case ISD::FPOW:
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004685 case ISD::FPOWI: {
Evan Cheng9d24ac52008-09-09 23:35:53 +00004686 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004687 // directly as well, which may be better.
4688 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng9d24ac52008-09-09 23:35:53 +00004689 Tmp2 = Node->getOperand(1);
4690 if (Node->getOpcode() == ISD::FPOW)
4691 Tmp2 = PromoteOp(Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004692 assert(Tmp1.getValueType() == NVT);
Evan Cheng9d24ac52008-09-09 23:35:53 +00004693 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004694 if (NoExcessFPPrecision)
4695 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4696 DAG.getValueType(VT));
4697 break;
4698 }
4699
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004700 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang28873102008-06-25 08:15:39 +00004701 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004702 Tmp2 = PromoteOp(Node->getOperand(2));
4703 Tmp3 = PromoteOp(Node->getOperand(3));
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004704 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getMemoryVT(),
4705 AtomNode->getChain(),
Mon P Wang28873102008-06-25 08:15:39 +00004706 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004707 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004708 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004709 // Remember that we legalized the chain.
4710 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4711 break;
4712 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004713 case ISD::ATOMIC_LOAD_ADD:
4714 case ISD::ATOMIC_LOAD_SUB:
4715 case ISD::ATOMIC_LOAD_AND:
4716 case ISD::ATOMIC_LOAD_OR:
4717 case ISD::ATOMIC_LOAD_XOR:
4718 case ISD::ATOMIC_LOAD_NAND:
4719 case ISD::ATOMIC_LOAD_MIN:
4720 case ISD::ATOMIC_LOAD_MAX:
4721 case ISD::ATOMIC_LOAD_UMIN:
4722 case ISD::ATOMIC_LOAD_UMAX:
4723 case ISD::ATOMIC_SWAP: {
Mon P Wang28873102008-06-25 08:15:39 +00004724 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004725 Tmp2 = PromoteOp(Node->getOperand(2));
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004726 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getMemoryVT(),
4727 AtomNode->getChain(),
Mon P Wang28873102008-06-25 08:15:39 +00004728 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004729 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004730 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004731 // Remember that we legalized the chain.
4732 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4733 break;
4734 }
4735
Chris Lattner03c85462005-01-15 05:21:40 +00004736 case ISD::AND:
4737 case ISD::OR:
4738 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004739 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004740 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004741 case ISD::MUL:
4742 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004743 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004744 // that too is okay if they are integer operations.
4745 Tmp1 = PromoteOp(Node->getOperand(0));
4746 Tmp2 = PromoteOp(Node->getOperand(1));
4747 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4748 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004749 break;
4750 case ISD::FADD:
4751 case ISD::FSUB:
4752 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004753 Tmp1 = PromoteOp(Node->getOperand(0));
4754 Tmp2 = PromoteOp(Node->getOperand(1));
4755 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4756 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4757
4758 // Floating point operations will give excess precision that we may not be
4759 // able to tolerate. If we DO allow excess precision, just leave it,
4760 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004761 // FIXME: Why would we need to round FP ops more than integer ones?
4762 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004763 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004764 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4765 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004766 break;
4767
Chris Lattner8b6fa222005-01-15 22:16:26 +00004768 case ISD::SDIV:
4769 case ISD::SREM:
4770 // These operators require that their input be sign extended.
4771 Tmp1 = PromoteOp(Node->getOperand(0));
4772 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004773 if (NVT.isInteger()) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004774 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4775 DAG.getValueType(VT));
4776 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4777 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004778 }
4779 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4780
4781 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004782 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004783 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4784 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004785 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004786 case ISD::FDIV:
4787 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004788 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004789 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004790 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004791 case Expand: assert(0 && "not implemented");
4792 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4793 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004794 }
4795 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004796 case Expand: assert(0 && "not implemented");
4797 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4798 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004799 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004800 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4801
4802 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004803 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004804 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4805 DAG.getValueType(VT));
4806 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004807
4808 case ISD::UDIV:
4809 case ISD::UREM:
4810 // These operators require that their input be zero extended.
4811 Tmp1 = PromoteOp(Node->getOperand(0));
4812 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004813 assert(NVT.isInteger() && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004814 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4815 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004816 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4817 break;
4818
4819 case ISD::SHL:
4820 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004821 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004822 break;
4823 case ISD::SRA:
4824 // The input value must be properly sign extended.
4825 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004826 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4827 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004828 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004829 break;
4830 case ISD::SRL:
4831 // The input value must be properly zero extended.
4832 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004833 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004834 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004835 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004836
4837 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004838 Tmp1 = Node->getOperand(0); // Get the chain.
4839 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004840 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4841 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands126d9072008-07-04 11:47:58 +00004842 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman0aed7842006-01-28 03:14:31 +00004843 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004844 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004845 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004846 // Increment the pointer, VAList, to the next vaarg
4847 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004848 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman0aed7842006-01-28 03:14:31 +00004849 TLI.getPointerTy()));
4850 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00004851 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004852 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004853 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004854 }
4855 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004856 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004857 break;
4858
Evan Cheng466685d2006-10-09 20:57:25 +00004859 case ISD::LOAD: {
4860 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004861 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4862 ? ISD::EXTLOAD : LD->getExtensionType();
4863 Result = DAG.getExtLoad(ExtType, NVT,
4864 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004865 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004866 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004867 LD->isVolatile(),
4868 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004869 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004870 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004871 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004872 }
Scott Michel8bf61e82008-06-02 22:18:03 +00004873 case ISD::SELECT: {
Chris Lattner03c85462005-01-15 05:21:40 +00004874 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4875 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel8bf61e82008-06-02 22:18:03 +00004876
Duncan Sands83ec4b62008-06-06 12:08:01 +00004877 MVT VT2 = Tmp2.getValueType();
Scott Michel8bf61e82008-06-02 22:18:03 +00004878 assert(VT2 == Tmp3.getValueType()
Scott Michelba12f572008-06-03 19:13:20 +00004879 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4880 // Ensure that the resulting node is at least the same size as the operands'
4881 // value types, because we cannot assume that TLI.getSetCCValueType() is
4882 // constant.
4883 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004884 break;
Scott Michel8bf61e82008-06-02 22:18:03 +00004885 }
Nate Begeman9373a812005-08-10 20:51:12 +00004886 case ISD::SELECT_CC:
4887 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4888 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4889 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004890 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004891 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004892 case ISD::BSWAP:
4893 Tmp1 = Node->getOperand(0);
4894 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4895 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4896 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004897 DAG.getConstant(NVT.getSizeInBits() -
4898 VT.getSizeInBits(),
Nate Begemand88fc032006-01-14 03:14:10 +00004899 TLI.getShiftAmountTy()));
4900 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004901 case ISD::CTPOP:
4902 case ISD::CTTZ:
4903 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004904 // Zero extend the argument
4905 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004906 // Perform the larger operation, then subtract if needed.
4907 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004908 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004909 case ISD::CTPOP:
4910 Result = Tmp1;
4911 break;
4912 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004913 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Duncan Sands5480c042009-01-01 15:52:00 +00004914 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004915 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanb55757e2007-05-18 17:52:13 +00004916 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004917 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004918 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004919 break;
4920 case ISD::CTLZ:
4921 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004922 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004923 DAG.getConstant(NVT.getSizeInBits() -
4924 VT.getSizeInBits(), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004925 break;
4926 }
4927 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004928 case ISD::EXTRACT_SUBVECTOR:
4929 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004930 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004931 case ISD::EXTRACT_VECTOR_ELT:
4932 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4933 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004934 }
4935
Gabor Greifba36cb52008-08-28 21:40:38 +00004936 assert(Result.getNode() && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004937
4938 // Make sure the result is itself legal.
4939 Result = LegalizeOp(Result);
4940
4941 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004942 AddPromotedOperand(Op, Result);
4943 return Result;
4944}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004945
Dan Gohman7f321562007-06-25 16:23:39 +00004946/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4947/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4948/// based on the vector type. The return type of this matches the element type
4949/// of the vector, which may not be legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00004950SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004951 // We know that operand #0 is the Vec vector. If the index is a constant
4952 // or if the invec is a supported hardware type, we can use it. Otherwise,
4953 // lower to a store then an indexed load.
Dan Gohman475871a2008-07-27 21:46:04 +00004954 SDValue Vec = Op.getOperand(0);
4955 SDValue Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004956
Duncan Sands83ec4b62008-06-06 12:08:01 +00004957 MVT TVT = Vec.getValueType();
4958 unsigned NumElems = TVT.getVectorNumElements();
Chris Lattner15972212006-03-31 17:55:51 +00004959
Dan Gohman7f321562007-06-25 16:23:39 +00004960 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4961 default: assert(0 && "This action is not supported yet!");
4962 case TargetLowering::Custom: {
4963 Vec = LegalizeOp(Vec);
4964 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004965 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004966 if (Tmp3.getNode())
Dan Gohman7f321562007-06-25 16:23:39 +00004967 return Tmp3;
4968 break;
4969 }
4970 case TargetLowering::Legal:
4971 if (isTypeLegal(TVT)) {
4972 Vec = LegalizeOp(Vec);
4973 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004974 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004975 }
4976 break;
Mon P Wang0c397192008-10-30 08:01:45 +00004977 case TargetLowering::Promote:
4978 assert(TVT.isVector() && "not vector type");
4979 // fall thru to expand since vectors are by default are promote
Dan Gohman7f321562007-06-25 16:23:39 +00004980 case TargetLowering::Expand:
4981 break;
4982 }
4983
4984 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004985 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004986 Op = ScalarizeVectorOp(Vec);
4987 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00004988 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00004989 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004990 SDValue Lo, Hi;
Chris Lattner15972212006-03-31 17:55:51 +00004991 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004992 if (CIdx->getZExtValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00004993 Vec = Lo;
4994 } else {
4995 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004996 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00004997 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004998 }
Dan Gohman7f321562007-06-25 16:23:39 +00004999
Chris Lattner15972212006-03-31 17:55:51 +00005000 // It's now an extract from the appropriate high or low part. Recurse.
5001 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00005002 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00005003 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00005004 // Store the value to a temporary stack slot, then LOAD the scalar
5005 // element back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005006 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
5007 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohman7f321562007-06-25 16:23:39 +00005008
5009 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005010 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohman7f321562007-06-25 16:23:39 +00005011 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
5012 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005013
Duncan Sands8e4eb092008-06-08 20:54:56 +00005014 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00005015 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005016 else
Chris Lattnerf185e672007-10-19 16:47:35 +00005017 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005018
Dan Gohman7f321562007-06-25 16:23:39 +00005019 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
5020
5021 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00005022 }
Dan Gohman7f321562007-06-25 16:23:39 +00005023 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00005024}
5025
Dan Gohman7f321562007-06-25 16:23:39 +00005026/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00005027/// we assume the operation can be split if it is not already legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005028SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman65956352007-06-13 15:12:02 +00005029 // We know that operand #0 is the Vec vector. For now we assume the index
5030 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman475871a2008-07-27 21:46:04 +00005031 SDValue Vec = Op.getOperand(0);
5032 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00005033
Duncan Sands83ec4b62008-06-06 12:08:01 +00005034 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohman65956352007-06-13 15:12:02 +00005035
Duncan Sands83ec4b62008-06-06 12:08:01 +00005036 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman65956352007-06-13 15:12:02 +00005037 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00005038 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00005039 }
5040
5041 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00005042 SDValue Lo, Hi;
Dan Gohman65956352007-06-13 15:12:02 +00005043 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005044 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohman65956352007-06-13 15:12:02 +00005045 Vec = Lo;
5046 } else {
5047 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005048 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
5049 Idx.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00005050 }
5051
5052 // It's now an extract from the appropriate high or low part. Recurse.
5053 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00005054 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00005055}
5056
Nate Begeman750ac1b2006-02-01 07:19:44 +00005057/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
5058/// with condition CC on the current target. This usually involves legalizing
5059/// or promoting the arguments. In the case where LHS and RHS must be expanded,
5060/// there may be no choice but to create a new SetCC node to represent the
5061/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00005062/// LHS, and the SDValue returned in RHS has a nil SDNode value.
5063void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
5064 SDValue &RHS,
5065 SDValue &CC) {
5066 SDValue Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005067
5068 switch (getTypeAction(LHS.getValueType())) {
5069 case Legal:
5070 Tmp1 = LegalizeOp(LHS); // LHS
5071 Tmp2 = LegalizeOp(RHS); // RHS
5072 break;
5073 case Promote:
5074 Tmp1 = PromoteOp(LHS); // LHS
5075 Tmp2 = PromoteOp(RHS); // RHS
5076
5077 // If this is an FP compare, the operands have already been extended.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005078 if (LHS.getValueType().isInteger()) {
5079 MVT VT = LHS.getValueType();
5080 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00005081
5082 // Otherwise, we have to insert explicit sign or zero extends. Note
5083 // that we could insert sign extends for ALL conditions, but zero extend
5084 // is cheaper on many machines (an AND instead of two shifts), so prefer
5085 // it.
5086 switch (cast<CondCodeSDNode>(CC)->get()) {
5087 default: assert(0 && "Unknown integer comparison!");
5088 case ISD::SETEQ:
5089 case ISD::SETNE:
5090 case ISD::SETUGE:
5091 case ISD::SETUGT:
5092 case ISD::SETULE:
5093 case ISD::SETULT:
5094 // ALL of these operations will work if we either sign or zero extend
5095 // the operands (including the unsigned comparisons!). Zero extend is
5096 // usually a simpler/cheaper operation, so prefer it.
5097 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
5098 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
5099 break;
5100 case ISD::SETGE:
5101 case ISD::SETGT:
5102 case ISD::SETLT:
5103 case ISD::SETLE:
5104 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
5105 DAG.getValueType(VT));
5106 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
5107 DAG.getValueType(VT));
Evan Chengefa53392008-10-13 18:46:18 +00005108 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5109 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Nate Begeman750ac1b2006-02-01 07:19:44 +00005110 break;
5111 }
5112 }
5113 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005114 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005115 MVT VT = LHS.getValueType();
Evan Cheng2b49c502006-12-15 02:59:56 +00005116 if (VT == MVT::f32 || VT == MVT::f64) {
5117 // Expand into one or more soft-fp libcall(s).
Evan Cheng6518c6e2008-07-01 21:35:46 +00005118 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00005119 switch (cast<CondCodeSDNode>(CC)->get()) {
5120 case ISD::SETEQ:
5121 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00005122 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005123 break;
5124 case ISD::SETNE:
5125 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00005126 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005127 break;
5128 case ISD::SETGE:
5129 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00005130 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005131 break;
5132 case ISD::SETLT:
5133 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00005134 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005135 break;
5136 case ISD::SETLE:
5137 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00005138 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005139 break;
5140 case ISD::SETGT:
5141 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00005142 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005143 break;
5144 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00005145 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5146 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005147 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00005148 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005149 break;
5150 default:
Evan Cheng56966222007-01-12 02:11:51 +00005151 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005152 switch (cast<CondCodeSDNode>(CC)->get()) {
5153 case ISD::SETONE:
5154 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00005155 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005156 // Fallthrough
5157 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00005158 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005159 break;
5160 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00005161 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005162 break;
5163 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00005164 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005165 break;
5166 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00005167 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005168 break;
Evan Cheng56966222007-01-12 02:11:51 +00005169 case ISD::SETUEQ:
5170 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00005171 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005172 default: assert(0 && "Unsupported FP setcc!");
5173 }
5174 }
Duncan Sandsf9516202008-06-30 10:19:09 +00005175
Dan Gohman475871a2008-07-27 21:46:04 +00005176 SDValue Dummy;
5177 SDValue Ops[2] = { LHS, RHS };
Gabor Greifba36cb52008-08-28 21:40:38 +00005178 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005179 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00005180 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00005181 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00005182 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Duncan Sands5480c042009-01-01 15:52:00 +00005183 Tmp1 = DAG.getNode(ISD::SETCC,
5184 TLI.getSetCCResultType(Tmp1.getValueType()),
5185 Tmp1, Tmp2, CC);
Gabor Greifba36cb52008-08-28 21:40:38 +00005186 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005187 false /*sign irrelevant*/, Dummy);
Duncan Sands5480c042009-01-01 15:52:00 +00005188 Tmp2 = DAG.getNode(ISD::SETCC,
5189 TLI.getSetCCResultType(LHS.getValueType()), LHS,
5190 Tmp2, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00005191 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman475871a2008-07-27 21:46:04 +00005192 Tmp2 = SDValue();
Evan Cheng2b49c502006-12-15 02:59:56 +00005193 }
Evan Cheng21cacc42008-07-07 07:18:09 +00005194 LHS = LegalizeOp(Tmp1);
Evan Cheng2b49c502006-12-15 02:59:56 +00005195 RHS = Tmp2;
5196 return;
5197 }
5198
Dan Gohman475871a2008-07-27 21:46:04 +00005199 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005200 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005201 ExpandOp(RHS, RHSLo, RHSHi);
5202 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5203
5204 if (VT==MVT::ppcf128) {
5205 // FIXME: This generated code sucks. We want to generate
Dale Johannesene2f20832008-09-12 00:30:56 +00005206 // FCMPU crN, hi1, hi2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005207 // BNE crN, L:
Dale Johannesene2f20832008-09-12 00:30:56 +00005208 // FCMPU crN, lo1, lo2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005209 // The following can be improved, but not that much.
Duncan Sands5480c042009-01-01 15:52:00 +00005210 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5211 LHSHi, RHSHi, ISD::SETOEQ);
5212 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5213 LHSLo, RHSLo, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005214 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Duncan Sands5480c042009-01-01 15:52:00 +00005215 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5216 LHSHi, RHSHi, ISD::SETUNE);
5217 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5218 LHSHi, RHSHi, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005219 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5220 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00005221 Tmp2 = SDValue();
Dale Johannesen638ccd52007-10-06 01:24:11 +00005222 break;
5223 }
5224
5225 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005226 case ISD::SETEQ:
5227 case ISD::SETNE:
5228 if (RHSLo == RHSHi)
5229 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5230 if (RHSCST->isAllOnesValue()) {
5231 // Comparison to -1.
5232 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5233 Tmp2 = RHSLo;
5234 break;
5235 }
5236
5237 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5238 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5239 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5240 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5241 break;
5242 default:
5243 // If this is a comparison of the sign bit, just look at the top part.
5244 // X > -1, x < 0
5245 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5246 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005247 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00005248 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5249 CST->isAllOnesValue())) { // X > -1
5250 Tmp1 = LHSHi;
5251 Tmp2 = RHSHi;
5252 break;
5253 }
5254
5255 // FIXME: This generated code sucks.
5256 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00005257 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005258 default: assert(0 && "Unknown integer setcc!");
5259 case ISD::SETLT:
5260 case ISD::SETULT: LowCC = ISD::SETULT; break;
5261 case ISD::SETGT:
5262 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5263 case ISD::SETLE:
5264 case ISD::SETULE: LowCC = ISD::SETULE; break;
5265 case ISD::SETGE:
5266 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5267 }
5268
5269 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5270 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5271 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5272
5273 // NOTE: on targets without efficient SELECT of bools, we can always use
5274 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00005275 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Duncan Sands5480c042009-01-01 15:52:00 +00005276 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5277 LHSLo, RHSLo, LowCC, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005278 if (!Tmp1.getNode())
Duncan Sands5480c042009-01-01 15:52:00 +00005279 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5280 LHSLo, RHSLo, LowCC);
5281 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5282 LHSHi, RHSHi, CCCode, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005283 if (!Tmp2.getNode())
Duncan Sands5480c042009-01-01 15:52:00 +00005284 Tmp2 = DAG.getNode(ISD::SETCC,
5285 TLI.getSetCCResultType(LHSHi.getValueType()),
5286 LHSHi, RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00005287
Gabor Greifba36cb52008-08-28 21:40:38 +00005288 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5289 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman002e5d02008-03-13 22:13:53 +00005290 if ((Tmp1C && Tmp1C->isNullValue()) ||
5291 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00005292 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5293 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00005294 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00005295 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5296 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5297 // low part is known false, returns high part.
5298 // For LE / GE, if high part is known false, ignore the low part.
5299 // For LT / GT, if high part is known true, ignore the low part.
5300 Tmp1 = Tmp2;
Dan Gohman475871a2008-07-27 21:46:04 +00005301 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005302 } else {
Duncan Sands5480c042009-01-01 15:52:00 +00005303 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5304 LHSHi, RHSHi, ISD::SETEQ, false,
5305 DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005306 if (!Result.getNode())
Duncan Sands5480c042009-01-01 15:52:00 +00005307 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5308 LHSHi, RHSHi, ISD::SETEQ);
Evan Cheng2e677812007-02-08 22:16:19 +00005309 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5310 Result, Tmp1, Tmp2));
5311 Tmp1 = Result;
Dan Gohman475871a2008-07-27 21:46:04 +00005312 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005313 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005314 }
5315 }
Evan Cheng2b49c502006-12-15 02:59:56 +00005316 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005317 LHS = Tmp1;
5318 RHS = Tmp2;
5319}
5320
Evan Cheng7f042682008-10-15 02:05:31 +00005321/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5322/// condition code CC on the current target. This routine assumes LHS and rHS
5323/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5324/// illegal condition code into AND / OR of multiple SETCC values.
5325void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5326 SDValue &LHS, SDValue &RHS,
5327 SDValue &CC) {
5328 MVT OpVT = LHS.getValueType();
5329 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5330 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5331 default: assert(0 && "Unknown condition code action!");
5332 case TargetLowering::Legal:
5333 // Nothing to do.
5334 break;
5335 case TargetLowering::Expand: {
5336 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5337 unsigned Opc = 0;
5338 switch (CCCode) {
5339 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00005340 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5341 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5342 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5343 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5344 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5345 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5346 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5347 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5348 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5349 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5350 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5351 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00005352 // FIXME: Implement more expansions.
5353 }
5354
5355 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5356 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5357 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5358 RHS = SDValue();
5359 CC = SDValue();
5360 break;
5361 }
5362 }
5363}
5364
Chris Lattner1401d152008-01-16 07:45:30 +00005365/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5366/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5367/// a load from the stack slot to DestVT, extending it if needed.
5368/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005369SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5370 MVT SlotVT,
5371 MVT DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00005372 // Create the stack frame object.
Mon P Wang364d73d2008-07-05 20:40:31 +00005373 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5374 SrcOp.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00005375 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang364d73d2008-07-05 20:40:31 +00005376
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005377 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005378 int SPFI = StackPtrFI->getIndex();
Dan Gohman15b38302009-01-29 21:02:43 +00005379 const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
5380
Duncan Sands83ec4b62008-06-06 12:08:01 +00005381 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5382 unsigned SlotSize = SlotVT.getSizeInBits();
5383 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang364d73d2008-07-05 20:40:31 +00005384 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5385 DestVT.getTypeForMVT());
Chris Lattner35481892005-12-23 00:16:34 +00005386
Chris Lattner1401d152008-01-16 07:45:30 +00005387 // Emit a store to the stack slot. Use a truncstore if the input value is
5388 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00005389 SDValue Store;
Mon P Wang364d73d2008-07-05 20:40:31 +00005390
Chris Lattner1401d152008-01-16 07:45:30 +00005391 if (SrcSize > SlotSize)
Dan Gohman69de1932008-02-06 22:27:42 +00005392 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00005393 SV, 0, SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005394 else {
5395 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman69de1932008-02-06 22:27:42 +00005396 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00005397 SV, 0, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005398 }
5399
Chris Lattner35481892005-12-23 00:16:34 +00005400 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00005401 if (SlotSize == DestSize)
Dan Gohman15b38302009-01-29 21:02:43 +00005402 return DAG.getLoad(DestVT, Store, FIPtr, SV, 0, false, DestAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005403
5404 assert(SlotSize < DestSize && "Unknown extension!");
Dan Gohman15b38302009-01-29 21:02:43 +00005405 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, SV, 0, SlotVT,
Mon P Wang364d73d2008-07-05 20:40:31 +00005406 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00005407}
5408
Dan Gohman475871a2008-07-27 21:46:04 +00005409SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Chris Lattner4352cc92006-04-04 17:23:26 +00005410 // Create a vector sized/aligned stack slot, store the value to element #0,
5411 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005412 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00005413
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005414 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005415 int SPFI = StackPtrFI->getIndex();
5416
Dan Gohman475871a2008-07-27 21:46:04 +00005417 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005418 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00005419 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005420 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00005421}
5422
5423
Chris Lattnerce872152006-03-19 06:31:19 +00005424/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00005425/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00005426SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Chris Lattnerce872152006-03-19 06:31:19 +00005427
5428 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00005429 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00005430 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00005431 bool isOnlyLowElement = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005432 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005433
Dan Gohman475871a2008-07-27 21:46:04 +00005434 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005435 // and use a bitmask instead of a list of elements.
Dan Gohman475871a2008-07-27 21:46:04 +00005436 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00005437 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005438 bool isConstant = true;
5439 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5440 SplatValue.getOpcode() != ISD::UNDEF)
5441 isConstant = false;
5442
Evan Cheng033e6812006-03-24 01:17:21 +00005443 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005444 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00005445 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00005446 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00005447 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00005448 if (SplatValue != V)
Dan Gohman475871a2008-07-27 21:46:04 +00005449 SplatValue = SDValue(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005450
5451 // If this isn't a constant element or an undef, we can't use a constant
5452 // pool load.
5453 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5454 V.getOpcode() != ISD::UNDEF)
5455 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00005456 }
5457
5458 if (isOnlyLowElement) {
5459 // If the low element is an undef too, then this whole things is an undef.
5460 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5461 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5462 // Otherwise, turn this into a scalar_to_vector node.
5463 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5464 Node->getOperand(0));
5465 }
5466
Chris Lattner2eb86532006-03-24 07:29:17 +00005467 // If all elements are constants, create a load from the constant pool.
5468 if (isConstant) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005469 MVT VT = Node->getValueType(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005470 std::vector<Constant*> CV;
5471 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5472 if (ConstantFPSDNode *V =
5473 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005474 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005475 } else if (ConstantSDNode *V =
Chris Lattner02a260a2008-04-20 00:41:09 +00005476 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005477 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005478 } else {
5479 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner02a260a2008-04-20 00:41:09 +00005480 const Type *OpNTy =
Duncan Sands83ec4b62008-06-06 12:08:01 +00005481 Node->getOperand(0).getValueType().getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00005482 CV.push_back(UndefValue::get(OpNTy));
5483 }
5484 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00005485 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005486 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005487 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman69de1932008-02-06 22:27:42 +00005488 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005489 PseudoSourceValue::getConstantPool(), 0,
5490 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00005491 }
5492
Gabor Greifba36cb52008-08-28 21:40:38 +00005493 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00005494 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005495 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman475871a2008-07-27 21:46:04 +00005496 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5497 std::vector<SDValue> ZeroVec(NumElems, Zero);
5498 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005499 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00005500
5501 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005502 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00005503 // Get the splatted value into the low element of a vector register.
Dan Gohman475871a2008-07-27 21:46:04 +00005504 SDValue LowValVec =
Chris Lattner87100e02006-03-20 01:52:29 +00005505 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5506
5507 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5508 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5509 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5510 SplatMask);
5511 }
5512 }
5513
Evan Cheng033e6812006-03-24 01:17:21 +00005514 // If there are only two unique elements, we may be able to turn this into a
5515 // vector shuffle.
5516 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005517 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00005518 SDValue Val1 = Node->getOperand(1);
5519 SDValue Val2;
5520 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005521 if (MI->first != Val1)
5522 Val2 = MI->first;
5523 else
5524 Val2 = (++MI)->first;
5525
5526 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5527 // vector shuffle has the undef vector on the RHS.
5528 if (Val1.getOpcode() == ISD::UNDEF)
5529 std::swap(Val1, Val2);
5530
Evan Cheng033e6812006-03-24 01:17:21 +00005531 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005532 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5533 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005534 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005535
5536 // Set elements of the shuffle mask for Val1.
5537 std::vector<unsigned> &Val1Elts = Values[Val1];
5538 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5539 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5540
5541 // Set elements of the shuffle mask for Val2.
5542 std::vector<unsigned> &Val2Elts = Values[Val2];
5543 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5544 if (Val2.getOpcode() != ISD::UNDEF)
5545 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5546 else
5547 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5548
Dan Gohman475871a2008-07-27 21:46:04 +00005549 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005550 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005551
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005552 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005553 if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR,
5554 Node->getValueType(0)) &&
Chris Lattner4352cc92006-04-04 17:23:26 +00005555 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005556 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5557 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman475871a2008-07-27 21:46:04 +00005558 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng033e6812006-03-24 01:17:21 +00005559
5560 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005561 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng033e6812006-03-24 01:17:21 +00005562 }
5563 }
Chris Lattnerce872152006-03-19 06:31:19 +00005564
5565 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5566 // aligned object on the stack, store each element into it, then load
5567 // the result as a vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005568 MVT VT = Node->getValueType(0);
Chris Lattnerce872152006-03-19 06:31:19 +00005569 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00005570 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohman15b38302009-01-29 21:02:43 +00005571 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
5572 const Value *SV = PseudoSourceValue::getFixedStack(FI);
5573
Chris Lattnerce872152006-03-19 06:31:19 +00005574 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00005575 SmallVector<SDValue, 8> Stores;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005576 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005577 // Store (in the right endianness) the elements to memory.
5578 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5579 // Ignore undef elements.
5580 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5581
Chris Lattner841c8822006-03-22 01:46:54 +00005582 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005583
Dan Gohman475871a2008-07-27 21:46:04 +00005584 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Chris Lattnerce872152006-03-19 06:31:19 +00005585 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5586
Evan Cheng786225a2006-10-05 23:01:46 +00005587 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Dan Gohman15b38302009-01-29 21:02:43 +00005588 SV, Offset));
Chris Lattnerce872152006-03-19 06:31:19 +00005589 }
5590
Dan Gohman475871a2008-07-27 21:46:04 +00005591 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00005592 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005593 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5594 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005595 else
5596 StoreChain = DAG.getEntryNode();
5597
5598 // Result is a load from the stack slot.
Dan Gohman15b38302009-01-29 21:02:43 +00005599 return DAG.getLoad(VT, StoreChain, FIPtr, SV, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005600}
5601
Chris Lattner5b359c62005-04-02 04:00:59 +00005602void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman475871a2008-07-27 21:46:04 +00005603 SDValue Op, SDValue Amt,
5604 SDValue &Lo, SDValue &Hi) {
Chris Lattner5b359c62005-04-02 04:00:59 +00005605 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00005606 SDValue LHSL, LHSH;
Chris Lattner5b359c62005-04-02 04:00:59 +00005607 ExpandOp(Op, LHSL, LHSH);
5608
Dan Gohman475871a2008-07-27 21:46:04 +00005609 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005610 MVT VT = LHSL.getValueType();
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00005611 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005612 Hi = Lo.getValue(1);
5613}
5614
5615
Chris Lattnere34b3962005-01-19 04:19:40 +00005616/// ExpandShift - Try to find a clever way to expand this shift operation out to
5617/// smaller elements. If we can't find a way that is more efficient than a
5618/// libcall on this target, return false. Otherwise, return true with the
5619/// low-parts expanded into Lo and Hi.
Dan Gohman475871a2008-07-27 21:46:04 +00005620bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5621 SDValue &Lo, SDValue &Hi) {
Chris Lattnere34b3962005-01-19 04:19:40 +00005622 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5623 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005624
Duncan Sands83ec4b62008-06-06 12:08:01 +00005625 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman475871a2008-07-27 21:46:04 +00005626 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005627 MVT ShTy = ShAmt.getValueType();
5628 unsigned ShBits = ShTy.getSizeInBits();
5629 unsigned VTBits = Op.getValueType().getSizeInBits();
5630 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005631
Chris Lattner7a3c8552007-10-14 20:35:12 +00005632 // Handle the case when Amt is an immediate.
Gabor Greifba36cb52008-08-28 21:40:38 +00005633 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005634 unsigned Cst = CN->getZExtValue();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005635 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005636 SDValue InL, InH;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005637 ExpandOp(Op, InL, InH);
5638 switch(Opc) {
5639 case ISD::SHL:
5640 if (Cst > VTBits) {
5641 Lo = DAG.getConstant(0, NVT);
5642 Hi = DAG.getConstant(0, NVT);
5643 } else if (Cst > NVTBits) {
5644 Lo = DAG.getConstant(0, NVT);
5645 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005646 } else if (Cst == NVTBits) {
5647 Lo = DAG.getConstant(0, NVT);
5648 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005649 } else {
5650 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5651 Hi = DAG.getNode(ISD::OR, NVT,
5652 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5653 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5654 }
5655 return true;
5656 case ISD::SRL:
5657 if (Cst > VTBits) {
5658 Lo = DAG.getConstant(0, NVT);
5659 Hi = DAG.getConstant(0, NVT);
5660 } else if (Cst > NVTBits) {
5661 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5662 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005663 } else if (Cst == NVTBits) {
5664 Lo = InH;
5665 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005666 } else {
5667 Lo = DAG.getNode(ISD::OR, NVT,
5668 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5669 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5670 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5671 }
5672 return true;
5673 case ISD::SRA:
5674 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005675 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005676 DAG.getConstant(NVTBits-1, ShTy));
5677 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005678 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005679 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005680 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005681 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005682 } else if (Cst == NVTBits) {
5683 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005684 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005685 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005686 } else {
5687 Lo = DAG.getNode(ISD::OR, NVT,
5688 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5689 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5690 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5691 }
5692 return true;
5693 }
5694 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005695
5696 // Okay, the shift amount isn't constant. However, if we can tell that it is
5697 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005698 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5699 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005700 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005701
Dan Gohman9e255b72008-02-22 01:12:31 +00005702 // If we know that if any of the high bits of the shift amount are one, then
5703 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005704 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005705 // Mask out the high bit, which we know is set.
5706 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005707 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005708
5709 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005710 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005711 ExpandOp(Op, InL, InH);
5712 switch(Opc) {
5713 case ISD::SHL:
5714 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5715 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5716 return true;
5717 case ISD::SRL:
5718 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5719 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5720 return true;
5721 case ISD::SRA:
5722 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5723 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5724 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5725 return true;
5726 }
5727 }
5728
Dan Gohman9e255b72008-02-22 01:12:31 +00005729 // If we know that the high bits of the shift amount are all zero, then we can
5730 // do this as a couple of simple shifts.
5731 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005732 // Compute 32-amt.
Dan Gohman475871a2008-07-27 21:46:04 +00005733 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005734 DAG.getConstant(NVTBits, Amt.getValueType()),
5735 Amt);
5736
5737 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005738 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005739 ExpandOp(Op, InL, InH);
5740 switch(Opc) {
5741 case ISD::SHL:
5742 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5743 Hi = DAG.getNode(ISD::OR, NVT,
5744 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5745 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5746 return true;
5747 case ISD::SRL:
5748 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5749 Lo = DAG.getNode(ISD::OR, NVT,
5750 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5751 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5752 return true;
5753 case ISD::SRA:
5754 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5755 Lo = DAG.getNode(ISD::OR, NVT,
5756 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5757 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5758 return true;
5759 }
5760 }
5761
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005762 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005763}
Chris Lattner77e77a62005-01-21 06:05:23 +00005764
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005765
Chris Lattner77e77a62005-01-21 06:05:23 +00005766// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5767// does not fit into a register, return the lo part and set the hi part to the
5768// by-reg argument. If it does fit into a single register, return the result
5769// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00005770SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5771 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005772 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5773 // The input chain to this libcall is the entry node of the function.
5774 // Legalizing the call will automatically add the previous call to the
5775 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00005776 SDValue InChain = DAG.getEntryNode();
Chris Lattner6831a812006-02-13 09:18:02 +00005777
Chris Lattner77e77a62005-01-21 06:05:23 +00005778 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005779 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005780 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005781 MVT ArgVT = Node->getOperand(i).getValueType();
5782 const Type *ArgTy = ArgVT.getTypeForMVT();
Reid Spencer47857812006-12-31 05:55:36 +00005783 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005784 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005785 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005786 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005787 }
Bill Wendling056292f2008-09-16 21:48:12 +00005788 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00005789 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005790
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005791 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005792 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman475871a2008-07-27 21:46:04 +00005793 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005794 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005795 CallingConv::C, false, Callee, Args, DAG,
5796 Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005797
Chris Lattner6831a812006-02-13 09:18:02 +00005798 // Legalize the call sequence, starting with the chain. This will advance
5799 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5800 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5801 LegalizeOp(CallInfo.second);
Dan Gohman475871a2008-07-27 21:46:04 +00005802 SDValue Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005803 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005804 default: assert(0 && "Unknown thing");
5805 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005806 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005807 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005808 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005809 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005810 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005811 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005812 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005813}
5814
Dan Gohman7f8613e2008-08-14 20:04:46 +00005815/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5816///
5817SDValue SelectionDAGLegalize::
Dale Johannesenaf435272009-02-02 19:03:57 +00005818LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op,
5819 DebugLoc dl) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00005820 bool isCustom = false;
5821 SDValue Tmp1;
5822 switch (getTypeAction(Op.getValueType())) {
5823 case Legal:
5824 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5825 Op.getValueType())) {
5826 default: assert(0 && "Unknown operation action!");
5827 case TargetLowering::Custom:
5828 isCustom = true;
5829 // FALLTHROUGH
5830 case TargetLowering::Legal:
5831 Tmp1 = LegalizeOp(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00005832 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005833 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5834 else
Dale Johannesenaf435272009-02-02 19:03:57 +00005835 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, dl,
Dan Gohman7f8613e2008-08-14 20:04:46 +00005836 DestTy, Tmp1);
5837 if (isCustom) {
5838 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005839 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005840 }
5841 break;
5842 case TargetLowering::Expand:
Dale Johannesenaf435272009-02-02 19:03:57 +00005843 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy, dl);
Dan Gohman7f8613e2008-08-14 20:04:46 +00005844 break;
5845 case TargetLowering::Promote:
Dale Johannesenaf435272009-02-02 19:03:57 +00005846 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned, dl);
Dan Gohman7f8613e2008-08-14 20:04:46 +00005847 break;
5848 }
5849 break;
5850 case Expand:
Dale Johannesenaf435272009-02-02 19:03:57 +00005851 Result = ExpandIntToFP(isSigned, DestTy, Op, dl) ;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005852 break;
5853 case Promote:
5854 Tmp1 = PromoteOp(Op);
5855 if (isSigned) {
Dale Johannesenaf435272009-02-02 19:03:57 +00005856 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Tmp1.getValueType(),
Dan Gohman7f8613e2008-08-14 20:04:46 +00005857 Tmp1, DAG.getValueType(Op.getValueType()));
5858 } else {
Dale Johannesenaf435272009-02-02 19:03:57 +00005859 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl,
Dan Gohman7f8613e2008-08-14 20:04:46 +00005860 Op.getValueType());
5861 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005862 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005863 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5864 else
Dale Johannesenaf435272009-02-02 19:03:57 +00005865 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, dl,
Dan Gohman7f8613e2008-08-14 20:04:46 +00005866 DestTy, Tmp1);
5867 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5868 break;
5869 }
5870 return Result;
5871}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005872
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005873/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5874///
Dan Gohman475871a2008-07-27 21:46:04 +00005875SDValue SelectionDAGLegalize::
Dale Johannesenaf435272009-02-02 19:03:57 +00005876ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005877 MVT SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005878 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005879
Dan Gohman7f8613e2008-08-14 20:04:46 +00005880 // Expand unsupported int-to-fp vector casts by unrolling them.
5881 if (DestTy.isVector()) {
5882 if (!ExpandSource)
5883 return LegalizeOp(UnrollVectorOp(Source));
5884 MVT DestEltTy = DestTy.getVectorElementType();
5885 if (DestTy.getVectorNumElements() == 1) {
5886 SDValue Scalar = ScalarizeVectorOp(Source);
5887 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +00005888 DestEltTy, Scalar, dl);
5889 return DAG.getNode(ISD::BUILD_VECTOR, dl, DestTy, Result);
Dan Gohman7f8613e2008-08-14 20:04:46 +00005890 }
5891 SDValue Lo, Hi;
5892 SplitVectorOp(Source, Lo, Hi);
5893 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5894 DestTy.getVectorNumElements() / 2);
Dale Johannesenaf435272009-02-02 19:03:57 +00005895 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy,
5896 Lo, dl);
5897 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy,
5898 Hi, dl);
5899 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, DestTy, LoResult,
Evan Chengefa53392008-10-13 18:46:18 +00005900 HiResult));
Dan Gohman7f8613e2008-08-14 20:04:46 +00005901 }
5902
Evan Cheng9845eb52008-04-01 02:18:22 +00005903 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5904 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005905 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005906 // incoming integer is set. To handle this, we dynamically test to see if
5907 // it is set, and, if so, add a fudge factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005908 SDValue Hi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005909 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005910 SDValue Lo;
Dan Gohman034f60e2008-03-11 01:59:03 +00005911 ExpandOp(Source, Lo, Hi);
Dale Johannesenaf435272009-02-02 19:03:57 +00005912 Source = DAG.getNode(ISD::BUILD_PAIR, dl, SourceVT, Lo, Hi);
Dan Gohman034f60e2008-03-11 01:59:03 +00005913 } else {
5914 // The comparison for the sign bit will use the entire operand.
5915 Hi = Source;
5916 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005917
Dale Johannesen53997b02008-11-04 20:52:49 +00005918 // Check to see if the target has a custom way to lower this. If so, use
5919 // it. (Note we've already expanded the operand in this case.)
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005920 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5921 default: assert(0 && "This action not implemented for this operation!");
5922 case TargetLowering::Legal:
5923 case TargetLowering::Expand:
5924 break; // This case is handled below.
5925 case TargetLowering::Custom: {
5926 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5927 Source), DAG);
5928 if (NV.getNode())
5929 return LegalizeOp(NV);
5930 break; // The target decided this was legal after all
5931 }
5932 }
5933
Chris Lattner66de05b2005-05-13 04:45:13 +00005934 // If this is unsigned, and not supported, first perform the conversion to
5935 // signed, then adjust the result if the sign bit is set.
Dale Johannesenaf435272009-02-02 19:03:57 +00005936 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source, dl);
Chris Lattner66de05b2005-05-13 04:45:13 +00005937
Dale Johannesenaf435272009-02-02 19:03:57 +00005938 SDValue SignSet = DAG.getSetCC(dl,
5939 TLI.getSetCCResultType(Hi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005940 Hi, DAG.getConstant(0, Hi.getValueType()),
5941 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005942 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00005943 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattnere9c35e72005-04-13 05:09:42 +00005944 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005945 uint64_t FF = 0x5f800000ULL;
5946 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohman34bc1782008-03-05 02:07:31 +00005947 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005948
Dan Gohman475871a2008-07-27 21:46:04 +00005949 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005950 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00005951 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005952 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005953 SDValue FudgeInReg;
Chris Lattnere9c35e72005-04-13 05:09:42 +00005954 if (DestTy == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00005955 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005956 PseudoSourceValue::getConstantPool(), 0,
5957 false, Alignment);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005958 else if (DestTy.bitsGT(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005959 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenaf435272009-02-02 19:03:57 +00005960 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, dl, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005961 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005962 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005963 MVT::f32, false, Alignment);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005964 else
5965 assert(0 && "Unexpected conversion");
5966
Duncan Sands83ec4b62008-06-06 12:08:01 +00005967 MVT SCVT = SignedConv.getValueType();
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005968 if (SCVT != DestTy) {
5969 // Destination type needs to be expanded as well. The FADD now we are
5970 // constructing will be expanded into a libcall.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005971 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5972 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dale Johannesenaf435272009-02-02 19:03:57 +00005973 SignedConv = DAG.getNode(ISD::BUILD_PAIR, dl, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005974 SignedConv, SignedConv.getValue(1));
5975 }
Dale Johannesenaf435272009-02-02 19:03:57 +00005976 SignedConv = DAG.getNode(ISD::BIT_CONVERT, dl, DestTy, SignedConv);
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005977 }
Dale Johannesenaf435272009-02-02 19:03:57 +00005978 return DAG.getNode(ISD::FADD, dl, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005979 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005980
Chris Lattnera88a2602005-05-14 05:33:54 +00005981 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00005982 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00005983 default: assert(0 && "This action not implemented for this operation!");
5984 case TargetLowering::Legal:
5985 case TargetLowering::Expand:
5986 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005987 case TargetLowering::Custom: {
Dale Johannesenaf435272009-02-02 19:03:57 +00005988 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, dl, DestTy,
Chris Lattner07dffd62005-08-26 00:14:16 +00005989 Source), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005990 if (NV.getNode())
Chris Lattner07dffd62005-08-26 00:14:16 +00005991 return LegalizeOp(NV);
5992 break; // The target decided this was legal after all
5993 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005994 }
5995
Chris Lattner13689e22005-05-12 07:00:44 +00005996 // Expand the source, then glue it back together for the call. We must expand
5997 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00005998 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005999 SDValue SrcLo, SrcHi;
Dan Gohman034f60e2008-03-11 01:59:03 +00006000 ExpandOp(Source, SrcLo, SrcHi);
Dale Johannesenaf435272009-02-02 19:03:57 +00006001 Source = DAG.getNode(ISD::BUILD_PAIR, dl, SourceVT, SrcLo, SrcHi);
Dan Gohman034f60e2008-03-11 01:59:03 +00006002 }
Chris Lattner13689e22005-05-12 07:00:44 +00006003
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006004 RTLIB::Libcall LC = isSigned ?
6005 RTLIB::getSINTTOFP(SourceVT, DestTy) :
6006 RTLIB::getUINTTOFP(SourceVT, DestTy);
6007 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
6008
Dale Johannesenaf435272009-02-02 19:03:57 +00006009 Source = DAG.getNode(ISD::SINT_TO_FP, dl, DestTy, Source);
Dan Gohman475871a2008-07-27 21:46:04 +00006010 SDValue HiPart;
Gabor Greifba36cb52008-08-28 21:40:38 +00006011 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
6012 if (Result.getValueType() != DestTy && HiPart.getNode())
Dale Johannesenaf435272009-02-02 19:03:57 +00006013 Result = DAG.getNode(ISD::BUILD_PAIR, dl, DestTy, Result, HiPart);
Dan Gohmana2e94852008-03-10 23:03:31 +00006014 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00006015}
Misha Brukmanedf128a2005-04-21 22:36:52 +00006016
Chris Lattner22cde6a2006-01-28 08:25:58 +00006017/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
6018/// INT_TO_FP operation of the specified operand when the target requests that
6019/// we expand it. At this point, we know that the result and operand types are
6020/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00006021SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
6022 SDValue Op0,
Dale Johannesenaf435272009-02-02 19:03:57 +00006023 MVT DestVT,
6024 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006025 if (Op0.getValueType() == MVT::i32) {
6026 // simple 32-bit [signed|unsigned] integer to float/double expansion
6027
Chris Lattner23594d42008-01-16 07:03:22 +00006028 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00006029 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner23594d42008-01-16 07:03:22 +00006030
Chris Lattner22cde6a2006-01-28 08:25:58 +00006031 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00006032 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00006033 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00006034 SDValue Hi = StackSlot;
Dale Johannesenaf435272009-02-02 19:03:57 +00006035 SDValue Lo = DAG.getNode(ISD::ADD, dl,
6036 TLI.getPointerTy(), StackSlot,WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00006037 if (TLI.isLittleEndian())
6038 std::swap(Hi, Lo);
6039
Chris Lattner22cde6a2006-01-28 08:25:58 +00006040 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00006041 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006042 if (isSigned) {
6043 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00006044 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dale Johannesenaf435272009-02-02 19:03:57 +00006045 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006046 } else {
6047 Op0Mapped = Op0;
6048 }
6049 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00006050 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00006051 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006052 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00006053 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006054 // store the hi of the constructed double - biased exponent
Dale Johannesenaf435272009-02-02 19:03:57 +00006055 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006056 // load the constructed double
Dale Johannesenaf435272009-02-02 19:03:57 +00006057 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006058 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00006059 SDValue Bias = DAG.getConstantFP(isSigned ?
Chris Lattner22cde6a2006-01-28 08:25:58 +00006060 BitsToDouble(0x4330000080000000ULL)
6061 : BitsToDouble(0x4330000000000000ULL),
6062 MVT::f64);
6063 // subtract the bias
Dale Johannesenaf435272009-02-02 19:03:57 +00006064 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006065 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00006066 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006067 // handle final rounding
6068 if (DestVT == MVT::f64) {
6069 // do nothing
6070 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006071 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00006072 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00006073 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00006074 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00006075 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006076 }
6077 return Result;
6078 }
6079 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesenaf435272009-02-02 19:03:57 +00006080 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006081
Dale Johannesenaf435272009-02-02 19:03:57 +00006082 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00006083 Op0, DAG.getConstant(0, Op0.getValueType()),
6084 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00006085 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00006086 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006087 SignSet, Four, Zero);
6088
6089 // If the sign bit of the integer is set, the large number will be treated
6090 // as a negative number. To counteract this, the dynamic code adds an
6091 // offset depending on the data type.
6092 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006093 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006094 default: assert(0 && "Unsupported integer type!");
6095 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6096 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6097 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6098 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6099 }
6100 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00006101 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006102
Dan Gohman475871a2008-07-27 21:46:04 +00006103 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00006104 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00006105 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00006106 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00006107 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006108 if (DestVT == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00006109 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00006110 PseudoSourceValue::getConstantPool(), 0,
6111 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006112 else {
Dan Gohman69de1932008-02-06 22:27:42 +00006113 FudgeInReg =
Dale Johannesenaf435272009-02-02 19:03:57 +00006114 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohman69de1932008-02-06 22:27:42 +00006115 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00006116 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00006117 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006118 }
6119
Dale Johannesenaf435272009-02-02 19:03:57 +00006120 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006121}
6122
6123/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6124/// *INT_TO_FP operation of the specified operand when the target requests that
6125/// we promote it. At this point, we know that the result and operand types are
6126/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6127/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00006128SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6129 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00006130 bool isSigned,
6131 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006132 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006133 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006134
6135 unsigned OpToUse = 0;
6136
6137 // Scan for the appropriate larger type to use.
6138 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006139 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6140 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006141
6142 // If the target supports SINT_TO_FP of this type, use it.
6143 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6144 default: break;
6145 case TargetLowering::Legal:
6146 if (!TLI.isTypeLegal(NewInTy))
6147 break; // Can't use this datatype.
6148 // FALL THROUGH.
6149 case TargetLowering::Custom:
6150 OpToUse = ISD::SINT_TO_FP;
6151 break;
6152 }
6153 if (OpToUse) break;
6154 if (isSigned) continue;
6155
6156 // If the target supports UINT_TO_FP of this type, use it.
6157 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6158 default: break;
6159 case TargetLowering::Legal:
6160 if (!TLI.isTypeLegal(NewInTy))
6161 break; // Can't use this datatype.
6162 // FALL THROUGH.
6163 case TargetLowering::Custom:
6164 OpToUse = ISD::UINT_TO_FP;
6165 break;
6166 }
6167 if (OpToUse) break;
6168
6169 // Otherwise, try a larger type.
6170 }
6171
6172 // Okay, we found the operation and type to use. Zero extend our input to the
6173 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00006174 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00006175 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00006176 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006177}
6178
6179/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6180/// FP_TO_*INT operation of the specified operand when the target requests that
6181/// we promote it. At this point, we know that the result and operand types are
6182/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6183/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00006184SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6185 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00006186 bool isSigned,
6187 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006188 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006189 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006190
6191 unsigned OpToUse = 0;
6192
6193 // Scan for the appropriate larger type to use.
6194 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006195 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6196 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006197
6198 // If the target supports FP_TO_SINT returning this type, use it.
6199 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6200 default: break;
6201 case TargetLowering::Legal:
6202 if (!TLI.isTypeLegal(NewOutTy))
6203 break; // Can't use this datatype.
6204 // FALL THROUGH.
6205 case TargetLowering::Custom:
6206 OpToUse = ISD::FP_TO_SINT;
6207 break;
6208 }
6209 if (OpToUse) break;
6210
6211 // If the target supports FP_TO_UINT of this type, use it.
6212 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6213 default: break;
6214 case TargetLowering::Legal:
6215 if (!TLI.isTypeLegal(NewOutTy))
6216 break; // Can't use this datatype.
6217 // FALL THROUGH.
6218 case TargetLowering::Custom:
6219 OpToUse = ISD::FP_TO_UINT;
6220 break;
6221 }
6222 if (OpToUse) break;
6223
6224 // Otherwise, try a larger type.
6225 }
6226
Chris Lattner27a6c732007-11-24 07:07:01 +00006227
6228 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00006229 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00006230
Chris Lattner27a6c732007-11-24 07:07:01 +00006231 // If the operation produces an invalid type, it must be custom lowered. Use
6232 // the target lowering hooks to expand it. Just keep the low part of the
6233 // expanded operation, we know that we're truncating anyway.
6234 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00006235 SmallVector<SDValue, 2> Results;
6236 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6237 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6238 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00006239 }
Duncan Sands126d9072008-07-04 11:47:58 +00006240
Chris Lattner27a6c732007-11-24 07:07:01 +00006241 // Truncate the result of the extended FP_TO_*INT operation to the desired
6242 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00006243 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006244}
6245
6246/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6247///
Dan Gohman475871a2008-07-27 21:46:04 +00006248SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006249 MVT VT = Op.getValueType();
6250 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00006251 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006252 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006253 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6254 case MVT::i16:
6255 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6256 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6257 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6258 case MVT::i32:
6259 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6260 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6261 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6262 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6263 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6264 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6265 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6266 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6267 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6268 case MVT::i64:
6269 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6270 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6271 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6272 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6273 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6274 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6275 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6276 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6277 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6278 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6279 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6280 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6281 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6282 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6283 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6284 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6285 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6286 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6287 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6288 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6289 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6290 }
6291}
6292
6293/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6294///
Dan Gohman475871a2008-07-27 21:46:04 +00006295SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006296 switch (Opc) {
6297 default: assert(0 && "Cannot expand this yet!");
6298 case ISD::CTPOP: {
6299 static const uint64_t mask[6] = {
6300 0x5555555555555555ULL, 0x3333333333333333ULL,
6301 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6302 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6303 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00006304 MVT VT = Op.getValueType();
6305 MVT ShVT = TLI.getShiftAmountTy();
6306 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006307 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6308 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006309 unsigned EltSize = VT.isVector() ?
6310 VT.getVectorElementType().getSizeInBits() : len;
6311 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
Dan Gohman475871a2008-07-27 21:46:04 +00006312 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006313 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6314 DAG.getNode(ISD::AND, VT,
6315 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6316 }
6317 return Op;
6318 }
6319 case ISD::CTLZ: {
6320 // for now, we do this:
6321 // x = x | (x >> 1);
6322 // x = x | (x >> 2);
6323 // ...
6324 // x = x | (x >>16);
6325 // x = x | (x >>32); // for 64-bit input
6326 // return popcount(~x);
6327 //
6328 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006329 MVT VT = Op.getValueType();
6330 MVT ShVT = TLI.getShiftAmountTy();
6331 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006332 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006333 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006334 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6335 }
Bill Wendling7581bfa2009-01-30 23:03:19 +00006336 Op = DAG.getNOT(DebugLoc::getUnknownLoc(), Op, VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006337 return DAG.getNode(ISD::CTPOP, VT, Op);
6338 }
6339 case ISD::CTTZ: {
6340 // for now, we use: { return popcount(~x & (x - 1)); }
6341 // unless the target has ctlz but not ctpop, in which case we use:
6342 // { return 32 - nlz(~x & (x-1)); }
6343 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006344 MVT VT = Op.getValueType();
Bill Wendling7581bfa2009-01-30 23:03:19 +00006345 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
6346 DAG.getNOT(DebugLoc::getUnknownLoc(), Op, VT),
6347 DAG.getNode(ISD::SUB, VT, Op,
6348 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006349 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006350 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
6351 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Chris Lattner22cde6a2006-01-28 08:25:58 +00006352 return DAG.getNode(ISD::SUB, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006353 DAG.getConstant(VT.getSizeInBits(), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006354 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6355 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6356 }
6357 }
6358}
Chris Lattnere34b3962005-01-19 04:19:40 +00006359
Dan Gohman475871a2008-07-27 21:46:04 +00006360/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner3e928bb2005-01-07 07:47:09 +00006361/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman929d3eb2008-10-01 15:07:49 +00006362/// LegalizedNodes map is filled in for any results that are not expanded, the
Chris Lattner3e928bb2005-01-07 07:47:09 +00006363/// ExpandedNodes map is filled in for any results that are expanded, and the
6364/// Lo/Hi values are returned.
Dan Gohman475871a2008-07-27 21:46:04 +00006365void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands83ec4b62008-06-06 12:08:01 +00006366 MVT VT = Op.getValueType();
6367 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greifba36cb52008-08-28 21:40:38 +00006368 SDNode *Node = Op.getNode();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006369 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00006370 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands83ec4b62008-06-06 12:08:01 +00006371 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006372
Chris Lattner6fdcb252005-09-02 20:32:45 +00006373 // See if we already expanded it.
Dan Gohman475871a2008-07-27 21:46:04 +00006374 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00006375 = ExpandedNodes.find(Op);
6376 if (I != ExpandedNodes.end()) {
6377 Lo = I->second.first;
6378 Hi = I->second.second;
6379 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006380 }
6381
Chris Lattner3e928bb2005-01-07 07:47:09 +00006382 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006383 case ISD::CopyFromReg:
6384 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006385 case ISD::FP_ROUND_INREG:
6386 if (VT == MVT::ppcf128 &&
6387 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6388 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006389 SDValue SrcLo, SrcHi, Src;
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006390 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6391 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman475871a2008-07-27 21:46:04 +00006392 SDValue Result = TLI.LowerOperation(
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006393 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006394 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6395 Lo = Result.getNode()->getOperand(0);
6396 Hi = Result.getNode()->getOperand(1);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006397 break;
6398 }
6399 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00006400 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006401#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006402 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006403#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00006404 assert(0 && "Do not know how to expand this operator!");
6405 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00006406 case ISD::EXTRACT_ELEMENT:
6407 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006408 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman1953ecb2008-02-27 01:52:30 +00006409 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00006410 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00006411 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen25f1d082007-10-31 00:32:36 +00006412 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6413 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6414 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006415 case ISD::UNDEF:
6416 Lo = DAG.getNode(ISD::UNDEF, NVT);
6417 Hi = DAG.getNode(ISD::UNDEF, NVT);
6418 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006419 case ISD::Constant: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006420 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman050f5502008-03-03 22:20:46 +00006421 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6422 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6423 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006424 break;
6425 }
Evan Cheng00495212006-12-12 21:32:44 +00006426 case ISD::ConstantFP: {
6427 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00006428 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen7111b022008-10-09 18:53:47 +00006429 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesena471c2e2007-10-11 18:07:22 +00006430 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6431 MVT::f64);
6432 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6433 MVT::f64);
6434 break;
6435 }
Evan Cheng279101e2006-12-12 22:19:28 +00006436 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00006437 if (getTypeAction(Lo.getValueType()) == Expand)
6438 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006439 break;
6440 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006441 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006442 // Return the operands.
6443 Lo = Node->getOperand(0);
6444 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006445 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006446
6447 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00006448 if (Node->getNumValues() == 1) {
6449 ExpandOp(Op.getOperand(0), Lo, Hi);
6450 break;
6451 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006452 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif99a6cb92008-08-26 22:36:50 +00006453 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattner27a6c732007-11-24 07:07:01 +00006454 Op.getValue(1).getValueType() == MVT::Other &&
6455 "unhandled MERGE_VALUES");
6456 ExpandOp(Op.getOperand(0), Lo, Hi);
6457 // Remember that we legalized the chain.
6458 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6459 break;
Chris Lattner58f79632005-12-12 22:27:43 +00006460
6461 case ISD::SIGN_EXTEND_INREG:
6462 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00006463 // sext_inreg the low part if needed.
6464 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6465
6466 // The high part gets the sign extension from the lo-part. This handles
6467 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00006468 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006469 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattner58f79632005-12-12 22:27:43 +00006470 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00006471 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006472
Nate Begemand88fc032006-01-14 03:14:10 +00006473 case ISD::BSWAP: {
6474 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006475 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Nate Begemand88fc032006-01-14 03:14:10 +00006476 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6477 Lo = TempLo;
6478 break;
6479 }
6480
Chris Lattneredb1add2005-05-11 04:51:16 +00006481 case ISD::CTPOP:
6482 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00006483 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6484 DAG.getNode(ISD::CTPOP, NVT, Lo),
6485 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00006486 Hi = DAG.getConstant(0, NVT);
6487 break;
6488
Chris Lattner39a8f332005-05-12 19:05:01 +00006489 case ISD::CTLZ: {
6490 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006491 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006492 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6493 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Duncan Sands5480c042009-01-01 15:52:00 +00006494 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), HLZ, BitsC,
6495 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006496 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Chris Lattner39a8f332005-05-12 19:05:01 +00006497 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6498
6499 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6500 Hi = DAG.getConstant(0, NVT);
6501 break;
6502 }
6503
6504 case ISD::CTTZ: {
6505 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006506 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006507 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6508 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Duncan Sands5480c042009-01-01 15:52:00 +00006509 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), LTZ, BitsC,
6510 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006511 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00006512 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6513
6514 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6515 Hi = DAG.getConstant(0, NVT);
6516 break;
6517 }
Chris Lattneredb1add2005-05-11 04:51:16 +00006518
Nate Begemanacc398c2006-01-25 18:21:52 +00006519 case ISD::VAARG: {
Dan Gohman475871a2008-07-27 21:46:04 +00006520 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6521 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00006522 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6523 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6524
6525 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00006526 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00006527 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00006528 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00006529 std::swap(Lo, Hi);
6530 break;
6531 }
6532
Chris Lattner3e928bb2005-01-07 07:47:09 +00006533 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00006534 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00006535 SDValue Ch = LD->getChain(); // Legalize the chain.
6536 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Cheng466685d2006-10-09 20:57:25 +00006537 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f8613e2008-08-14 20:04:46 +00006538 const Value *SV = LD->getSrcValue();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006539 int SVOffset = LD->getSrcValueOffset();
6540 unsigned Alignment = LD->getAlignment();
6541 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006542
Evan Cheng466685d2006-10-09 20:57:25 +00006543 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00006544 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006545 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00006546 if (VT == MVT::f32 || VT == MVT::f64) {
6547 // f32->i32 or f64->i64 one to one expansion.
6548 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006549 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00006550 // Recursively expand the new load.
6551 if (getTypeAction(NVT) == Expand)
6552 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006553 break;
6554 }
Chris Lattnerec39a452005-01-19 18:02:17 +00006555
Evan Cheng466685d2006-10-09 20:57:25 +00006556 // Increment the pointer to the other half.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006557 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Evan Cheng466685d2006-10-09 20:57:25 +00006558 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006559 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006560 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006561 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00006562 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006563 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00006564
Evan Cheng466685d2006-10-09 20:57:25 +00006565 // Build a factor node to remember that this load is independent of the
6566 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00006567 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Evan Cheng466685d2006-10-09 20:57:25 +00006568 Hi.getValue(1));
6569
6570 // Remember that we legalized the chain.
6571 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00006572 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00006573 std::swap(Lo, Hi);
6574 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006575 MVT EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00006576
Dale Johannesenb6210fc2007-10-19 20:29:00 +00006577 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6578 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00006579 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman7f8613e2008-08-14 20:04:46 +00006580 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006581 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00006582 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006583 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Evan Cheng548f6112006-12-13 03:19:57 +00006584 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6585 break;
6586 }
Evan Cheng466685d2006-10-09 20:57:25 +00006587
6588 if (EVT == NVT)
Dan Gohman7f8613e2008-08-14 20:04:46 +00006589 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006590 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006591 else
Dan Gohman7f8613e2008-08-14 20:04:46 +00006592 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006593 SVOffset, EVT, isVolatile,
6594 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006595
6596 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006597 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng466685d2006-10-09 20:57:25 +00006598
6599 if (ExtType == ISD::SEXTLOAD) {
6600 // The high part is obtained by SRA'ing all but one of the bits of the
6601 // lo part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006602 unsigned LoSize = Lo.getValueType().getSizeInBits();
Evan Cheng466685d2006-10-09 20:57:25 +00006603 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6604 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6605 } else if (ExtType == ISD::ZEXTLOAD) {
6606 // The high part is just a zero.
6607 Hi = DAG.getConstant(0, NVT);
6608 } else /* if (ExtType == ISD::EXTLOAD) */ {
6609 // The high part is undefined.
6610 Hi = DAG.getNode(ISD::UNDEF, NVT);
6611 }
6612 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006613 break;
6614 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006615 case ISD::AND:
6616 case ISD::OR:
6617 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman475871a2008-07-27 21:46:04 +00006618 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006619 ExpandOp(Node->getOperand(0), LL, LH);
6620 ExpandOp(Node->getOperand(1), RL, RH);
6621 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6622 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6623 break;
6624 }
6625 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006626 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006627 ExpandOp(Node->getOperand(1), LL, LH);
6628 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00006629 if (getTypeAction(NVT) == Expand)
6630 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00006631 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00006632 if (VT != MVT::f32)
6633 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006634 break;
6635 }
Nate Begeman9373a812005-08-10 20:51:12 +00006636 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00006637 SDValue TL, TH, FL, FH;
Nate Begeman9373a812005-08-10 20:51:12 +00006638 ExpandOp(Node->getOperand(2), TL, TH);
6639 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00006640 if (getTypeAction(NVT) == Expand)
6641 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00006642 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6643 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00006644 if (VT != MVT::f32)
6645 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6646 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006647 break;
6648 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006649 case ISD::ANY_EXTEND:
6650 // The low part is any extension of the input (which degenerates to a copy).
6651 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6652 // The high part is undefined.
6653 Hi = DAG.getNode(ISD::UNDEF, NVT);
6654 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006655 case ISD::SIGN_EXTEND: {
6656 // The low part is just a sign extension of the input (which degenerates to
6657 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006658 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006659
Chris Lattner3e928bb2005-01-07 07:47:09 +00006660 // The high part is obtained by SRA'ing all but one of the bits of the lo
6661 // part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006662 unsigned LoSize = Lo.getValueType().getSizeInBits();
Chris Lattner8137c9e2006-01-28 05:07:51 +00006663 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6664 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006665 break;
6666 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006667 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006668 // The low part is just a zero extension of the input (which degenerates to
6669 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006670 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006671
Chris Lattner3e928bb2005-01-07 07:47:09 +00006672 // The high part is just a zero.
6673 Hi = DAG.getConstant(0, NVT);
6674 break;
Chris Lattner35481892005-12-23 00:16:34 +00006675
Chris Lattner4c948eb2007-02-13 23:55:16 +00006676 case ISD::TRUNCATE: {
6677 // The input value must be larger than this value. Expand *it*.
Dan Gohman475871a2008-07-27 21:46:04 +00006678 SDValue NewLo;
Chris Lattner4c948eb2007-02-13 23:55:16 +00006679 ExpandOp(Node->getOperand(0), NewLo, Hi);
6680
6681 // The low part is now either the right size, or it is closer. If not the
6682 // right size, make an illegal truncate so we recursively expand it.
6683 if (NewLo.getValueType() != Node->getValueType(0))
6684 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6685 ExpandOp(NewLo, Lo, Hi);
6686 break;
6687 }
6688
Chris Lattner35481892005-12-23 00:16:34 +00006689 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006690 SDValue Tmp;
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006691 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6692 // If the target wants to, allow it to lower this itself.
6693 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6694 case Expand: assert(0 && "cannot expand FP!");
6695 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6696 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6697 }
6698 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6699 }
6700
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006701 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006702 if (VT == MVT::f32 || VT == MVT::f64) {
6703 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006704 if (getTypeAction(NVT) == Expand)
6705 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006706 break;
6707 }
6708
6709 // If source operand will be expanded to the same type as VT, i.e.
6710 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006711 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006712 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6713 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006714 break;
6715 }
6716
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006717 // Turn this into a load/store pair by default.
Gabor Greifba36cb52008-08-28 21:40:38 +00006718 if (Tmp.getNode() == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00006719 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006720
Chris Lattner35481892005-12-23 00:16:34 +00006721 ExpandOp(Tmp, Lo, Hi);
6722 break;
6723 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006724
Chris Lattner27a6c732007-11-24 07:07:01 +00006725 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006726 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6727 TargetLowering::Custom &&
6728 "Must custom expand ReadCycleCounter");
Dan Gohman475871a2008-07-27 21:46:04 +00006729 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006730 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattner27a6c732007-11-24 07:07:01 +00006731 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006732 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006733 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006734 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006735 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006736
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006737 case ISD::ATOMIC_CMP_SWAP: {
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006738 // This operation does not need a loop.
6739 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6740 assert(Tmp.getNode() && "Node must be custom expanded!");
6741 ExpandOp(Tmp.getValue(0), Lo, Hi);
6742 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6743 LegalizeOp(Tmp.getValue(1)));
6744 break;
6745 }
6746
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006747 case ISD::ATOMIC_LOAD_ADD:
6748 case ISD::ATOMIC_LOAD_SUB:
6749 case ISD::ATOMIC_LOAD_AND:
6750 case ISD::ATOMIC_LOAD_OR:
6751 case ISD::ATOMIC_LOAD_XOR:
6752 case ISD::ATOMIC_LOAD_NAND:
6753 case ISD::ATOMIC_SWAP: {
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006754 // These operations require a loop to be generated. We can't do that yet,
6755 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006756 SDValue In2Lo, In2Hi, In2;
6757 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6758 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006759 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6760 SDValue Replace =
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006761 DAG.getAtomic(Op.getOpcode(), Anode->getMemoryVT(),
6762 Op.getOperand(0), Op.getOperand(1), In2,
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006763 Anode->getSrcValue(), Anode->getAlignment());
6764 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006765 ExpandOp(Result.getValue(0), Lo, Hi);
6766 // Remember that we legalized the chain.
6767 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006768 break;
6769 }
6770
Chris Lattner4e6c7462005-01-08 19:27:05 +00006771 // These operators cannot be expanded directly, emit them as calls to
6772 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006773 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006774 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006775 SDValue Op;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006776 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6777 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006778 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6779 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006780 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006781
Chris Lattnerf20d1832005-07-30 01:40:57 +00006782 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6783
Chris Lattner80a3e942005-07-29 00:33:32 +00006784 // Now that the custom expander is done, expand the result, which is still
6785 // VT.
Gabor Greifba36cb52008-08-28 21:40:38 +00006786 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006787 ExpandOp(Op, Lo, Hi);
6788 break;
6789 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006790 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006791
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006792 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6793 VT);
6794 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6795 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006796 break;
Evan Cheng56966222007-01-12 02:11:51 +00006797 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006798
Evan Cheng56966222007-01-12 02:11:51 +00006799 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006800 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006801 SDValue Op;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006802 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6803 case Expand: assert(0 && "cannot expand FP!");
6804 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6805 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6806 }
6807
6808 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6809
6810 // Now that the custom expander is done, expand the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00006811 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006812 ExpandOp(Op, Lo, Hi);
6813 break;
6814 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006815 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006816
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006817 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6818 VT);
6819 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6820 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006821 break;
Evan Cheng56966222007-01-12 02:11:51 +00006822 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006823
Evan Cheng05a2d562006-01-09 18:31:59 +00006824 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006825 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006826 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006827 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006828 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006829 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006830 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006831 // Now that the custom expander is done, expand the result, which is
6832 // still VT.
6833 ExpandOp(Op, Lo, Hi);
6834 break;
6835 }
6836 }
6837
Chris Lattner79980b02006-09-13 03:50:39 +00006838 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6839 // this X << 1 as X+X.
6840 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006841 if (ShAmt->getAPIntValue() == 1 &&
6842 TLI.isOperationLegalOrCustom(ISD::ADDC, NVT) &&
6843 TLI.isOperationLegalOrCustom(ISD::ADDE, NVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006844 SDValue LoOps[2], HiOps[3];
Chris Lattner79980b02006-09-13 03:50:39 +00006845 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6846 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6847 LoOps[1] = LoOps[0];
6848 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6849
6850 HiOps[1] = HiOps[0];
6851 HiOps[2] = Lo.getValue(1);
6852 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6853 break;
6854 }
6855 }
6856
Chris Lattnere34b3962005-01-19 04:19:40 +00006857 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006858 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006859 break;
Chris Lattner47599822005-04-02 03:38:53 +00006860
6861 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006862 TargetLowering::LegalizeAction Action =
6863 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6864 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6865 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006866 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006867 break;
6868 }
6869
Chris Lattnere34b3962005-01-19 04:19:40 +00006870 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006871 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006872 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006873 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006874
Evan Cheng05a2d562006-01-09 18:31:59 +00006875 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006876 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006877 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006878 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006879 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006880 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006881 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006882 // Now that the custom expander is done, expand the result, which is
6883 // still VT.
6884 ExpandOp(Op, Lo, Hi);
6885 break;
6886 }
6887 }
6888
Chris Lattnere34b3962005-01-19 04:19:40 +00006889 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006890 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006891 break;
Chris Lattner47599822005-04-02 03:38:53 +00006892
6893 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006894 TargetLowering::LegalizeAction Action =
6895 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6896 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6897 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006898 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006899 break;
6900 }
6901
Chris Lattnere34b3962005-01-19 04:19:40 +00006902 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006903 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006904 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006905 }
6906
6907 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006908 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006909 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006910 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006911 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006912 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006913 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006914 // Now that the custom expander is done, expand the result, which is
6915 // still VT.
6916 ExpandOp(Op, Lo, Hi);
6917 break;
6918 }
6919 }
6920
Chris Lattnere34b3962005-01-19 04:19:40 +00006921 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006922 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006923 break;
Chris Lattner47599822005-04-02 03:38:53 +00006924
6925 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006926 TargetLowering::LegalizeAction Action =
6927 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6928 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6929 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006930 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006931 break;
6932 }
6933
Chris Lattnere34b3962005-01-19 04:19:40 +00006934 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006935 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006936 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006937 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006938
Misha Brukmanedf128a2005-04-21 22:36:52 +00006939 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006940 case ISD::SUB: {
6941 // If the target wants to custom expand this, let them.
6942 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6943 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006944 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006945 if (Result.getNode()) {
Duncan Sands69bfb152008-06-22 09:42:16 +00006946 ExpandOp(Result, Lo, Hi);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006947 break;
6948 }
6949 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006950 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006951 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006952 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6953 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman475871a2008-07-27 21:46:04 +00006954 SDValue LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006955 LoOps[0] = LHSL;
6956 LoOps[1] = RHSL;
6957 HiOps[0] = LHSH;
6958 HiOps[1] = RHSH;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006959
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006960 //cascaded check to see if any smaller size has a a carry flag.
6961 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6962 bool hasCarry = false;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006963 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6964 MVT AVT = MVT::getIntegerVT(BitSize);
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006965 if (TLI.isOperationLegalOrCustom(OpV, AVT)) {
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006966 hasCarry = true;
6967 break;
6968 }
6969 }
6970
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006971 if(hasCarry) {
Evan Cheng637ed032008-12-12 18:49:09 +00006972 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006973 if (Node->getOpcode() == ISD::ADD) {
6974 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6975 HiOps[2] = Lo.getValue(1);
6976 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6977 } else {
6978 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6979 HiOps[2] = Lo.getValue(1);
6980 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6981 }
6982 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006983 } else {
Andrew Lenharth40d51392008-10-07 14:15:42 +00006984 if (Node->getOpcode() == ISD::ADD) {
Evan Cheng637ed032008-12-12 18:49:09 +00006985 Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
6986 Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
Duncan Sands5480c042009-01-01 15:52:00 +00006987 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(NVT),
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006988 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006989 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6990 DAG.getConstant(1, NVT),
6991 DAG.getConstant(0, NVT));
Duncan Sands5480c042009-01-01 15:52:00 +00006992 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(NVT),
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006993 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006994 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6995 DAG.getConstant(1, NVT),
6996 Carry1);
6997 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6998 } else {
Evan Cheng637ed032008-12-12 18:49:09 +00006999 Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
7000 Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007001 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
7002 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
7003 DAG.getConstant(1, NVT),
7004 DAG.getConstant(0, NVT));
7005 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
7006 }
7007 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00007008 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00007009 }
Chris Lattnerb429f732007-05-17 18:15:41 +00007010
7011 case ISD::ADDC:
7012 case ISD::SUBC: {
7013 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00007014 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00007015 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7016 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7017 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00007018 SDValue LoOps[2] = { LHSL, RHSL };
7019 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00007020
7021 if (Node->getOpcode() == ISD::ADDC) {
7022 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
7023 HiOps[2] = Lo.getValue(1);
7024 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
7025 } else {
7026 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
7027 HiOps[2] = Lo.getValue(1);
7028 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
7029 }
7030 // Remember that we legalized the flag.
7031 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7032 break;
7033 }
7034 case ISD::ADDE:
7035 case ISD::SUBE: {
7036 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00007037 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00007038 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7039 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7040 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00007041 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
7042 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00007043
7044 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
7045 HiOps[2] = Lo.getValue(1);
7046 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
7047
7048 // Remember that we legalized the flag.
7049 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7050 break;
7051 }
Nate Begemanc7c16572005-04-11 03:01:51 +00007052 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00007053 // If the target wants to custom expand this, let them.
7054 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00007055 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007056 if (New.getNode()) {
Chris Lattner8829dc82006-09-16 05:08:34 +00007057 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00007058 break;
7059 }
7060 }
7061
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007062 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, NVT);
7063 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, NVT);
7064 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, NVT);
7065 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00007066 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman475871a2008-07-27 21:46:04 +00007067 SDValue LL, LH, RL, RH;
Nate Begemanc7c16572005-04-11 03:01:51 +00007068 ExpandOp(Node->getOperand(0), LL, LH);
7069 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007070 unsigned OuterBitSize = Op.getValueSizeInBits();
7071 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00007072 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
7073 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00007074 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
7075 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
7076 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00007077 // The inputs are both zero-extended.
7078 if (HasUMUL_LOHI) {
7079 // We can emit a umul_lohi.
7080 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00007081 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00007082 break;
7083 }
7084 if (HasMULHU) {
7085 // We can emit a mulhu+mul.
7086 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7087 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7088 break;
7089 }
Dan Gohman525178c2007-10-08 18:33:35 +00007090 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007091 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00007092 // The input values are both sign-extended.
7093 if (HasSMUL_LOHI) {
7094 // We can emit a smul_lohi.
7095 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00007096 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00007097 break;
7098 }
7099 if (HasMULHS) {
7100 // We can emit a mulhs+mul.
7101 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7102 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
7103 break;
7104 }
7105 }
7106 if (HasUMUL_LOHI) {
7107 // Lo,Hi = umul LHS, RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00007108 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman525178c2007-10-08 18:33:35 +00007109 DAG.getVTList(NVT, NVT), LL, RL);
7110 Lo = UMulLOHI;
7111 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00007112 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7113 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7114 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7115 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00007116 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00007117 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00007118 if (HasMULHU) {
7119 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7120 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7121 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7122 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7123 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7124 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7125 break;
7126 }
Nate Begemanc7c16572005-04-11 03:01:51 +00007127 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00007128
Dan Gohman525178c2007-10-08 18:33:35 +00007129 // If nothing else, we can make a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00007130 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00007131 break;
7132 }
Evan Cheng56966222007-01-12 02:11:51 +00007133 case ISD::SDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007134 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007135 break;
7136 case ISD::UDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007137 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007138 break;
7139 case ISD::SREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007140 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007141 break;
7142 case ISD::UREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007143 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007144 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00007145
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007146 case ISD::FADD:
Duncan Sands460a14e2008-04-12 17:14:18 +00007147 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7148 RTLIB::ADD_F64,
7149 RTLIB::ADD_F80,
7150 RTLIB::ADD_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007151 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007152 break;
7153 case ISD::FSUB:
Duncan Sands460a14e2008-04-12 17:14:18 +00007154 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7155 RTLIB::SUB_F64,
7156 RTLIB::SUB_F80,
7157 RTLIB::SUB_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007158 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007159 break;
7160 case ISD::FMUL:
Duncan Sands460a14e2008-04-12 17:14:18 +00007161 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7162 RTLIB::MUL_F64,
7163 RTLIB::MUL_F80,
7164 RTLIB::MUL_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007165 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007166 break;
7167 case ISD::FDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007168 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7169 RTLIB::DIV_F64,
7170 RTLIB::DIV_F80,
7171 RTLIB::DIV_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007172 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007173 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007174 case ISD::FP_EXTEND: {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007175 if (VT == MVT::ppcf128) {
7176 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7177 Node->getOperand(0).getValueType()==MVT::f64);
7178 const uint64_t zero = 0;
7179 if (Node->getOperand(0).getValueType()==MVT::f32)
7180 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
7181 else
7182 Hi = Node->getOperand(0);
7183 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7184 break;
7185 }
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007186 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7187 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7188 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007189 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007190 }
7191 case ISD::FP_ROUND: {
7192 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7193 VT);
7194 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7195 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007196 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007197 }
Evan Cheng4b887022008-09-09 23:02:14 +00007198 case ISD::FSQRT:
7199 case ISD::FSIN:
7200 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007201 case ISD::FLOG:
7202 case ISD::FLOG2:
7203 case ISD::FLOG10:
7204 case ISD::FEXP:
7205 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007206 case ISD::FTRUNC:
7207 case ISD::FFLOOR:
7208 case ISD::FCEIL:
7209 case ISD::FRINT:
7210 case ISD::FNEARBYINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00007211 case ISD::FPOW:
7212 case ISD::FPOWI: {
Evan Cheng56966222007-01-12 02:11:51 +00007213 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007214 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00007215 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00007216 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7217 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007218 break;
7219 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00007220 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7221 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007222 break;
7223 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00007224 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7225 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007226 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007227 case ISD::FLOG:
7228 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7229 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7230 break;
7231 case ISD::FLOG2:
7232 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7233 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7234 break;
7235 case ISD::FLOG10:
7236 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7237 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7238 break;
7239 case ISD::FEXP:
7240 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7241 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7242 break;
7243 case ISD::FEXP2:
7244 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7245 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7246 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007247 case ISD::FTRUNC:
7248 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7249 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7250 break;
7251 case ISD::FFLOOR:
7252 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7253 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7254 break;
7255 case ISD::FCEIL:
7256 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7257 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7258 break;
7259 case ISD::FRINT:
7260 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7261 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7262 break;
7263 case ISD::FNEARBYINT:
7264 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7265 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7266 break;
Evan Cheng4b887022008-09-09 23:02:14 +00007267 case ISD::FPOW:
7268 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7269 RTLIB::POW_PPCF128);
7270 break;
7271 case ISD::FPOWI:
7272 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7273 RTLIB::POWI_PPCF128);
7274 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007275 default: assert(0 && "Unreachable!");
7276 }
Duncan Sands460a14e2008-04-12 17:14:18 +00007277 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00007278 break;
7279 }
Evan Cheng966bf242006-12-16 00:52:40 +00007280 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007281 if (VT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00007282 SDValue Tmp;
Dale Johannesenf6467742007-10-12 19:02:17 +00007283 ExpandOp(Node->getOperand(0), Lo, Tmp);
7284 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7285 // lo = hi==fabs(hi) ? lo : -lo;
7286 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7287 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7288 DAG.getCondCode(ISD::SETEQ));
7289 break;
7290 }
Dan Gohman475871a2008-07-27 21:46:04 +00007291 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007292 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7293 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7294 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7295 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7296 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7297 if (getTypeAction(NVT) == Expand)
7298 ExpandOp(Lo, Lo, Hi);
7299 break;
7300 }
7301 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007302 if (VT == MVT::ppcf128) {
7303 ExpandOp(Node->getOperand(0), Lo, Hi);
7304 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7305 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7306 break;
7307 }
Dan Gohman475871a2008-07-27 21:46:04 +00007308 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007309 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7310 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7311 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7312 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7313 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7314 if (getTypeAction(NVT) == Expand)
7315 ExpandOp(Lo, Lo, Hi);
7316 break;
7317 }
Evan Cheng912095b2007-01-04 21:56:39 +00007318 case ISD::FCOPYSIGN: {
7319 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7320 if (getTypeAction(NVT) == Expand)
7321 ExpandOp(Lo, Lo, Hi);
7322 break;
7323 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007324 case ISD::SINT_TO_FP:
7325 case ISD::UINT_TO_FP: {
7326 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007327 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00007328
7329 // Promote the operand if needed. Do this before checking for
7330 // ppcf128 so conversions of i16 and i8 work.
7331 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman475871a2008-07-27 21:46:04 +00007332 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesencf498192008-03-18 17:28:38 +00007333 Tmp = isSigned
7334 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7335 DAG.getValueType(SrcVT))
7336 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greifba36cb52008-08-28 21:40:38 +00007337 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesencf498192008-03-18 17:28:38 +00007338 SrcVT = Node->getOperand(0).getValueType();
7339 }
7340
Dan Gohmana2e94852008-03-10 23:03:31 +00007341 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007342 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007343 if (isSigned) {
7344 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7345 Node->getOperand(0)));
7346 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7347 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007348 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007349 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7350 Node->getOperand(0)));
7351 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7352 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00007353 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007354 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7355 DAG.getConstant(0, MVT::i32),
7356 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7357 DAG.getConstantFP(
7358 APFloat(APInt(128, 2, TwoE32)),
7359 MVT::ppcf128)),
7360 Hi,
7361 DAG.getCondCode(ISD::SETLT)),
7362 Lo, Hi);
7363 }
7364 break;
7365 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00007366 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7367 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007368 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen6e63e092007-10-12 17:52:03 +00007369 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7370 Lo, Hi);
7371 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7372 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7373 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7374 DAG.getConstant(0, MVT::i64),
7375 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7376 DAG.getConstantFP(
7377 APFloat(APInt(128, 2, TwoE64)),
7378 MVT::ppcf128)),
7379 Hi,
7380 DAG.getCondCode(ISD::SETLT)),
7381 Lo, Hi);
7382 break;
7383 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007384
Dan Gohmana2e94852008-03-10 23:03:31 +00007385 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
Dale Johannesenaf435272009-02-02 19:03:57 +00007386 Node->getOperand(0), Node->getDebugLoc());
Evan Cheng110cf482008-04-01 01:50:16 +00007387 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng6ad2f932008-04-01 01:51:26 +00007388 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng110cf482008-04-01 01:50:16 +00007389 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00007390 break;
7391 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00007392 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00007393
Chris Lattner83397362005-12-21 18:02:52 +00007394 // Make sure the resultant values have been legalized themselves, unless this
7395 // is a type that requires multi-step expansion.
7396 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7397 Lo = LegalizeOp(Lo);
Gabor Greifba36cb52008-08-28 21:40:38 +00007398 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00007399 // Don't legalize the high part if it is expanded to a single node.
7400 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00007401 }
Evan Cheng05a2d562006-01-09 18:31:59 +00007402
7403 // Remember in a map if the values will be reused later.
Dan Gohman6b345ee2008-07-07 17:46:23 +00007404 bool isNew =
7405 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng05a2d562006-01-09 18:31:59 +00007406 assert(isNew && "Value already expanded?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007407 isNew = isNew;
Chris Lattner3e928bb2005-01-07 07:47:09 +00007408}
7409
Dan Gohman7f321562007-06-25 16:23:39 +00007410/// SplitVectorOp - Given an operand of vector type, break it down into
7411/// two smaller values, still of vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00007412void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7413 SDValue &Hi) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007414 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007415 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007416 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattnerc7029802006-03-18 01:44:44 +00007417 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00007418
Duncan Sands83ec4b62008-06-06 12:08:01 +00007419 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman5db1afb2007-11-15 21:15:26 +00007420
7421 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7422 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7423
Duncan Sands83ec4b62008-06-06 12:08:01 +00007424 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7425 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007426
Chris Lattnerc7029802006-03-18 01:44:44 +00007427 // See if we already split it.
Dan Gohman475871a2008-07-27 21:46:04 +00007428 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattnerc7029802006-03-18 01:44:44 +00007429 = SplitNodes.find(Op);
7430 if (I != SplitNodes.end()) {
7431 Lo = I->second.first;
7432 Hi = I->second.second;
7433 return;
7434 }
7435
7436 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007437 default:
7438#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007439 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007440#endif
7441 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007442 case ISD::UNDEF:
7443 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7444 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7445 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007446 case ISD::BUILD_PAIR:
7447 Lo = Node->getOperand(0);
7448 Hi = Node->getOperand(1);
7449 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00007450 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman68679912008-04-25 18:07:40 +00007451 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7452 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007453 unsigned Index = Idx->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007454 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman68679912008-04-25 18:07:40 +00007455 if (Index < NewNumElts_Lo)
7456 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7457 DAG.getIntPtrConstant(Index));
7458 else
7459 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7460 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7461 break;
7462 }
Dan Gohman475871a2008-07-27 21:46:04 +00007463 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman68679912008-04-25 18:07:40 +00007464 Node->getOperand(1),
7465 Node->getOperand(2));
7466 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohman9fe46622007-09-28 23:53:40 +00007467 break;
7468 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00007469 case ISD::VECTOR_SHUFFLE: {
7470 // Build the low part.
Dan Gohman475871a2008-07-27 21:46:04 +00007471 SDValue Mask = Node->getOperand(2);
7472 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007473 MVT PtrVT = TLI.getPointerTy();
Chris Lattner6c9c6802007-11-19 21:16:54 +00007474
7475 // Insert all of the elements from the input that are needed. We use
7476 // buildvector of extractelement here because the input vectors will have
7477 // to be legalized, so this makes the code simpler.
7478 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007479 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007480 if (IdxNode.getOpcode() == ISD::UNDEF) {
7481 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7482 continue;
7483 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007484 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007485 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007486 if (Idx >= NumElements) {
7487 InVec = Node->getOperand(1);
7488 Idx -= NumElements;
7489 }
7490 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7491 DAG.getConstant(Idx, PtrVT)));
7492 }
7493 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7494 Ops.clear();
7495
7496 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007497 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007498 if (IdxNode.getOpcode() == ISD::UNDEF) {
7499 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7500 continue;
7501 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007502 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007503 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007504 if (Idx >= NumElements) {
7505 InVec = Node->getOperand(1);
7506 Idx -= NumElements;
7507 }
7508 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7509 DAG.getConstant(Idx, PtrVT)));
7510 }
Mon P Wang92879f32008-07-25 01:30:26 +00007511 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007512 break;
7513 }
Dan Gohman7f321562007-06-25 16:23:39 +00007514 case ISD::BUILD_VECTOR: {
Dan Gohman475871a2008-07-27 21:46:04 +00007515 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00007516 Node->op_begin()+NewNumElts_Lo);
7517 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007518
Dan Gohman475871a2008-07-27 21:46:04 +00007519 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00007520 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007521 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007522 break;
7523 }
Dan Gohman7f321562007-06-25 16:23:39 +00007524 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00007525 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00007526 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7527 if (NewNumSubvectors == 1) {
7528 Lo = Node->getOperand(0);
7529 Hi = Node->getOperand(1);
7530 } else {
Mon P Wangaeb06d22008-11-10 04:46:22 +00007531 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7532 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007533 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00007534
Mon P Wangaeb06d22008-11-10 04:46:22 +00007535 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohman7f321562007-06-25 16:23:39 +00007536 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007537 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00007538 }
Dan Gohman65956352007-06-13 15:12:02 +00007539 break;
7540 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00007541 case ISD::EXTRACT_SUBVECTOR: {
7542 SDValue Vec = Op.getOperand(0);
7543 SDValue Idx = Op.getOperand(1);
7544 MVT IdxVT = Idx.getValueType();
7545
7546 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7547 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7548 if (CIdx) {
7549 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7550 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7551 IdxVT));
7552 } else {
7553 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7554 DAG.getConstant(NewNumElts_Lo, IdxVT));
7555 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7556 }
7557 break;
7558 }
Dan Gohmanc6230962007-10-17 14:48:28 +00007559 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007560 SDValue Cond = Node->getOperand(0);
Dan Gohmanc6230962007-10-17 14:48:28 +00007561
Dan Gohman475871a2008-07-27 21:46:04 +00007562 SDValue LL, LH, RL, RH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007563 SplitVectorOp(Node->getOperand(1), LL, LH);
7564 SplitVectorOp(Node->getOperand(2), RL, RH);
7565
Duncan Sands83ec4b62008-06-06 12:08:01 +00007566 if (Cond.getValueType().isVector()) {
Dan Gohmanc6230962007-10-17 14:48:28 +00007567 // Handle a vector merge.
Dan Gohman475871a2008-07-27 21:46:04 +00007568 SDValue CL, CH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007569 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007570 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7571 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007572 } else {
7573 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00007574 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7575 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007576 }
7577 break;
7578 }
Chris Lattner80c1a562008-06-30 02:43:01 +00007579 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007580 SDValue CondLHS = Node->getOperand(0);
7581 SDValue CondRHS = Node->getOperand(1);
7582 SDValue CondCode = Node->getOperand(4);
Chris Lattner80c1a562008-06-30 02:43:01 +00007583
Dan Gohman475871a2008-07-27 21:46:04 +00007584 SDValue LL, LH, RL, RH;
Chris Lattner80c1a562008-06-30 02:43:01 +00007585 SplitVectorOp(Node->getOperand(2), LL, LH);
7586 SplitVectorOp(Node->getOperand(3), RL, RH);
7587
7588 // Handle a simple select with vector operands.
7589 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7590 LL, RL, CondCode);
7591 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7592 LH, RH, CondCode);
7593 break;
7594 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00007595 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007596 SDValue LL, LH, RL, RH;
Nate Begemanb43e9c12008-05-12 19:40:03 +00007597 SplitVectorOp(Node->getOperand(0), LL, LH);
7598 SplitVectorOp(Node->getOperand(1), RL, RH);
7599 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7600 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7601 break;
7602 }
Dan Gohman7f321562007-06-25 16:23:39 +00007603 case ISD::ADD:
7604 case ISD::SUB:
7605 case ISD::MUL:
7606 case ISD::FADD:
7607 case ISD::FSUB:
7608 case ISD::FMUL:
7609 case ISD::SDIV:
7610 case ISD::UDIV:
7611 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00007612 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007613 case ISD::AND:
7614 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00007615 case ISD::XOR:
7616 case ISD::UREM:
7617 case ISD::SREM:
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007618 case ISD::FREM:
7619 case ISD::SHL:
7620 case ISD::SRA:
7621 case ISD::SRL: {
Dan Gohman475871a2008-07-27 21:46:04 +00007622 SDValue LL, LH, RL, RH;
Chris Lattnerc7029802006-03-18 01:44:44 +00007623 SplitVectorOp(Node->getOperand(0), LL, LH);
7624 SplitVectorOp(Node->getOperand(1), RL, RH);
7625
Nate Begeman5db1afb2007-11-15 21:15:26 +00007626 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7627 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00007628 break;
7629 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00007630 case ISD::FP_ROUND:
Dan Gohman82669522007-10-11 23:57:53 +00007631 case ISD::FPOWI: {
Dan Gohman475871a2008-07-27 21:46:04 +00007632 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007633 SplitVectorOp(Node->getOperand(0), L, H);
7634
Nate Begeman5db1afb2007-11-15 21:15:26 +00007635 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7636 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00007637 break;
7638 }
7639 case ISD::CTTZ:
7640 case ISD::CTLZ:
7641 case ISD::CTPOP:
7642 case ISD::FNEG:
7643 case ISD::FABS:
7644 case ISD::FSQRT:
7645 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00007646 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007647 case ISD::FLOG:
7648 case ISD::FLOG2:
7649 case ISD::FLOG10:
7650 case ISD::FEXP:
7651 case ISD::FEXP2:
Nate Begemanb348d182007-11-17 03:58:34 +00007652 case ISD::FP_TO_SINT:
7653 case ISD::FP_TO_UINT:
7654 case ISD::SINT_TO_FP:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007655 case ISD::UINT_TO_FP:
7656 case ISD::TRUNCATE:
7657 case ISD::ANY_EXTEND:
7658 case ISD::SIGN_EXTEND:
7659 case ISD::ZERO_EXTEND:
7660 case ISD::FP_EXTEND: {
Dan Gohman475871a2008-07-27 21:46:04 +00007661 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007662 SplitVectorOp(Node->getOperand(0), L, H);
7663
Nate Begeman5db1afb2007-11-15 21:15:26 +00007664 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7665 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00007666 break;
7667 }
Mon P Wang77cdf302008-11-10 20:54:11 +00007668 case ISD::CONVERT_RNDSAT: {
7669 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7670 SDValue L, H;
7671 SplitVectorOp(Node->getOperand(0), L, H);
7672 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7673 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7674 SDValue STyOpL = DAG.getValueType(L.getValueType());
7675 SDValue STyOpH = DAG.getValueType(H.getValueType());
7676
7677 SDValue RndOp = Node->getOperand(3);
7678 SDValue SatOp = Node->getOperand(4);
7679
7680 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7681 RndOp, SatOp, CvtCode);
7682 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7683 RndOp, SatOp, CvtCode);
7684 break;
7685 }
Dan Gohman7f321562007-06-25 16:23:39 +00007686 case ISD::LOAD: {
7687 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007688 SDValue Ch = LD->getChain();
7689 SDValue Ptr = LD->getBasePtr();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007690 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007691 const Value *SV = LD->getSrcValue();
7692 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007693 MVT MemoryVT = LD->getMemoryVT();
Dan Gohman7f321562007-06-25 16:23:39 +00007694 unsigned Alignment = LD->getAlignment();
7695 bool isVolatile = LD->isVolatile();
7696
Dan Gohman7f8613e2008-08-14 20:04:46 +00007697 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7698 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7699
7700 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7701 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7702 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7703
7704 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7705 NewVT_Lo, Ch, Ptr, Offset,
7706 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7707 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00007708 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00007709 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00007710 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00007711 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00007712 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7713 NewVT_Hi, Ch, Ptr, Offset,
7714 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00007715
7716 // Build a factor node to remember that this load is independent of the
7717 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00007718 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Chris Lattnerc7029802006-03-18 01:44:44 +00007719 Hi.getValue(1));
7720
7721 // Remember that we legalized the chain.
7722 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00007723 break;
7724 }
Dan Gohman7f321562007-06-25 16:23:39 +00007725 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00007726 // We know the result is a vector. The input may be either a vector or a
7727 // scalar value.
Dan Gohman475871a2008-07-27 21:46:04 +00007728 SDValue InOp = Node->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007729 if (!InOp.getValueType().isVector() ||
7730 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman10a7aa62007-06-29 00:09:08 +00007731 // The input is a scalar or single-element vector.
7732 // Lower to a store/load so that it can be split.
7733 // FIXME: this could be improved probably.
Mon P Wang2920d2b2008-07-15 05:28:34 +00007734 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7735 Op.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00007736 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greifba36cb52008-08-28 21:40:38 +00007737 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Chris Lattner7692eb42006-03-23 21:16:34 +00007738
Dan Gohman475871a2008-07-27 21:46:04 +00007739 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00007740 InOp, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007741 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00007742 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007743 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00007744 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00007745 // Split the vector and convert each of the pieces now.
7746 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007747 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7748 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00007749 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00007750 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007751 }
7752
7753 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00007754 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00007755 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00007756 assert(isNew && "Value already split?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007757 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007758}
7759
7760
Dan Gohman89b20c02007-06-27 14:06:22 +00007761/// ScalarizeVectorOp - Given an operand of single-element vector type
7762/// (e.g. v1f32), convert it into the equivalent operation that returns a
7763/// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +00007764SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007765 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007766 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007767 MVT NewVT = Op.getValueType().getVectorElementType();
7768 assert(Op.getValueType().getVectorNumElements() == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00007769
Dan Gohman7f321562007-06-25 16:23:39 +00007770 // See if we already scalarized it.
Dan Gohman475871a2008-07-27 21:46:04 +00007771 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohman7f321562007-06-25 16:23:39 +00007772 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00007773
Dan Gohman475871a2008-07-27 21:46:04 +00007774 SDValue Result;
Chris Lattnerc7029802006-03-18 01:44:44 +00007775 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00007776 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007777#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007778 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007779#endif
Dan Gohman7f321562007-06-25 16:23:39 +00007780 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7781 case ISD::ADD:
7782 case ISD::FADD:
7783 case ISD::SUB:
7784 case ISD::FSUB:
7785 case ISD::MUL:
7786 case ISD::FMUL:
7787 case ISD::SDIV:
7788 case ISD::UDIV:
7789 case ISD::FDIV:
7790 case ISD::SREM:
7791 case ISD::UREM:
7792 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00007793 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007794 case ISD::AND:
7795 case ISD::OR:
7796 case ISD::XOR:
7797 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00007798 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007799 ScalarizeVectorOp(Node->getOperand(0)),
7800 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00007801 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007802 case ISD::FNEG:
7803 case ISD::FABS:
7804 case ISD::FSQRT:
7805 case ISD::FSIN:
7806 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007807 case ISD::FLOG:
7808 case ISD::FLOG2:
7809 case ISD::FLOG10:
7810 case ISD::FEXP:
7811 case ISD::FEXP2:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007812 case ISD::FP_TO_SINT:
7813 case ISD::FP_TO_UINT:
7814 case ISD::SINT_TO_FP:
7815 case ISD::UINT_TO_FP:
7816 case ISD::SIGN_EXTEND:
7817 case ISD::ZERO_EXTEND:
7818 case ISD::ANY_EXTEND:
7819 case ISD::TRUNCATE:
7820 case ISD::FP_EXTEND:
Dan Gohman7f321562007-06-25 16:23:39 +00007821 Result = DAG.getNode(Node->getOpcode(),
7822 NewVT,
7823 ScalarizeVectorOp(Node->getOperand(0)));
7824 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00007825 case ISD::CONVERT_RNDSAT: {
7826 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7827 Result = DAG.getConvertRndSat(NewVT, Op0,
7828 DAG.getValueType(NewVT),
7829 DAG.getValueType(Op0.getValueType()),
7830 Node->getOperand(3),
7831 Node->getOperand(4),
7832 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7833 break;
7834 }
Dan Gohman9e04c822007-10-12 14:13:46 +00007835 case ISD::FPOWI:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007836 case ISD::FP_ROUND:
Dan Gohman9e04c822007-10-12 14:13:46 +00007837 Result = DAG.getNode(Node->getOpcode(),
7838 NewVT,
7839 ScalarizeVectorOp(Node->getOperand(0)),
7840 Node->getOperand(1));
7841 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007842 case ISD::LOAD: {
7843 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007844 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7845 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman7f8613e2008-08-14 20:04:46 +00007846 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007847 const Value *SV = LD->getSrcValue();
7848 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007849 MVT MemoryVT = LD->getMemoryVT();
7850 unsigned Alignment = LD->getAlignment();
7851 bool isVolatile = LD->isVolatile();
7852
7853 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7854 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7855
7856 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7857 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7858 MemoryVT.getVectorElementType(),
7859 isVolatile, Alignment);
Dan Gohman7f321562007-06-25 16:23:39 +00007860
Chris Lattnerc7029802006-03-18 01:44:44 +00007861 // Remember that we legalized the chain.
7862 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7863 break;
7864 }
Dan Gohman7f321562007-06-25 16:23:39 +00007865 case ISD::BUILD_VECTOR:
7866 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007867 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007868 case ISD::INSERT_VECTOR_ELT:
7869 // Returning the inserted scalar element.
7870 Result = Node->getOperand(1);
7871 break;
7872 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007873 assert(Node->getOperand(0).getValueType() == NewVT &&
7874 "Concat of non-legal vectors not yet supported!");
7875 Result = Node->getOperand(0);
7876 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007877 case ISD::VECTOR_SHUFFLE: {
7878 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman475871a2008-07-27 21:46:04 +00007879 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007880 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohman7f321562007-06-25 16:23:39 +00007881 Result = ScalarizeVectorOp(Node->getOperand(1));
7882 else
7883 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007884 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007885 }
7886 case ISD::EXTRACT_SUBVECTOR:
Mon P Wange0b436a2008-11-06 22:52:21 +00007887 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wangaeb06d22008-11-10 04:46:22 +00007888 Node->getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00007889 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007890 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007891 SDValue Op0 = Op.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007892 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng446efdd2008-05-16 17:19:05 +00007893 Op0 = ScalarizeVectorOp(Op0);
7894 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Chris Lattner5b2316e2006-03-28 20:24:43 +00007895 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007896 }
Dan Gohman7f321562007-06-25 16:23:39 +00007897 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007898 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007899 ScalarizeVectorOp(Op.getOperand(1)),
7900 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007901 break;
Chris Lattner80c1a562008-06-30 02:43:01 +00007902 case ISD::SELECT_CC:
7903 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7904 Node->getOperand(1),
7905 ScalarizeVectorOp(Op.getOperand(2)),
7906 ScalarizeVectorOp(Op.getOperand(3)),
7907 Node->getOperand(4));
7908 break;
Nate Begeman0d1704b2008-05-12 23:09:43 +00007909 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007910 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7911 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Duncan Sands5480c042009-01-01 15:52:00 +00007912 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0.getValueType()),
7913 Op0, Op1, Op.getOperand(2));
Nate Begeman0d1704b2008-05-12 23:09:43 +00007914 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7915 DAG.getConstant(-1ULL, NewVT),
7916 DAG.getConstant(0ULL, NewVT));
7917 break;
7918 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007919 }
7920
7921 if (TLI.isTypeLegal(NewVT))
7922 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007923 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7924 assert(isNew && "Value already scalarized?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007925 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007926 return Result;
7927}
7928
Chris Lattner3e928bb2005-01-07 07:47:09 +00007929
Mon P Wang0c397192008-10-30 08:01:45 +00007930SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7931 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7932 if (I != WidenNodes.end()) return I->second;
7933
7934 MVT VT = Op.getValueType();
7935 assert(VT.isVector() && "Cannot widen non-vector type!");
7936
7937 SDValue Result;
7938 SDNode *Node = Op.getNode();
7939 MVT EVT = VT.getVectorElementType();
7940
7941 unsigned NumElts = VT.getVectorNumElements();
7942 unsigned NewNumElts = WidenVT.getVectorNumElements();
7943 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7944 assert(NewNumElts < 17);
7945
7946 // When widen is called, it is assumed that it is more efficient to use a
7947 // wide type. The default action is to widen to operation to a wider legal
7948 // vector type and then do the operation if it is legal by calling LegalizeOp
7949 // again. If there is no vector equivalent, we will unroll the operation, do
7950 // it, and rebuild the vector. If most of the operations are vectorizible to
7951 // the legal type, the resulting code will be more efficient. If this is not
7952 // the case, the resulting code will preform badly as we end up generating
7953 // code to pack/unpack the results. It is the function that calls widen
Mon P Wangf007a8b2008-11-06 05:31:54 +00007954 // that is responsible for seeing this doesn't happen.
Mon P Wang0c397192008-10-30 08:01:45 +00007955 switch (Node->getOpcode()) {
7956 default:
7957#ifndef NDEBUG
7958 Node->dump(&DAG);
7959#endif
7960 assert(0 && "Unexpected operation in WidenVectorOp!");
7961 break;
7962 case ISD::CopyFromReg:
Mon P Wang49292f12008-11-15 06:05:52 +00007963 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang0c397192008-10-30 08:01:45 +00007964 case ISD::Constant:
7965 case ISD::ConstantFP:
7966 // To build a vector of these elements, clients should call BuildVector
7967 // and with each element instead of creating a node with a vector type
7968 assert(0 && "Unexpected operation in WidenVectorOp!");
7969 case ISD::VAARG:
7970 // Variable Arguments with vector types doesn't make any sense to me
7971 assert(0 && "Unexpected operation in WidenVectorOp!");
7972 break;
Mon P Wang49292f12008-11-15 06:05:52 +00007973 case ISD::UNDEF:
7974 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7975 break;
Mon P Wang0c397192008-10-30 08:01:45 +00007976 case ISD::BUILD_VECTOR: {
7977 // Build a vector with undefined for the new nodes
7978 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7979 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7980 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7981 }
7982 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7983 break;
7984 }
7985 case ISD::INSERT_VECTOR_ELT: {
7986 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7987 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7988 Node->getOperand(1), Node->getOperand(2));
7989 break;
7990 }
7991 case ISD::VECTOR_SHUFFLE: {
7992 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7993 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7994 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7995 // used as permutation array. We build the vector here instead of widening
7996 // because we don't want to legalize and have it turned to something else.
7997 SDValue PermOp = Node->getOperand(2);
7998 SDValueVector NewOps;
7999 MVT PVT = PermOp.getValueType().getVectorElementType();
8000 for (unsigned i = 0; i < NumElts; ++i) {
8001 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
8002 NewOps.push_back(PermOp.getOperand(i));
8003 } else {
8004 unsigned Idx =
Mon P Wange1a0b2e2008-12-13 08:15:14 +00008005 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
Mon P Wang0c397192008-10-30 08:01:45 +00008006 if (Idx < NumElts) {
8007 NewOps.push_back(PermOp.getOperand(i));
8008 }
8009 else {
8010 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
8011 PermOp.getOperand(i).getValueType()));
8012 }
8013 }
8014 }
8015 for (unsigned i = NumElts; i < NewNumElts; ++i) {
8016 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
8017 }
8018
8019 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
8020 MVT::getVectorVT(PVT, NewOps.size()),
8021 &NewOps[0], NewOps.size());
8022
8023 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
8024 break;
8025 }
8026 case ISD::LOAD: {
8027 // If the load widen returns true, we can use a single load for the
8028 // vector. Otherwise, it is returning a token factor for multiple
8029 // loads.
8030 SDValue TFOp;
8031 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
8032 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
8033 else
8034 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
8035 break;
8036 }
8037
8038 case ISD::BIT_CONVERT: {
8039 SDValue Tmp1 = Node->getOperand(0);
8040 // Converts between two different types so we need to determine
8041 // the correct widen type for the input operand.
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008042 MVT InVT = Tmp1.getValueType();
8043 unsigned WidenSize = WidenVT.getSizeInBits();
8044 if (InVT.isVector()) {
8045 MVT InEltVT = InVT.getVectorElementType();
8046 unsigned InEltSize = InEltVT.getSizeInBits();
8047 assert(WidenSize % InEltSize == 0 &&
8048 "can not widen bit convert that are not multiple of element type");
8049 MVT NewInWidenVT = MVT::getVectorVT(InEltVT, WidenSize / InEltSize);
8050 Tmp1 = WidenVectorOp(Tmp1, NewInWidenVT);
8051 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
8052 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Tmp1);
8053 } else {
8054 // If the result size is a multiple of the input size, widen the input
8055 // and then convert.
8056 unsigned InSize = InVT.getSizeInBits();
8057 assert(WidenSize % InSize == 0 &&
8058 "can not widen bit convert that are not multiple of element type");
8059 unsigned NewNumElts = WidenSize / InSize;
8060 SmallVector<SDValue, 16> Ops(NewNumElts);
8061 SDValue UndefVal = DAG.getNode(ISD::UNDEF, InVT);
8062 Ops[0] = Tmp1;
8063 for (unsigned i = 1; i < NewNumElts; ++i)
8064 Ops[i] = UndefVal;
Mon P Wang0c397192008-10-30 08:01:45 +00008065
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008066 MVT NewInVT = MVT::getVectorVT(InVT, NewNumElts);
8067 Result = DAG.getNode(ISD::BUILD_VECTOR, NewInVT, &Ops[0], NewNumElts);
8068 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00008069 }
8070 break;
8071 }
8072
8073 case ISD::SINT_TO_FP:
8074 case ISD::UINT_TO_FP:
8075 case ISD::FP_TO_SINT:
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008076 case ISD::FP_TO_UINT:
8077 case ISD::FP_ROUND: {
Mon P Wang0c397192008-10-30 08:01:45 +00008078 SDValue Tmp1 = Node->getOperand(0);
8079 // Converts between two different types so we need to determine
8080 // the correct widen type for the input operand.
8081 MVT TVT = Tmp1.getValueType();
8082 assert(TVT.isVector() && "can not widen non vector type");
8083 MVT TEVT = TVT.getVectorElementType();
8084 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
8085 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
8086 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
8087 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00008088 break;
8089 }
8090
8091 case ISD::FP_EXTEND:
8092 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
8093 case ISD::TRUNCATE:
8094 case ISD::SIGN_EXTEND:
8095 case ISD::ZERO_EXTEND:
8096 case ISD::ANY_EXTEND:
Mon P Wang0c397192008-10-30 08:01:45 +00008097 case ISD::SIGN_EXTEND_INREG:
8098 case ISD::FABS:
8099 case ISD::FNEG:
8100 case ISD::FSQRT:
8101 case ISD::FSIN:
Mon P Wang49292f12008-11-15 06:05:52 +00008102 case ISD::FCOS:
8103 case ISD::CTPOP:
8104 case ISD::CTTZ:
8105 case ISD::CTLZ: {
Mon P Wang0c397192008-10-30 08:01:45 +00008106 // Unary op widening
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008107 SDValue Tmp1;
Mon P Wang0c397192008-10-30 08:01:45 +00008108 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8109 assert(Tmp1.getValueType() == WidenVT);
8110 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00008111 break;
8112 }
Mon P Wang77cdf302008-11-10 20:54:11 +00008113 case ISD::CONVERT_RNDSAT: {
8114 SDValue RndOp = Node->getOperand(3);
8115 SDValue SatOp = Node->getOperand(4);
Mon P Wang77cdf302008-11-10 20:54:11 +00008116 SDValue SrcOp = Node->getOperand(0);
8117
8118 // Converts between two different types so we need to determine
8119 // the correct widen type for the input operand.
8120 MVT SVT = SrcOp.getValueType();
8121 assert(SVT.isVector() && "can not widen non vector type");
8122 MVT SEVT = SVT.getVectorElementType();
8123 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8124
8125 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8126 assert(SrcOp.getValueType() == WidenVT);
8127 SDValue DTyOp = DAG.getValueType(WidenVT);
8128 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8129 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8130
8131 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
8132 RndOp, SatOp, CvtCode);
Mon P Wang77cdf302008-11-10 20:54:11 +00008133 break;
8134 }
Mon P Wang0c397192008-10-30 08:01:45 +00008135 case ISD::FPOW:
8136 case ISD::FPOWI:
8137 case ISD::ADD:
8138 case ISD::SUB:
8139 case ISD::MUL:
8140 case ISD::MULHS:
8141 case ISD::MULHU:
8142 case ISD::AND:
8143 case ISD::OR:
8144 case ISD::XOR:
8145 case ISD::FADD:
8146 case ISD::FSUB:
8147 case ISD::FMUL:
8148 case ISD::SDIV:
8149 case ISD::SREM:
8150 case ISD::FDIV:
8151 case ISD::FREM:
8152 case ISD::FCOPYSIGN:
8153 case ISD::UDIV:
8154 case ISD::UREM:
8155 case ISD::BSWAP: {
8156 // Binary op widening
Mon P Wang0c397192008-10-30 08:01:45 +00008157 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8158 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8159 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8160 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008161 break;
8162 }
8163
8164 case ISD::SHL:
8165 case ISD::SRA:
8166 case ISD::SRL: {
Mon P Wang0c397192008-10-30 08:01:45 +00008167 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8168 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangfb13f002008-12-02 07:35:08 +00008169 SDValue ShOp = Node->getOperand(1);
8170 MVT ShVT = ShOp.getValueType();
8171 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8172 WidenVT.getVectorNumElements());
8173 ShOp = WidenVectorOp(ShOp, NewShVT);
8174 assert(ShOp.getValueType() == NewShVT);
8175 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, ShOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008176 break;
8177 }
Mon P Wangfb13f002008-12-02 07:35:08 +00008178
Mon P Wang0c397192008-10-30 08:01:45 +00008179 case ISD::EXTRACT_VECTOR_ELT: {
8180 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8181 assert(Tmp1.getValueType() == WidenVT);
8182 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8183 break;
8184 }
8185 case ISD::CONCAT_VECTORS: {
8186 // We concurrently support only widen on a multiple of the incoming vector.
8187 // We could widen on a multiple of the incoming operand if necessary.
8188 unsigned NumConcat = NewNumElts / NumElts;
8189 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Mon P Wangfb13f002008-12-02 07:35:08 +00008190 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
Mon P Wang0c397192008-10-30 08:01:45 +00008191 SmallVector<SDValue, 8> MOps;
8192 MOps.push_back(Op);
8193 for (unsigned i = 1; i != NumConcat; ++i) {
8194 MOps.push_back(UndefVal);
8195 }
8196 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8197 &MOps[0], MOps.size()));
8198 break;
8199 }
8200 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang49292f12008-11-15 06:05:52 +00008201 SDValue Tmp1 = Node->getOperand(0);
8202 SDValue Idx = Node->getOperand(1);
8203 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8204 if (CIdx && CIdx->getZExtValue() == 0) {
8205 // Since we are access the start of the vector, the incoming
8206 // vector type might be the proper.
8207 MVT Tmp1VT = Tmp1.getValueType();
8208 if (Tmp1VT == WidenVT)
8209 return Tmp1;
8210 else {
8211 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8212 if (Tmp1VTNumElts < NewNumElts)
8213 Result = WidenVectorOp(Tmp1, WidenVT);
8214 else
8215 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8216 }
8217 } else if (NewNumElts % NumElts == 0) {
8218 // Widen the extracted subvector.
8219 unsigned NumConcat = NewNumElts / NumElts;
8220 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8221 SmallVector<SDValue, 8> MOps;
8222 MOps.push_back(Op);
8223 for (unsigned i = 1; i != NumConcat; ++i) {
8224 MOps.push_back(UndefVal);
8225 }
8226 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8227 &MOps[0], MOps.size()));
8228 } else {
8229 assert(0 && "can not widen extract subvector");
8230 // This could be implemented using insert and build vector but I would
8231 // like to see when this happens.
8232 }
Mon P Wang0c397192008-10-30 08:01:45 +00008233 break;
8234 }
8235
8236 case ISD::SELECT: {
Mon P Wang0c397192008-10-30 08:01:45 +00008237 // Determine new condition widen type and widen
8238 SDValue Cond1 = Node->getOperand(0);
8239 MVT CondVT = Cond1.getValueType();
8240 assert(CondVT.isVector() && "can not widen non vector type");
8241 MVT CondEVT = CondVT.getVectorElementType();
8242 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8243 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8244 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8245
8246 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8247 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8248 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8249 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008250 break;
8251 }
8252
8253 case ISD::SELECT_CC: {
Mon P Wang0c397192008-10-30 08:01:45 +00008254 // Determine new condition widen type and widen
8255 SDValue Cond1 = Node->getOperand(0);
8256 SDValue Cond2 = Node->getOperand(1);
8257 MVT CondVT = Cond1.getValueType();
8258 assert(CondVT.isVector() && "can not widen non vector type");
8259 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8260 MVT CondEVT = CondVT.getVectorElementType();
8261 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8262 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8263 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8264 assert(Cond1.getValueType() == CondWidenVT &&
8265 Cond2.getValueType() == CondWidenVT && "condition not widen");
8266
8267 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8268 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8269 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8270 "operands not widen");
8271 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8272 Tmp2, Node->getOperand(4));
Mon P Wang0c397192008-10-30 08:01:45 +00008273 break;
Mon P Wang2eb13c32008-10-30 18:21:52 +00008274 }
8275 case ISD::VSETCC: {
8276 // Determine widen for the operand
8277 SDValue Tmp1 = Node->getOperand(0);
8278 MVT TmpVT = Tmp1.getValueType();
8279 assert(TmpVT.isVector() && "can not widen non vector type");
8280 MVT TmpEVT = TmpVT.getVectorElementType();
8281 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8282 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8283 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008284 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
Mon P Wang2eb13c32008-10-30 18:21:52 +00008285 Node->getOperand(2));
Mon P Wang0c397192008-10-30 08:01:45 +00008286 break;
8287 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008288 case ISD::ATOMIC_CMP_SWAP:
8289 case ISD::ATOMIC_LOAD_ADD:
8290 case ISD::ATOMIC_LOAD_SUB:
8291 case ISD::ATOMIC_LOAD_AND:
8292 case ISD::ATOMIC_LOAD_OR:
8293 case ISD::ATOMIC_LOAD_XOR:
8294 case ISD::ATOMIC_LOAD_NAND:
8295 case ISD::ATOMIC_LOAD_MIN:
8296 case ISD::ATOMIC_LOAD_MAX:
8297 case ISD::ATOMIC_LOAD_UMIN:
8298 case ISD::ATOMIC_LOAD_UMAX:
8299 case ISD::ATOMIC_SWAP: {
Mon P Wang0c397192008-10-30 08:01:45 +00008300 // For now, we assume that using vectors for these operations don't make
8301 // much sense so we just split it. We return an empty result
8302 SDValue X, Y;
8303 SplitVectorOp(Op, X, Y);
8304 return Result;
8305 break;
8306 }
8307
8308 } // end switch (Node->getOpcode())
8309
8310 assert(Result.getNode() && "Didn't set a result!");
8311 if (Result != Op)
8312 Result = LegalizeOp(Result);
8313
Mon P Wangf007a8b2008-11-06 05:31:54 +00008314 AddWidenedOperand(Op, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00008315 return Result;
8316}
8317
8318// Utility function to find a legal vector type and its associated element
8319// type from a preferred width and whose vector type must be the same size
8320// as the VVT.
8321// TLI: Target lowering used to determine legal types
8322// Width: Preferred width of element type
8323// VVT: Vector value type whose size we must match.
8324// Returns VecEVT and EVT - the vector type and its associated element type
Dan Gohman0d137d72009-01-15 16:43:02 +00008325static void FindWidenVecType(const TargetLowering &TLI, unsigned Width, MVT VVT,
Mon P Wang0c397192008-10-30 08:01:45 +00008326 MVT& EVT, MVT& VecEVT) {
8327 // We start with the preferred width, make it a power of 2 and see if
8328 // we can find a vector type of that width. If not, we reduce it by
8329 // another power of 2. If we have widen the type, a vector of bytes should
8330 // always be legal.
8331 assert(TLI.isTypeLegal(VVT));
8332 unsigned EWidth = Width + 1;
8333 do {
8334 assert(EWidth > 0);
8335 EWidth = (1 << Log2_32(EWidth-1));
8336 EVT = MVT::getIntegerVT(EWidth);
8337 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8338 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8339 } while (!TLI.isTypeLegal(VecEVT) ||
8340 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8341}
8342
8343SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8344 SDValue Chain,
8345 SDValue BasePtr,
8346 const Value *SV,
8347 int SVOffset,
8348 unsigned Alignment,
8349 bool isVolatile,
8350 unsigned LdWidth,
8351 MVT ResType) {
8352 // We assume that we have good rules to handle loading power of two loads so
8353 // we break down the operations to power of 2 loads. The strategy is to
8354 // load the largest power of 2 that we can easily transform to a legal vector
8355 // and then insert into that vector, and the cast the result into the legal
8356 // vector that we want. This avoids unnecessary stack converts.
8357 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8358 // the load is nonvolatile, we an use a wider load for the value.
8359 // Find a vector length we can load a large chunk
8360 MVT EVT, VecEVT;
8361 unsigned EVTWidth;
8362 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8363 EVTWidth = EVT.getSizeInBits();
8364
8365 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8366 isVolatile, Alignment);
8367 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8368 LdChain.push_back(LdOp.getValue(1));
8369
8370 // Check if we can load the element with one instruction
8371 if (LdWidth == EVTWidth) {
8372 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8373 }
8374
8375 // The vector element order is endianness dependent.
8376 unsigned Idx = 1;
8377 LdWidth -= EVTWidth;
8378 unsigned Offset = 0;
8379
8380 while (LdWidth > 0) {
8381 unsigned Increment = EVTWidth / 8;
8382 Offset += Increment;
8383 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8384 DAG.getIntPtrConstant(Increment));
8385
8386 if (LdWidth < EVTWidth) {
8387 // Our current type we are using is too large, use a smaller size by
8388 // using a smaller power of 2
8389 unsigned oEVTWidth = EVTWidth;
8390 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8391 EVTWidth = EVT.getSizeInBits();
8392 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008393 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang0c397192008-10-30 08:01:45 +00008394 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8395 }
8396
8397 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8398 SVOffset+Offset, isVolatile,
8399 MinAlign(Alignment, Offset));
8400 LdChain.push_back(LdOp.getValue(1));
8401 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8402 DAG.getIntPtrConstant(Idx++));
8403
8404 LdWidth -= EVTWidth;
8405 }
8406
8407 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8408}
8409
8410bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8411 SDValue& TFOp,
8412 SDValue Op,
8413 MVT NVT) {
8414 // TODO: Add support for ConcatVec and the ability to load many vector
8415 // types (e.g., v4i8). This will not work when a vector register
8416 // to memory mapping is strange (e.g., vector elements are not
8417 // stored in some sequential order).
8418
8419 // It must be true that the widen vector type is bigger than where
8420 // we need to load from.
8421 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8422 MVT LdVT = LD->getMemoryVT();
8423 assert(LdVT.isVector() && NVT.isVector());
8424 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8425
8426 // Load information
8427 SDValue Chain = LD->getChain();
8428 SDValue BasePtr = LD->getBasePtr();
8429 int SVOffset = LD->getSrcValueOffset();
8430 unsigned Alignment = LD->getAlignment();
8431 bool isVolatile = LD->isVolatile();
8432 const Value *SV = LD->getSrcValue();
8433 unsigned int LdWidth = LdVT.getSizeInBits();
8434
8435 // Load value as a large register
8436 SDValueVector LdChain;
8437 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8438 Alignment, isVolatile, LdWidth, NVT);
8439
8440 if (LdChain.size() == 1) {
8441 TFOp = LdChain[0];
8442 return true;
8443 }
8444 else {
8445 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8446 return false;
8447 }
8448}
8449
8450
8451void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8452 SDValue Chain,
8453 SDValue BasePtr,
8454 const Value *SV,
8455 int SVOffset,
8456 unsigned Alignment,
8457 bool isVolatile,
Mon P Wang49292f12008-11-15 06:05:52 +00008458 SDValue ValOp,
Mon P Wang0c397192008-10-30 08:01:45 +00008459 unsigned StWidth) {
8460 // Breaks the stores into a series of power of 2 width stores. For any
8461 // width, we convert the vector to the vector of element size that we
8462 // want to store. This avoids requiring a stack convert.
8463
8464 // Find a width of the element type we can store with
8465 MVT VVT = ValOp.getValueType();
8466 MVT EVT, VecEVT;
8467 unsigned EVTWidth;
8468 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8469 EVTWidth = EVT.getSizeInBits();
8470
8471 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8472 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wange0b436a2008-11-06 22:52:21 +00008473 DAG.getIntPtrConstant(0));
Mon P Wang0c397192008-10-30 08:01:45 +00008474 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8475 isVolatile, Alignment);
8476 StChain.push_back(StOp);
8477
8478 // Check if we are done
8479 if (StWidth == EVTWidth) {
8480 return;
8481 }
8482
8483 unsigned Idx = 1;
8484 StWidth -= EVTWidth;
8485 unsigned Offset = 0;
8486
8487 while (StWidth > 0) {
8488 unsigned Increment = EVTWidth / 8;
8489 Offset += Increment;
8490 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8491 DAG.getIntPtrConstant(Increment));
8492
8493 if (StWidth < EVTWidth) {
8494 // Our current type we are using is too large, use a smaller size by
8495 // using a smaller power of 2
8496 unsigned oEVTWidth = EVTWidth;
8497 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8498 EVTWidth = EVT.getSizeInBits();
8499 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008500 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang0c397192008-10-30 08:01:45 +00008501 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8502 }
8503
8504 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang49292f12008-11-15 06:05:52 +00008505 DAG.getIntPtrConstant(Idx++));
Mon P Wang0c397192008-10-30 08:01:45 +00008506 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8507 SVOffset + Offset, isVolatile,
8508 MinAlign(Alignment, Offset)));
8509 StWidth -= EVTWidth;
8510 }
8511}
8512
8513
8514SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8515 SDValue Chain,
8516 SDValue BasePtr) {
8517 // TODO: It might be cleaner if we can use SplitVector and have more legal
8518 // vector types that can be stored into memory (e.g., v4xi8 can
8519 // be stored as a word). This will not work when a vector register
8520 // to memory mapping is strange (e.g., vector elements are not
8521 // stored in some sequential order).
8522
8523 MVT StVT = ST->getMemoryVT();
8524 SDValue ValOp = ST->getValue();
8525
8526 // Check if we have widen this node with another value
8527 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8528 if (I != WidenNodes.end())
8529 ValOp = I->second;
8530
8531 MVT VVT = ValOp.getValueType();
8532
8533 // It must be true that we the widen vector type is bigger than where
8534 // we need to store.
8535 assert(StVT.isVector() && VVT.isVector());
Dan Gohmanf83c81a2009-01-28 03:10:52 +00008536 assert(StVT.bitsLT(VVT));
Mon P Wang0c397192008-10-30 08:01:45 +00008537 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8538
8539 // Store value
8540 SDValueVector StChain;
8541 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8542 ST->getSrcValueOffset(), ST->getAlignment(),
8543 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8544 if (StChain.size() == 1)
8545 return StChain[0];
8546 else
8547 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8548}
8549
8550
Chris Lattner3e928bb2005-01-07 07:47:09 +00008551// SelectionDAG::Legalize - This is the entry point for the file.
8552//
Duncan Sandsb6862bb2008-12-14 09:43:15 +00008553void SelectionDAG::Legalize(bool TypesNeedLegalizing) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00008554 /// run - This is the main entry point to this class.
8555 ///
Duncan Sandsb6862bb2008-12-14 09:43:15 +00008556 SelectionDAGLegalize(*this, TypesNeedLegalizing).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00008557}
8558