blob: b0ab178a33259852a958174e39807b9a51247805 [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
Mon P Wange5ab34e2009-02-04 19:38:14 +000071 /// IsLegalizingCallArguments - This member is used only for the purpose
72 /// of providing assert to check for LegalizeTypes because legalizing an
73 /// operation might introduce call nodes that might need type legalization.
74 bool IsLegalizingCallArgs;
75
Chris Lattner3e928bb2005-01-07 07:47:09 +000076 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000077 Legal, // The target natively supports this operation.
78 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000079 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000080 };
Chris Lattner6831a812006-02-13 09:18:02 +000081
Chris Lattner3e928bb2005-01-07 07:47:09 +000082 /// ValueTypeActions - This is a bitvector that contains two bits for each
83 /// value type, where the two bits correspond to the LegalizeAction enum.
84 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000085 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000086
Chris Lattner3e928bb2005-01-07 07:47:09 +000087 /// LegalizedNodes - For nodes that are of legal width, and that have more
88 /// than one use, this map indicates what regularized operand to use. This
89 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000090 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000091
Chris Lattner03c85462005-01-15 05:21:40 +000092 /// PromotedNodes - For nodes that are below legal width, and that have more
93 /// than one use, this map indicates what promoted value to use. This allows
94 /// us to avoid promoting the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000095 DenseMap<SDValue, SDValue> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000096
Chris Lattnerc7029802006-03-18 01:44:44 +000097 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +000098 /// which operands are the expanded version of the input. This allows
Chris Lattnerc7029802006-03-18 01:44:44 +000099 /// us to avoid expanding the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +0000100 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000101
Chris Lattnerc7029802006-03-18 01:44:44 +0000102 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +0000103 /// which operands are the split version of the input. This allows us
Chris Lattnerc7029802006-03-18 01:44:44 +0000104 /// to avoid splitting the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +0000105 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000106
Dan Gohman7f321562007-06-25 16:23:39 +0000107 /// ScalarizedNodes - For nodes that need to be converted from vector types to
108 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000109 /// processed to the result.
Dan Gohman475871a2008-07-27 21:46:04 +0000110 std::map<SDValue, SDValue> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000111
Mon P Wangf007a8b2008-11-06 05:31:54 +0000112 /// WidenNodes - For nodes that need to be widened from one vector type to
113 /// another, this contains the mapping of those that we have already widen.
114 /// This allows us to avoid widening more than once.
Mon P Wang0c397192008-10-30 08:01:45 +0000115 std::map<SDValue, SDValue> WidenNodes;
116
Dan Gohman475871a2008-07-27 21:46:04 +0000117 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000118 LegalizedNodes.insert(std::make_pair(From, To));
119 // If someone requests legalization of the new node, return itself.
120 if (From != To)
121 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000122 }
Dan Gohman475871a2008-07-27 21:46:04 +0000123 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman6b345ee2008-07-07 17:46:23 +0000124 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Chris Lattner03c85462005-01-15 05:21:40 +0000125 assert(isNew && "Got into the map somehow?");
Evan Cheng24ac4082008-11-24 07:09:49 +0000126 isNew = isNew;
Chris Lattner69a889e2005-12-20 00:53:54 +0000127 // If someone requests legalization of the new node, return itself.
128 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000129 }
Mon P Wangf007a8b2008-11-06 05:31:54 +0000130 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang0c397192008-10-30 08:01:45 +0000131 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
132 assert(isNew && "Got into the map somehow?");
Evan Cheng24ac4082008-11-24 07:09:49 +0000133 isNew = isNew;
Mon P Wang0c397192008-10-30 08:01:45 +0000134 // If someone requests legalization of the new node, return itself.
135 LegalizedNodes.insert(std::make_pair(To, To));
136 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000137
Chris Lattner3e928bb2005-01-07 07:47:09 +0000138public:
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000139 explicit SelectionDAGLegalize(SelectionDAG &DAG, bool TypesNeedLegalizing);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000140
Chris Lattner3e928bb2005-01-07 07:47:09 +0000141 /// getTypeAction - Return how we should legalize values of this type, either
142 /// it is already legal or we need to expand it into multiple registers of
143 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000144 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000145 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000146 }
147
148 /// isTypeLegal - Return true if this type is legal on this target.
149 ///
Duncan Sands83ec4b62008-06-06 12:08:01 +0000150 bool isTypeLegal(MVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000151 return getTypeAction(VT) == Legal;
152 }
153
Chris Lattner3e928bb2005-01-07 07:47:09 +0000154 void LegalizeDAG();
155
Chris Lattner456a93a2006-01-28 07:39:30 +0000156private:
Dan Gohman7f321562007-06-25 16:23:39 +0000157 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000158 /// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000159 void HandleOp(SDValue Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000160
161 /// LegalizeOp - We know that the specified value has a legal type.
162 /// Recursively ensure that the operands have legal types, then return the
163 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000164 SDValue LegalizeOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000165
Dan Gohman82669522007-10-11 23:57:53 +0000166 /// UnrollVectorOp - We know that the given vector has a legal type, however
167 /// the operation it performs is not legal and is an operation that we have
168 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
169 /// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000170 SDValue UnrollVectorOp(SDValue O);
Nate Begeman68679912008-04-25 18:07:40 +0000171
172 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
173 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
174 /// is necessary to spill the vector being inserted into to memory, perform
175 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000176 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Dale Johannesenbb5da912009-02-02 20:41:04 +0000177 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +0000178
Chris Lattnerc7029802006-03-18 01:44:44 +0000179 /// PromoteOp - Given an operation that produces a value in an invalid type,
180 /// promote it to compute the value into a larger type. The produced value
181 /// will have the correct bits for the low portion of the register, but no
182 /// guarantee is made about the top bits: it may be zero, sign-extended, or
183 /// garbage.
Dan Gohman475871a2008-07-27 21:46:04 +0000184 SDValue PromoteOp(SDValue O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000185
Dan Gohman475871a2008-07-27 21:46:04 +0000186 /// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattnerc7029802006-03-18 01:44:44 +0000187 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman929d3eb2008-10-01 15:07:49 +0000188 /// the LegalizedNodes map is filled in for any results that are not expanded,
Chris Lattnerc7029802006-03-18 01:44:44 +0000189 /// the ExpandedNodes map is filled in for any results that are expanded, and
190 /// the Lo/Hi values are returned. This applies to integer types and Vector
191 /// types.
Dan Gohman475871a2008-07-27 21:46:04 +0000192 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000193
Mon P Wangf007a8b2008-11-06 05:31:54 +0000194 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
195 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
196 /// for the existing elements but no guarantee is made about the new elements
197 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
198 /// when we have an instruction operating on an illegal vector type and we
199 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang0c397192008-10-30 08:01:45 +0000200 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
201
Dan Gohman7f321562007-06-25 16:23:39 +0000202 /// SplitVectorOp - Given an operand of vector type, break it down into
203 /// two smaller values.
Dan Gohman475871a2008-07-27 21:46:04 +0000204 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000205
Dan Gohman89b20c02007-06-27 14:06:22 +0000206 /// ScalarizeVectorOp - Given an operand of single-element vector type
207 /// (e.g. v1f32), convert it into the equivalent operation that returns a
208 /// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +0000209 SDValue ScalarizeVectorOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000210
Mon P Wangf007a8b2008-11-06 05:31:54 +0000211 /// Useful 16 element vector type that is used to pass operands for widening.
Mon P Wang0c397192008-10-30 08:01:45 +0000212 typedef SmallVector<SDValue, 16> SDValueVector;
213
214 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
215 /// the LdChain contains a single load and false if it contains a token
216 /// factor for multiple loads. It takes
217 /// Result: location to return the result
218 /// LdChain: location to return the load chain
219 /// Op: load operation to widen
220 /// NVT: widen vector result type we want for the load
221 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
222 SDValue Op, MVT NVT);
223
224 /// Helper genWidenVectorLoads - Helper function to generate a set of
225 /// loads to load a vector with a resulting wider type. It takes
226 /// LdChain: list of chains for the load we have generated
227 /// Chain: incoming chain for the ld vector
228 /// BasePtr: base pointer to load from
229 /// SV: memory disambiguation source value
230 /// SVOffset: memory disambiugation offset
231 /// Alignment: alignment of the memory
232 /// isVolatile: volatile load
233 /// LdWidth: width of memory that we want to load
234 /// ResType: the wider result result type for the resulting loaded vector
235 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
236 SDValue BasePtr, const Value *SV,
237 int SVOffset, unsigned Alignment,
238 bool isVolatile, unsigned LdWidth,
Dale Johannesenc6be1102009-02-02 22:49:46 +0000239 MVT ResType, DebugLoc dl);
Mon P Wang0c397192008-10-30 08:01:45 +0000240
241 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
242 /// location. It takes
243 /// ST: store node that we want to replace
244 /// Chain: incoming store chain
245 /// BasePtr: base address of where we want to store into
246 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
247 SDValue BasePtr);
248
249 /// Helper genWidenVectorStores - Helper function to generate a set of
250 /// stores to store a widen vector into non widen memory
251 // It takes
252 // StChain: list of chains for the stores we have generated
253 // Chain: incoming chain for the ld vector
254 // BasePtr: base pointer to load from
255 // SV: memory disambiguation source value
256 // SVOffset: memory disambiugation offset
257 // Alignment: alignment of the memory
258 // isVolatile: volatile lod
259 // ValOp: value to store
260 // StWidth: width of memory that we want to store
261 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
262 SDValue BasePtr, const Value *SV,
263 int SVOffset, unsigned Alignment,
264 bool isVolatile, SDValue ValOp,
Dale Johannesenc6be1102009-02-02 22:49:46 +0000265 unsigned StWidth, DebugLoc dl);
Mon P Wang0c397192008-10-30 08:01:45 +0000266
Duncan Sandsd038e042008-07-21 10:20:31 +0000267 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Chris Lattner4352cc92006-04-04 17:23:26 +0000268 /// specified mask and type. Targets can specify exactly which masks they
269 /// support and the code generator is tasked with not creating illegal masks.
270 ///
271 /// Note that this will also return true for shuffles that are promoted to a
272 /// different type.
273 ///
274 /// If this is a legal shuffle, this method returns the (possibly promoted)
275 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman475871a2008-07-27 21:46:04 +0000276 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Chris Lattner4352cc92006-04-04 17:23:26 +0000277
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000278 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000279 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000280
Dale Johannesenbb5da912009-02-02 20:41:04 +0000281 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC,
282 DebugLoc dl);
283 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
284 DebugLoc dl);
285 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
286 DebugLoc dl) {
287 LegalizeSetCCOperands(LHS, RHS, CC, dl);
288 LegalizeSetCCCondCode(VT, LHS, RHS, CC, dl);
Evan Cheng7f042682008-10-15 02:05:31 +0000289 }
Nate Begeman750ac1b2006-02-01 07:19:44 +0000290
Dan Gohman475871a2008-07-27 21:46:04 +0000291 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
292 SDValue &Hi);
Dale Johannesenaf435272009-02-02 19:03:57 +0000293 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source, DebugLoc dl);
Chris Lattnercad063f2005-07-16 00:19:57 +0000294
Dale Johannesen8a782a22009-02-02 22:12:50 +0000295 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000296 SDValue ExpandBUILD_VECTOR(SDNode *Node);
297 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dale Johannesenaf435272009-02-02 19:03:57 +0000298 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy,
299 SDValue Op, DebugLoc dl);
300 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT,
301 DebugLoc dl);
302 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned,
303 DebugLoc dl);
304 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned,
305 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000306
Dale Johannesen8a782a22009-02-02 22:12:50 +0000307 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
308 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000309 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
Dale Johannesen8a782a22009-02-02 22:12:50 +0000310 SDValue &Lo, SDValue &Hi, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000311 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
Dale Johannesen8a782a22009-02-02 22:12:50 +0000312 SDValue &Lo, SDValue &Hi, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000313
Dan Gohman475871a2008-07-27 21:46:04 +0000314 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
315 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000316};
317}
318
Chris Lattner4352cc92006-04-04 17:23:26 +0000319/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
320/// specified mask and type. Targets can specify exactly which masks they
321/// support and the code generator is tasked with not creating illegal masks.
322///
323/// Note that this will also return true for shuffles that are promoted to a
324/// different type.
Dan Gohman475871a2008-07-27 21:46:04 +0000325SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Chris Lattner4352cc92006-04-04 17:23:26 +0000326 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
327 default: return 0;
328 case TargetLowering::Legal:
329 case TargetLowering::Custom:
330 break;
331 case TargetLowering::Promote: {
332 // If this is promoted to a different type, convert the shuffle mask and
333 // ask if it is legal in the promoted type!
Duncan Sands83ec4b62008-06-06 12:08:01 +0000334 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd038e042008-07-21 10:20:31 +0000335 MVT EltVT = NVT.getVectorElementType();
Chris Lattner4352cc92006-04-04 17:23:26 +0000336
337 // If we changed # elements, change the shuffle mask.
338 unsigned NumEltsGrowth =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000339 NVT.getVectorNumElements() / VT.getVectorNumElements();
Chris Lattner4352cc92006-04-04 17:23:26 +0000340 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
341 if (NumEltsGrowth > 1) {
342 // Renumber the elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000343 SmallVector<SDValue, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000344 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000345 SDValue InOp = Mask.getOperand(i);
Chris Lattner4352cc92006-04-04 17:23:26 +0000346 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
347 if (InOp.getOpcode() == ISD::UNDEF)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000348 Ops.push_back(DAG.getNode(ISD::UNDEF,
349 InOp.getNode()->getDebugLoc(), EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000350 else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000351 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd038e042008-07-21 10:20:31 +0000352 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000353 }
354 }
355 }
Dale Johannesenbb5da912009-02-02 20:41:04 +0000356 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getNode()->getDebugLoc(),
357 NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000358 }
359 VT = NVT;
360 break;
361 }
362 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000363 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Chris Lattner4352cc92006-04-04 17:23:26 +0000364}
365
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000366SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag, bool types)
367 : TLI(dag.getTargetLoweringInfo()), DAG(dag), TypesNeedLegalizing(types),
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000368 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000369 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000370 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000371}
372
Chris Lattner3e928bb2005-01-07 07:47:09 +0000373void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000374 LastCALLSEQ_END = DAG.getEntryNode();
375 IsLegalizingCall = false;
Mon P Wange5ab34e2009-02-04 19:38:14 +0000376 IsLegalizingCallArgs = false;
377
Chris Lattnerab510a72005-10-02 17:49:46 +0000378 // The legalize process is inherently a bottom-up recursive process (users
379 // legalize their uses before themselves). Given infinite stack space, we
380 // could just start legalizing on the root and traverse the whole graph. In
381 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000382 // blocks. To avoid this problem, compute an ordering of the nodes where each
383 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000384 DAG.AssignTopologicalOrder();
385 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
386 E = prior(DAG.allnodes_end()); I != next(E); ++I)
387 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000388
389 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000390 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000391 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
392 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000393
394 ExpandedNodes.clear();
395 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000396 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000397 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000398 ScalarizedNodes.clear();
Mon P Wang0c397192008-10-30 08:01:45 +0000399 WidenNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000400
401 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000402 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000403}
404
Chris Lattner6831a812006-02-13 09:18:02 +0000405
406/// FindCallEndFromCallStart - Given a chained node that is part of a call
407/// sequence, find the CALLSEQ_END node that terminates the call sequence.
408static SDNode *FindCallEndFromCallStart(SDNode *Node) {
409 if (Node->getOpcode() == ISD::CALLSEQ_END)
410 return Node;
411 if (Node->use_empty())
412 return 0; // No CallSeqEnd
413
414 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000415 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000416 if (TheChain.getValueType() != MVT::Other) {
417 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000418 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000419 if (TheChain.getValueType() != MVT::Other) {
420 // Otherwise, hunt for it.
421 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
422 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000423 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000424 break;
425 }
426
427 // Otherwise, we walked into a node without a chain.
428 if (TheChain.getValueType() != MVT::Other)
429 return 0;
430 }
431 }
432
433 for (SDNode::use_iterator UI = Node->use_begin(),
434 E = Node->use_end(); UI != E; ++UI) {
435
436 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000437 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000438 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
439 if (User->getOperand(i) == TheChain)
440 if (SDNode *Result = FindCallEndFromCallStart(User))
441 return Result;
442 }
443 return 0;
444}
445
446/// FindCallStartFromCallEnd - Given a chained node that is part of a call
447/// sequence, find the CALLSEQ_START node that initiates the call sequence.
448static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
449 assert(Node && "Didn't find callseq_start for a call??");
450 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
451
452 assert(Node->getOperand(0).getValueType() == MVT::Other &&
453 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000454 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000455}
456
457/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
458/// see if any uses can reach Dest. If no dest operands can get to dest,
459/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000460///
461/// Keep track of the nodes we fine that actually do lead to Dest in
462/// NodesLeadingTo. This avoids retraversing them exponential number of times.
463///
464bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000465 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000466 if (N == Dest) return true; // N certainly leads to Dest :)
467
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000468 // If we've already processed this node and it does lead to Dest, there is no
469 // need to reprocess it.
470 if (NodesLeadingTo.count(N)) return true;
471
Chris Lattner6831a812006-02-13 09:18:02 +0000472 // If the first result of this node has been already legalized, then it cannot
473 // reach N.
474 switch (getTypeAction(N->getValueType(0))) {
475 case Legal:
Dan Gohman475871a2008-07-27 21:46:04 +0000476 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000477 break;
478 case Promote:
Dan Gohman475871a2008-07-27 21:46:04 +0000479 if (PromotedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000480 break;
481 case Expand:
Dan Gohman475871a2008-07-27 21:46:04 +0000482 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000483 break;
484 }
485
486 // Okay, this node has not already been legalized. Check and legalize all
487 // operands. If none lead to Dest, then we can legalize this node.
488 bool OperandsLeadToDest = false;
489 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
490 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000491 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000492
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000493 if (OperandsLeadToDest) {
494 NodesLeadingTo.insert(N);
495 return true;
496 }
Chris Lattner6831a812006-02-13 09:18:02 +0000497
498 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000499 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000500 return false;
501}
502
Mon P Wang0c397192008-10-30 08:01:45 +0000503/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000504/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000505void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000506 MVT VT = Op.getValueType();
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000507 // If the type legalizer was run then we should never see any illegal result
508 // types here except for target constants (the type legalizer does not touch
Mon P Wang87c8a8f2008-12-18 20:03:17 +0000509 // those) or for build vector used as a mask for a vector shuffle.
510 // FIXME: We can removed the BUILD_VECTOR case when we fix PR2957.
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000511 assert((TypesNeedLegalizing || getTypeAction(VT) == Legal ||
Mon P Wange5ab34e2009-02-04 19:38:14 +0000512 IsLegalizingCallArgs || Op.getOpcode() == ISD::TargetConstant ||
Mon P Wang87c8a8f2008-12-18 20:03:17 +0000513 Op.getOpcode() == ISD::BUILD_VECTOR) &&
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000514 "Illegal type introduced after type legalization?");
Dan Gohman7f321562007-06-25 16:23:39 +0000515 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000516 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000517 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang0c397192008-10-30 08:01:45 +0000518 case Promote:
519 if (!VT.isVector()) {
520 (void)PromoteOp(Op);
521 break;
522 }
523 else {
524 // See if we can widen otherwise use Expand to either scalarize or split
525 MVT WidenVT = TLI.getWidenVectorType(VT);
526 if (WidenVT != MVT::Other) {
527 (void) WidenVectorOp(Op, WidenVT);
528 break;
529 }
530 // else fall thru to expand since we can't widen the vector
531 }
Chris Lattnerc7029802006-03-18 01:44:44 +0000532 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000533 if (!VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000534 // If this is an illegal scalar, expand it into its two component
535 // pieces.
Dan Gohman475871a2008-07-27 21:46:04 +0000536 SDValue X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000537 if (Op.getOpcode() == ISD::TargetConstant)
538 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000539 ExpandOp(Op, X, Y);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000540 } else if (VT.getVectorNumElements() == 1) {
Dan Gohman7f321562007-06-25 16:23:39 +0000541 // If this is an illegal single element vector, convert it to a
542 // scalar operation.
543 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000544 } else {
Mon P Wang0c397192008-10-30 08:01:45 +0000545 // This is an illegal multiple element vector.
Dan Gohman7f321562007-06-25 16:23:39 +0000546 // Split it in half and legalize both parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000547 SDValue X, Y;
Dan Gohman7f321562007-06-25 16:23:39 +0000548 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000549 }
550 break;
551 }
552}
Chris Lattner6831a812006-02-13 09:18:02 +0000553
Evan Cheng9f877882006-12-13 20:57:08 +0000554/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
555/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000556static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000557 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000558 bool Extend = false;
559
560 // If a FP immediate is precise when represented as a float and if the
561 // target can do an extending load from float to double, we put it into
562 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000563 // double. This shrinks FP constants and canonicalizes them for targets where
564 // an FP extending load is the same cost as a normal load (such as on the x87
565 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000566 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000567 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000568 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000569 if (VT!=MVT::f64 && VT!=MVT::f32)
570 assert(0 && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000571 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000572 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000573 }
574
Duncan Sands83ec4b62008-06-06 12:08:01 +0000575 MVT OrigVT = VT;
576 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000577 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000578 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000579 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
580 // Only do this if the target has a native EXTLOAD instruction from
581 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000582 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000583 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000584 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000585 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
586 VT = SVT;
587 Extend = true;
588 }
Evan Cheng00495212006-12-12 21:32:44 +0000589 }
590
Dan Gohman475871a2008-07-27 21:46:04 +0000591 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +0000592 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000593 if (Extend)
Dale Johannesen39355f92009-02-04 02:34:38 +0000594 return DAG.getExtLoad(ISD::EXTLOAD, CFP->getDebugLoc(),
595 OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000596 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000597 0, VT, false, Alignment);
Evan Chengef120572008-03-04 08:05:30 +0000598 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000599 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000600}
601
Chris Lattner6831a812006-02-13 09:18:02 +0000602
Evan Cheng912095b2007-01-04 21:56:39 +0000603/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
604/// operations.
605static
Dan Gohman475871a2008-07-27 21:46:04 +0000606SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
Dan Gohman0d137d72009-01-15 16:43:02 +0000607 SelectionDAG &DAG,
608 const TargetLowering &TLI) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000609 DebugLoc dl = Node->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000610 MVT VT = Node->getValueType(0);
611 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000612 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
613 "fcopysign expansion only supported for f32 and f64");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000614 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000615
Evan Cheng912095b2007-01-04 21:56:39 +0000616 // First get the sign bit of second operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000617 SDValue Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000618 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
619 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000620 Mask1 = DAG.getNode(ISD::BIT_CONVERT, dl, SrcNVT, Mask1);
621 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, dl, SrcNVT,
622 Node->getOperand(1));
623 SignBit = DAG.getNode(ISD::AND, dl, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000624 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000625 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Evan Cheng912095b2007-01-04 21:56:39 +0000626 if (SizeDiff > 0) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000627 SignBit = DAG.getNode(ISD::SRL, dl, SrcNVT, SignBit,
Evan Cheng912095b2007-01-04 21:56:39 +0000628 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000629 SignBit = DAG.getNode(ISD::TRUNCATE, dl, NVT, SignBit);
Chris Lattnerc563e1d2008-07-10 23:46:13 +0000630 } else if (SizeDiff < 0) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000631 SignBit = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, SignBit);
632 SignBit = DAG.getNode(ISD::SHL, dl, NVT, SignBit,
Chris Lattnerc563e1d2008-07-10 23:46:13 +0000633 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
634 }
Evan Cheng068c5f42007-01-05 21:31:51 +0000635
636 // Clear the sign bit of first operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000637 SDValue Mask2 = (VT == MVT::f64)
Evan Cheng068c5f42007-01-05 21:31:51 +0000638 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
639 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000640 Mask2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask2);
641 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
642 Result = DAG.getNode(ISD::AND, dl, NVT, Result, Mask2);
Evan Cheng068c5f42007-01-05 21:31:51 +0000643
644 // Or the value with the sign bit.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000645 Result = DAG.getNode(ISD::OR, dl, NVT, Result, SignBit);
Evan Cheng912095b2007-01-04 21:56:39 +0000646 return Result;
647}
648
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000649/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
650static
Dan Gohman475871a2008-07-27 21:46:04 +0000651SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000652 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000653 SDValue Chain = ST->getChain();
654 SDValue Ptr = ST->getBasePtr();
655 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000656 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000657 int Alignment = ST->getAlignment();
658 int SVOffset = ST->getSrcValueOffset();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000659 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000660 if (ST->getMemoryVT().isFloatingPoint() ||
661 ST->getMemoryVT().isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000662 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
663 if (TLI.isTypeLegal(intVT)) {
664 // Expand to a bitconvert of the value to the integer type of the
665 // same size, then a (misaligned) int store.
666 // FIXME: Does not handle truncating floating point stores!
Dale Johannesenbb5da912009-02-02 20:41:04 +0000667 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, intVT, Val);
668 return DAG.getStore(Chain, dl, Result, Ptr, ST->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000669 SVOffset, ST->isVolatile(), Alignment);
670 } else {
671 // Do a (aligned) store to a stack slot, then copy from the stack slot
672 // to the final destination using (unaligned) integer loads and stores.
673 MVT StoredVT = ST->getMemoryVT();
674 MVT RegVT =
675 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
676 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
677 unsigned RegBytes = RegVT.getSizeInBits() / 8;
678 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000679
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000680 // Make sure the stack slot is also aligned for the register type.
681 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
682
683 // Perform the original store, only redirected to the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000684 SDValue Store = DAG.getTruncStore(Chain, dl,
685 Val, StackPtr, NULL, 0,StoredVT);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000686 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
687 SmallVector<SDValue, 8> Stores;
688 unsigned Offset = 0;
689
690 // Do all but one copies using the full register width.
691 for (unsigned i = 1; i < NumRegs; i++) {
692 // Load one integer register's worth from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000693 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, NULL, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000694 // Store it to the final location. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000695 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000696 ST->getSrcValue(), SVOffset + Offset,
697 ST->isVolatile(),
698 MinAlign(ST->getAlignment(), Offset)));
699 // Increment the pointers.
700 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000701 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000702 Increment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000703 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000704 }
705
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000706 // The last store may be partial. Do a truncating store. On big-endian
707 // machines this requires an extending load from the stack slot to ensure
708 // that the bits are in the right place.
709 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000710
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000711 // Load from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000712 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000713 NULL, 0, MemVT);
714
Dale Johannesenbb5da912009-02-02 20:41:04 +0000715 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000716 ST->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000717 MemVT, ST->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000718 MinAlign(ST->getAlignment(), Offset)));
719 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000720 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000721 Stores.size());
722 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000723 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000724 assert(ST->getMemoryVT().isInteger() &&
725 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000726 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000727 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000728 MVT NewStoredVT =
729 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
730 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000731 int IncrementSize = NumBits / 8;
732
733 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000734 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
735 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000736 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000737
738 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000739 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000740 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000741 ST->getSrcValue(), SVOffset, NewStoredVT,
742 ST->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000743 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000744 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000745 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000746 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000747 ST->getSrcValue(), SVOffset + IncrementSize,
748 NewStoredVT, ST->isVolatile(), Alignment);
749
Dale Johannesenbb5da912009-02-02 20:41:04 +0000750 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000751}
752
753/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
754static
Dan Gohman475871a2008-07-27 21:46:04 +0000755SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000756 const TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000757 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000758 SDValue Chain = LD->getChain();
759 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000760 MVT VT = LD->getValueType(0);
761 MVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000762 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000763 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000764 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
765 if (TLI.isTypeLegal(intVT)) {
766 // Expand to a (misaligned) integer load of the same size,
767 // then bitconvert to floating point or vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000768 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000769 SVOffset, LD->isVolatile(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000770 LD->getAlignment());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000771 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000772 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000773 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000774
Duncan Sands05e11fa2008-12-12 21:47:02 +0000775 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000776 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000777 } else {
778 // Copy the value to a (aligned) stack slot using (unaligned) integer
779 // loads and stores, then do a (aligned) load from the stack slot.
780 MVT RegVT = TLI.getRegisterType(intVT);
781 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
782 unsigned RegBytes = RegVT.getSizeInBits() / 8;
783 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
784
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000785 // Make sure the stack slot is also aligned for the register type.
786 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
787
Duncan Sands05e11fa2008-12-12 21:47:02 +0000788 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
789 SmallVector<SDValue, 8> Stores;
790 SDValue StackPtr = StackBase;
791 unsigned Offset = 0;
792
793 // Do all but one copies using the full register width.
794 for (unsigned i = 1; i < NumRegs; i++) {
795 // Load one integer register's worth from the original location.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000796 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000797 SVOffset + Offset, LD->isVolatile(),
798 MinAlign(LD->getAlignment(), Offset));
799 // Follow the load with a store to the stack slot. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000800 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000801 NULL, 0));
802 // Increment the pointers.
803 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000804 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
805 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000806 Increment);
807 }
808
809 // The last copy may be partial. Do an extending load.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000810 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000811 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000812 LD->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000813 MemVT, LD->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000814 MinAlign(LD->getAlignment(), Offset));
815 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000816 // On big-endian machines this requires a truncating store to ensure
817 // that the bits end up in the right place.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000818 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000819 NULL, 0, MemVT));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000820
821 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000822 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000823 Stores.size());
824
825 // Finally, perform the original load only redirected to the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000826 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000827 NULL, 0, LoadedVT);
828
829 // Callers expect a MERGE_VALUES node.
830 SDValue Ops[] = { Load, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000831 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000832 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000833 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000834 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000835 "Unaligned load of unsupported type.");
836
Dale Johannesen8155d642008-02-27 22:36:00 +0000837 // Compute the new VT that is half the size of the old one. This is an
838 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000839 unsigned NumBits = LoadedVT.getSizeInBits();
840 MVT NewLoadedVT;
841 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000842 NumBits >>= 1;
843
844 unsigned Alignment = LD->getAlignment();
845 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000846 ISD::LoadExtType HiExtType = LD->getExtensionType();
847
848 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
849 if (HiExtType == ISD::NON_EXTLOAD)
850 HiExtType = ISD::ZEXTLOAD;
851
852 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000853 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000854 if (TLI.isLittleEndian()) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000855 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000856 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000857 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000858 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000859 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000860 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000861 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000862 } else {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000863 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
864 SVOffset, NewLoadedVT,LD->isVolatile(), Alignment);
865 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000866 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000867 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000868 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000869 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000870 }
871
872 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000873 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000874 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
875 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000876
Dale Johannesenbb5da912009-02-02 20:41:04 +0000877 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000878 Hi.getValue(1));
879
Dan Gohman475871a2008-07-27 21:46:04 +0000880 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000881 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000882}
Evan Cheng912095b2007-01-04 21:56:39 +0000883
Dan Gohman82669522007-10-11 23:57:53 +0000884/// UnrollVectorOp - We know that the given vector has a legal type, however
885/// the operation it performs is not legal and is an operation that we have
886/// no way of lowering. "Unroll" the vector, splitting out the scalars and
887/// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000888SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000889 MVT VT = Op.getValueType();
Dan Gohman82669522007-10-11 23:57:53 +0000890 assert(isTypeLegal(VT) &&
891 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000892 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman82669522007-10-11 23:57:53 +0000893 "Can't unroll a vector with multiple results!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000894 unsigned NE = VT.getVectorNumElements();
895 MVT EltVT = VT.getVectorElementType();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000896 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman82669522007-10-11 23:57:53 +0000897
Dan Gohman475871a2008-07-27 21:46:04 +0000898 SmallVector<SDValue, 8> Scalars;
899 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman82669522007-10-11 23:57:53 +0000900 for (unsigned i = 0; i != NE; ++i) {
901 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +0000902 SDValue Operand = Op.getOperand(j);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000903 MVT OperandVT = Operand.getValueType();
904 if (OperandVT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +0000905 // A vector operand; extract a single element.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000906 MVT OperandEltVT = OperandVT.getVectorElementType();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000907 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dan Gohman82669522007-10-11 23:57:53 +0000908 OperandEltVT,
909 Operand,
910 DAG.getConstant(i, MVT::i32));
911 } else {
912 // A scalar operand; just use it as is.
913 Operands[j] = Operand;
914 }
915 }
Mon P Wange9f10152008-12-09 05:46:39 +0000916
917 switch (Op.getOpcode()) {
918 default:
Dale Johannesenbb5da912009-02-02 20:41:04 +0000919 Scalars.push_back(DAG.getNode(Op.getOpcode(), dl, EltVT,
Mon P Wange9f10152008-12-09 05:46:39 +0000920 &Operands[0], Operands.size()));
921 break;
922 case ISD::SHL:
923 case ISD::SRA:
924 case ISD::SRL:
Duncan Sands92abc622009-01-31 15:50:11 +0000925 case ISD::ROTL:
926 case ISD::ROTR:
Dale Johannesenbb5da912009-02-02 20:41:04 +0000927 Scalars.push_back(DAG.getNode(Op.getOpcode(), dl, EltVT, Operands[0],
Duncan Sands92abc622009-01-31 15:50:11 +0000928 DAG.getShiftAmountOperand(Operands[1])));
Mon P Wange9f10152008-12-09 05:46:39 +0000929 break;
930 }
Dan Gohman82669522007-10-11 23:57:53 +0000931 }
932
Dale Johannesenbb5da912009-02-02 20:41:04 +0000933 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Scalars[0], Scalars.size());
Dan Gohman82669522007-10-11 23:57:53 +0000934}
935
Duncan Sands007f9842008-01-10 10:28:30 +0000936/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000937static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000938 RTLIB::Libcall Call_F32,
939 RTLIB::Libcall Call_F64,
940 RTLIB::Libcall Call_F80,
941 RTLIB::Libcall Call_PPCF128) {
942 return
943 VT == MVT::f32 ? Call_F32 :
944 VT == MVT::f64 ? Call_F64 :
945 VT == MVT::f80 ? Call_F80 :
946 VT == MVT::ppcf128 ? Call_PPCF128 :
947 RTLIB::UNKNOWN_LIBCALL;
948}
949
Nate Begeman68679912008-04-25 18:07:40 +0000950/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
951/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
952/// is necessary to spill the vector being inserted into to memory, perform
953/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000954SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000955PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
956 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000957 SDValue Tmp1 = Vec;
958 SDValue Tmp2 = Val;
959 SDValue Tmp3 = Idx;
Nate Begeman68679912008-04-25 18:07:40 +0000960
961 // If the target doesn't support this, we have to spill the input vector
962 // to a temporary stack slot, update the element, then reload it. This is
963 // badness. We could also load the value into a vector register (either
964 // with a "move to register" or "extload into register" instruction, then
965 // permute it into place, if the idx is a constant and if the idx is
966 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000967 MVT VT = Tmp1.getValueType();
968 MVT EltVT = VT.getVectorElementType();
969 MVT IdxVT = Tmp3.getValueType();
970 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000971 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000972
Gabor Greifba36cb52008-08-28 21:40:38 +0000973 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000974
975 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000976 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000977 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000978
979 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000980 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000981 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000982 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000983 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000984 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
985 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000986 // Store the scalar value.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000987 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000988 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000989 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000990 return DAG.getLoad(VT, dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +0000991 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000992}
993
Mon P Wange9f10152008-12-09 05:46:39 +0000994
Dan Gohmana3466152007-07-13 20:14:11 +0000995/// LegalizeOp - We know that the specified value has a legal type, and
996/// that its operands are legal. Now ensure that the operation itself
997/// is legal, recursively ensuring that the operands' operations remain
998/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000999SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +00001000 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
1001 return Op;
1002
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001003 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +00001004 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001005 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001006 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +00001007
Chris Lattner3e928bb2005-01-07 07:47:09 +00001008 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +00001009 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +00001010 if (Node->getNumValues() > 1) {
1011 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +00001012 if (getTypeAction(Node->getValueType(i)) != Legal) {
1013 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001014 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +00001015 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +00001016 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +00001017 }
1018 }
1019
Chris Lattner45982da2005-05-12 16:53:42 +00001020 // Note that LegalizeOp may be reentered even from single-use nodes, which
1021 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00001022 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001023 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001024
Dan Gohman475871a2008-07-27 21:46:04 +00001025 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
1026 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +00001027 bool isCustom = false;
1028
Chris Lattner3e928bb2005-01-07 07:47:09 +00001029 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +00001030 case ISD::FrameIndex:
1031 case ISD::EntryToken:
1032 case ISD::Register:
1033 case ISD::BasicBlock:
1034 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +00001035 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +00001036 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +00001037 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +00001038 case ISD::TargetConstantPool:
1039 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001040 case ISD::TargetGlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001041 case ISD::TargetExternalSymbol:
Chris Lattner948c1b12006-01-28 08:31:04 +00001042 case ISD::VALUETYPE:
1043 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +00001044 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +00001045 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +00001046 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +00001047 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00001048 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +00001049 "This must be legal!");
1050 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001051 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001052 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1053 // If this is a target node, legalize it by legalizing the operands then
1054 // passing it through.
Dan Gohman475871a2008-07-27 21:46:04 +00001055 SmallVector<SDValue, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001056 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001057 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001058
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001059 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001060
1061 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1062 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001063 return Result.getValue(Op.getResNo());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001064 }
1065 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001066#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00001067 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001068#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00001069 assert(0 && "Do not know how to legalize this operator!");
1070 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +00001071 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001072 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001073 case ISD::GlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001074 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +00001075 case ISD::ConstantPool:
1076 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001077 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1078 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +00001079 case TargetLowering::Custom:
1080 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001081 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner948c1b12006-01-28 08:31:04 +00001082 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001083 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001084 break;
1085 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001086 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +00001087 case ISD::FRAMEADDR:
1088 case ISD::RETURNADDR:
1089 // The only option for these nodes is to custom lower them. If the target
1090 // does not custom lower them, then return zero.
1091 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001092 if (Tmp1.getNode())
Nate Begemanbcc5f362007-01-29 22:58:52 +00001093 Result = Tmp1;
1094 else
1095 Result = DAG.getConstant(0, TLI.getPointerTy());
1096 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001097 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001098 MVT VT = Node->getValueType(0);
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001099 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1100 default: assert(0 && "This action is not supported yet!");
1101 case TargetLowering::Custom:
1102 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001103 if (Result.getNode()) break;
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001104 // Fall Thru
1105 case TargetLowering::Legal:
1106 Result = DAG.getConstant(0, VT);
1107 break;
1108 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001109 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001110 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001111 case ISD::EXCEPTIONADDR: {
1112 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001113 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001114 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1115 default: assert(0 && "This action is not supported yet!");
1116 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +00001117 unsigned Reg = TLI.getExceptionAddressRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +00001118 Result = DAG.getCopyFromReg(Tmp1, dl, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001119 }
1120 break;
1121 case TargetLowering::Custom:
1122 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001123 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001124 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001125 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001126 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Dale Johannesenca57b842009-02-02 23:46:53 +00001127 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001128 break;
1129 }
1130 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001131 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001132 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001133
Gabor Greifba36cb52008-08-28 21:40:38 +00001134 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001135 "Cannot return more than two values!");
1136
1137 // Since we produced two values, make sure to remember that we
1138 // legalized both of them.
1139 Tmp1 = LegalizeOp(Result);
1140 Tmp2 = LegalizeOp(Result.getValue(1));
1141 AddLegalizedOperand(Op.getValue(0), Tmp1);
1142 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001143 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +00001144 case ISD::EHSELECTION: {
1145 Tmp1 = LegalizeOp(Node->getOperand(0));
1146 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001147 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +00001148 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1149 default: assert(0 && "This action is not supported yet!");
1150 case TargetLowering::Expand: {
1151 unsigned Reg = TLI.getExceptionSelectorRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +00001152 Result = DAG.getCopyFromReg(Tmp2, dl, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +00001153 }
1154 break;
1155 case TargetLowering::Custom:
1156 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001157 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +00001158 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001159 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001160 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Dale Johannesenca57b842009-02-02 23:46:53 +00001161 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey8782d482007-02-28 20:43:58 +00001162 break;
1163 }
1164 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001165 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001166 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001167
Gabor Greifba36cb52008-08-28 21:40:38 +00001168 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001169 "Cannot return more than two values!");
1170
1171 // Since we produced two values, make sure to remember that we
1172 // legalized both of them.
1173 Tmp1 = LegalizeOp(Result);
1174 Tmp2 = LegalizeOp(Result.getValue(1));
1175 AddLegalizedOperand(Op.getValue(0), Tmp1);
1176 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001177 return Op.getResNo() ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001178 case ISD::EH_RETURN: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001179 MVT VT = Node->getValueType(0);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001180 // The only "good" option for this node is to custom lower it.
1181 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1182 default: assert(0 && "This action is not supported at all!");
1183 case TargetLowering::Custom:
1184 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001185 if (Result.getNode()) break;
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001186 // Fall Thru
1187 case TargetLowering::Legal:
1188 // Target does not know, how to lower this, lower to noop
1189 Result = LegalizeOp(Node->getOperand(0));
1190 break;
1191 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001192 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001193 break;
Chris Lattner08951a32005-09-02 01:15:01 +00001194 case ISD::AssertSext:
1195 case ISD::AssertZext:
1196 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001197 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +00001198 break;
Chris Lattner308575b2005-11-20 22:56:56 +00001199 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001200 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif99a6cb92008-08-26 22:36:50 +00001201 Result = Node->getOperand(Op.getResNo());
Chris Lattner456a93a2006-01-28 07:39:30 +00001202 break;
Chris Lattner69a52152005-01-14 22:38:01 +00001203 case ISD::CopyFromReg:
1204 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001205 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001206 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001207 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +00001208 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001209 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001210 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001211 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001212 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1213 } else {
1214 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1215 }
Chris Lattner7310fb12005-12-18 15:27:43 +00001216 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1217 }
Chris Lattner13c184d2005-01-28 06:27:38 +00001218 // Since CopyFromReg produces two values, make sure to remember that we
1219 // legalized both of them.
1220 AddLegalizedOperand(Op.getValue(0), Result);
1221 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001222 return Result.getValue(Op.getResNo());
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001223 case ISD::UNDEF: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001224 MVT VT = Op.getValueType();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001225 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +00001226 default: assert(0 && "This action is not supported yet!");
1227 case TargetLowering::Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001228 if (VT.isInteger())
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001229 Result = DAG.getConstant(0, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001230 else if (VT.isFloatingPoint())
1231 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf41db212007-09-26 17:26:49 +00001232 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001233 else
1234 assert(0 && "Unknown value type!");
1235 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001236 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001237 break;
1238 }
1239 break;
1240 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001241
Chris Lattner48b61a72006-03-28 00:40:33 +00001242 case ISD::INTRINSIC_W_CHAIN:
1243 case ISD::INTRINSIC_WO_CHAIN:
1244 case ISD::INTRINSIC_VOID: {
Dan Gohman475871a2008-07-27 21:46:04 +00001245 SmallVector<SDValue, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001246 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1247 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001248 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001249
1250 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001251 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001252 TargetLowering::Custom) {
1253 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001254 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001255 }
1256
Gabor Greifba36cb52008-08-28 21:40:38 +00001257 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001258
1259 // Must have return value and chain result.
Gabor Greifba36cb52008-08-28 21:40:38 +00001260 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattner13fc2f12006-03-27 20:28:29 +00001261 "Cannot return more than two values!");
1262
1263 // Since loads produce two values, make sure to remember that we
1264 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001265 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1266 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001267 return Result.getValue(Op.getResNo());
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001268 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001269
Dan Gohman7f460202008-06-30 20:59:49 +00001270 case ISD::DBG_STOPPOINT:
1271 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +00001272 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1273
Dan Gohman7f460202008-06-30 20:59:49 +00001274 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +00001275 case TargetLowering::Promote:
1276 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001277 case TargetLowering::Expand: {
Devang Patel83489bb2009-01-13 00:35:13 +00001278 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf560ffa2009-01-28 17:46:25 +00001279 bool useDEBUG_LOC = TLI.isOperationLegalOrCustom(ISD::DEBUG_LOC,
1280 MVT::Other);
1281 bool useLABEL = TLI.isOperationLegalOrCustom(ISD::DBG_LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001282
Dan Gohman7f460202008-06-30 20:59:49 +00001283 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Devang Patel83489bb2009-01-13 00:35:13 +00001284 GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
1285 if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
1286 DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
1287 unsigned SrcFile = DW->RecordSource(CU.getDirectory(),
1288 CU.getFilename());
1289
Dan Gohman7f460202008-06-30 20:59:49 +00001290 unsigned Line = DSP->getLine();
1291 unsigned Col = DSP->getColumn();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001292
Dale Johannesenca57b842009-02-02 23:46:53 +00001293 // A bit self-referential to have DebugLoc on Debug_Loc nodes, but
1294 // it won't hurt anything.
Jim Laskeyabf6d172006-01-05 01:25:28 +00001295 if (useDEBUG_LOC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001296 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Cheng71e86852008-07-08 20:06:39 +00001297 DAG.getConstant(Col, MVT::i32),
1298 DAG.getConstant(SrcFile, MVT::i32) };
Dale Johannesenca57b842009-02-02 23:46:53 +00001299 Result = DAG.getNode(ISD::DEBUG_LOC, dl, MVT::Other, Ops, 4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001300 } else {
Devang Patel83489bb2009-01-13 00:35:13 +00001301 unsigned ID = DW->RecordSourceLine(Line, Col, SrcFile);
Dale Johannesenca57b842009-02-02 23:46:53 +00001302 Result = DAG.getLabel(ISD::DBG_LABEL, dl, Tmp1, ID);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001303 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001304 } else {
1305 Result = Tmp1; // chain
1306 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001307 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001308 }
Evan Cheng71e86852008-07-08 20:06:39 +00001309 case TargetLowering::Legal: {
1310 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1311 if (Action == Legal && Tmp1 == Node->getOperand(0))
1312 break;
1313
Dan Gohman475871a2008-07-27 21:46:04 +00001314 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001315 Ops.push_back(Tmp1);
1316 if (Action == Legal) {
1317 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1318 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1319 } else {
1320 // Otherwise promote them.
1321 Ops.push_back(PromoteOp(Node->getOperand(1)));
1322 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner36ce6912005-11-29 06:21:05 +00001323 }
Evan Cheng71e86852008-07-08 20:06:39 +00001324 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1325 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1326 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001327 break;
1328 }
Evan Cheng71e86852008-07-08 20:06:39 +00001329 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001330 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001331
1332 case ISD::DECLARE:
1333 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1334 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1335 default: assert(0 && "This action is not supported yet!");
1336 case TargetLowering::Legal:
1337 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1338 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1339 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1340 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1341 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001342 case TargetLowering::Expand:
1343 Result = LegalizeOp(Node->getOperand(0));
1344 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001345 }
1346 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001347
1348 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001349 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001350 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001351 default: assert(0 && "This action is not supported yet!");
Evan Cheng71e86852008-07-08 20:06:39 +00001352 case TargetLowering::Legal: {
1353 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001354 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Cheng71e86852008-07-08 20:06:39 +00001355 if (Action == Legal && Tmp1 == Node->getOperand(0))
1356 break;
1357 if (Action == Legal) {
1358 Tmp2 = Node->getOperand(1);
1359 Tmp3 = Node->getOperand(2);
1360 Tmp4 = Node->getOperand(3);
1361 } else {
1362 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1363 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1364 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1365 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001366 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001367 break;
1368 }
Evan Cheng71e86852008-07-08 20:06:39 +00001369 }
Jim Laskeyabf6d172006-01-05 01:25:28 +00001370 break;
1371
Dan Gohman44066042008-07-01 00:05:16 +00001372 case ISD::DBG_LABEL:
1373 case ISD::EH_LABEL:
1374 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1375 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001376 default: assert(0 && "This action is not supported yet!");
1377 case TargetLowering::Legal:
1378 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohman44066042008-07-01 00:05:16 +00001379 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001380 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001381 case TargetLowering::Expand:
1382 Result = LegalizeOp(Node->getOperand(0));
1383 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001384 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001385 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001386
Evan Cheng27b7db52008-03-08 00:58:38 +00001387 case ISD::PREFETCH:
1388 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1389 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1390 default: assert(0 && "This action is not supported yet!");
1391 case TargetLowering::Legal:
1392 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1393 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1394 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1395 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1396 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1397 break;
1398 case TargetLowering::Expand:
1399 // It's a noop.
1400 Result = LegalizeOp(Node->getOperand(0));
1401 break;
1402 }
1403 break;
1404
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001405 case ISD::MEMBARRIER: {
1406 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001407 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1408 default: assert(0 && "This action is not supported yet!");
1409 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001410 SDValue Ops[6];
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001411 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001412 for (int x = 1; x < 6; ++x) {
1413 Ops[x] = Node->getOperand(x);
1414 if (!isTypeLegal(Ops[x].getValueType()))
1415 Ops[x] = PromoteOp(Ops[x]);
1416 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001417 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1418 break;
1419 }
1420 case TargetLowering::Expand:
1421 //There is no libgcc call for this op
1422 Result = Node->getOperand(0); // Noop
1423 break;
1424 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001425 break;
1426 }
1427
Dan Gohman0b1d4a72008-12-23 21:37:04 +00001428 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang63307c32008-05-05 19:05:59 +00001429 unsigned int num_operands = 4;
1430 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001431 SDValue Ops[4];
Mon P Wang63307c32008-05-05 19:05:59 +00001432 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001433 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang63307c32008-05-05 19:05:59 +00001434 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1435
1436 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1437 default: assert(0 && "This action is not supported yet!");
1438 case TargetLowering::Custom:
1439 Result = TLI.LowerOperation(Result, DAG);
1440 break;
1441 case TargetLowering::Legal:
1442 break;
1443 }
Dan Gohman475871a2008-07-27 21:46:04 +00001444 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1445 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001446 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001447 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00001448 case ISD::ATOMIC_LOAD_ADD:
1449 case ISD::ATOMIC_LOAD_SUB:
1450 case ISD::ATOMIC_LOAD_AND:
1451 case ISD::ATOMIC_LOAD_OR:
1452 case ISD::ATOMIC_LOAD_XOR:
1453 case ISD::ATOMIC_LOAD_NAND:
1454 case ISD::ATOMIC_LOAD_MIN:
1455 case ISD::ATOMIC_LOAD_MAX:
1456 case ISD::ATOMIC_LOAD_UMIN:
1457 case ISD::ATOMIC_LOAD_UMAX:
1458 case ISD::ATOMIC_SWAP: {
Mon P Wang63307c32008-05-05 19:05:59 +00001459 unsigned int num_operands = 3;
1460 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001461 SDValue Ops[3];
Mon P Wang63307c32008-05-05 19:05:59 +00001462 for (unsigned int x = 0; x < num_operands; ++x)
1463 Ops[x] = LegalizeOp(Node->getOperand(x));
1464 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands126d9072008-07-04 11:47:58 +00001465
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001466 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001467 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001468 case TargetLowering::Custom:
1469 Result = TLI.LowerOperation(Result, DAG);
1470 break;
1471 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001472 break;
1473 }
Dan Gohman475871a2008-07-27 21:46:04 +00001474 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1475 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001476 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001477 }
Scott Michelc1513d22007-08-08 23:23:31 +00001478 case ISD::Constant: {
1479 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1480 unsigned opAction =
1481 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1482
Chris Lattner3e928bb2005-01-07 07:47:09 +00001483 // We know we don't need to expand constants here, constants only have one
1484 // value and we check that it is fine above.
1485
Scott Michelc1513d22007-08-08 23:23:31 +00001486 if (opAction == TargetLowering::Custom) {
1487 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001488 if (Tmp1.getNode())
Scott Michelc1513d22007-08-08 23:23:31 +00001489 Result = Tmp1;
1490 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001491 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001492 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001493 case ISD::ConstantFP: {
1494 // Spill FP immediates to the constant pool if the target cannot directly
1495 // codegen them. Targets often have some immediate values that can be
1496 // efficiently generated into an FP register without a load. We explicitly
1497 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001498 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1499
Chris Lattner3181a772006-01-29 06:26:56 +00001500 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1501 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001502 case TargetLowering::Legal:
1503 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001504 case TargetLowering::Custom:
1505 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001506 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001507 Result = Tmp3;
1508 break;
1509 }
1510 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001511 case TargetLowering::Expand: {
1512 // Check to see if this FP immediate is already legal.
1513 bool isLegal = false;
1514 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1515 E = TLI.legal_fpimm_end(); I != E; ++I) {
1516 if (CFP->isExactlyValue(*I)) {
1517 isLegal = true;
1518 break;
1519 }
1520 }
1521 // If this is a legal constant, turn it into a TargetConstantFP node.
1522 if (isLegal)
1523 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001524 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001525 }
Nate Begemane1795842008-02-14 08:57:00 +00001526 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001527 break;
1528 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001529 case ISD::TokenFactor:
1530 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001531 Tmp1 = LegalizeOp(Node->getOperand(0));
1532 Tmp2 = LegalizeOp(Node->getOperand(1));
1533 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1534 } else if (Node->getNumOperands() == 3) {
1535 Tmp1 = LegalizeOp(Node->getOperand(0));
1536 Tmp2 = LegalizeOp(Node->getOperand(1));
1537 Tmp3 = LegalizeOp(Node->getOperand(2));
1538 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001539 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001540 SmallVector<SDValue, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001541 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001542 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1543 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001544 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001545 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001546 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001547
1548 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001549 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001550 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001551 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001552 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001553 // A call within a calling sequence must be legalized to something
1554 // other than the normal CALLSEQ_END. Violating this gets Legalize
1555 // into an infinite loop.
1556 assert ((!IsLegalizingCall ||
1557 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001558 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001559 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001560
1561 // The number of incoming and outgoing values should match; unless the final
1562 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001563 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1564 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1565 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001566 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001567 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001568
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001569 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001570 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001571 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1572 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001573 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001574 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001575 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001576 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001577 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001578 }
1579 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001580 case ISD::EXTRACT_SUBREG: {
1581 Tmp1 = LegalizeOp(Node->getOperand(0));
1582 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1583 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001584 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001585 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1586 }
1587 break;
1588 case ISD::INSERT_SUBREG: {
1589 Tmp1 = LegalizeOp(Node->getOperand(0));
1590 Tmp2 = LegalizeOp(Node->getOperand(1));
1591 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1592 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001593 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001594 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1595 }
1596 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001597 case ISD::BUILD_VECTOR:
1598 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001599 default: assert(0 && "This action is not supported yet!");
1600 case TargetLowering::Custom:
1601 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001602 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001603 Result = Tmp3;
1604 break;
1605 }
1606 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001607 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001608 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001609 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001610 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001611 break;
1612 case ISD::INSERT_VECTOR_ELT:
1613 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001614 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001615
1616 // The type of the value to insert may not be legal, even though the vector
1617 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1618 // here.
1619 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1620 default: assert(0 && "Cannot expand insert element operand");
1621 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1622 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang0c397192008-10-30 08:01:45 +00001623 case Expand:
1624 // FIXME: An alternative would be to check to see if the target is not
1625 // going to custom lower this operation, we could bitcast to half elt
1626 // width and perform two inserts at that width, if that is legal.
1627 Tmp2 = Node->getOperand(1);
1628 break;
Nate Begeman0325d902008-02-13 06:43:04 +00001629 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001630 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1631
1632 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1633 Node->getValueType(0))) {
1634 default: assert(0 && "This action is not supported yet!");
1635 case TargetLowering::Legal:
1636 break;
1637 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001638 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001639 if (Tmp4.getNode()) {
Nate Begeman2281a992008-01-05 20:47:37 +00001640 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001641 break;
1642 }
1643 // FALLTHROUGH
Mon P Wang0c397192008-10-30 08:01:45 +00001644 case TargetLowering::Promote:
1645 // Fall thru for vector case
Chris Lattner2332b9f2006-03-19 01:17:20 +00001646 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001647 // If the insert index is a constant, codegen this as a scalar_to_vector,
1648 // then a shuffle that inserts it into the right position in the vector.
1649 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001650 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1651 // match the element type of the vector being created.
1652 if (Tmp2.getValueType() ==
Duncan Sands83ec4b62008-06-06 12:08:01 +00001653 Op.getValueType().getVectorElementType()) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001654 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Nate Begeman0325d902008-02-13 06:43:04 +00001655 Tmp1.getValueType(), Tmp2);
1656
Duncan Sands83ec4b62008-06-06 12:08:01 +00001657 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1658 MVT ShufMaskVT =
1659 MVT::getIntVectorWithNumElements(NumElts);
1660 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman0325d902008-02-13 06:43:04 +00001661
1662 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1663 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1664 // elt 0 of the RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00001665 SmallVector<SDValue, 8> ShufOps;
Nate Begeman0325d902008-02-13 06:43:04 +00001666 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001667 if (i != InsertPos->getZExtValue())
Nate Begeman0325d902008-02-13 06:43:04 +00001668 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1669 else
1670 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1671 }
Dale Johannesenca57b842009-02-02 23:46:53 +00001672 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, ShufMaskVT,
Nate Begeman0325d902008-02-13 06:43:04 +00001673 &ShufOps[0], ShufOps.size());
1674
Dale Johannesenca57b842009-02-02 23:46:53 +00001675 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, Tmp1.getValueType(),
Nate Begeman0325d902008-02-13 06:43:04 +00001676 Tmp1, ScVec, ShufMask);
1677 Result = LegalizeOp(Result);
1678 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001679 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001680 }
Dale Johannesenbb5da912009-02-02 20:41:04 +00001681 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3, dl);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001682 break;
1683 }
1684 }
1685 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001686 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001687 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1688 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1689 break;
1690 }
1691
Chris Lattnerce872152006-03-19 06:31:19 +00001692 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1693 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1694 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1695 Node->getValueType(0))) {
1696 default: assert(0 && "This action is not supported yet!");
1697 case TargetLowering::Legal:
1698 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001699 case TargetLowering::Custom:
1700 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001701 if (Tmp3.getNode()) {
Chris Lattner4d3abee2006-03-19 06:47:21 +00001702 Result = Tmp3;
1703 break;
1704 }
1705 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001706 case TargetLowering::Expand:
1707 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001708 break;
1709 }
Chris Lattnerce872152006-03-19 06:31:19 +00001710 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001711 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001712 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1713 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1714 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1715
1716 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001717 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1718 default: assert(0 && "Unknown operation action!");
1719 case TargetLowering::Legal:
1720 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1721 "vector shuffle should not be created if not legal!");
1722 break;
1723 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001724 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001725 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001726 Result = Tmp3;
1727 break;
1728 }
1729 // FALLTHROUGH
1730 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001731 MVT VT = Node->getValueType(0);
1732 MVT EltVT = VT.getVectorElementType();
1733 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001734 SDValue Mask = Node->getOperand(2);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001735 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001736 SmallVector<SDValue,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001737 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001738 SDValue Arg = Mask.getOperand(i);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001739 if (Arg.getOpcode() == ISD::UNDEF) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001740 Ops.push_back(DAG.getNode(ISD::UNDEF, dl, EltVT));
Evan Cheng18dd6d02006-04-05 06:07:11 +00001741 } else {
1742 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001743 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng18dd6d02006-04-05 06:07:11 +00001744 if (Idx < NumElems)
Dale Johannesenca57b842009-02-02 23:46:53 +00001745 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp1,
Evan Cheng18dd6d02006-04-05 06:07:11 +00001746 DAG.getConstant(Idx, PtrVT)));
1747 else
Dale Johannesenca57b842009-02-02 23:46:53 +00001748 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp2,
Evan Cheng18dd6d02006-04-05 06:07:11 +00001749 DAG.getConstant(Idx - NumElems, PtrVT)));
1750 }
1751 }
Dale Johannesenca57b842009-02-02 23:46:53 +00001752 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001753 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001754 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001755 case TargetLowering::Promote: {
1756 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001757 MVT OVT = Node->getValueType(0);
1758 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001759
1760 // Cast the two input vectors.
Dale Johannesenca57b842009-02-02 23:46:53 +00001761 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
1762 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
Chris Lattner4352cc92006-04-04 17:23:26 +00001763
1764 // Convert the shuffle mask to the right # elements.
Dan Gohman475871a2008-07-27 21:46:04 +00001765 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001766 assert(Tmp3.getNode() && "Shuffle not legal?");
Dale Johannesenca57b842009-02-02 23:46:53 +00001767 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, NVT, Tmp1, Tmp2, Tmp3);
1768 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Chris Lattner4352cc92006-04-04 17:23:26 +00001769 break;
1770 }
Chris Lattner87100e02006-03-20 01:52:29 +00001771 }
1772 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001773
1774 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001775 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001776 Tmp2 = LegalizeOp(Node->getOperand(1));
1777 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001778 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001779 break;
1780
Dan Gohman7f321562007-06-25 16:23:39 +00001781 case ISD::EXTRACT_SUBVECTOR:
1782 Tmp1 = Node->getOperand(0);
1783 Tmp2 = LegalizeOp(Node->getOperand(1));
1784 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1785 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001786 break;
1787
Mon P Wang0c397192008-10-30 08:01:45 +00001788 case ISD::CONCAT_VECTORS: {
1789 // Use extract/insert/build vector for now. We might try to be
1790 // more clever later.
1791 MVT PtrVT = TLI.getPointerTy();
1792 SmallVector<SDValue, 8> Ops;
1793 unsigned NumOperands = Node->getNumOperands();
1794 for (unsigned i=0; i < NumOperands; ++i) {
1795 SDValue SubOp = Node->getOperand(i);
1796 MVT VVT = SubOp.getNode()->getValueType(0);
1797 MVT EltVT = VVT.getVectorElementType();
1798 unsigned NumSubElem = VVT.getVectorNumElements();
1799 for (unsigned j=0; j < NumSubElem; ++j) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001800 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
Mon P Wang0c397192008-10-30 08:01:45 +00001801 DAG.getConstant(j, PtrVT)));
1802 }
1803 }
Dale Johannesenca57b842009-02-02 23:46:53 +00001804 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0),
Mon P Wang0c397192008-10-30 08:01:45 +00001805 &Ops[0], Ops.size()));
1806 }
1807
Chris Lattner6831a812006-02-13 09:18:02 +00001808 case ISD::CALLSEQ_START: {
1809 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1810
1811 // Recursively Legalize all of the inputs of the call end that do not lead
1812 // to this call start. This ensures that any libcalls that need be inserted
1813 // are inserted *before* the CALLSEQ_START.
Mon P Wange5ab34e2009-02-04 19:38:14 +00001814 IsLegalizingCallArgs = true;
Chris Lattner00755df2007-02-04 00:27:56 +00001815 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001816 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001817 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001818 NodesLeadingTo);
1819 }
Mon P Wange5ab34e2009-02-04 19:38:14 +00001820 IsLegalizingCallArgs = false;
Chris Lattner6831a812006-02-13 09:18:02 +00001821
1822 // Now that we legalized all of the inputs (which may have inserted
1823 // libcalls) create the new CALLSEQ_START node.
1824 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1825
1826 // Merge in the last call, to ensure that this call start after the last
1827 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001828 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001829 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1830 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001831 Tmp1 = LegalizeOp(Tmp1);
1832 }
Chris Lattner6831a812006-02-13 09:18:02 +00001833
1834 // Do not try to legalize the target-specific arguments (#1+).
1835 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001836 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001837 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001838 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001839 }
1840
1841 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001842 AddLegalizedOperand(Op.getValue(0), Result);
1843 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1844 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1845
Chris Lattner6831a812006-02-13 09:18:02 +00001846 // Now that the callseq_start and all of the non-call nodes above this call
1847 // sequence have been legalized, legalize the call itself. During this
1848 // process, no libcalls can/will be inserted, guaranteeing that no calls
1849 // can overlap.
1850 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001851 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001852 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001853 IsLegalizingCall = true;
1854
1855 // Legalize the call, starting from the CALLSEQ_END.
1856 LegalizeOp(LastCALLSEQ_END);
1857 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1858 return Result;
1859 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001860 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001861 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1862 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001863 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001864 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1865 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001866 assert(I != LegalizedNodes.end() &&
1867 "Legalizing the call start should have legalized this node!");
1868 return I->second;
1869 }
1870
1871 // Otherwise, the call start has been legalized and everything is going
1872 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001873 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001874 // Do not try to legalize the target-specific arguments (#1+), except for
1875 // an optional flag input.
1876 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1877 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001878 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001879 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001880 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001881 }
1882 } else {
1883 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1884 if (Tmp1 != Node->getOperand(0) ||
1885 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001886 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001887 Ops[0] = Tmp1;
1888 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001889 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001890 }
Chris Lattner6a542892006-01-24 05:48:21 +00001891 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001892 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001893 // This finishes up call legalization.
1894 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001895
1896 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001897 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001898 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001899 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001900 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001901 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001902 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001903 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1904 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1905 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001906 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001907
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001908 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001909 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001910 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001911 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001912 case TargetLowering::Expand: {
1913 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1914 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1915 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001916 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001917
1918 // Chain the dynamic stack allocation so that it doesn't modify the stack
1919 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001920 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001921
Dan Gohman475871a2008-07-27 21:46:04 +00001922 SDValue Size = Tmp2.getOperand(1);
Dale Johannesenc460ae92009-02-04 00:13:36 +00001923 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001924 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001925 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001926 unsigned StackAlign =
1927 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1928 if (Align > StackAlign)
Dale Johannesenca57b842009-02-02 23:46:53 +00001929 SP = DAG.getNode(ISD::AND, dl, VT, SP,
Evan Cheng3e20bba2007-08-17 18:02:22 +00001930 DAG.getConstant(-(uint64_t)Align, VT));
Dale Johannesenca57b842009-02-02 23:46:53 +00001931 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenc460ae92009-02-04 00:13:36 +00001932 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001933
Dale Johannesenca57b842009-02-02 23:46:53 +00001934 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
Chris Lattnere563bbc2008-10-11 22:08:30 +00001935 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001936
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001937 Tmp1 = LegalizeOp(Tmp1);
1938 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001939 break;
1940 }
1941 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001942 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001943 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001944 Tmp1 = LegalizeOp(Tmp3);
1945 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001946 }
Chris Lattner903d2782006-01-15 08:54:32 +00001947 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001948 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001949 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001950 }
Chris Lattner903d2782006-01-15 08:54:32 +00001951 // Since this op produce two values, make sure to remember that we
1952 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001953 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1954 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001955 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001956 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001957 case ISD::INLINEASM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001958 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001959 bool Changed = false;
1960 // Legalize all of the operands of the inline asm, in case they are nodes
1961 // that need to be expanded or something. Note we skip the asm string and
1962 // all of the TargetConstant flags.
Dan Gohman475871a2008-07-27 21:46:04 +00001963 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001964 Changed = Op != Ops[0];
1965 Ops[0] = Op;
1966
1967 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1968 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001969 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Chris Lattner25a022c2006-07-11 01:40:09 +00001970 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman475871a2008-07-27 21:46:04 +00001971 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001972 if (Op != Ops[i]) {
1973 Changed = true;
1974 Ops[i] = Op;
1975 }
1976 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001977 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001978
1979 if (HasInFlag) {
1980 Op = LegalizeOp(Ops.back());
1981 Changed |= Op != Ops.back();
1982 Ops.back() = Op;
1983 }
1984
1985 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001986 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001987
1988 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman475871a2008-07-27 21:46:04 +00001989 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1990 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001991 return Result.getValue(Op.getResNo());
Chris Lattner25a022c2006-07-11 01:40:09 +00001992 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001993 case ISD::BR:
1994 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001995 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001996 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001997 Tmp1 = LegalizeOp(Tmp1);
1998 LastCALLSEQ_END = DAG.getEntryNode();
1999
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002000 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00002001 break;
Nate Begeman37efe672006-04-22 18:53:45 +00002002 case ISD::BRIND:
2003 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2004 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00002005 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Nate Begeman37efe672006-04-22 18:53:45 +00002006 Tmp1 = LegalizeOp(Tmp1);
2007 LastCALLSEQ_END = DAG.getEntryNode();
2008
2009 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2010 default: assert(0 && "Indirect target must be legal type (pointer)!");
2011 case Legal:
2012 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2013 break;
2014 }
2015 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2016 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00002017 case ISD::BR_JT:
2018 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2019 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00002020 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002021 Tmp1 = LegalizeOp(Tmp1);
2022 LastCALLSEQ_END = DAG.getEntryNode();
2023
2024 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
2025 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2026
2027 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
2028 default: assert(0 && "This action is not supported yet!");
2029 case TargetLowering::Legal: break;
2030 case TargetLowering::Custom:
2031 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002032 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00002033 break;
2034 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002035 SDValue Chain = Result.getOperand(0);
2036 SDValue Table = Result.getOperand(1);
2037 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002038
Duncan Sands83ec4b62008-06-06 12:08:01 +00002039 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002040 MachineFunction &MF = DAG.getMachineFunction();
2041 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Dale Johannesenca57b842009-02-02 23:46:53 +00002042 Index= DAG.getNode(ISD::MUL, dl, PTy,
2043 Index, DAG.getConstant(EntrySize, PTy));
2044 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002045
Duncan Sands712f7b32008-12-12 08:13:38 +00002046 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
Dale Johannesenca57b842009-02-02 23:46:53 +00002047 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Duncan Sands712f7b32008-12-12 08:13:38 +00002048 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00002049 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002050 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00002051 // For PIC, the sequence is:
2052 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00002053 // RelocBase can be JumpTable, GOT or some sort of global base.
Dale Johannesenca57b842009-02-02 23:46:53 +00002054 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
Evan Chengcc415862007-11-09 01:32:10 +00002055 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00002056 }
Dale Johannesenca57b842009-02-02 23:46:53 +00002057 Result = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002058 }
2059 }
2060 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002061 case ISD::BRCOND:
2062 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002063 // Ensure that libcalls are emitted before a return.
Dale Johannesenca57b842009-02-02 23:46:53 +00002064 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00002065 Tmp1 = LegalizeOp(Tmp1);
2066 LastCALLSEQ_END = DAG.getEntryNode();
2067
Chris Lattner47e92232005-01-18 19:27:06 +00002068 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2069 case Expand: assert(0 && "It's impossible to expand bools");
2070 case Legal:
2071 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2072 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002073 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00002074 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00002075
2076 // The top bits of the promoted condition are not necessarily zero, ensure
2077 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002078 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002079 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002080 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dale Johannesenca57b842009-02-02 23:46:53 +00002081 Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002082 break;
2083 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002084 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002085
2086 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002087 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00002088
2089 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
2090 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002091 case TargetLowering::Legal: break;
2092 case TargetLowering::Custom:
2093 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002094 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002095 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002096 case TargetLowering::Expand:
2097 // Expand brcond's setcc into its constituent parts and create a BR_CC
2098 // Node.
2099 if (Tmp2.getOpcode() == ISD::SETCC) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002100 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
2101 Tmp1, Tmp2.getOperand(2),
Nate Begeman7cbd5252005-08-16 19:49:35 +00002102 Tmp2.getOperand(0), Tmp2.getOperand(1),
2103 Node->getOperand(2));
2104 } else {
Dale Johannesenca57b842009-02-02 23:46:53 +00002105 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Nate Begeman7cbd5252005-08-16 19:49:35 +00002106 DAG.getCondCode(ISD::SETNE), Tmp2,
2107 DAG.getConstant(0, Tmp2.getValueType()),
2108 Node->getOperand(2));
2109 }
2110 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002111 }
2112 break;
2113 case ISD::BR_CC:
2114 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002115 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00002116 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00002117 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002118 Tmp2 = Node->getOperand(2); // LHS
2119 Tmp3 = Node->getOperand(3); // RHS
2120 Tmp4 = Node->getOperand(1); // CC
2121
Dale Johannesenbb5da912009-02-02 20:41:04 +00002122 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()),
2123 Tmp2, Tmp3, Tmp4, dl);
Evan Cheng722cb362006-12-18 22:55:34 +00002124 LastCALLSEQ_END = DAG.getEntryNode();
2125
Evan Cheng7f042682008-10-15 02:05:31 +00002126 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002127 // the LHS is a legal SETCC itself. In this case, we need to compare
2128 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002129 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002130 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2131 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00002132 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00002133
2134 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2135 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002136
Chris Lattner181b7a32005-12-17 23:46:46 +00002137 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2138 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002139 case TargetLowering::Legal: break;
2140 case TargetLowering::Custom:
2141 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002142 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00002143 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002144 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002145 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002146 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00002147 LoadSDNode *LD = cast<LoadSDNode>(Node);
2148 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2149 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002150
Evan Cheng466685d2006-10-09 20:57:25 +00002151 ISD::LoadExtType ExtType = LD->getExtensionType();
2152 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002153 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00002154 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2155 Tmp3 = Result.getValue(0);
2156 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002157
Evan Cheng466685d2006-10-09 20:57:25 +00002158 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2159 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002160 case TargetLowering::Legal:
2161 // If this is an unaligned load and the target doesn't support it,
2162 // expand it.
2163 if (!TLI.allowsUnalignedMemoryAccesses()) {
2164 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002165 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002166 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002167 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002168 TLI);
2169 Tmp3 = Result.getOperand(0);
2170 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00002171 Tmp3 = LegalizeOp(Tmp3);
2172 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002173 }
2174 }
2175 break;
Evan Cheng466685d2006-10-09 20:57:25 +00002176 case TargetLowering::Custom:
2177 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002178 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002179 Tmp3 = LegalizeOp(Tmp1);
2180 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00002181 }
Evan Cheng466685d2006-10-09 20:57:25 +00002182 break;
2183 case TargetLowering::Promote: {
2184 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002185 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00002186 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002187 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002188
Dale Johannesenca57b842009-02-02 23:46:53 +00002189 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002190 LD->getSrcValueOffset(),
2191 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00002192 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2193 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002194 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00002195 }
Evan Cheng466685d2006-10-09 20:57:25 +00002196 }
2197 // Since loads produce two values, make sure to remember that we
2198 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002199 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2200 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002201 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00002202 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002203 MVT SrcVT = LD->getMemoryVT();
2204 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002205 int SVOffset = LD->getSrcValueOffset();
2206 unsigned Alignment = LD->getAlignment();
2207 bool isVolatile = LD->isVolatile();
2208
Duncan Sands83ec4b62008-06-06 12:08:01 +00002209 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002210 // Some targets pretend to have an i1 loading operation, and actually
2211 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2212 // bits are guaranteed to be zero; it helps the optimizers understand
2213 // that these bits are zero. It is also useful for EXTLOAD, since it
2214 // tells the optimizers that those bits are undefined. It would be
2215 // nice to have an effective generic way of getting these benefits...
2216 // Until such a way is found, don't insist on promoting i1 here.
2217 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00002218 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002219 // Promote to a byte-sized load if not loading an integral number of
2220 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002221 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2222 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002223 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002224
2225 // The extra bits are guaranteed to be zero, since we stored them that
2226 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2227
2228 ISD::LoadExtType NewExtType =
2229 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2230
Dale Johannesenca57b842009-02-02 23:46:53 +00002231 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002232 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2233 NVT, isVolatile, Alignment);
2234
2235 Ch = Result.getValue(1); // The chain.
2236
2237 if (ExtType == ISD::SEXTLOAD)
2238 // Having the top bits zero doesn't help when sign extending.
Dale Johannesenca57b842009-02-02 23:46:53 +00002239 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
2240 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002241 Result, DAG.getValueType(SrcVT));
2242 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2243 // All the top bits are guaranteed to be zero - inform the optimizers.
Dale Johannesenca57b842009-02-02 23:46:53 +00002244 Result = DAG.getNode(ISD::AssertZext, dl,
2245 Result.getValueType(), Result,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002246 DAG.getValueType(SrcVT));
2247
2248 Tmp1 = LegalizeOp(Result);
2249 Tmp2 = LegalizeOp(Ch);
2250 } else if (SrcWidth & (SrcWidth - 1)) {
2251 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002252 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002253 "Unsupported extload!");
2254 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2255 assert(RoundWidth < SrcWidth);
2256 unsigned ExtraWidth = SrcWidth - RoundWidth;
2257 assert(ExtraWidth < RoundWidth);
2258 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2259 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002260 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2261 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002262 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002263 unsigned IncrementSize;
2264
2265 if (TLI.isLittleEndian()) {
2266 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2267 // Load the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00002268 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
2269 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002270 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2271 Alignment);
2272
2273 // Load the remaining ExtraWidth bits.
2274 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00002275 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002276 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00002277 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002278 LD->getSrcValue(), SVOffset + IncrementSize,
2279 ExtraVT, isVolatile,
2280 MinAlign(Alignment, IncrementSize));
2281
2282 // Build a factor node to remember that this load is independent of the
2283 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00002284 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002285 Hi.getValue(1));
2286
2287 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00002288 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002289 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2290
2291 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00002292 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002293 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002294 // Big endian - avoid unaligned loads.
2295 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2296 // Load the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00002297 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002298 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2299 Alignment);
2300
2301 // Load the remaining ExtraWidth bits.
2302 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00002303 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002304 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00002305 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
2306 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002307 LD->getSrcValue(), SVOffset + IncrementSize,
2308 ExtraVT, isVolatile,
2309 MinAlign(Alignment, IncrementSize));
2310
2311 // Build a factor node to remember that this load is independent of the
2312 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00002313 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002314 Hi.getValue(1));
2315
2316 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00002317 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002318 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2319
2320 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00002321 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002322 }
2323
2324 Tmp1 = LegalizeOp(Result);
2325 Tmp2 = LegalizeOp(Ch);
2326 } else {
Evan Cheng03294662008-10-14 21:26:46 +00002327 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002328 default: assert(0 && "This action is not supported yet!");
2329 case TargetLowering::Custom:
2330 isCustom = true;
2331 // FALLTHROUGH
2332 case TargetLowering::Legal:
2333 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2334 Tmp1 = Result.getValue(0);
2335 Tmp2 = Result.getValue(1);
2336
2337 if (isCustom) {
2338 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002339 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002340 Tmp1 = LegalizeOp(Tmp3);
2341 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2342 }
2343 } else {
2344 // If this is an unaligned load and the target doesn't support it,
2345 // expand it.
2346 if (!TLI.allowsUnalignedMemoryAccesses()) {
2347 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002348 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002349 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002350 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002351 TLI);
2352 Tmp1 = Result.getOperand(0);
2353 Tmp2 = Result.getOperand(1);
2354 Tmp1 = LegalizeOp(Tmp1);
2355 Tmp2 = LegalizeOp(Tmp2);
2356 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002357 }
2358 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002359 break;
2360 case TargetLowering::Expand:
2361 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2362 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002363 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002364 LD->getSrcValueOffset(),
2365 LD->isVolatile(), LD->getAlignment());
Dale Johannesenca57b842009-02-02 23:46:53 +00002366 Result = DAG.getNode(ISD::FP_EXTEND, dl,
2367 Node->getValueType(0), Load);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002368 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2369 Tmp2 = LegalizeOp(Load.getValue(1));
2370 break;
2371 }
2372 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2373 // Turn the unsupported load into an EXTLOAD followed by an explicit
2374 // zero/sign extend inreg.
Dale Johannesenca57b842009-02-02 23:46:53 +00002375 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002376 Tmp1, Tmp2, LD->getSrcValue(),
2377 LD->getSrcValueOffset(), SrcVT,
2378 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00002379 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002380 if (ExtType == ISD::SEXTLOAD)
Dale Johannesenca57b842009-02-02 23:46:53 +00002381 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
2382 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002383 Result, DAG.getValueType(SrcVT));
2384 else
Dale Johannesenca57b842009-02-02 23:46:53 +00002385 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002386 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2387 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002388 break;
2389 }
Evan Cheng466685d2006-10-09 20:57:25 +00002390 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002391
Evan Cheng466685d2006-10-09 20:57:25 +00002392 // Since loads produce two values, make sure to remember that we legalized
2393 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002394 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2395 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002396 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002397 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002398 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002399 case ISD::EXTRACT_ELEMENT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002400 MVT OpTy = Node->getOperand(0).getValueType();
Nate Begeman5dc897b2005-10-19 00:06:56 +00002401 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002402 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002403 case Legal:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002404 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Nate Begeman5dc897b2005-10-19 00:06:56 +00002405 // 1 -> Hi
Dale Johannesenca57b842009-02-02 23:46:53 +00002406 Result = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002407 DAG.getConstant(OpTy.getSizeInBits()/2,
Nate Begeman5dc897b2005-10-19 00:06:56 +00002408 TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002409 Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result);
Nate Begeman5dc897b2005-10-19 00:06:56 +00002410 } else {
2411 // 0 -> Lo
Dale Johannesenca57b842009-02-02 23:46:53 +00002412 Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
Nate Begeman5dc897b2005-10-19 00:06:56 +00002413 Node->getOperand(0));
2414 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002415 break;
2416 case Expand:
2417 // Get both the low and high parts.
2418 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002419 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Nate Begeman5dc897b2005-10-19 00:06:56 +00002420 Result = Tmp2; // 1 -> Hi
2421 else
2422 Result = Tmp1; // 0 -> Lo
2423 break;
2424 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002425 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002426 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002427
2428 case ISD::CopyToReg:
2429 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002430
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002431 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002432 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002433 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002434 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002435 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002436 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002437 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002438 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002439 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002440 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002441 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2442 Tmp3);
2443 } else {
2444 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002445 }
2446
2447 // Since this produces two values, make sure to remember that we legalized
2448 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002449 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2450 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002451 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002452 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002453 break;
2454
2455 case ISD::RET:
2456 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002457
2458 // Ensure that libcalls are emitted before a return.
Dale Johannesenca57b842009-02-02 23:46:53 +00002459 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00002460 Tmp1 = LegalizeOp(Tmp1);
2461 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002462
Chris Lattner3e928bb2005-01-07 07:47:09 +00002463 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002464 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002465 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002466 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002467 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002468 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002469 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002470 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002471 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002472 if (!Tmp2.getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002473 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002474 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002475
2476 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002477 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002478 std::swap(Lo, Hi);
2479
Gabor Greifba36cb52008-08-28 21:40:38 +00002480 if (Hi.getNode())
Dale Johannesenca57b842009-02-02 23:46:53 +00002481 Result = DAG.getNode(ISD::RET, dl, MVT::Other,
2482 Tmp1, Lo, Tmp3, Hi,Tmp3);
Evan Cheng13acce32006-12-11 19:27:14 +00002483 else
Dale Johannesenca57b842009-02-02 23:46:53 +00002484 Result = DAG.getNode(ISD::RET, dl, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002485 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002486 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00002487 SDNode *InVal = Tmp2.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002488 int InIx = Tmp2.getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002489 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2490 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002491
Dan Gohman7f321562007-06-25 16:23:39 +00002492 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002493 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002494 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002495 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002496 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002497 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002498 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002499 } else if (NumElems == 1) {
2500 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002501 Tmp2 = ScalarizeVectorOp(Tmp2);
2502 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002503 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002504
2505 // FIXME: Returns of gcc generic vectors smaller than a legal type
2506 // should be returned in integer registers!
2507
Chris Lattnerf87324e2006-04-11 01:31:51 +00002508 // The scalarized value type may not be legal, e.g. it might require
2509 // promotion or expansion. Relegalize the return.
2510 Result = LegalizeOp(Result);
2511 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002512 // FIXME: Returns of gcc generic vectors larger than a legal vector
2513 // type should be returned by reference!
Dan Gohman475871a2008-07-27 21:46:04 +00002514 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002515 SplitVectorOp(Tmp2, Lo, Hi);
Dale Johannesenca57b842009-02-02 23:46:53 +00002516 Result = DAG.getNode(ISD::RET, dl, MVT::Other,
2517 Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002518 Result = LegalizeOp(Result);
2519 }
2520 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002521 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002522 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002523 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002524 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002525 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002526 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002527 }
2528 break;
2529 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002530 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002531 break;
2532 default: { // ret <values>
Dan Gohman475871a2008-07-27 21:46:04 +00002533 SmallVector<SDValue, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002534 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002535 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002536 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2537 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002538 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002539 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002540 break;
2541 case Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002542 SDValue Lo, Hi;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002543 assert(!Node->getOperand(i).getValueType().isExtended() &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002544 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002545 ExpandOp(Node->getOperand(i), Lo, Hi);
2546 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002547 NewValues.push_back(Node->getOperand(i+1));
Gabor Greifba36cb52008-08-28 21:40:38 +00002548 if (Hi.getNode()) {
Evan Cheng13acce32006-12-11 19:27:14 +00002549 NewValues.push_back(Hi);
2550 NewValues.push_back(Node->getOperand(i+1));
2551 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002552 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002553 }
2554 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002555 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002556 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002557
2558 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002559 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002560 else
Dale Johannesenca57b842009-02-02 23:46:53 +00002561 Result = DAG.getNode(ISD::RET, dl, MVT::Other,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002562 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002563 break;
2564 }
2565 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002566
Chris Lattner6862dbc2006-01-29 21:02:23 +00002567 if (Result.getOpcode() == ISD::RET) {
2568 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2569 default: assert(0 && "This action is not supported yet!");
2570 case TargetLowering::Legal: break;
2571 case TargetLowering::Custom:
2572 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002573 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner6862dbc2006-01-29 21:02:23 +00002574 break;
2575 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002576 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002577 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002578 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002579 StoreSDNode *ST = cast<StoreSDNode>(Node);
2580 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2581 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002582 int SVOffset = ST->getSrcValueOffset();
2583 unsigned Alignment = ST->getAlignment();
2584 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002585
Evan Cheng8b2794a2006-10-13 21:14:26 +00002586 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002587 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2588 // FIXME: We shouldn't do this for TargetConstantFP's.
2589 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2590 // to phase ordering between legalized code and the dag combiner. This
2591 // probably means that we need to integrate dag combiner and legalizer
2592 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002593 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002594 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002595 if (CFP->getValueType(0) == MVT::f32 &&
2596 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002597 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00002598 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002599 MVT::i32);
Dale Johannesenca57b842009-02-02 23:46:53 +00002600 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002601 SVOffset, isVolatile, Alignment);
2602 break;
2603 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002604 // If this target supports 64-bit registers, do a single 64-bit store.
2605 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00002606 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002607 zextOrTrunc(64), MVT::i64);
Dale Johannesenca57b842009-02-02 23:46:53 +00002608 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00002609 SVOffset, isVolatile, Alignment);
2610 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002611 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002612 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2613 // stores. If the target supports neither 32- nor 64-bits, this
2614 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00002615 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00002616 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2617 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002618 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002619
Dale Johannesenca57b842009-02-02 23:46:53 +00002620 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00002621 SVOffset, isVolatile, Alignment);
Dale Johannesenca57b842009-02-02 23:46:53 +00002622 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002623 DAG.getIntPtrConstant(4));
Dale Johannesenca57b842009-02-02 23:46:53 +00002624 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002625 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002626
Dale Johannesenca57b842009-02-02 23:46:53 +00002627 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002628 break;
2629 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002630 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002631 }
2632
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002633 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002634 case Legal: {
2635 Tmp3 = LegalizeOp(ST->getValue());
2636 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2637 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002638
Duncan Sands83ec4b62008-06-06 12:08:01 +00002639 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002640 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2641 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002642 case TargetLowering::Legal:
2643 // If this is an unaligned store and the target doesn't support it,
2644 // expand it.
2645 if (!TLI.allowsUnalignedMemoryAccesses()) {
2646 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002647 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002648 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002649 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002650 TLI);
2651 }
2652 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002653 case TargetLowering::Custom:
2654 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002655 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002656 break;
2657 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002658 assert(VT.isVector() && "Unknown legal promote case!");
Dale Johannesenca57b842009-02-02 23:46:53 +00002659 Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002660 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00002661 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002662 ST->getSrcValue(), SVOffset, isVolatile,
2663 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002664 break;
2665 }
2666 break;
2667 }
2668 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002669 if (!ST->getMemoryVT().isVector()) {
2670 // Truncate the value and store the result.
2671 Tmp3 = PromoteOp(ST->getValue());
Dale Johannesenca57b842009-02-02 23:46:53 +00002672 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Mon P Wang0c397192008-10-30 08:01:45 +00002673 SVOffset, ST->getMemoryVT(),
2674 isVolatile, Alignment);
2675 break;
2676 }
2677 // Fall thru to expand for vector
2678 case Expand: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002679 unsigned IncrementSize = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002680 SDValue Lo, Hi;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002681
2682 // If this is a vector type, then we have to calculate the increment as
2683 // the product of the element size in bytes, and the number of elements
2684 // in the high half of the vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002685 if (ST->getValue().getValueType().isVector()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002686 SDNode *InVal = ST->getValue().getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002687 int InIx = ST->getValue().getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002688 MVT InVT = InVal->getValueType(InIx);
2689 unsigned NumElems = InVT.getVectorNumElements();
2690 MVT EVT = InVT.getVectorElementType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002691
Dan Gohman7f321562007-06-25 16:23:39 +00002692 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002693 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002694 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002695 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002696 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002697 Tmp3 = LegalizeOp(ST->getValue());
Dale Johannesenca57b842009-02-02 23:46:53 +00002698 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002699 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002700 Result = LegalizeOp(Result);
2701 break;
2702 } else if (NumElems == 1) {
2703 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002704 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dale Johannesenca57b842009-02-02 23:46:53 +00002705 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002706 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002707 // The scalarized value type may not be legal, e.g. it might require
2708 // promotion or expansion. Relegalize the scalar store.
2709 Result = LegalizeOp(Result);
2710 break;
2711 } else {
Mon P Wang0c397192008-10-30 08:01:45 +00002712 // Check if we have widen this node with another value
2713 std::map<SDValue, SDValue>::iterator I =
2714 WidenNodes.find(ST->getValue());
2715 if (I != WidenNodes.end()) {
2716 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2717 break;
2718 }
2719 else {
2720 SplitVectorOp(ST->getValue(), Lo, Hi);
2721 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2722 EVT.getSizeInBits()/8;
2723 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002724 }
2725 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002726 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greifba36cb52008-08-28 21:40:38 +00002727 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002728
Richard Pennington4b052dc2008-09-25 16:15:10 +00002729 if (Hi.getNode() && TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002730 std::swap(Lo, Hi);
2731 }
2732
Dale Johannesenca57b842009-02-02 23:46:53 +00002733 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002734 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002735
Gabor Greifba36cb52008-08-28 21:40:38 +00002736 if (Hi.getNode() == NULL) {
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002737 // Must be int <-> float one-to-one expansion.
2738 Result = Lo;
2739 break;
2740 }
2741
Dale Johannesenca57b842009-02-02 23:46:53 +00002742 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002743 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002744 assert(isTypeLegal(Tmp2.getValueType()) &&
2745 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002746 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002747 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenca57b842009-02-02 23:46:53 +00002748 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002749 SVOffset, isVolatile, Alignment);
Dale Johannesenca57b842009-02-02 23:46:53 +00002750 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002751 break;
Mon P Wang0c397192008-10-30 08:01:45 +00002752 } // case Expand
Evan Cheng8b2794a2006-10-13 21:14:26 +00002753 }
2754 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002755 switch (getTypeAction(ST->getValue().getValueType())) {
2756 case Legal:
2757 Tmp3 = LegalizeOp(ST->getValue());
2758 break;
2759 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002760 if (!ST->getValue().getValueType().isVector()) {
2761 // We can promote the value, the truncstore will still take care of it.
2762 Tmp3 = PromoteOp(ST->getValue());
2763 break;
2764 }
2765 // Vector case falls through to expand
Chris Lattnerddf89562008-01-17 19:59:44 +00002766 case Expand:
2767 // Just store the low part. This may become a non-trunc store, so make
2768 // sure to use getTruncStore, not UpdateNodeOperands below.
2769 ExpandOp(ST->getValue(), Tmp3, Tmp4);
Dale Johannesenca57b842009-02-02 23:46:53 +00002770 return DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Chris Lattnerddf89562008-01-17 19:59:44 +00002771 SVOffset, MVT::i8, isVolatile, Alignment);
2772 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002773
Duncan Sands83ec4b62008-06-06 12:08:01 +00002774 MVT StVT = ST->getMemoryVT();
2775 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00002776
Duncan Sands83ec4b62008-06-06 12:08:01 +00002777 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00002778 // Promote to a byte-sized store with upper bits zero if not
2779 // storing an integral number of bytes. For example, promote
2780 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002781 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00002782 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
2783 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002784 SVOffset, NVT, isVolatile, Alignment);
2785 } else if (StWidth & (StWidth - 1)) {
2786 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002787 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00002788 "Unsupported truncstore!");
2789 unsigned RoundWidth = 1 << Log2_32(StWidth);
2790 assert(RoundWidth < StWidth);
2791 unsigned ExtraWidth = StWidth - RoundWidth;
2792 assert(ExtraWidth < RoundWidth);
2793 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2794 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002795 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2796 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002797 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00002798 unsigned IncrementSize;
2799
2800 if (TLI.isLittleEndian()) {
2801 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2802 // Store the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00002803 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002804 SVOffset, RoundVT,
2805 isVolatile, Alignment);
2806
2807 // Store the remaining ExtraWidth bits.
2808 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00002809 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00002810 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00002811 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00002812 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002813 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002814 SVOffset + IncrementSize, ExtraVT, isVolatile,
2815 MinAlign(Alignment, IncrementSize));
2816 } else {
2817 // Big endian - avoid unaligned stores.
2818 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2819 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00002820 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00002821 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002822 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
2823 SVOffset, RoundVT, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00002824
2825 // Store the remaining ExtraWidth bits.
2826 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00002827 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00002828 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00002829 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002830 SVOffset + IncrementSize, ExtraVT, isVolatile,
2831 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002832 }
Duncan Sands7e857202008-01-22 07:17:34 +00002833
2834 // The order of the stores doesn't matter.
Dale Johannesenca57b842009-02-02 23:46:53 +00002835 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00002836 } else {
2837 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2838 Tmp2 != ST->getBasePtr())
2839 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2840 ST->getOffset());
2841
2842 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2843 default: assert(0 && "This action is not supported yet!");
2844 case TargetLowering::Legal:
2845 // If this is an unaligned store and the target doesn't support it,
2846 // expand it.
2847 if (!TLI.allowsUnalignedMemoryAccesses()) {
2848 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002849 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00002850 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002851 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00002852 TLI);
2853 }
2854 break;
2855 case TargetLowering::Custom:
2856 Result = TLI.LowerOperation(Result, DAG);
2857 break;
2858 case Expand:
2859 // TRUNCSTORE:i16 i32 -> STORE i16
2860 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00002861 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
2862 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
2863 SVOffset, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00002864 break;
2865 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002866 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002867 }
2868 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002869 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002870 case ISD::PCMARKER:
2871 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002872 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002873 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002874 case ISD::STACKSAVE:
2875 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002876 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2877 Tmp1 = Result.getValue(0);
2878 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002879
Chris Lattner140d53c2006-01-13 02:50:02 +00002880 switch (TLI.getOperationAction(ISD::STACKSAVE, 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:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002884 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002885 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002886 Tmp1 = LegalizeOp(Tmp3);
2887 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002888 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002889 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002890 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002891 // Expand to CopyFromReg if the target set
2892 // StackPointerRegisterToSaveRestore.
2893 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Dale Johannesenc460ae92009-02-04 00:13:36 +00002894 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), dl, SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002895 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002896 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002897 } else {
Dale Johannesenca57b842009-02-02 23:46:53 +00002898 Tmp1 = DAG.getNode(ISD::UNDEF, dl, Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002899 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002900 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002901 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002902 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002903
2904 // Since stacksave produce two values, make sure to remember that we
2905 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002906 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2907 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002908 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002909
Chris Lattner140d53c2006-01-13 02:50:02 +00002910 case ISD::STACKRESTORE:
2911 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2912 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002913 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002914
2915 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2916 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002917 case TargetLowering::Legal: break;
2918 case TargetLowering::Custom:
2919 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002920 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002921 break;
2922 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002923 // Expand to CopyToReg if the target set
2924 // StackPointerRegisterToSaveRestore.
2925 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Dale Johannesenc460ae92009-02-04 00:13:36 +00002926 Result = DAG.getCopyToReg(Tmp1, dl, SP, Tmp2);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002927 } else {
2928 Result = Tmp1;
2929 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002930 break;
2931 }
2932 break;
2933
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002934 case ISD::READCYCLECOUNTER:
2935 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002936 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002937 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2938 Node->getValueType(0))) {
2939 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002940 case TargetLowering::Legal:
2941 Tmp1 = Result.getValue(0);
2942 Tmp2 = Result.getValue(1);
2943 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002944 case TargetLowering::Custom:
2945 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002946 Tmp1 = LegalizeOp(Result.getValue(0));
2947 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002948 break;
2949 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002950
2951 // Since rdcc produce two values, make sure to remember that we legalized
2952 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002953 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2954 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002955 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002956
Chris Lattner2ee743f2005-01-14 22:08:15 +00002957 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002958 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2959 case Expand: assert(0 && "It's impossible to expand bools");
2960 case Legal:
2961 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2962 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002963 case Promote: {
Mon P Wang0c397192008-10-30 08:01:45 +00002964 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Chris Lattner47e92232005-01-18 19:27:06 +00002965 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002966 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002967 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002968 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002969 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dale Johannesenca57b842009-02-02 23:46:53 +00002970 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002971 break;
2972 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002973 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002974 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002975 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002976
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002977 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002978
Nate Begemanb942a3d2005-08-23 04:29:48 +00002979 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002980 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002981 case TargetLowering::Legal: break;
2982 case TargetLowering::Custom: {
2983 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002984 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002985 break;
2986 }
Nate Begeman9373a812005-08-10 20:51:12 +00002987 case TargetLowering::Expand:
2988 if (Tmp1.getOpcode() == ISD::SETCC) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002989 Result = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
Nate Begeman9373a812005-08-10 20:51:12 +00002990 Tmp2, Tmp3,
2991 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2992 } else {
Dale Johannesenca57b842009-02-02 23:46:53 +00002993 Result = DAG.getSelectCC(dl, Tmp1,
Nate Begeman9373a812005-08-10 20:51:12 +00002994 DAG.getConstant(0, Tmp1.getValueType()),
2995 Tmp2, Tmp3, ISD::SETNE);
2996 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002997 break;
2998 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002999 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003000 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
3001 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003002 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00003003 ExtOp = ISD::BIT_CONVERT;
3004 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003005 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003006 ExtOp = ISD::ANY_EXTEND;
3007 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003008 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00003009 ExtOp = ISD::FP_EXTEND;
3010 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003011 }
3012 // Promote each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00003013 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Tmp2);
3014 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Tmp3);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003015 // Perform the larger operation, then round down.
Dale Johannesenca57b842009-02-02 23:46:53 +00003016 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00003017 if (TruncOp != ISD::FP_ROUND)
Dale Johannesenca57b842009-02-02 23:46:53 +00003018 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00003019 else
Dale Johannesenca57b842009-02-02 23:46:53 +00003020 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result,
Chris Lattner0bd48932008-01-17 07:00:52 +00003021 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003022 break;
3023 }
3024 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003025 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00003026 case ISD::SELECT_CC: {
3027 Tmp1 = Node->getOperand(0); // LHS
3028 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00003029 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
3030 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00003031 SDValue CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00003032
Dale Johannesenbb5da912009-02-02 20:41:04 +00003033 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
3034 Tmp1, Tmp2, CC, dl);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003035
Evan Cheng7f042682008-10-15 02:05:31 +00003036 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00003037 // the LHS is a legal SETCC itself. In this case, we need to compare
3038 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00003039 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003040 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3041 CC = DAG.getCondCode(ISD::SETNE);
3042 }
3043 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
3044
3045 // Everything is legal, see if we should expand this op or something.
3046 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
3047 default: assert(0 && "This action is not supported yet!");
3048 case TargetLowering::Legal: break;
3049 case TargetLowering::Custom:
3050 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003051 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00003052 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003053 }
3054 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00003055 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003056 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00003057 Tmp1 = Node->getOperand(0);
3058 Tmp2 = Node->getOperand(1);
3059 Tmp3 = Node->getOperand(2);
Dale Johannesenbb5da912009-02-02 20:41:04 +00003060 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003061
3062 // If we had to Expand the SetCC operands into a SELECT node, then it may
3063 // not always be possible to return a true LHS & RHS. In this case, just
3064 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003065 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003066 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003067 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003068 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003069
Chris Lattner73e142f2006-01-30 22:43:50 +00003070 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003071 default: assert(0 && "Cannot handle this action for SETCC yet!");
3072 case TargetLowering::Custom:
3073 isCustom = true;
3074 // FALLTHROUGH.
3075 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00003076 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00003077 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00003078 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003079 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00003080 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003081 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00003082 case TargetLowering::Promote: {
3083 // First step, figure out the appropriate operation to use.
3084 // Allow SETCC to not be supported for all legal data types
3085 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00003086 MVT NewInTy = Node->getOperand(0).getValueType();
3087 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00003088
3089 // Scan for the appropriate larger type to use.
3090 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003091 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00003092
Duncan Sands83ec4b62008-06-06 12:08:01 +00003093 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003094 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003095 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003096 "Fell off of the edge of the floating point world");
3097
3098 // If the target supports SETCC of this type, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003099 if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00003100 break;
3101 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003102 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00003103 assert(0 && "Cannot promote Legal Integer SETCC yet");
3104 else {
Dale Johannesenca57b842009-02-02 23:46:53 +00003105 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp1);
3106 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp2);
Andrew Lenharthae355752005-11-30 17:12:26 +00003107 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003108 Tmp1 = LegalizeOp(Tmp1);
3109 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00003110 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00003111 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00003112 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00003113 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003114 case TargetLowering::Expand:
3115 // Expand a setcc node into a select_cc of the same condition, lhs, and
3116 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003117 MVT VT = Node->getValueType(0);
Dale Johannesenca57b842009-02-02 23:46:53 +00003118 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
Nate Begemanb942a3d2005-08-23 04:29:48 +00003119 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00003120 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003121 break;
3122 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003123 break;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003124 case ISD::VSETCC: {
3125 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3126 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman475871a2008-07-27 21:46:04 +00003127 SDValue CC = Node->getOperand(2);
Nate Begemanb43e9c12008-05-12 19:40:03 +00003128
3129 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3130
3131 // Everything is legal, see if we should expand this op or something.
3132 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3133 default: assert(0 && "This action is not supported yet!");
3134 case TargetLowering::Legal: break;
3135 case TargetLowering::Custom:
3136 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003137 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003138 break;
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003139 case TargetLowering::Expand: {
3140 // Unroll into a nasty set of scalar code for now.
3141 MVT VT = Node->getValueType(0);
3142 unsigned NumElems = VT.getVectorNumElements();
3143 MVT EltVT = VT.getVectorElementType();
3144 MVT TmpEltVT = Tmp1.getValueType().getVectorElementType();
3145 SmallVector<SDValue, 8> Ops(NumElems);
3146 for (unsigned i = 0; i < NumElems; ++i) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003147 SDValue In1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT,
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003148 Tmp1, DAG.getIntPtrConstant(i));
Dale Johannesenca57b842009-02-02 23:46:53 +00003149 Ops[i] = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(TmpEltVT),
3150 In1, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3151 TmpEltVT, Tmp2,
3152 DAG.getIntPtrConstant(i)),
Mon P Wang84aff842008-12-17 08:49:47 +00003153 CC);
Dale Johannesenca57b842009-02-02 23:46:53 +00003154 Ops[i] = DAG.getNode(ISD::SELECT, dl, EltVT, Ops[i], DAG.getConstant(
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003155 APInt::getAllOnesValue(EltVT.getSizeInBits()),
3156 EltVT), DAG.getConstant(0, EltVT));
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003157 }
Dale Johannesenca57b842009-02-02 23:46:53 +00003158 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElems);
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003159 break;
3160 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00003161 }
3162 break;
3163 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00003164
Chris Lattner5b359c62005-04-02 04:00:59 +00003165 case ISD::SHL_PARTS:
3166 case ISD::SRA_PARTS:
3167 case ISD::SRL_PARTS: {
Dan Gohman475871a2008-07-27 21:46:04 +00003168 SmallVector<SDValue, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00003169 bool Changed = false;
Duncan Sands92abc622009-01-31 15:50:11 +00003170 unsigned N = Node->getNumOperands();
3171 for (unsigned i = 0; i + 1 < N; ++i) {
Chris Lattner84f67882005-01-20 18:52:28 +00003172 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3173 Changed |= Ops.back() != Node->getOperand(i);
3174 }
Duncan Sands92abc622009-01-31 15:50:11 +00003175 Ops.push_back(LegalizeOp(DAG.getShiftAmountOperand(Node->getOperand(N-1))));
3176 Changed |= Ops.back() != Node->getOperand(N-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003177 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003178 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00003179
Evan Cheng05a2d562006-01-09 18:31:59 +00003180 switch (TLI.getOperationAction(Node->getOpcode(),
3181 Node->getValueType(0))) {
3182 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003183 case TargetLowering::Legal: break;
3184 case TargetLowering::Custom:
3185 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003186 if (Tmp1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003187 SDValue Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00003188 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003189 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman475871a2008-07-27 21:46:04 +00003190 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003191 if (i == Op.getResNo())
Evan Cheng12f22742006-01-19 04:54:52 +00003192 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00003193 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003194 assert(RetVal.getNode() && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00003195 return RetVal;
3196 }
Evan Cheng05a2d562006-01-09 18:31:59 +00003197 break;
3198 }
3199
Chris Lattner2c8086f2005-04-02 05:00:07 +00003200 // Since these produce multiple values, make sure to remember that we
3201 // legalized all of them.
3202 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +00003203 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00003204 return Result.getValue(Op.getResNo());
Chris Lattner84f67882005-01-20 18:52:28 +00003205 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003206
3207 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00003208 case ISD::ADD:
3209 case ISD::SUB:
3210 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00003211 case ISD::MULHS:
3212 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003213 case ISD::UDIV:
3214 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003215 case ISD::AND:
3216 case ISD::OR:
3217 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00003218 case ISD::SHL:
3219 case ISD::SRL:
3220 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00003221 case ISD::FADD:
3222 case ISD::FSUB:
3223 case ISD::FMUL:
3224 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00003225 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003226 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands92abc622009-01-31 15:50:11 +00003227 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003228
3229 if ((Node->getOpcode() == ISD::SHL ||
3230 Node->getOpcode() == ISD::SRL ||
3231 Node->getOpcode() == ISD::SRA) &&
Duncan Sands92abc622009-01-31 15:50:11 +00003232 !Node->getValueType(0).isVector())
3233 Tmp2 = DAG.getShiftAmountOperand(Tmp2);
3234
3235 switch (getTypeAction(Tmp2.getValueType())) {
3236 case Expand: assert(0 && "Not possible");
3237 case Legal:
3238 Tmp2 = LegalizeOp(Tmp2); // Legalize the RHS.
3239 break;
3240 case Promote:
3241 Tmp2 = PromoteOp(Tmp2); // Promote the RHS.
3242 break;
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003243 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003244
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003245 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003246
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003247 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003248 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00003249 case TargetLowering::Legal: break;
3250 case TargetLowering::Custom:
3251 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003252 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003253 Result = Tmp1;
3254 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00003255 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003256 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003257 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003258 MVT VT = Op.getValueType();
Mon P Wang0c397192008-10-30 08:01:45 +00003259
Dan Gohman525178c2007-10-08 18:33:35 +00003260 // See if multiply or divide can be lowered using two-result operations.
3261 SDVTList VTs = DAG.getVTList(VT, VT);
3262 if (Node->getOpcode() == ISD::MUL) {
3263 // We just need the low half of the multiply; try both the signed
3264 // and unsigned forms. If the target supports both SMUL_LOHI and
3265 // UMUL_LOHI, form a preference by checking which forms of plain
3266 // MULH it supports.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003267 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3268 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3269 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3270 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
Dan Gohman525178c2007-10-08 18:33:35 +00003271 unsigned OpToUse = 0;
3272 if (HasSMUL_LOHI && !HasMULHS) {
3273 OpToUse = ISD::SMUL_LOHI;
3274 } else if (HasUMUL_LOHI && !HasMULHU) {
3275 OpToUse = ISD::UMUL_LOHI;
3276 } else if (HasSMUL_LOHI) {
3277 OpToUse = ISD::SMUL_LOHI;
3278 } else if (HasUMUL_LOHI) {
3279 OpToUse = ISD::UMUL_LOHI;
3280 }
3281 if (OpToUse) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003282 Result = SDValue(DAG.getNode(OpToUse, dl, VTs, Tmp1, Tmp2).getNode(),
3283 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003284 break;
3285 }
3286 }
3287 if (Node->getOpcode() == ISD::MULHS &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003288 TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003289 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl,
3290 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00003291 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003292 break;
3293 }
3294 if (Node->getOpcode() == ISD::MULHU &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003295 TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003296 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl,
3297 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00003298 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003299 break;
3300 }
3301 if (Node->getOpcode() == ISD::SDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003302 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003303 Result = SDValue(DAG.getNode(ISD::SDIVREM, dl,
3304 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00003305 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003306 break;
3307 }
3308 if (Node->getOpcode() == ISD::UDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003309 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003310 Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
3311 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00003312 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003313 break;
3314 }
Mon P Wang87c8a8f2008-12-18 20:03:17 +00003315
Dan Gohman82669522007-10-11 23:57:53 +00003316 // Check to see if we have a libcall for this operator.
3317 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3318 bool isSigned = false;
3319 switch (Node->getOpcode()) {
3320 case ISD::UDIV:
3321 case ISD::SDIV:
3322 if (VT == MVT::i32) {
3323 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang0c397192008-10-30 08:01:45 +00003324 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00003325 isSigned = Node->getOpcode() == ISD::SDIV;
3326 }
3327 break;
Chris Lattner31d71612008-10-04 21:27:46 +00003328 case ISD::MUL:
3329 if (VT == MVT::i32)
3330 LC = RTLIB::MUL_I32;
Nate Begeman9b994852009-01-24 22:12:48 +00003331 else if (VT == MVT::i64)
Scott Michel845145f2008-12-29 03:21:37 +00003332 LC = RTLIB::MUL_I64;
Chris Lattner31d71612008-10-04 21:27:46 +00003333 break;
Dan Gohman82669522007-10-11 23:57:53 +00003334 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003335 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3336 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003337 break;
Scott Micheld1e8d9c2009-01-21 04:58:48 +00003338 case ISD::FDIV:
3339 LC = GetFPLibCall(VT, RTLIB::DIV_F32, RTLIB::DIV_F64, RTLIB::DIV_F80,
3340 RTLIB::DIV_PPCF128);
3341 break;
Dan Gohman82669522007-10-11 23:57:53 +00003342 default: break;
3343 }
3344 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00003345 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003346 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003347 break;
3348 }
Mon P Wang0c397192008-10-30 08:01:45 +00003349
Duncan Sands83ec4b62008-06-06 12:08:01 +00003350 assert(Node->getValueType(0).isVector() &&
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003351 "Cannot expand this binary operator!");
3352 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003353 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003354 break;
3355 }
Evan Chengcc987612006-04-12 21:20:24 +00003356 case TargetLowering::Promote: {
3357 switch (Node->getOpcode()) {
3358 default: assert(0 && "Do not know how to promote this BinOp!");
3359 case ISD::AND:
3360 case ISD::OR:
3361 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003362 MVT OVT = Node->getValueType(0);
3363 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3364 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00003365 // Bit convert each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00003366 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
3367 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
3368 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Evan Chengcc987612006-04-12 21:20:24 +00003369 // Bit convert the result back the original type.
Dale Johannesenca57b842009-02-02 23:46:53 +00003370 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Evan Chengcc987612006-04-12 21:20:24 +00003371 break;
3372 }
3373 }
3374 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003375 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003376 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003377
Dan Gohmane14ea862007-10-05 14:17:22 +00003378 case ISD::SMUL_LOHI:
3379 case ISD::UMUL_LOHI:
3380 case ISD::SDIVREM:
3381 case ISD::UDIVREM:
3382 // These nodes will only be produced by target-specific lowering, so
3383 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003384 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003385 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003386
3387 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3388 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3389 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003390 break;
3391
Chris Lattnera09f8482006-03-05 05:09:38 +00003392 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3393 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3394 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3395 case Expand: assert(0 && "Not possible");
3396 case Legal:
3397 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3398 break;
3399 case Promote:
3400 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3401 break;
3402 }
3403
3404 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3405
3406 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3407 default: assert(0 && "Operation not supported");
3408 case TargetLowering::Custom:
3409 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003410 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003411 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003412 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003413 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003414 // If this target supports fabs/fneg natively and select is cheap,
3415 // do this efficiently.
3416 if (!TLI.isSelectExpensive() &&
3417 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3418 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003419 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003420 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003421 // Get the sign bit of the RHS.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003422 MVT IVT =
Chris Lattner8f4191d2006-03-13 06:08:38 +00003423 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dale Johannesenca57b842009-02-02 23:46:53 +00003424 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2);
3425 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(IVT),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003426 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3427 // Get the absolute value of the result.
Dale Johannesenca57b842009-02-02 23:46:53 +00003428 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
Chris Lattner8f4191d2006-03-13 06:08:38 +00003429 // Select between the nabs and abs value based on the sign bit of
3430 // the input.
Dale Johannesenca57b842009-02-02 23:46:53 +00003431 Result = DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
Chris Lattner8f4191d2006-03-13 06:08:38 +00003432 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3433 AbsVal),
3434 AbsVal);
3435 Result = LegalizeOp(Result);
3436 break;
3437 }
3438
3439 // Otherwise, do bitwise ops!
Duncan Sands83ec4b62008-06-06 12:08:01 +00003440 MVT NVT =
Evan Cheng912095b2007-01-04 21:56:39 +00003441 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3442 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
Dale Johannesenca57b842009-02-02 23:46:53 +00003443 Result = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0), Result);
Evan Cheng912095b2007-01-04 21:56:39 +00003444 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003445 break;
3446 }
Evan Cheng912095b2007-01-04 21:56:39 +00003447 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003448 break;
3449
Nate Begeman551bf3f2006-02-17 05:43:56 +00003450 case ISD::ADDC:
3451 case ISD::SUBC:
3452 Tmp1 = LegalizeOp(Node->getOperand(0));
3453 Tmp2 = LegalizeOp(Node->getOperand(1));
3454 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003455 Tmp3 = Result.getValue(0);
3456 Tmp4 = Result.getValue(1);
3457
3458 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3459 default: assert(0 && "This action is not supported yet!");
3460 case TargetLowering::Legal:
3461 break;
3462 case TargetLowering::Custom:
3463 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3464 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003465 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003466 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3467 }
3468 break;
3469 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003470 // Since this produces two values, make sure to remember that we legalized
3471 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003472 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3473 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3474 return Op.getResNo() ? Tmp4 : Tmp3;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003475
Nate Begeman551bf3f2006-02-17 05:43:56 +00003476 case ISD::ADDE:
3477 case ISD::SUBE:
3478 Tmp1 = LegalizeOp(Node->getOperand(0));
3479 Tmp2 = LegalizeOp(Node->getOperand(1));
3480 Tmp3 = LegalizeOp(Node->getOperand(2));
3481 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003482 Tmp3 = Result.getValue(0);
3483 Tmp4 = Result.getValue(1);
3484
3485 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3486 default: assert(0 && "This action is not supported yet!");
3487 case TargetLowering::Legal:
3488 break;
3489 case TargetLowering::Custom:
3490 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3491 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003492 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003493 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3494 }
3495 break;
3496 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003497 // Since this produces two values, make sure to remember that we legalized
3498 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003499 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3500 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3501 return Op.getResNo() ? Tmp4 : Tmp3;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003502
Nate Begeman419f8b62005-10-18 00:27:41 +00003503 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003504 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00003505 // TODO: handle the case where the Lo and Hi operands are not of legal type
3506 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3507 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3508 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003509 case TargetLowering::Promote:
3510 case TargetLowering::Custom:
3511 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003512 case TargetLowering::Legal:
3513 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Dale Johannesenca57b842009-02-02 23:46:53 +00003514 Result = DAG.getNode(ISD::BUILD_PAIR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003515 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003516 case TargetLowering::Expand:
Dale Johannesenca57b842009-02-02 23:46:53 +00003517 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Tmp1);
3518 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Tmp2);
3519 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003520 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00003521 TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00003522 Result = DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003523 break;
3524 }
3525 break;
3526 }
3527
Nate Begemanc105e192005-04-06 00:23:54 +00003528 case ISD::UREM:
3529 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003530 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003531 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3532 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003533
Nate Begemanc105e192005-04-06 00:23:54 +00003534 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003535 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3536 case TargetLowering::Custom:
3537 isCustom = true;
3538 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003539 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003540 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003541 if (isCustom) {
3542 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003543 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003544 }
Nate Begemanc105e192005-04-06 00:23:54 +00003545 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003546 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003547 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003548 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003549 MVT VT = Node->getValueType(0);
Dan Gohman525178c2007-10-08 18:33:35 +00003550
3551 // See if remainder can be lowered using two-result operations.
3552 SDVTList VTs = DAG.getVTList(VT, VT);
3553 if (Node->getOpcode() == ISD::SREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003554 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003555 Result = SDValue(DAG.getNode(ISD::SDIVREM, dl,
3556 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003557 break;
3558 }
3559 if (Node->getOpcode() == ISD::UREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003560 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003561 Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
3562 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003563 break;
3564 }
3565
Duncan Sands83ec4b62008-06-06 12:08:01 +00003566 if (VT.isInteger()) {
Dan Gohman525178c2007-10-08 18:33:35 +00003567 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003568 TargetLowering::Legal) {
3569 // X % Y -> X-X/Y*Y
Dale Johannesenca57b842009-02-02 23:46:53 +00003570 Result = DAG.getNode(DivOpc, dl, VT, Tmp1, Tmp2);
3571 Result = DAG.getNode(ISD::MUL, dl, VT, Result, Tmp2);
3572 Result = DAG.getNode(ISD::SUB, dl, VT, Tmp1, Result);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003573 } else if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003574 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003575 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003576 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003577 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003578 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3579 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman475871a2008-07-27 21:46:04 +00003580 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003581 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003582 }
Dan Gohman0d974262007-11-06 22:11:54 +00003583 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003584 assert(VT.isFloatingPoint() &&
Dan Gohman0d974262007-11-06 22:11:54 +00003585 "remainder op must have integer or floating-point type");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003586 if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003587 Result = LegalizeOp(UnrollVectorOp(Op));
3588 } else {
3589 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003590 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3591 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003592 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003593 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman80176312007-11-05 23:35:22 +00003594 }
Nate Begemanc105e192005-04-06 00:23:54 +00003595 }
3596 break;
3597 }
Dan Gohman525178c2007-10-08 18:33:35 +00003598 }
Nate Begemanc105e192005-04-06 00:23:54 +00003599 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003600 case ISD::VAARG: {
3601 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3602 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3603
Duncan Sands83ec4b62008-06-06 12:08:01 +00003604 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003605 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3606 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003607 case TargetLowering::Custom:
3608 isCustom = true;
3609 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003610 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003611 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3612 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003613 Tmp1 = Result.getValue(1);
3614
3615 if (isCustom) {
3616 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003617 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003618 Result = LegalizeOp(Tmp2);
3619 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3620 }
3621 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003622 break;
3623 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003624 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00003625 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003626 // Increment the pointer, VAList, to the next vaarg
Dale Johannesenca57b842009-02-02 23:46:53 +00003627 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00003628 DAG.getConstant(TLI.getTargetData()->
3629 getTypePaddedSize(VT.getTypeForMVT()),
3630 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00003631 // Store the incremented VAList to the legalized pointer
Dale Johannesenca57b842009-02-02 23:46:53 +00003632 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003633 // Load the actual argument out of the pointer VAList
Dale Johannesenca57b842009-02-02 23:46:53 +00003634 Result = DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003635 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003636 Result = LegalizeOp(Result);
3637 break;
3638 }
3639 }
3640 // Since VAARG produces two values, make sure to remember that we
3641 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003642 AddLegalizedOperand(SDValue(Node, 0), Result);
3643 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003644 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003645 }
3646
3647 case ISD::VACOPY:
3648 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3649 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3650 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3651
3652 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3653 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003654 case TargetLowering::Custom:
3655 isCustom = true;
3656 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003657 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003658 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3659 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003660 if (isCustom) {
3661 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003662 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003663 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003664 break;
3665 case TargetLowering::Expand:
3666 // This defaults to loading a pointer from the input and storing it to the
3667 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003668 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3669 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00003670 Tmp4 = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp3, VS, 0);
3671 Result = DAG.getStore(Tmp4.getValue(1), dl, Tmp4, Tmp2, VD, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003672 break;
3673 }
3674 break;
3675
3676 case ISD::VAEND:
3677 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3678 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3679
3680 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3681 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003682 case TargetLowering::Custom:
3683 isCustom = true;
3684 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003685 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003686 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003687 if (isCustom) {
3688 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003689 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003690 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003691 break;
3692 case TargetLowering::Expand:
3693 Result = Tmp1; // Default to a no-op, return the chain
3694 break;
3695 }
3696 break;
3697
3698 case ISD::VASTART:
3699 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3700 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3701
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003702 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3703
Nate Begemanacc398c2006-01-25 18:21:52 +00003704 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3705 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003706 case TargetLowering::Legal: break;
3707 case TargetLowering::Custom:
3708 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003709 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003710 break;
3711 }
3712 break;
3713
Nate Begeman35ef9132006-01-11 21:21:00 +00003714 case ISD::ROTL:
3715 case ISD::ROTR:
3716 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands92abc622009-01-31 15:50:11 +00003717 Tmp2 = LegalizeOp(DAG.getShiftAmountOperand(Node->getOperand(1))); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003718 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003719 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3720 default:
3721 assert(0 && "ROTL/ROTR legalize operation not supported");
3722 break;
3723 case TargetLowering::Legal:
3724 break;
3725 case TargetLowering::Custom:
3726 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003727 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelc9dc1142007-04-02 21:36:32 +00003728 break;
3729 case TargetLowering::Promote:
3730 assert(0 && "Do not know how to promote ROTL/ROTR");
3731 break;
3732 case TargetLowering::Expand:
3733 assert(0 && "Do not know how to expand ROTL/ROTR");
3734 break;
3735 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003736 break;
3737
Nate Begemand88fc032006-01-14 03:14:10 +00003738 case ISD::BSWAP:
3739 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3740 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003741 case TargetLowering::Custom:
3742 assert(0 && "Cannot custom legalize this yet!");
3743 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003744 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003745 break;
3746 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003747 MVT OVT = Tmp1.getValueType();
3748 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3749 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begemand88fc032006-01-14 03:14:10 +00003750
Dale Johannesenca57b842009-02-02 23:46:53 +00003751 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
3752 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3753 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Chris Lattner456a93a2006-01-28 07:39:30 +00003754 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3755 break;
3756 }
3757 case TargetLowering::Expand:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003758 Result = ExpandBSWAP(Tmp1, dl);
Chris Lattner456a93a2006-01-28 07:39:30 +00003759 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003760 }
3761 break;
3762
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003763 case ISD::CTPOP:
3764 case ISD::CTTZ:
3765 case ISD::CTLZ:
3766 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3767 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003768 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003769 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003770 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003771 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003772 TargetLowering::Custom) {
3773 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003774 if (Tmp1.getNode()) {
Scott Michel335f4f72007-08-02 02:22:46 +00003775 Result = Tmp1;
3776 }
Scott Michel910b66d2007-07-30 21:00:31 +00003777 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003778 break;
3779 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003780 MVT OVT = Tmp1.getValueType();
3781 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003782
3783 // Zero extend the argument.
Dale Johannesenca57b842009-02-02 23:46:53 +00003784 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003785 // Perform the larger operation, then subtract if needed.
Dale Johannesenca57b842009-02-02 23:46:53 +00003786 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003787 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003788 case ISD::CTPOP:
3789 Result = Tmp1;
3790 break;
3791 case ISD::CTTZ:
3792 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Dale Johannesenca57b842009-02-02 23:46:53 +00003793 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3794 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003795 ISD::SETEQ);
Dale Johannesenca57b842009-02-02 23:46:53 +00003796 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003797 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003798 break;
3799 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003800 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Dale Johannesenca57b842009-02-02 23:46:53 +00003801 Result = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003802 DAG.getConstant(NVT.getSizeInBits() -
3803 OVT.getSizeInBits(), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003804 break;
3805 }
3806 break;
3807 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003808 case TargetLowering::Expand:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003809 Result = ExpandBitCount(Node->getOpcode(), Tmp1, dl);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003810 break;
3811 }
3812 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003813
Chris Lattner2c8086f2005-04-02 05:00:07 +00003814 // Unary operators
3815 case ISD::FABS:
3816 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +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 Gohman509e84f2008-08-21 17:55:02 +00003825 case ISD::FTRUNC:
3826 case ISD::FFLOOR:
3827 case ISD::FCEIL:
3828 case ISD::FRINT:
3829 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003830 Tmp1 = LegalizeOp(Node->getOperand(0));
3831 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003832 case TargetLowering::Promote:
3833 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003834 isCustom = true;
3835 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003836 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003837 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003838 if (isCustom) {
3839 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003840 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00003841 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003842 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003843 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003844 switch (Node->getOpcode()) {
3845 default: assert(0 && "Unreachable!");
3846 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003847 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3848 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00003849 Result = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003850 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003851 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003852 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003853 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003854 Tmp2 = DAG.getConstantFP(0.0, VT);
Dale Johannesenca57b842009-02-02 23:46:53 +00003855 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00003856 Tmp1, Tmp2, ISD::SETUGT);
Dale Johannesenca57b842009-02-02 23:46:53 +00003857 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
3858 Result = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003859 break;
3860 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00003861 case ISD::FSQRT:
3862 case ISD::FSIN:
3863 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003864 case ISD::FLOG:
3865 case ISD::FLOG2:
3866 case ISD::FLOG10:
3867 case ISD::FEXP:
3868 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003869 case ISD::FTRUNC:
3870 case ISD::FFLOOR:
3871 case ISD::FCEIL:
3872 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00003873 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003874 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003875
3876 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003877 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003878 Result = LegalizeOp(UnrollVectorOp(Op));
3879 break;
3880 }
3881
Evan Cheng56966222007-01-12 02:11:51 +00003882 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003883 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003884 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003885 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3886 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003887 break;
3888 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003889 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3890 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003891 break;
3892 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003893 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3894 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003895 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003896 case ISD::FLOG:
3897 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3898 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3899 break;
3900 case ISD::FLOG2:
3901 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3902 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3903 break;
3904 case ISD::FLOG10:
3905 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3906 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3907 break;
3908 case ISD::FEXP:
3909 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3910 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3911 break;
3912 case ISD::FEXP2:
3913 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3914 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3915 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003916 case ISD::FTRUNC:
3917 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3918 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3919 break;
3920 case ISD::FFLOOR:
3921 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3922 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3923 break;
3924 case ISD::FCEIL:
3925 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3926 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3927 break;
3928 case ISD::FRINT:
3929 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3930 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3931 break;
3932 case ISD::FNEARBYINT:
3933 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3934 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3935 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00003936 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003937 default: assert(0 && "Unreachable!");
3938 }
Dan Gohman475871a2008-07-27 21:46:04 +00003939 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003940 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003941 break;
3942 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003943 }
3944 break;
3945 }
3946 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003947 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003948 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003949
3950 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003951 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003952 Result = LegalizeOp(UnrollVectorOp(Op));
3953 break;
3954 }
3955
3956 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003957 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3958 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003959 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003960 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003961 break;
3962 }
Chris Lattner35481892005-12-23 00:16:34 +00003963 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003964 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003965 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003966 Node->getValueType(0), dl);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003967 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003968 // The input has to be a vector type, we have to either scalarize it, pack
3969 // it, or convert it based on whether the input vector type is legal.
Gabor Greifba36cb52008-08-28 21:40:38 +00003970 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00003971 int InIx = Node->getOperand(0).getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003972 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3973 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohman7f321562007-06-25 16:23:39 +00003974
3975 // Figure out if there is a simple type corresponding to this Vector
3976 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003977 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00003978 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003979 // Turn this into a bit convert of the vector input.
Dale Johannesenca57b842009-02-02 23:46:53 +00003980 Result = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0),
Dan Gohman7f321562007-06-25 16:23:39 +00003981 LegalizeOp(Node->getOperand(0)));
3982 break;
3983 } else if (NumElems == 1) {
3984 // Turn this into a bit convert of the scalar input.
Dale Johannesenca57b842009-02-02 23:46:53 +00003985 Result = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0),
Dan Gohman7f321562007-06-25 16:23:39 +00003986 ScalarizeVectorOp(Node->getOperand(0)));
3987 break;
3988 } else {
3989 // FIXME: UNIMP! Store then reload
3990 assert(0 && "Cast from unsupported vector type not implemented yet!");
3991 }
Chris Lattner67993f72006-01-23 07:30:46 +00003992 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003993 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3994 Node->getOperand(0).getValueType())) {
3995 default: assert(0 && "Unknown operation action!");
3996 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003997 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003998 Node->getValueType(0), dl);
Chris Lattner35481892005-12-23 00:16:34 +00003999 break;
4000 case TargetLowering::Legal:
4001 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004002 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00004003 break;
4004 }
4005 }
4006 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00004007 case ISD::CONVERT_RNDSAT: {
4008 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4009 switch (CvtCode) {
4010 default: assert(0 && "Unknown cvt code!");
4011 case ISD::CVT_SF:
4012 case ISD::CVT_UF:
Mon P Wang77cdf302008-11-10 20:54:11 +00004013 case ISD::CVT_FF:
Mon P Wang1cd46bb2008-12-09 07:27:39 +00004014 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00004015 case ISD::CVT_FS:
4016 case ISD::CVT_FU:
4017 case ISD::CVT_SS:
4018 case ISD::CVT_SU:
4019 case ISD::CVT_US:
4020 case ISD::CVT_UU: {
4021 SDValue DTyOp = Node->getOperand(1);
4022 SDValue STyOp = Node->getOperand(2);
4023 SDValue RndOp = Node->getOperand(3);
4024 SDValue SatOp = Node->getOperand(4);
4025 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4026 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4027 case Legal:
4028 Tmp1 = LegalizeOp(Node->getOperand(0));
4029 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
4030 RndOp, SatOp);
4031 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4032 TargetLowering::Custom) {
4033 Tmp1 = TLI.LowerOperation(Result, DAG);
4034 if (Tmp1.getNode()) Result = Tmp1;
4035 }
4036 break;
4037 case Promote:
4038 Result = PromoteOp(Node->getOperand(0));
4039 // For FP, make Op1 a i32
4040
Dale Johannesenc460ae92009-02-04 00:13:36 +00004041 Result = DAG.getConvertRndSat(Op.getValueType(), dl, Result,
Mon P Wang77cdf302008-11-10 20:54:11 +00004042 DTyOp, STyOp, RndOp, SatOp, CvtCode);
4043 break;
4044 }
4045 break;
4046 }
4047 } // end switch CvtCode
4048 break;
4049 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00004050 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00004051 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004052 case ISD::UINT_TO_FP: {
4053 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman7f8613e2008-08-14 20:04:46 +00004054 Result = LegalizeINT_TO_FP(Result, isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +00004055 Node->getValueType(0), Node->getOperand(0), dl);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004056 break;
4057 }
4058 case ISD::TRUNCATE:
4059 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4060 case Legal:
4061 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel546d7b52008-12-02 19:55:08 +00004062 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
4063 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
4064 case TargetLowering::Custom:
Mon P Wangf67303d2008-12-11 00:44:22 +00004065 isCustom = true;
4066 // FALLTHROUGH
Scott Michel546d7b52008-12-02 19:55:08 +00004067 case TargetLowering::Legal:
Mon P Wangf67303d2008-12-11 00:44:22 +00004068 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4069 if (isCustom) {
4070 Tmp1 = TLI.LowerOperation(Result, DAG);
4071 if (Tmp1.getNode()) Result = Tmp1;
4072 }
4073 break;
Mon P Wang9e5ecb82008-12-12 01:25:51 +00004074 case TargetLowering::Expand:
4075 assert(Result.getValueType().isVector() && "must be vector type");
4076 // Unroll the truncate. We should do better.
4077 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Schellerb0a5cdd2008-12-02 12:12:25 +00004078 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004079 break;
4080 case Expand:
4081 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4082
4083 // Since the result is legal, we should just be able to truncate the low
4084 // part of the source.
Dale Johannesenca57b842009-02-02 23:46:53 +00004085 Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004086 break;
4087 case Promote:
4088 Result = PromoteOp(Node->getOperand(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00004089 Result = DAG.getNode(ISD::TRUNCATE, dl, Op.getValueType(), Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004090 break;
4091 }
4092 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004093
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004094 case ISD::FP_TO_SINT:
4095 case ISD::FP_TO_UINT:
4096 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4097 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00004098 Tmp1 = LegalizeOp(Node->getOperand(0));
4099
Chris Lattner1618beb2005-07-29 00:11:56 +00004100 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
4101 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004102 case TargetLowering::Custom:
4103 isCustom = true;
4104 // FALLTHROUGH
4105 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004106 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004107 if (isCustom) {
4108 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004109 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00004110 }
4111 break;
4112 case TargetLowering::Promote:
4113 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Dale Johannesenaf435272009-02-02 19:03:57 +00004114 Node->getOpcode() == ISD::FP_TO_SINT,
4115 dl);
Chris Lattner456a93a2006-01-28 07:39:30 +00004116 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00004117 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00004118 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004119 SDValue True, False;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004120 MVT VT = Node->getOperand(0).getValueType();
4121 MVT NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00004122 const uint64_t zero[] = {0, 0};
Duncan Sands83ec4b62008-06-06 12:08:01 +00004123 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
4124 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohmanc7773bf2008-02-29 01:44:25 +00004125 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00004126 Tmp2 = DAG.getConstantFP(apf, VT);
Dale Johannesenaf435272009-02-02 19:03:57 +00004127 Tmp3 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
4128 Node->getOperand(0),
Duncan Sands5480c042009-01-01 15:52:00 +00004129 Tmp2, ISD::SETLT);
Dale Johannesenaf435272009-02-02 19:03:57 +00004130 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
4131 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
4132 DAG.getNode(ISD::FSUB, dl, VT,
4133 Node->getOperand(0), Tmp2));
4134 False = DAG.getNode(ISD::XOR, dl, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00004135 DAG.getConstant(x, NVT));
Dale Johannesenaf435272009-02-02 19:03:57 +00004136 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp3, True, False);
Chris Lattner456a93a2006-01-28 07:39:30 +00004137 break;
Nate Begemand2558e32005-08-14 01:20:53 +00004138 } else {
4139 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
4140 }
4141 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00004142 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004143 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00004144 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004145 MVT VT = Op.getValueType();
4146 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004147 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004148 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004149 if (Node->getOpcode() == ISD::FP_TO_SINT) {
Dale Johannesenaf435272009-02-02 19:03:57 +00004150 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
Chris Lattner0bd48932008-01-17 07:00:52 +00004151 Node->getOperand(0), DAG.getValueType(MVT::f64));
Dale Johannesenaf435272009-02-02 19:03:57 +00004152 Result = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Result,
Chris Lattner0bd48932008-01-17 07:00:52 +00004153 DAG.getIntPtrConstant(1));
Dale Johannesenaf435272009-02-02 19:03:57 +00004154 Result = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00004155 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004156 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4157 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4158 Tmp2 = DAG.getConstantFP(apf, OVT);
4159 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4160 // FIXME: generated code sucks.
Dale Johannesenaf435272009-02-02 19:03:57 +00004161 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Node->getOperand(0),
4162 Tmp2,
4163 DAG.getNode(ISD::ADD, dl, MVT::i32,
4164 DAG.getNode(ISD::FP_TO_SINT, dl, VT,
4165 DAG.getNode(ISD::FSUB, dl, OVT,
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004166 Node->getOperand(0), Tmp2)),
4167 DAG.getConstant(0x80000000, MVT::i32)),
Dale Johannesenaf435272009-02-02 19:03:57 +00004168 DAG.getNode(ISD::FP_TO_SINT, dl, VT,
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004169 Node->getOperand(0)),
4170 DAG.getCondCode(ISD::SETGE));
4171 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004172 break;
4173 }
Dan Gohmana2e94852008-03-10 23:03:31 +00004174 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsb2ff8852008-07-17 02:36:29 +00004175 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4176 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4177 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman475871a2008-07-27 21:46:04 +00004178 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00004179 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00004180 break;
4181 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004182 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004183 Tmp1 = PromoteOp(Node->getOperand(0));
4184 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4185 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004186 break;
4187 }
4188 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004189
Chris Lattnerf2670a82008-01-16 06:57:07 +00004190 case ISD::FP_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004191 MVT DstVT = Op.getValueType();
4192 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004193 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4194 // The only other way we can lower this is to turn it into a STORE,
4195 // LOAD pair, targetting a temporary location (a stack slot).
Dale Johannesen8a782a22009-02-02 22:12:50 +00004196 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT, dl);
Chris Lattner0bd48932008-01-17 07:00:52 +00004197 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00004198 }
4199 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4200 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4201 case Legal:
4202 Tmp1 = LegalizeOp(Node->getOperand(0));
4203 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4204 break;
4205 case Promote:
4206 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00004207 Result = DAG.getNode(ISD::FP_EXTEND, dl, Op.getValueType(), Tmp1);
Chris Lattnerf2670a82008-01-16 06:57:07 +00004208 break;
4209 }
4210 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004211 }
Dale Johannesen5411a392007-08-09 01:04:01 +00004212 case ISD::FP_ROUND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004213 MVT DstVT = Op.getValueType();
4214 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004215 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4216 if (SrcVT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00004217 SDValue Lo;
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004218 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00004219 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004220 if (DstVT!=MVT::f64)
Dale Johannesenca57b842009-02-02 23:46:53 +00004221 Result = DAG.getNode(ISD::FP_ROUND, dl,
4222 DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00004223 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00004224 }
Chris Lattner0bd48932008-01-17 07:00:52 +00004225 // The only other way we can lower this is to turn it into a STORE,
4226 // LOAD pair, targetting a temporary location (a stack slot).
Dale Johannesen8a782a22009-02-02 22:12:50 +00004227 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT, dl);
Chris Lattner0bd48932008-01-17 07:00:52 +00004228 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00004229 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00004230 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4231 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4232 case Legal:
4233 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004234 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004235 break;
4236 case Promote:
4237 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00004238 Result = DAG.getNode(ISD::FP_ROUND, dl, Op.getValueType(), Tmp1,
Chris Lattner0bd48932008-01-17 07:00:52 +00004239 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004240 break;
4241 }
4242 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004243 }
Chris Lattner13c78e22005-09-02 00:18:10 +00004244 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004245 case ISD::ZERO_EXTEND:
4246 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004247 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00004248 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004249 case Legal:
4250 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel82747a52008-04-30 00:26:38 +00004251 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel0123b7d2008-02-15 23:05:48 +00004252 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4253 TargetLowering::Custom) {
Scott Michel82747a52008-04-30 00:26:38 +00004254 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004255 if (Tmp1.getNode()) Result = Tmp1;
Scott Michel0123b7d2008-02-15 23:05:48 +00004256 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004257 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004258 case Promote:
4259 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00004260 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004261 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00004262 Result = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00004263 break;
Chris Lattner1713e732005-01-16 00:38:00 +00004264 case ISD::ZERO_EXTEND:
4265 Result = PromoteOp(Node->getOperand(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00004266 Result = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), Result);
4267 Result = DAG.getZeroExtendInReg(Result, dl,
Chris Lattner23993562005-04-13 02:38:47 +00004268 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00004269 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004270 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00004271 Result = PromoteOp(Node->getOperand(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00004272 Result = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), Result);
4273 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004274 Result,
4275 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00004276 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004277 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004278 }
4279 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00004280 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00004281 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00004282 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004283 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00004284
4285 // If this operation is not supported, convert it to a shl/shr or load/store
4286 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004287 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4288 default: assert(0 && "This action not supported for this op yet!");
4289 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004290 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004291 break;
4292 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00004293 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00004294 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00004295 // NOTE: we could fall back on load/store here too for targets without
4296 // SAR. However, it is doubtful that any exist.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004297 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4298 ExtraVT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00004299 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Dale Johannesenca57b842009-02-02 23:46:53 +00004300 Result = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
Chris Lattner45b8caf2005-01-15 07:15:18 +00004301 Node->getOperand(0), ShiftCst);
Dale Johannesenca57b842009-02-02 23:46:53 +00004302 Result = DAG.getNode(ISD::SRA, dl, Node->getValueType(0),
Chris Lattner45b8caf2005-01-15 07:15:18 +00004303 Result, ShiftCst);
4304 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00004305 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00004306 // EXTLOAD pair, targetting a temporary location (a stack slot).
4307
4308 // NOTE: there is a choice here between constantly creating new stack
4309 // slots and always reusing the same one. We currently always create
4310 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00004311 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00004312 Node->getValueType(0), dl);
Chris Lattner45b8caf2005-01-15 07:15:18 +00004313 } else {
4314 assert(0 && "Unknown op");
4315 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004316 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00004317 }
Chris Lattner0f69b292005-01-15 06:18:18 +00004318 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004319 }
Duncan Sands36397f52007-07-27 12:58:54 +00004320 case ISD::TRAMPOLINE: {
Dan Gohman475871a2008-07-27 21:46:04 +00004321 SDValue Ops[6];
Duncan Sands36397f52007-07-27 12:58:54 +00004322 for (unsigned i = 0; i != 6; ++i)
4323 Ops[i] = LegalizeOp(Node->getOperand(i));
4324 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4325 // The only option for this node is to custom lower it.
4326 Result = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004327 assert(Result.getNode() && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00004328
4329 // Since trampoline produces two values, make sure to remember that we
4330 // legalized both of them.
4331 Tmp1 = LegalizeOp(Result.getValue(1));
4332 Result = LegalizeOp(Result);
Dan Gohman475871a2008-07-27 21:46:04 +00004333 AddLegalizedOperand(SDValue(Node, 0), Result);
4334 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00004335 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00004336 }
Dan Gohman9c78a392008-05-14 00:43:10 +00004337 case ISD::FLT_ROUNDS_: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004338 MVT VT = Node->getValueType(0);
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004339 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4340 default: assert(0 && "This action not supported for this op yet!");
4341 case TargetLowering::Custom:
4342 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004343 if (Result.getNode()) break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004344 // Fall Thru
4345 case TargetLowering::Legal:
4346 // If this operation is not supported, lower it to constant 1
4347 Result = DAG.getConstant(1, VT);
4348 break;
4349 }
Dan Gohman9ab9ee82008-05-12 16:07:15 +00004350 break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004351 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004352 case ISD::TRAP: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004353 MVT VT = Node->getValueType(0);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004354 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4355 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00004356 case TargetLowering::Legal:
4357 Tmp1 = LegalizeOp(Node->getOperand(0));
4358 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4359 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004360 case TargetLowering::Custom:
4361 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004362 if (Result.getNode()) break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004363 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00004364 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004365 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00004366 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004367 TargetLowering::ArgListTy Args;
Dan Gohman475871a2008-07-27 21:46:04 +00004368 std::pair<SDValue,SDValue> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00004369 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00004370 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00004371 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Dale Johannesen7d2ad622009-01-30 23:10:59 +00004372 Args, DAG, dl);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004373 Result = CallResult.second;
4374 break;
4375 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004376 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004377 }
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004378
Bill Wendling74c37652008-12-09 22:08:41 +00004379 case ISD::SADDO:
4380 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004381 MVT VT = Node->getValueType(0);
4382 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4383 default: assert(0 && "This action not supported for this op yet!");
4384 case TargetLowering::Custom:
4385 Result = TLI.LowerOperation(Op, DAG);
4386 if (Result.getNode()) break;
4387 // FALLTHROUGH
4388 case TargetLowering::Legal: {
4389 SDValue LHS = LegalizeOp(Node->getOperand(0));
4390 SDValue RHS = LegalizeOp(Node->getOperand(1));
4391
Bill Wendling74c37652008-12-09 22:08:41 +00004392 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00004393 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00004394 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004395 MVT OType = Node->getValueType(1);
4396
Bill Wendlinga6af91a2008-11-25 08:19:22 +00004397 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004398
Bill Wendling740464e2008-11-25 19:40:17 +00004399 // LHSSign -> LHS >= 0
4400 // RHSSign -> RHS >= 0
4401 // SumSign -> Sum >= 0
4402 //
Bill Wendling74c37652008-12-09 22:08:41 +00004403 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00004404 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00004405 // Sub:
4406 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00004407 //
Dale Johannesenca57b842009-02-02 23:46:53 +00004408 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
4409 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
4410 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
Bill Wendling74c37652008-12-09 22:08:41 +00004411 Node->getOpcode() == ISD::SADDO ?
4412 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004413
Dale Johannesenca57b842009-02-02 23:46:53 +00004414 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
4415 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004416
Dale Johannesenca57b842009-02-02 23:46:53 +00004417 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004418
4419 MVT ValueVTs[] = { LHS.getValueType(), OType };
4420 SDValue Ops[] = { Sum, Cmp };
4421
Dale Johannesenca57b842009-02-02 23:46:53 +00004422 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
4423 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00004424 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004425 SDNode *RNode = Result.getNode();
4426 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4427 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4428 break;
4429 }
4430 }
4431
4432 break;
4433 }
Bill Wendling74c37652008-12-09 22:08:41 +00004434 case ISD::UADDO:
4435 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00004436 MVT VT = Node->getValueType(0);
4437 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4438 default: assert(0 && "This action not supported for this op yet!");
4439 case TargetLowering::Custom:
4440 Result = TLI.LowerOperation(Op, DAG);
4441 if (Result.getNode()) break;
4442 // FALLTHROUGH
4443 case TargetLowering::Legal: {
4444 SDValue LHS = LegalizeOp(Node->getOperand(0));
4445 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004446
Bill Wendling74c37652008-12-09 22:08:41 +00004447 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00004448 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00004449 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004450 MVT OType = Node->getValueType(1);
Dale Johannesenca57b842009-02-02 23:46:53 +00004451 SDValue Cmp = DAG.getSetCC(dl, OType, Sum, LHS,
Bill Wendling74c37652008-12-09 22:08:41 +00004452 Node->getOpcode () == ISD::UADDO ?
4453 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004454
Bill Wendling41ea7e72008-11-24 19:21:46 +00004455 MVT ValueVTs[] = { LHS.getValueType(), OType };
4456 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004457
Dale Johannesenca57b842009-02-02 23:46:53 +00004458 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
4459 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00004460 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004461 SDNode *RNode = Result.getNode();
4462 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4463 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4464 break;
4465 }
4466 }
4467
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004468 break;
4469 }
Bill Wendling74c37652008-12-09 22:08:41 +00004470 case ISD::SMULO:
4471 case ISD::UMULO: {
4472 MVT VT = Node->getValueType(0);
4473 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4474 default: assert(0 && "This action is not supported at all!");
4475 case TargetLowering::Custom:
4476 Result = TLI.LowerOperation(Op, DAG);
4477 if (Result.getNode()) break;
4478 // Fall Thru
4479 case TargetLowering::Legal:
4480 // FIXME: According to Hacker's Delight, this can be implemented in
4481 // target independent lowering, but it would be inefficient, since it
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00004482 // requires a division + a branch.
Bill Wendling74c37652008-12-09 22:08:41 +00004483 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4484 break;
4485 }
4486 break;
4487 }
4488
Chris Lattner45b8caf2005-01-15 07:15:18 +00004489 }
Chris Lattner456a93a2006-01-28 07:39:30 +00004490
Chris Lattner4ddd2832006-04-08 04:13:17 +00004491 assert(Result.getValueType() == Op.getValueType() &&
4492 "Bad legalization!");
4493
Chris Lattner456a93a2006-01-28 07:39:30 +00004494 // Make sure that the generated code is itself legal.
4495 if (Result != Op)
4496 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004497
Chris Lattner45982da2005-05-12 16:53:42 +00004498 // Note that LegalizeOp may be reentered even from single-use nodes, which
4499 // means that we always must cache transformed nodes.
4500 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004501 return Result;
4502}
4503
Chris Lattner8b6fa222005-01-15 22:16:26 +00004504/// PromoteOp - Given an operation that produces a value in an invalid type,
4505/// promote it to compute the value into a larger type. The produced value will
4506/// have the correct bits for the low portion of the register, but no guarantee
4507/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman475871a2008-07-27 21:46:04 +00004508SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004509 MVT VT = Op.getValueType();
4510 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004511 assert(getTypeAction(VT) == Promote &&
4512 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004513 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner03c85462005-01-15 05:21:40 +00004514 "Cannot promote to smaller type!");
4515
Dan Gohman475871a2008-07-27 21:46:04 +00004516 SDValue Tmp1, Tmp2, Tmp3;
4517 SDValue Result;
Gabor Greifba36cb52008-08-28 21:40:38 +00004518 SDNode *Node = Op.getNode();
Dale Johannesen8a782a22009-02-02 22:12:50 +00004519 DebugLoc dl = Node->getDebugLoc();
Chris Lattner03c85462005-01-15 05:21:40 +00004520
Dan Gohman475871a2008-07-27 21:46:04 +00004521 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004522 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004523
Chris Lattner03c85462005-01-15 05:21:40 +00004524 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004525 case ISD::CopyFromReg:
4526 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004527 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004528#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004529 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004530#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004531 assert(0 && "Do not know how to promote this operator!");
4532 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004533 case ISD::UNDEF:
Dale Johannesen8a782a22009-02-02 22:12:50 +00004534 Result = DAG.getNode(ISD::UNDEF, dl, NVT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004535 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004536 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004537 if (VT != MVT::i1)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004538 Result = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, Op);
Chris Lattnerec176e32005-08-30 16:56:19 +00004539 else
Dale Johannesen8a782a22009-02-02 22:12:50 +00004540 Result = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004541 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4542 break;
4543 case ISD::ConstantFP:
Dale Johannesen8a782a22009-02-02 22:12:50 +00004544 Result = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004545 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4546 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004547
Duncan Sands5480c042009-01-01 15:52:00 +00004548 case ISD::SETCC: {
4549 MVT VT0 = Node->getOperand(0).getValueType();
4550 assert(isTypeLegal(TLI.getSetCCResultType(VT0))
Nate Begeman5922f562008-03-14 00:53:31 +00004551 && "SetCC type is not legal??");
Dale Johannesen8a782a22009-02-02 22:12:50 +00004552 Result = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(VT0),
Nate Begeman5922f562008-03-14 00:53:31 +00004553 Node->getOperand(0), Node->getOperand(1),
4554 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004555 break;
Duncan Sands5480c042009-01-01 15:52:00 +00004556 }
Chris Lattner03c85462005-01-15 05:21:40 +00004557 case ISD::TRUNCATE:
4558 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4559 case Legal:
4560 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004561 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner03c85462005-01-15 05:21:40 +00004562 "This truncation doesn't make sense!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004563 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Dale Johannesen8a782a22009-02-02 22:12:50 +00004564 Result = DAG.getNode(ISD::TRUNCATE, dl, NVT, Result);
Chris Lattner03c85462005-01-15 05:21:40 +00004565 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004566 case Promote:
4567 // The truncation is not required, because we don't guarantee anything
4568 // about high bits anyway.
4569 Result = PromoteOp(Node->getOperand(0));
4570 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004571 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004572 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4573 // Truncate the low part of the expanded value to the result type
Dale Johannesen8a782a22009-02-02 22:12:50 +00004574 Result = DAG.getNode(ISD::TRUNCATE, dl, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004575 }
4576 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004577 case ISD::SIGN_EXTEND:
4578 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004579 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004580 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4581 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4582 case Legal:
4583 // Input is legal? Just do extend all the way to the larger type.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004584 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004585 break;
4586 case Promote:
4587 // Promote the reg if it's smaller.
4588 Result = PromoteOp(Node->getOperand(0));
4589 // The high bits are not guaranteed to be anything. Insert an extend.
4590 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004591 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004592 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004593 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004594 Result = DAG.getZeroExtendInReg(Result, dl,
Chris Lattner23993562005-04-13 02:38:47 +00004595 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004596 break;
4597 }
4598 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00004599 case ISD::CONVERT_RNDSAT: {
4600 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4601 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4602 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4603 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4604 "can only promote integers");
Dale Johannesenc460ae92009-02-04 00:13:36 +00004605 Result = DAG.getConvertRndSat(NVT, dl, Node->getOperand(0),
Mon P Wang77cdf302008-11-10 20:54:11 +00004606 Node->getOperand(1), Node->getOperand(2),
4607 Node->getOperand(3), Node->getOperand(4),
4608 CvtCode);
4609 break;
4610
4611 }
Chris Lattner35481892005-12-23 00:16:34 +00004612 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004613 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
Dale Johannesen8a782a22009-02-02 22:12:50 +00004614 Node->getValueType(0), dl);
Chris Lattner35481892005-12-23 00:16:34 +00004615 Result = PromoteOp(Result);
4616 break;
4617
Chris Lattner8b6fa222005-01-15 22:16:26 +00004618 case ISD::FP_EXTEND:
4619 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4620 case ISD::FP_ROUND:
4621 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4622 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4623 case Promote: assert(0 && "Unreachable with 2 FP types!");
4624 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004625 if (Node->getConstantOperandVal(1) == 0) {
4626 // Input is legal? Do an FP_ROUND_INREG.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004627 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Node->getOperand(0),
Chris Lattner0bd48932008-01-17 07:00:52 +00004628 DAG.getValueType(VT));
4629 } else {
4630 // Just remove the truncate, it isn't affecting the value.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004631 Result = DAG.getNode(ISD::FP_ROUND, dl, NVT, Node->getOperand(0),
Chris Lattner0bd48932008-01-17 07:00:52 +00004632 Node->getOperand(1));
4633 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004634 break;
4635 }
4636 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004637 case ISD::SINT_TO_FP:
4638 case ISD::UINT_TO_FP:
4639 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4640 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004641 // No extra round required here.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004642 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004643 break;
4644
4645 case Promote:
4646 Result = PromoteOp(Node->getOperand(0));
4647 if (Node->getOpcode() == ISD::SINT_TO_FP)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004648 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004649 Result,
4650 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004651 else
Dale Johannesen8a782a22009-02-02 22:12:50 +00004652 Result = DAG.getZeroExtendInReg(Result, dl,
Chris Lattner23993562005-04-13 02:38:47 +00004653 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004654 // No extra round required here.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004655 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004656 break;
4657 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004658 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00004659 Node->getOperand(0), dl);
Chris Lattner77e77a62005-01-21 06:05:23 +00004660 // Round if we cannot tolerate excess precision.
4661 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004662 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004663 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004664 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004665 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004666 break;
4667
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004668 case ISD::SIGN_EXTEND_INREG:
4669 Result = PromoteOp(Node->getOperand(0));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004670 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Result,
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004671 Node->getOperand(1));
4672 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004673 case ISD::FP_TO_SINT:
4674 case ISD::FP_TO_UINT:
4675 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4676 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004677 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004678 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004679 break;
4680 case Promote:
4681 // The input result is prerounded, so we don't have to do anything
4682 // special.
4683 Tmp1 = PromoteOp(Node->getOperand(0));
4684 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004685 }
Nate Begemand2558e32005-08-14 01:20:53 +00004686 // If we're promoting a UINT to a larger size, check to see if the new node
4687 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4688 // we can use that instead. This allows us to generate better code for
4689 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4690 // legal, such as PowerPC.
4691 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004692 !TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NVT) &&
4693 (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT) ||
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004694 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Dale Johannesen8a782a22009-02-02 22:12:50 +00004695 Result = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Tmp1);
Nate Begemand2558e32005-08-14 01:20:53 +00004696 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00004697 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Nate Begemand2558e32005-08-14 01:20:53 +00004698 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004699 break;
4700
Chris Lattner2c8086f2005-04-02 05:00:07 +00004701 case ISD::FABS:
4702 case ISD::FNEG:
4703 Tmp1 = PromoteOp(Node->getOperand(0));
4704 assert(Tmp1.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004705 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Chris Lattner2c8086f2005-04-02 05:00:07 +00004706 // NOTE: we do not have to do any extra rounding here for
4707 // NoExcessFPPrecision, because we know the input will have the appropriate
4708 // precision, and these operations don't modify precision at all.
4709 break;
4710
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004711 case ISD::FLOG:
4712 case ISD::FLOG2:
4713 case ISD::FLOG10:
4714 case ISD::FEXP:
4715 case ISD::FEXP2:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004716 case ISD::FSQRT:
4717 case ISD::FSIN:
4718 case ISD::FCOS:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00004719 case ISD::FTRUNC:
4720 case ISD::FFLOOR:
4721 case ISD::FCEIL:
4722 case ISD::FRINT:
4723 case ISD::FNEARBYINT:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004724 Tmp1 = PromoteOp(Node->getOperand(0));
4725 assert(Tmp1.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004726 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004727 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004728 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004729 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004730 break;
4731
Evan Cheng9d24ac52008-09-09 23:35:53 +00004732 case ISD::FPOW:
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004733 case ISD::FPOWI: {
Evan Cheng9d24ac52008-09-09 23:35:53 +00004734 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004735 // directly as well, which may be better.
4736 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng9d24ac52008-09-09 23:35:53 +00004737 Tmp2 = Node->getOperand(1);
4738 if (Node->getOpcode() == ISD::FPOW)
4739 Tmp2 = PromoteOp(Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004740 assert(Tmp1.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004741 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004742 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004743 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004744 DAG.getValueType(VT));
4745 break;
4746 }
4747
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004748 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang28873102008-06-25 08:15:39 +00004749 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004750 Tmp2 = PromoteOp(Node->getOperand(2));
4751 Tmp3 = PromoteOp(Node->getOperand(3));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004752 Result = DAG.getAtomic(Node->getOpcode(), dl, AtomNode->getMemoryVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004753 AtomNode->getChain(),
Mon P Wang28873102008-06-25 08:15:39 +00004754 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004755 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004756 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004757 // Remember that we legalized the chain.
4758 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4759 break;
4760 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004761 case ISD::ATOMIC_LOAD_ADD:
4762 case ISD::ATOMIC_LOAD_SUB:
4763 case ISD::ATOMIC_LOAD_AND:
4764 case ISD::ATOMIC_LOAD_OR:
4765 case ISD::ATOMIC_LOAD_XOR:
4766 case ISD::ATOMIC_LOAD_NAND:
4767 case ISD::ATOMIC_LOAD_MIN:
4768 case ISD::ATOMIC_LOAD_MAX:
4769 case ISD::ATOMIC_LOAD_UMIN:
4770 case ISD::ATOMIC_LOAD_UMAX:
4771 case ISD::ATOMIC_SWAP: {
Mon P Wang28873102008-06-25 08:15:39 +00004772 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004773 Tmp2 = PromoteOp(Node->getOperand(2));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004774 Result = DAG.getAtomic(Node->getOpcode(), dl, AtomNode->getMemoryVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004775 AtomNode->getChain(),
Mon P Wang28873102008-06-25 08:15:39 +00004776 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004777 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004778 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004779 // Remember that we legalized the chain.
4780 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4781 break;
4782 }
4783
Chris Lattner03c85462005-01-15 05:21:40 +00004784 case ISD::AND:
4785 case ISD::OR:
4786 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004787 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004788 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004789 case ISD::MUL:
4790 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004791 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004792 // that too is okay if they are integer operations.
4793 Tmp1 = PromoteOp(Node->getOperand(0));
4794 Tmp2 = PromoteOp(Node->getOperand(1));
4795 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004796 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004797 break;
4798 case ISD::FADD:
4799 case ISD::FSUB:
4800 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004801 Tmp1 = PromoteOp(Node->getOperand(0));
4802 Tmp2 = PromoteOp(Node->getOperand(1));
4803 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004804 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004805
4806 // Floating point operations will give excess precision that we may not be
4807 // able to tolerate. If we DO allow excess precision, just leave it,
4808 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004809 // FIXME: Why would we need to round FP ops more than integer ones?
4810 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004811 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004812 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004813 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004814 break;
4815
Chris Lattner8b6fa222005-01-15 22:16:26 +00004816 case ISD::SDIV:
4817 case ISD::SREM:
4818 // These operators require that their input be sign extended.
4819 Tmp1 = PromoteOp(Node->getOperand(0));
4820 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004821 if (NVT.isInteger()) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00004822 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
Chris Lattner15e4b012005-07-10 00:07:11 +00004823 DAG.getValueType(VT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004824 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp2,
Chris Lattner15e4b012005-07-10 00:07:11 +00004825 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004826 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00004827 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004828
4829 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004830 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004831 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004832 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004833 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004834 case ISD::FDIV:
4835 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004836 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004837 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004838 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004839 case Expand: assert(0 && "not implemented");
4840 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4841 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004842 }
4843 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004844 case Expand: assert(0 && "not implemented");
4845 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4846 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004847 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00004848 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004849
4850 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004851 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004852 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner01b3d732005-09-28 22:28:18 +00004853 DAG.getValueType(VT));
4854 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004855
4856 case ISD::UDIV:
4857 case ISD::UREM:
4858 // These operators require that their input be zero extended.
4859 Tmp1 = PromoteOp(Node->getOperand(0));
4860 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004861 assert(NVT.isInteger() && "Operators don't apply to FP!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00004862 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
4863 Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, VT);
4864 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004865 break;
4866
4867 case ISD::SHL:
4868 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004869 Result = DAG.getNode(ISD::SHL, dl, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004870 break;
4871 case ISD::SRA:
4872 // The input value must be properly sign extended.
4873 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004874 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
Chris Lattner15e4b012005-07-10 00:07:11 +00004875 DAG.getValueType(VT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004876 Result = DAG.getNode(ISD::SRA, dl, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004877 break;
4878 case ISD::SRL:
4879 // The input value must be properly zero extended.
4880 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004881 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
4882 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004883 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004884
4885 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004886 Tmp1 = Node->getOperand(0); // Get the chain.
4887 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004888 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
Dale Johannesenc460ae92009-02-04 00:13:36 +00004889 Tmp3 = DAG.getVAArg(VT, dl, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands126d9072008-07-04 11:47:58 +00004890 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman0aed7842006-01-28 03:14:31 +00004891 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004892 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenc460ae92009-02-04 00:13:36 +00004893 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004894 // Increment the pointer, VAList, to the next vaarg
Dale Johannesen8a782a22009-02-02 22:12:50 +00004895 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004896 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman0aed7842006-01-28 03:14:31 +00004897 TLI.getPointerTy()));
4898 // Store the incremented VAList to the legalized pointer
Dale Johannesen8a782a22009-02-02 22:12:50 +00004899 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004900 // Load the actual argument out of the pointer VAList
Dale Johannesen8a782a22009-02-02 22:12:50 +00004901 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004902 }
4903 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004904 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004905 break;
4906
Evan Cheng466685d2006-10-09 20:57:25 +00004907 case ISD::LOAD: {
4908 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004909 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4910 ? ISD::EXTLOAD : LD->getExtensionType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00004911 Result = DAG.getExtLoad(ExtType, dl, NVT,
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004912 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004913 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004914 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004915 LD->isVolatile(),
4916 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004917 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004918 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004919 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004920 }
Scott Michel8bf61e82008-06-02 22:18:03 +00004921 case ISD::SELECT: {
Chris Lattner03c85462005-01-15 05:21:40 +00004922 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4923 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel8bf61e82008-06-02 22:18:03 +00004924
Duncan Sands83ec4b62008-06-06 12:08:01 +00004925 MVT VT2 = Tmp2.getValueType();
Scott Michel8bf61e82008-06-02 22:18:03 +00004926 assert(VT2 == Tmp3.getValueType()
Scott Michelba12f572008-06-03 19:13:20 +00004927 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4928 // Ensure that the resulting node is at least the same size as the operands'
4929 // value types, because we cannot assume that TLI.getSetCCValueType() is
4930 // constant.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004931 Result = DAG.getNode(ISD::SELECT, dl, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004932 break;
Scott Michel8bf61e82008-06-02 22:18:03 +00004933 }
Nate Begeman9373a812005-08-10 20:51:12 +00004934 case ISD::SELECT_CC:
4935 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4936 Tmp3 = PromoteOp(Node->getOperand(3)); // False
Dale Johannesen8a782a22009-02-02 22:12:50 +00004937 Result = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004938 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004939 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004940 case ISD::BSWAP:
4941 Tmp1 = Node->getOperand(0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004942 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
4943 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
4944 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004945 DAG.getConstant(NVT.getSizeInBits() -
4946 VT.getSizeInBits(),
Nate Begemand88fc032006-01-14 03:14:10 +00004947 TLI.getShiftAmountTy()));
4948 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004949 case ISD::CTPOP:
4950 case ISD::CTTZ:
4951 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004952 // Zero extend the argument
Dale Johannesen8a782a22009-02-02 22:12:50 +00004953 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004954 // Perform the larger operation, then subtract if needed.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004955 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004956 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004957 case ISD::CTPOP:
4958 Result = Tmp1;
4959 break;
4960 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004961 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004962 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004963 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanb55757e2007-05-18 17:52:13 +00004964 ISD::SETEQ);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004965 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004966 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004967 break;
4968 case ISD::CTLZ:
4969 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00004970 Result = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004971 DAG.getConstant(NVT.getSizeInBits() -
4972 VT.getSizeInBits(), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004973 break;
4974 }
4975 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004976 case ISD::EXTRACT_SUBVECTOR:
4977 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004978 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004979 case ISD::EXTRACT_VECTOR_ELT:
4980 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4981 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004982 }
4983
Gabor Greifba36cb52008-08-28 21:40:38 +00004984 assert(Result.getNode() && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004985
4986 // Make sure the result is itself legal.
4987 Result = LegalizeOp(Result);
4988
4989 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004990 AddPromotedOperand(Op, Result);
4991 return Result;
4992}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004993
Dan Gohman7f321562007-06-25 16:23:39 +00004994/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4995/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4996/// based on the vector type. The return type of this matches the element type
4997/// of the vector, which may not be legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00004998SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004999 // We know that operand #0 is the Vec vector. If the index is a constant
5000 // or if the invec is a supported hardware type, we can use it. Otherwise,
5001 // lower to a store then an indexed load.
Dan Gohman475871a2008-07-27 21:46:04 +00005002 SDValue Vec = Op.getOperand(0);
5003 SDValue Idx = Op.getOperand(1);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005004 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattner15972212006-03-31 17:55:51 +00005005
Duncan Sands83ec4b62008-06-06 12:08:01 +00005006 MVT TVT = Vec.getValueType();
5007 unsigned NumElems = TVT.getVectorNumElements();
Chris Lattner15972212006-03-31 17:55:51 +00005008
Dan Gohman7f321562007-06-25 16:23:39 +00005009 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
5010 default: assert(0 && "This action is not supported yet!");
5011 case TargetLowering::Custom: {
5012 Vec = LegalizeOp(Vec);
5013 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00005014 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005015 if (Tmp3.getNode())
Dan Gohman7f321562007-06-25 16:23:39 +00005016 return Tmp3;
5017 break;
5018 }
5019 case TargetLowering::Legal:
5020 if (isTypeLegal(TVT)) {
5021 Vec = LegalizeOp(Vec);
5022 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00005023 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00005024 }
5025 break;
Mon P Wang0c397192008-10-30 08:01:45 +00005026 case TargetLowering::Promote:
5027 assert(TVT.isVector() && "not vector type");
5028 // fall thru to expand since vectors are by default are promote
Dan Gohman7f321562007-06-25 16:23:39 +00005029 case TargetLowering::Expand:
5030 break;
5031 }
5032
5033 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00005034 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00005035 Op = ScalarizeVectorOp(Vec);
5036 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00005037 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00005038 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00005039 SDValue Lo, Hi;
Chris Lattner15972212006-03-31 17:55:51 +00005040 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005041 if (CIdx->getZExtValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00005042 Vec = Lo;
5043 } else {
5044 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005045 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00005046 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00005047 }
Dan Gohman7f321562007-06-25 16:23:39 +00005048
Chris Lattner15972212006-03-31 17:55:51 +00005049 // It's now an extract from the appropriate high or low part. Recurse.
5050 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00005051 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00005052 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00005053 // Store the value to a temporary stack slot, then LOAD the scalar
5054 // element back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005055 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dale Johannesenbb5da912009-02-02 20:41:04 +00005056 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0);
Dan Gohman7f321562007-06-25 16:23:39 +00005057
5058 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005059 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +00005060 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
Dan Gohman7f321562007-06-25 16:23:39 +00005061 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005062
Duncan Sands8e4eb092008-06-08 20:54:56 +00005063 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Dale Johannesenbb5da912009-02-02 20:41:04 +00005064 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005065 else
Dale Johannesenbb5da912009-02-02 20:41:04 +00005066 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005067
Dale Johannesenbb5da912009-02-02 20:41:04 +00005068 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
Dan Gohman7f321562007-06-25 16:23:39 +00005069
Dale Johannesenbb5da912009-02-02 20:41:04 +00005070 Op = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00005071 }
Dan Gohman7f321562007-06-25 16:23:39 +00005072 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00005073}
5074
Dan Gohman7f321562007-06-25 16:23:39 +00005075/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00005076/// we assume the operation can be split if it is not already legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005077SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman65956352007-06-13 15:12:02 +00005078 // We know that operand #0 is the Vec vector. For now we assume the index
5079 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman475871a2008-07-27 21:46:04 +00005080 SDValue Vec = Op.getOperand(0);
5081 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00005082
Duncan Sands83ec4b62008-06-06 12:08:01 +00005083 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohman65956352007-06-13 15:12:02 +00005084
Duncan Sands83ec4b62008-06-06 12:08:01 +00005085 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman65956352007-06-13 15:12:02 +00005086 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00005087 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00005088 }
5089
5090 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00005091 SDValue Lo, Hi;
Dan Gohman65956352007-06-13 15:12:02 +00005092 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005093 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohman65956352007-06-13 15:12:02 +00005094 Vec = Lo;
5095 } else {
5096 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005097 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
5098 Idx.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00005099 }
5100
5101 // It's now an extract from the appropriate high or low part. Recurse.
5102 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00005103 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00005104}
5105
Nate Begeman750ac1b2006-02-01 07:19:44 +00005106/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
5107/// with condition CC on the current target. This usually involves legalizing
5108/// or promoting the arguments. In the case where LHS and RHS must be expanded,
5109/// there may be no choice but to create a new SetCC node to represent the
5110/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00005111/// LHS, and the SDValue returned in RHS has a nil SDNode value.
5112void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
5113 SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00005114 SDValue &CC,
5115 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00005116 SDValue Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005117
5118 switch (getTypeAction(LHS.getValueType())) {
5119 case Legal:
5120 Tmp1 = LegalizeOp(LHS); // LHS
5121 Tmp2 = LegalizeOp(RHS); // RHS
5122 break;
5123 case Promote:
5124 Tmp1 = PromoteOp(LHS); // LHS
5125 Tmp2 = PromoteOp(RHS); // RHS
5126
5127 // If this is an FP compare, the operands have already been extended.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005128 if (LHS.getValueType().isInteger()) {
5129 MVT VT = LHS.getValueType();
5130 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00005131
5132 // Otherwise, we have to insert explicit sign or zero extends. Note
5133 // that we could insert sign extends for ALL conditions, but zero extend
5134 // is cheaper on many machines (an AND instead of two shifts), so prefer
5135 // it.
5136 switch (cast<CondCodeSDNode>(CC)->get()) {
5137 default: assert(0 && "Unknown integer comparison!");
5138 case ISD::SETEQ:
5139 case ISD::SETNE:
5140 case ISD::SETUGE:
5141 case ISD::SETUGT:
5142 case ISD::SETULE:
5143 case ISD::SETULT:
5144 // ALL of these operations will work if we either sign or zero extend
5145 // the operands (including the unsigned comparisons!). Zero extend is
5146 // usually a simpler/cheaper operation, so prefer it.
Dale Johannesenbb5da912009-02-02 20:41:04 +00005147 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
5148 Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00005149 break;
5150 case ISD::SETGE:
5151 case ISD::SETGT:
5152 case ISD::SETLT:
5153 case ISD::SETLE:
Dale Johannesenbb5da912009-02-02 20:41:04 +00005154 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
Nate Begeman750ac1b2006-02-01 07:19:44 +00005155 DAG.getValueType(VT));
Dale Johannesenbb5da912009-02-02 20:41:04 +00005156 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp2,
Nate Begeman750ac1b2006-02-01 07:19:44 +00005157 DAG.getValueType(VT));
Evan Chengefa53392008-10-13 18:46:18 +00005158 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5159 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Nate Begeman750ac1b2006-02-01 07:19:44 +00005160 break;
5161 }
5162 }
5163 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005164 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005165 MVT VT = LHS.getValueType();
Evan Cheng2b49c502006-12-15 02:59:56 +00005166 if (VT == MVT::f32 || VT == MVT::f64) {
5167 // Expand into one or more soft-fp libcall(s).
Evan Cheng6518c6e2008-07-01 21:35:46 +00005168 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00005169 switch (cast<CondCodeSDNode>(CC)->get()) {
5170 case ISD::SETEQ:
5171 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00005172 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005173 break;
5174 case ISD::SETNE:
5175 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00005176 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005177 break;
5178 case ISD::SETGE:
5179 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00005180 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005181 break;
5182 case ISD::SETLT:
5183 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00005184 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005185 break;
5186 case ISD::SETLE:
5187 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00005188 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005189 break;
5190 case ISD::SETGT:
5191 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00005192 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005193 break;
5194 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00005195 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5196 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005197 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00005198 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005199 break;
5200 default:
Evan Cheng56966222007-01-12 02:11:51 +00005201 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005202 switch (cast<CondCodeSDNode>(CC)->get()) {
5203 case ISD::SETONE:
5204 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00005205 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005206 // Fallthrough
5207 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00005208 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005209 break;
5210 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00005211 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005212 break;
5213 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00005214 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005215 break;
5216 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00005217 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005218 break;
Evan Cheng56966222007-01-12 02:11:51 +00005219 case ISD::SETUEQ:
5220 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00005221 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005222 default: assert(0 && "Unsupported FP setcc!");
5223 }
5224 }
Duncan Sandsf9516202008-06-30 10:19:09 +00005225
Dan Gohman475871a2008-07-27 21:46:04 +00005226 SDValue Dummy;
5227 SDValue Ops[2] = { LHS, RHS };
Dale Johannesenbb5da912009-02-02 20:41:04 +00005228 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2, dl).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005229 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00005230 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00005231 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00005232 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Dale Johannesenbb5da912009-02-02 20:41:04 +00005233 Tmp1 = DAG.getNode(ISD::SETCC, dl,
Duncan Sands5480c042009-01-01 15:52:00 +00005234 TLI.getSetCCResultType(Tmp1.getValueType()),
5235 Tmp1, Tmp2, CC);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005236 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2, dl).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005237 false /*sign irrelevant*/, Dummy);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005238 Tmp2 = DAG.getNode(ISD::SETCC, dl,
Duncan Sands5480c042009-01-01 15:52:00 +00005239 TLI.getSetCCResultType(LHS.getValueType()), LHS,
5240 Tmp2, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Dale Johannesenbb5da912009-02-02 20:41:04 +00005241 Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman475871a2008-07-27 21:46:04 +00005242 Tmp2 = SDValue();
Evan Cheng2b49c502006-12-15 02:59:56 +00005243 }
Evan Cheng21cacc42008-07-07 07:18:09 +00005244 LHS = LegalizeOp(Tmp1);
Evan Cheng2b49c502006-12-15 02:59:56 +00005245 RHS = Tmp2;
5246 return;
5247 }
5248
Dan Gohman475871a2008-07-27 21:46:04 +00005249 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005250 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005251 ExpandOp(RHS, RHSLo, RHSHi);
5252 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5253
5254 if (VT==MVT::ppcf128) {
5255 // FIXME: This generated code sucks. We want to generate
Dale Johannesene2f20832008-09-12 00:30:56 +00005256 // FCMPU crN, hi1, hi2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005257 // BNE crN, L:
Dale Johannesene2f20832008-09-12 00:30:56 +00005258 // FCMPU crN, lo1, lo2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005259 // The following can be improved, but not that much.
Dale Johannesenbb5da912009-02-02 20:41:04 +00005260 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005261 LHSHi, RHSHi, ISD::SETOEQ);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005262 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005263 LHSLo, RHSLo, CCCode);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005264 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
5265 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005266 LHSHi, RHSHi, ISD::SETUNE);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005267 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005268 LHSHi, RHSHi, CCCode);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005269 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
5270 Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00005271 Tmp2 = SDValue();
Dale Johannesen638ccd52007-10-06 01:24:11 +00005272 break;
5273 }
5274
5275 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005276 case ISD::SETEQ:
5277 case ISD::SETNE:
5278 if (RHSLo == RHSHi)
5279 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5280 if (RHSCST->isAllOnesValue()) {
5281 // Comparison to -1.
Dale Johannesenbb5da912009-02-02 20:41:04 +00005282 Tmp1 = DAG.getNode(ISD::AND, dl,LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00005283 Tmp2 = RHSLo;
5284 break;
5285 }
5286
Dale Johannesenbb5da912009-02-02 20:41:04 +00005287 Tmp1 = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
5288 Tmp2 = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
5289 Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00005290 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5291 break;
5292 default:
5293 // If this is a comparison of the sign bit, just look at the top part.
5294 // X > -1, x < 0
5295 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5296 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005297 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00005298 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5299 CST->isAllOnesValue())) { // X > -1
5300 Tmp1 = LHSHi;
5301 Tmp2 = RHSHi;
5302 break;
5303 }
5304
5305 // FIXME: This generated code sucks.
5306 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00005307 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005308 default: assert(0 && "Unknown integer setcc!");
5309 case ISD::SETLT:
5310 case ISD::SETULT: LowCC = ISD::SETULT; break;
5311 case ISD::SETGT:
5312 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5313 case ISD::SETLE:
5314 case ISD::SETULE: LowCC = ISD::SETULE; break;
5315 case ISD::SETGE:
5316 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5317 }
5318
5319 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5320 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5321 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5322
5323 // NOTE: on targets without efficient SELECT of bools, we can always use
5324 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00005325 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Duncan Sands5480c042009-01-01 15:52:00 +00005326 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00005327 LHSLo, RHSLo, LowCC, false, DagCombineInfo, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00005328 if (!Tmp1.getNode())
Dale Johannesenbb5da912009-02-02 20:41:04 +00005329 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005330 LHSLo, RHSLo, LowCC);
5331 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00005332 LHSHi, RHSHi, CCCode, false, DagCombineInfo, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00005333 if (!Tmp2.getNode())
Dale Johannesenbb5da912009-02-02 20:41:04 +00005334 Tmp2 = DAG.getNode(ISD::SETCC, dl,
Duncan Sands5480c042009-01-01 15:52:00 +00005335 TLI.getSetCCResultType(LHSHi.getValueType()),
5336 LHSHi, RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00005337
Gabor Greifba36cb52008-08-28 21:40:38 +00005338 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5339 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman002e5d02008-03-13 22:13:53 +00005340 if ((Tmp1C && Tmp1C->isNullValue()) ||
5341 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00005342 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5343 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00005344 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00005345 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5346 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5347 // low part is known false, returns high part.
5348 // For LE / GE, if high part is known false, ignore the low part.
5349 // For LT / GT, if high part is known true, ignore the low part.
5350 Tmp1 = Tmp2;
Dan Gohman475871a2008-07-27 21:46:04 +00005351 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005352 } else {
Duncan Sands5480c042009-01-01 15:52:00 +00005353 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5354 LHSHi, RHSHi, ISD::SETEQ, false,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00005355 DagCombineInfo, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00005356 if (!Result.getNode())
Dale Johannesenbb5da912009-02-02 20:41:04 +00005357 Result=DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005358 LHSHi, RHSHi, ISD::SETEQ);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005359 Result = LegalizeOp(DAG.getNode(ISD::SELECT, dl, Tmp1.getValueType(),
Evan Cheng2e677812007-02-08 22:16:19 +00005360 Result, Tmp1, Tmp2));
5361 Tmp1 = Result;
Dan Gohman475871a2008-07-27 21:46:04 +00005362 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005363 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005364 }
5365 }
Evan Cheng2b49c502006-12-15 02:59:56 +00005366 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005367 LHS = Tmp1;
5368 RHS = Tmp2;
5369}
5370
Evan Cheng7f042682008-10-15 02:05:31 +00005371/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5372/// condition code CC on the current target. This routine assumes LHS and rHS
5373/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5374/// illegal condition code into AND / OR of multiple SETCC values.
5375void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5376 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00005377 SDValue &CC,
5378 DebugLoc dl) {
Evan Cheng7f042682008-10-15 02:05:31 +00005379 MVT OpVT = LHS.getValueType();
5380 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5381 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5382 default: assert(0 && "Unknown condition code action!");
5383 case TargetLowering::Legal:
5384 // Nothing to do.
5385 break;
5386 case TargetLowering::Expand: {
5387 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5388 unsigned Opc = 0;
5389 switch (CCCode) {
5390 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00005391 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5392 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5393 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5394 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5395 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5396 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5397 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5398 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5399 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5400 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5401 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5402 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00005403 // FIXME: Implement more expansions.
5404 }
5405
Dale Johannesenbb5da912009-02-02 20:41:04 +00005406 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
5407 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
5408 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00005409 RHS = SDValue();
5410 CC = SDValue();
5411 break;
5412 }
5413 }
5414}
5415
Chris Lattner1401d152008-01-16 07:45:30 +00005416/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5417/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5418/// a load from the stack slot to DestVT, extending it if needed.
5419/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005420SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5421 MVT SlotVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005422 MVT DestVT,
5423 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00005424 // Create the stack frame object.
Mon P Wang364d73d2008-07-05 20:40:31 +00005425 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5426 SrcOp.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00005427 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang364d73d2008-07-05 20:40:31 +00005428
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005429 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005430 int SPFI = StackPtrFI->getIndex();
Dan Gohman15b38302009-01-29 21:02:43 +00005431 const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
5432
Duncan Sands83ec4b62008-06-06 12:08:01 +00005433 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5434 unsigned SlotSize = SlotVT.getSizeInBits();
5435 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang364d73d2008-07-05 20:40:31 +00005436 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5437 DestVT.getTypeForMVT());
Chris Lattner35481892005-12-23 00:16:34 +00005438
Chris Lattner1401d152008-01-16 07:45:30 +00005439 // Emit a store to the stack slot. Use a truncstore if the input value is
5440 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00005441 SDValue Store;
Mon P Wang364d73d2008-07-05 20:40:31 +00005442
Chris Lattner1401d152008-01-16 07:45:30 +00005443 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00005444 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00005445 SV, 0, SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005446 else {
5447 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00005448 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00005449 SV, 0, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005450 }
5451
Chris Lattner35481892005-12-23 00:16:34 +00005452 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00005453 if (SlotSize == DestSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00005454 return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, DestAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005455
5456 assert(SlotSize < DestSize && "Unknown extension!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00005457 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT,
Mon P Wang364d73d2008-07-05 20:40:31 +00005458 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00005459}
5460
Dan Gohman475871a2008-07-27 21:46:04 +00005461SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005462 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00005463 // Create a vector sized/aligned stack slot, store the value to element #0,
5464 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005465 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00005466
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005467 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005468 int SPFI = StackPtrFI->getIndex();
5469
Dale Johannesen8a782a22009-02-02 22:12:50 +00005470 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(0),
5471 StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005472 PseudoSourceValue::getFixedStack(SPFI), 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00005473 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005474 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00005475}
5476
5477
Chris Lattnerce872152006-03-19 06:31:19 +00005478/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00005479/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00005480SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Chris Lattnerce872152006-03-19 06:31:19 +00005481
5482 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00005483 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00005484 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00005485 bool isOnlyLowElement = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005486 SDValue SplatValue = Node->getOperand(0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00005487 DebugLoc dl = Node->getDebugLoc();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005488
Dan Gohman475871a2008-07-27 21:46:04 +00005489 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005490 // and use a bitmask instead of a list of elements.
Dan Gohman475871a2008-07-27 21:46:04 +00005491 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00005492 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005493 bool isConstant = true;
5494 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5495 SplatValue.getOpcode() != ISD::UNDEF)
5496 isConstant = false;
5497
Evan Cheng033e6812006-03-24 01:17:21 +00005498 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005499 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00005500 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00005501 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00005502 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00005503 if (SplatValue != V)
Dan Gohman475871a2008-07-27 21:46:04 +00005504 SplatValue = SDValue(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005505
5506 // If this isn't a constant element or an undef, we can't use a constant
5507 // pool load.
5508 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5509 V.getOpcode() != ISD::UNDEF)
5510 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00005511 }
5512
5513 if (isOnlyLowElement) {
5514 // If the low element is an undef too, then this whole things is an undef.
5515 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
Dale Johannesen8a782a22009-02-02 22:12:50 +00005516 return DAG.getNode(ISD::UNDEF, dl, Node->getValueType(0));
Chris Lattnerce872152006-03-19 06:31:19 +00005517 // Otherwise, turn this into a scalar_to_vector node.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005518 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, Node->getValueType(0),
Chris Lattnerce872152006-03-19 06:31:19 +00005519 Node->getOperand(0));
5520 }
5521
Chris Lattner2eb86532006-03-24 07:29:17 +00005522 // If all elements are constants, create a load from the constant pool.
5523 if (isConstant) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005524 MVT VT = Node->getValueType(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005525 std::vector<Constant*> CV;
5526 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5527 if (ConstantFPSDNode *V =
5528 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005529 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005530 } else if (ConstantSDNode *V =
Chris Lattner02a260a2008-04-20 00:41:09 +00005531 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005532 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005533 } else {
5534 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner02a260a2008-04-20 00:41:09 +00005535 const Type *OpNTy =
Duncan Sands83ec4b62008-06-06 12:08:01 +00005536 Node->getOperand(0).getValueType().getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00005537 CV.push_back(UndefValue::get(OpNTy));
5538 }
5539 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00005540 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005541 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005542 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00005543 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005544 PseudoSourceValue::getConstantPool(), 0,
5545 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00005546 }
5547
Gabor Greifba36cb52008-08-28 21:40:38 +00005548 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00005549 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005550 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman475871a2008-07-27 21:46:04 +00005551 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5552 std::vector<SDValue> ZeroVec(NumElems, Zero);
Dale Johannesen8a782a22009-02-02 22:12:50 +00005553 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005554 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00005555
5556 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005557 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00005558 // Get the splatted value into the low element of a vector register.
Dan Gohman475871a2008-07-27 21:46:04 +00005559 SDValue LowValVec =
Dale Johannesen8a782a22009-02-02 22:12:50 +00005560 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5561 Node->getValueType(0), SplatValue);
Chris Lattner87100e02006-03-20 01:52:29 +00005562
5563 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Dale Johannesen8a782a22009-02-02 22:12:50 +00005564 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl,
5565 Node->getValueType(0), LowValVec,
Chris Lattner87100e02006-03-20 01:52:29 +00005566 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5567 SplatMask);
5568 }
5569 }
5570
Evan Cheng033e6812006-03-24 01:17:21 +00005571 // If there are only two unique elements, we may be able to turn this into a
5572 // vector shuffle.
5573 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005574 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00005575 SDValue Val1 = Node->getOperand(1);
5576 SDValue Val2;
5577 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005578 if (MI->first != Val1)
5579 Val2 = MI->first;
5580 else
5581 Val2 = (++MI)->first;
5582
5583 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5584 // vector shuffle has the undef vector on the RHS.
5585 if (Val1.getOpcode() == ISD::UNDEF)
5586 std::swap(Val1, Val2);
5587
Evan Cheng033e6812006-03-24 01:17:21 +00005588 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005589 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5590 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005591 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005592
5593 // Set elements of the shuffle mask for Val1.
5594 std::vector<unsigned> &Val1Elts = Values[Val1];
5595 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5596 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5597
5598 // Set elements of the shuffle mask for Val2.
5599 std::vector<unsigned> &Val2Elts = Values[Val2];
5600 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5601 if (Val2.getOpcode() != ISD::UNDEF)
5602 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5603 else
Dale Johannesen8a782a22009-02-02 22:12:50 +00005604 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, dl, MaskEltVT);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005605
Dale Johannesen8a782a22009-02-02 22:12:50 +00005606 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005607 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005608
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005609 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005610 if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR,
5611 Node->getValueType(0)) &&
Chris Lattner4352cc92006-04-04 17:23:26 +00005612 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005613 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,Node->getValueType(0), Val1);
5614 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,Node->getValueType(0), Val2);
Dan Gohman475871a2008-07-27 21:46:04 +00005615 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng033e6812006-03-24 01:17:21 +00005616
5617 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Dale Johannesen8a782a22009-02-02 22:12:50 +00005618 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl,Node->getValueType(0), Ops, 3);
Evan Cheng033e6812006-03-24 01:17:21 +00005619 }
5620 }
Chris Lattnerce872152006-03-19 06:31:19 +00005621
5622 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5623 // aligned object on the stack, store each element into it, then load
5624 // the result as a vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005625 MVT VT = Node->getValueType(0);
Chris Lattnerce872152006-03-19 06:31:19 +00005626 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00005627 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohman15b38302009-01-29 21:02:43 +00005628 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
5629 const Value *SV = PseudoSourceValue::getFixedStack(FI);
5630
Chris Lattnerce872152006-03-19 06:31:19 +00005631 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00005632 SmallVector<SDValue, 8> Stores;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005633 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005634 // Store (in the right endianness) the elements to memory.
5635 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5636 // Ignore undef elements.
5637 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5638
Chris Lattner841c8822006-03-22 01:46:54 +00005639 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005640
Dan Gohman475871a2008-07-27 21:46:04 +00005641 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00005642 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
Chris Lattnerce872152006-03-19 06:31:19 +00005643
Dale Johannesen8a782a22009-02-02 22:12:50 +00005644 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
5645 Idx, SV, Offset));
Chris Lattnerce872152006-03-19 06:31:19 +00005646 }
5647
Dan Gohman475871a2008-07-27 21:46:04 +00005648 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00005649 if (!Stores.empty()) // Not all undef elements?
Dale Johannesen8a782a22009-02-02 22:12:50 +00005650 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005651 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005652 else
5653 StoreChain = DAG.getEntryNode();
5654
5655 // Result is a load from the stack slot.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005656 return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005657}
5658
Chris Lattner5b359c62005-04-02 04:00:59 +00005659void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman475871a2008-07-27 21:46:04 +00005660 SDValue Op, SDValue Amt,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005661 SDValue &Lo, SDValue &Hi,
5662 DebugLoc dl) {
Chris Lattner5b359c62005-04-02 04:00:59 +00005663 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00005664 SDValue LHSL, LHSH;
Chris Lattner5b359c62005-04-02 04:00:59 +00005665 ExpandOp(Op, LHSL, LHSH);
5666
Dan Gohman475871a2008-07-27 21:46:04 +00005667 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005668 MVT VT = LHSL.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00005669 Lo = DAG.getNode(NodeOp, dl, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005670 Hi = Lo.getValue(1);
5671}
5672
5673
Chris Lattnere34b3962005-01-19 04:19:40 +00005674/// ExpandShift - Try to find a clever way to expand this shift operation out to
5675/// smaller elements. If we can't find a way that is more efficient than a
5676/// libcall on this target, return false. Otherwise, return true with the
5677/// low-parts expanded into Lo and Hi.
Dan Gohman475871a2008-07-27 21:46:04 +00005678bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005679 SDValue &Lo, SDValue &Hi,
5680 DebugLoc dl) {
Chris Lattnere34b3962005-01-19 04:19:40 +00005681 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5682 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005683
Duncan Sands83ec4b62008-06-06 12:08:01 +00005684 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman475871a2008-07-27 21:46:04 +00005685 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005686 MVT ShTy = ShAmt.getValueType();
5687 unsigned ShBits = ShTy.getSizeInBits();
5688 unsigned VTBits = Op.getValueType().getSizeInBits();
5689 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005690
Chris Lattner7a3c8552007-10-14 20:35:12 +00005691 // Handle the case when Amt is an immediate.
Gabor Greifba36cb52008-08-28 21:40:38 +00005692 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005693 unsigned Cst = CN->getZExtValue();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005694 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005695 SDValue InL, InH;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005696 ExpandOp(Op, InL, InH);
5697 switch(Opc) {
5698 case ISD::SHL:
5699 if (Cst > VTBits) {
5700 Lo = DAG.getConstant(0, NVT);
5701 Hi = DAG.getConstant(0, NVT);
5702 } else if (Cst > NVTBits) {
5703 Lo = DAG.getConstant(0, NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00005704 Hi = DAG.getNode(ISD::SHL, dl,
5705 NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005706 } else if (Cst == NVTBits) {
5707 Lo = DAG.getConstant(0, NVT);
5708 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005709 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005710 Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, DAG.getConstant(Cst, ShTy));
5711 Hi = DAG.getNode(ISD::OR, dl, NVT,
5712 DAG.getNode(ISD::SHL, dl, NVT, InH, DAG.getConstant(Cst, ShTy)),
5713 DAG.getNode(ISD::SRL, dl, NVT, InL,
5714 DAG.getConstant(NVTBits-Cst, ShTy)));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005715 }
5716 return true;
5717 case ISD::SRL:
5718 if (Cst > VTBits) {
5719 Lo = DAG.getConstant(0, NVT);
5720 Hi = DAG.getConstant(0, NVT);
5721 } else if (Cst > NVTBits) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005722 Lo = DAG.getNode(ISD::SRL, dl, NVT,
5723 InH, DAG.getConstant(Cst-NVTBits,ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005724 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005725 } else if (Cst == NVTBits) {
5726 Lo = InH;
5727 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005728 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005729 Lo = DAG.getNode(ISD::OR, dl, NVT,
5730 DAG.getNode(ISD::SRL, dl, NVT, InL, DAG.getConstant(Cst, ShTy)),
5731 DAG.getNode(ISD::SHL, dl, NVT, InH,
5732 DAG.getConstant(NVTBits-Cst, ShTy)));
5733 Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, DAG.getConstant(Cst, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005734 }
5735 return true;
5736 case ISD::SRA:
5737 if (Cst > VTBits) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005738 Hi = Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005739 DAG.getConstant(NVTBits-1, ShTy));
5740 } else if (Cst > NVTBits) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005741 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005742 DAG.getConstant(Cst-NVTBits, ShTy));
Dale Johannesen8a782a22009-02-02 22:12:50 +00005743 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005744 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005745 } else if (Cst == NVTBits) {
5746 Lo = InH;
Dale Johannesen8a782a22009-02-02 22:12:50 +00005747 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005748 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005749 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005750 Lo = DAG.getNode(ISD::OR, dl, NVT,
5751 DAG.getNode(ISD::SRL, dl, NVT, InL, DAG.getConstant(Cst, ShTy)),
5752 DAG.getNode(ISD::SHL, dl,
5753 NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5754 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, DAG.getConstant(Cst, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005755 }
5756 return true;
5757 }
5758 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005759
5760 // Okay, the shift amount isn't constant. However, if we can tell that it is
5761 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005762 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5763 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005764 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005765
Dan Gohman9e255b72008-02-22 01:12:31 +00005766 // If we know that if any of the high bits of the shift amount are one, then
5767 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005768 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005769 // Mask out the high bit, which we know is set.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005770 Amt = DAG.getNode(ISD::AND, dl, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005771 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005772
5773 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005774 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005775 ExpandOp(Op, InL, InH);
5776 switch(Opc) {
5777 case ISD::SHL:
5778 Lo = DAG.getConstant(0, NVT); // Low part is zero.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005779 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005780 return true;
5781 case ISD::SRL:
5782 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005783 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005784 return true;
5785 case ISD::SRA:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005786 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005787 DAG.getConstant(NVTBits-1, Amt.getValueType()));
Dale Johannesen8a782a22009-02-02 22:12:50 +00005788 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005789 return true;
5790 }
5791 }
5792
Dan Gohman9e255b72008-02-22 01:12:31 +00005793 // If we know that the high bits of the shift amount are all zero, then we can
5794 // do this as a couple of simple shifts.
5795 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005796 // Compute 32-amt.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005797 SDValue Amt2 = DAG.getNode(ISD::SUB, dl, Amt.getValueType(),
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005798 DAG.getConstant(NVTBits, Amt.getValueType()),
5799 Amt);
5800
5801 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005802 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005803 ExpandOp(Op, InL, InH);
5804 switch(Opc) {
5805 case ISD::SHL:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005806 Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
5807 Hi = DAG.getNode(ISD::OR, dl, NVT,
5808 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
5809 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt2));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005810 return true;
5811 case ISD::SRL:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005812 Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
5813 Lo = DAG.getNode(ISD::OR, dl, NVT,
5814 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
5815 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005816 return true;
5817 case ISD::SRA:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005818 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
5819 Lo = DAG.getNode(ISD::OR, dl, NVT,
5820 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
5821 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005822 return true;
5823 }
5824 }
5825
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005826 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005827}
Chris Lattner77e77a62005-01-21 06:05:23 +00005828
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005829
Chris Lattner77e77a62005-01-21 06:05:23 +00005830// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5831// does not fit into a register, return the lo part and set the hi part to the
5832// by-reg argument. If it does fit into a single register, return the result
5833// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00005834SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5835 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005836 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5837 // The input chain to this libcall is the entry node of the function.
5838 // Legalizing the call will automatically add the previous call to the
5839 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00005840 SDValue InChain = DAG.getEntryNode();
Chris Lattner6831a812006-02-13 09:18:02 +00005841
Chris Lattner77e77a62005-01-21 06:05:23 +00005842 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005843 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005844 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005845 MVT ArgVT = Node->getOperand(i).getValueType();
5846 const Type *ArgTy = ArgVT.getTypeForMVT();
Reid Spencer47857812006-12-31 05:55:36 +00005847 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005848 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005849 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005850 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005851 }
Bill Wendling056292f2008-09-16 21:48:12 +00005852 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00005853 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005854
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005855 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005856 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman475871a2008-07-27 21:46:04 +00005857 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005858 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005859 CallingConv::C, false, Callee, Args, DAG,
5860 Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005861
Chris Lattner6831a812006-02-13 09:18:02 +00005862 // Legalize the call sequence, starting with the chain. This will advance
5863 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5864 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5865 LegalizeOp(CallInfo.second);
Dan Gohman475871a2008-07-27 21:46:04 +00005866 SDValue Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005867 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005868 default: assert(0 && "Unknown thing");
5869 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005870 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005871 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005872 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005873 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005874 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005875 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005876 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005877}
5878
Dan Gohman7f8613e2008-08-14 20:04:46 +00005879/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5880///
5881SDValue SelectionDAGLegalize::
Dale Johannesenaf435272009-02-02 19:03:57 +00005882LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op,
5883 DebugLoc dl) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00005884 bool isCustom = false;
5885 SDValue Tmp1;
5886 switch (getTypeAction(Op.getValueType())) {
5887 case Legal:
5888 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5889 Op.getValueType())) {
5890 default: assert(0 && "Unknown operation action!");
5891 case TargetLowering::Custom:
5892 isCustom = true;
5893 // FALLTHROUGH
5894 case TargetLowering::Legal:
5895 Tmp1 = LegalizeOp(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00005896 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005897 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5898 else
Dale Johannesenaf435272009-02-02 19:03:57 +00005899 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, dl,
Dan Gohman7f8613e2008-08-14 20:04:46 +00005900 DestTy, Tmp1);
5901 if (isCustom) {
5902 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005903 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005904 }
5905 break;
5906 case TargetLowering::Expand:
Dale Johannesenaf435272009-02-02 19:03:57 +00005907 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy, dl);
Dan Gohman7f8613e2008-08-14 20:04:46 +00005908 break;
5909 case TargetLowering::Promote:
Dale Johannesenaf435272009-02-02 19:03:57 +00005910 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned, dl);
Dan Gohman7f8613e2008-08-14 20:04:46 +00005911 break;
5912 }
5913 break;
5914 case Expand:
Dale Johannesenaf435272009-02-02 19:03:57 +00005915 Result = ExpandIntToFP(isSigned, DestTy, Op, dl) ;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005916 break;
5917 case Promote:
5918 Tmp1 = PromoteOp(Op);
5919 if (isSigned) {
Dale Johannesenaf435272009-02-02 19:03:57 +00005920 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Tmp1.getValueType(),
Dan Gohman7f8613e2008-08-14 20:04:46 +00005921 Tmp1, DAG.getValueType(Op.getValueType()));
5922 } else {
Dale Johannesenaf435272009-02-02 19:03:57 +00005923 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl,
Dan Gohman7f8613e2008-08-14 20:04:46 +00005924 Op.getValueType());
5925 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005926 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005927 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5928 else
Dale Johannesenaf435272009-02-02 19:03:57 +00005929 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, dl,
Dan Gohman7f8613e2008-08-14 20:04:46 +00005930 DestTy, Tmp1);
5931 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5932 break;
5933 }
5934 return Result;
5935}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005936
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005937/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5938///
Dan Gohman475871a2008-07-27 21:46:04 +00005939SDValue SelectionDAGLegalize::
Dale Johannesenaf435272009-02-02 19:03:57 +00005940ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005941 MVT SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005942 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005943
Dan Gohman7f8613e2008-08-14 20:04:46 +00005944 // Expand unsupported int-to-fp vector casts by unrolling them.
5945 if (DestTy.isVector()) {
5946 if (!ExpandSource)
5947 return LegalizeOp(UnrollVectorOp(Source));
5948 MVT DestEltTy = DestTy.getVectorElementType();
5949 if (DestTy.getVectorNumElements() == 1) {
5950 SDValue Scalar = ScalarizeVectorOp(Source);
5951 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +00005952 DestEltTy, Scalar, dl);
5953 return DAG.getNode(ISD::BUILD_VECTOR, dl, DestTy, Result);
Dan Gohman7f8613e2008-08-14 20:04:46 +00005954 }
5955 SDValue Lo, Hi;
5956 SplitVectorOp(Source, Lo, Hi);
5957 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5958 DestTy.getVectorNumElements() / 2);
Dale Johannesenaf435272009-02-02 19:03:57 +00005959 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy,
5960 Lo, dl);
5961 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy,
5962 Hi, dl);
5963 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, DestTy, LoResult,
Evan Chengefa53392008-10-13 18:46:18 +00005964 HiResult));
Dan Gohman7f8613e2008-08-14 20:04:46 +00005965 }
5966
Evan Cheng9845eb52008-04-01 02:18:22 +00005967 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5968 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005969 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005970 // incoming integer is set. To handle this, we dynamically test to see if
5971 // it is set, and, if so, add a fudge factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005972 SDValue Hi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005973 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005974 SDValue Lo;
Dan Gohman034f60e2008-03-11 01:59:03 +00005975 ExpandOp(Source, Lo, Hi);
Dale Johannesenaf435272009-02-02 19:03:57 +00005976 Source = DAG.getNode(ISD::BUILD_PAIR, dl, SourceVT, Lo, Hi);
Dan Gohman034f60e2008-03-11 01:59:03 +00005977 } else {
5978 // The comparison for the sign bit will use the entire operand.
5979 Hi = Source;
5980 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005981
Dale Johannesen53997b02008-11-04 20:52:49 +00005982 // Check to see if the target has a custom way to lower this. If so, use
5983 // it. (Note we've already expanded the operand in this case.)
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005984 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5985 default: assert(0 && "This action not implemented for this operation!");
5986 case TargetLowering::Legal:
5987 case TargetLowering::Expand:
5988 break; // This case is handled below.
5989 case TargetLowering::Custom: {
5990 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5991 Source), DAG);
5992 if (NV.getNode())
5993 return LegalizeOp(NV);
5994 break; // The target decided this was legal after all
5995 }
5996 }
5997
Chris Lattner66de05b2005-05-13 04:45:13 +00005998 // If this is unsigned, and not supported, first perform the conversion to
5999 // signed, then adjust the result if the sign bit is set.
Dale Johannesenaf435272009-02-02 19:03:57 +00006000 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source, dl);
Chris Lattner66de05b2005-05-13 04:45:13 +00006001
Dale Johannesenaf435272009-02-02 19:03:57 +00006002 SDValue SignSet = DAG.getSetCC(dl,
6003 TLI.getSetCCResultType(Hi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00006004 Hi, DAG.getConstant(0, Hi.getValueType()),
6005 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00006006 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00006007 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattnere9c35e72005-04-13 05:09:42 +00006008 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00006009 uint64_t FF = 0x5f800000ULL;
6010 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohman34bc1782008-03-05 02:07:31 +00006011 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00006012
Dan Gohman475871a2008-07-27 21:46:04 +00006013 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00006014 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00006015 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00006016 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00006017 SDValue FudgeInReg;
Chris Lattnere9c35e72005-04-13 05:09:42 +00006018 if (DestTy == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00006019 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00006020 PseudoSourceValue::getConstantPool(), 0,
6021 false, Alignment);
Duncan Sands8e4eb092008-06-08 20:54:56 +00006022 else if (DestTy.bitsGT(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006023 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenaf435272009-02-02 19:03:57 +00006024 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, dl, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00006025 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00006026 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00006027 MVT::f32, false, Alignment);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006028 else
6029 assert(0 && "Unexpected conversion");
6030
Duncan Sands83ec4b62008-06-06 12:08:01 +00006031 MVT SCVT = SignedConv.getValueType();
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006032 if (SCVT != DestTy) {
6033 // Destination type needs to be expanded as well. The FADD now we are
6034 // constructing will be expanded into a libcall.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006035 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
6036 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dale Johannesenaf435272009-02-02 19:03:57 +00006037 SignedConv = DAG.getNode(ISD::BUILD_PAIR, dl, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006038 SignedConv, SignedConv.getValue(1));
6039 }
Dale Johannesenaf435272009-02-02 19:03:57 +00006040 SignedConv = DAG.getNode(ISD::BIT_CONVERT, dl, DestTy, SignedConv);
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006041 }
Dale Johannesenaf435272009-02-02 19:03:57 +00006042 return DAG.getNode(ISD::FADD, dl, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00006043 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00006044
Chris Lattnera88a2602005-05-14 05:33:54 +00006045 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00006046 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00006047 default: assert(0 && "This action not implemented for this operation!");
6048 case TargetLowering::Legal:
6049 case TargetLowering::Expand:
6050 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00006051 case TargetLowering::Custom: {
Dale Johannesenaf435272009-02-02 19:03:57 +00006052 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, dl, DestTy,
Chris Lattner07dffd62005-08-26 00:14:16 +00006053 Source), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006054 if (NV.getNode())
Chris Lattner07dffd62005-08-26 00:14:16 +00006055 return LegalizeOp(NV);
6056 break; // The target decided this was legal after all
6057 }
Chris Lattnera88a2602005-05-14 05:33:54 +00006058 }
6059
Chris Lattner13689e22005-05-12 07:00:44 +00006060 // Expand the source, then glue it back together for the call. We must expand
6061 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00006062 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00006063 SDValue SrcLo, SrcHi;
Dan Gohman034f60e2008-03-11 01:59:03 +00006064 ExpandOp(Source, SrcLo, SrcHi);
Dale Johannesenaf435272009-02-02 19:03:57 +00006065 Source = DAG.getNode(ISD::BUILD_PAIR, dl, SourceVT, SrcLo, SrcHi);
Dan Gohman034f60e2008-03-11 01:59:03 +00006066 }
Chris Lattner13689e22005-05-12 07:00:44 +00006067
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006068 RTLIB::Libcall LC = isSigned ?
6069 RTLIB::getSINTTOFP(SourceVT, DestTy) :
6070 RTLIB::getUINTTOFP(SourceVT, DestTy);
6071 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
6072
Dale Johannesenaf435272009-02-02 19:03:57 +00006073 Source = DAG.getNode(ISD::SINT_TO_FP, dl, DestTy, Source);
Dan Gohman475871a2008-07-27 21:46:04 +00006074 SDValue HiPart;
Gabor Greifba36cb52008-08-28 21:40:38 +00006075 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
6076 if (Result.getValueType() != DestTy && HiPart.getNode())
Dale Johannesenaf435272009-02-02 19:03:57 +00006077 Result = DAG.getNode(ISD::BUILD_PAIR, dl, DestTy, Result, HiPart);
Dan Gohmana2e94852008-03-10 23:03:31 +00006078 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00006079}
Misha Brukmanedf128a2005-04-21 22:36:52 +00006080
Chris Lattner22cde6a2006-01-28 08:25:58 +00006081/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
6082/// INT_TO_FP operation of the specified operand when the target requests that
6083/// we expand it. At this point, we know that the result and operand types are
6084/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00006085SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
6086 SDValue Op0,
Dale Johannesenaf435272009-02-02 19:03:57 +00006087 MVT DestVT,
6088 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006089 if (Op0.getValueType() == MVT::i32) {
6090 // simple 32-bit [signed|unsigned] integer to float/double expansion
6091
Chris Lattner23594d42008-01-16 07:03:22 +00006092 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00006093 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner23594d42008-01-16 07:03:22 +00006094
Chris Lattner22cde6a2006-01-28 08:25:58 +00006095 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00006096 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00006097 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00006098 SDValue Hi = StackSlot;
Dale Johannesenaf435272009-02-02 19:03:57 +00006099 SDValue Lo = DAG.getNode(ISD::ADD, dl,
6100 TLI.getPointerTy(), StackSlot,WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00006101 if (TLI.isLittleEndian())
6102 std::swap(Hi, Lo);
6103
Chris Lattner22cde6a2006-01-28 08:25:58 +00006104 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00006105 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006106 if (isSigned) {
6107 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00006108 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dale Johannesenaf435272009-02-02 19:03:57 +00006109 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006110 } else {
6111 Op0Mapped = Op0;
6112 }
6113 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00006114 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00006115 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006116 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00006117 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006118 // store the hi of the constructed double - biased exponent
Dale Johannesenaf435272009-02-02 19:03:57 +00006119 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006120 // load the constructed double
Dale Johannesenaf435272009-02-02 19:03:57 +00006121 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006122 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00006123 SDValue Bias = DAG.getConstantFP(isSigned ?
Chris Lattner22cde6a2006-01-28 08:25:58 +00006124 BitsToDouble(0x4330000080000000ULL)
6125 : BitsToDouble(0x4330000000000000ULL),
6126 MVT::f64);
6127 // subtract the bias
Dale Johannesenaf435272009-02-02 19:03:57 +00006128 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006129 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00006130 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006131 // handle final rounding
6132 if (DestVT == MVT::f64) {
6133 // do nothing
6134 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006135 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00006136 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00006137 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00006138 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00006139 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006140 }
6141 return Result;
6142 }
6143 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesenaf435272009-02-02 19:03:57 +00006144 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006145
Dale Johannesenaf435272009-02-02 19:03:57 +00006146 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00006147 Op0, DAG.getConstant(0, Op0.getValueType()),
6148 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00006149 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00006150 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006151 SignSet, Four, Zero);
6152
6153 // If the sign bit of the integer is set, the large number will be treated
6154 // as a negative number. To counteract this, the dynamic code adds an
6155 // offset depending on the data type.
6156 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006157 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006158 default: assert(0 && "Unsupported integer type!");
6159 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6160 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6161 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6162 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6163 }
6164 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00006165 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006166
Dan Gohman475871a2008-07-27 21:46:04 +00006167 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00006168 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00006169 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00006170 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00006171 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006172 if (DestVT == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00006173 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00006174 PseudoSourceValue::getConstantPool(), 0,
6175 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006176 else {
Dan Gohman69de1932008-02-06 22:27:42 +00006177 FudgeInReg =
Dale Johannesenaf435272009-02-02 19:03:57 +00006178 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohman69de1932008-02-06 22:27:42 +00006179 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00006180 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00006181 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006182 }
6183
Dale Johannesenaf435272009-02-02 19:03:57 +00006184 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006185}
6186
6187/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6188/// *INT_TO_FP operation of the specified operand when the target requests that
6189/// we promote it. At this point, we know that the result and operand types are
6190/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6191/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00006192SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6193 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00006194 bool isSigned,
6195 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006196 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006197 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006198
6199 unsigned OpToUse = 0;
6200
6201 // Scan for the appropriate larger type to use.
6202 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006203 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6204 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006205
6206 // If the target supports SINT_TO_FP of this type, use it.
6207 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6208 default: break;
6209 case TargetLowering::Legal:
6210 if (!TLI.isTypeLegal(NewInTy))
6211 break; // Can't use this datatype.
6212 // FALL THROUGH.
6213 case TargetLowering::Custom:
6214 OpToUse = ISD::SINT_TO_FP;
6215 break;
6216 }
6217 if (OpToUse) break;
6218 if (isSigned) continue;
6219
6220 // If the target supports UINT_TO_FP of this type, use it.
6221 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6222 default: break;
6223 case TargetLowering::Legal:
6224 if (!TLI.isTypeLegal(NewInTy))
6225 break; // Can't use this datatype.
6226 // FALL THROUGH.
6227 case TargetLowering::Custom:
6228 OpToUse = ISD::UINT_TO_FP;
6229 break;
6230 }
6231 if (OpToUse) break;
6232
6233 // Otherwise, try a larger type.
6234 }
6235
6236 // Okay, we found the operation and type to use. Zero extend our input to the
6237 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00006238 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00006239 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00006240 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006241}
6242
6243/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6244/// FP_TO_*INT operation of the specified operand when the target requests that
6245/// we promote it. At this point, we know that the result and operand types are
6246/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6247/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00006248SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6249 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00006250 bool isSigned,
6251 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006252 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006253 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006254
6255 unsigned OpToUse = 0;
6256
6257 // Scan for the appropriate larger type to use.
6258 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006259 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6260 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006261
6262 // If the target supports FP_TO_SINT returning this type, use it.
6263 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6264 default: break;
6265 case TargetLowering::Legal:
6266 if (!TLI.isTypeLegal(NewOutTy))
6267 break; // Can't use this datatype.
6268 // FALL THROUGH.
6269 case TargetLowering::Custom:
6270 OpToUse = ISD::FP_TO_SINT;
6271 break;
6272 }
6273 if (OpToUse) break;
6274
6275 // If the target supports FP_TO_UINT of this type, use it.
6276 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6277 default: break;
6278 case TargetLowering::Legal:
6279 if (!TLI.isTypeLegal(NewOutTy))
6280 break; // Can't use this datatype.
6281 // FALL THROUGH.
6282 case TargetLowering::Custom:
6283 OpToUse = ISD::FP_TO_UINT;
6284 break;
6285 }
6286 if (OpToUse) break;
6287
6288 // Otherwise, try a larger type.
6289 }
6290
Chris Lattner27a6c732007-11-24 07:07:01 +00006291
6292 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00006293 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00006294
Chris Lattner27a6c732007-11-24 07:07:01 +00006295 // If the operation produces an invalid type, it must be custom lowered. Use
6296 // the target lowering hooks to expand it. Just keep the low part of the
6297 // expanded operation, we know that we're truncating anyway.
6298 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00006299 SmallVector<SDValue, 2> Results;
6300 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6301 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6302 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00006303 }
Duncan Sands126d9072008-07-04 11:47:58 +00006304
Chris Lattner27a6c732007-11-24 07:07:01 +00006305 // Truncate the result of the extended FP_TO_*INT operation to the desired
6306 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00006307 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006308}
6309
6310/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6311///
Dale Johannesen8a782a22009-02-02 22:12:50 +00006312SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006313 MVT VT = Op.getValueType();
6314 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00006315 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006316 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006317 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6318 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00006319 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
6320 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
6321 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006322 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00006323 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
6324 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
6325 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
6326 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
6327 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6328 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6329 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
6330 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
6331 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006332 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00006333 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
6334 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
6335 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
6336 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
6337 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
6338 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
6339 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
6340 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
6341 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6342 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6343 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6344 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6345 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6346 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6347 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
6348 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
6349 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
6350 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
6351 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
6352 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
6353 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006354 }
6355}
6356
6357/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6358///
Dale Johannesen8a782a22009-02-02 22:12:50 +00006359SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
6360 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006361 switch (Opc) {
6362 default: assert(0 && "Cannot expand this yet!");
6363 case ISD::CTPOP: {
6364 static const uint64_t mask[6] = {
6365 0x5555555555555555ULL, 0x3333333333333333ULL,
6366 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6367 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6368 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00006369 MVT VT = Op.getValueType();
6370 MVT ShVT = TLI.getShiftAmountTy();
6371 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006372 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6373 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006374 unsigned EltSize = VT.isVector() ?
6375 VT.getVectorElementType().getSizeInBits() : len;
6376 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
Dan Gohman475871a2008-07-27 21:46:04 +00006377 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006378 Op = DAG.getNode(ISD::ADD, dl, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6379 DAG.getNode(ISD::AND, dl, VT,
6380 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3),
6381 Tmp2));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006382 }
6383 return Op;
6384 }
6385 case ISD::CTLZ: {
6386 // for now, we do this:
6387 // x = x | (x >> 1);
6388 // x = x | (x >> 2);
6389 // ...
6390 // x = x | (x >>16);
6391 // x = x | (x >>32); // for 64-bit input
6392 // return popcount(~x);
6393 //
6394 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006395 MVT VT = Op.getValueType();
6396 MVT ShVT = TLI.getShiftAmountTy();
6397 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006398 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006399 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006400 Op = DAG.getNode(ISD::OR, dl, VT, Op,
6401 DAG.getNode(ISD::SRL, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006402 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00006403 Op = DAG.getNOT(dl, Op, VT);
6404 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006405 }
6406 case ISD::CTTZ: {
6407 // for now, we use: { return popcount(~x & (x - 1)); }
6408 // unless the target has ctlz but not ctpop, in which case we use:
6409 // { return 32 - nlz(~x & (x-1)); }
6410 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006411 MVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00006412 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
6413 DAG.getNOT(dl, Op, VT),
6414 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00006415 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006416 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006417 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
6418 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00006419 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006420 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00006421 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
6422 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006423 }
6424 }
6425}
Chris Lattnere34b3962005-01-19 04:19:40 +00006426
Dan Gohman475871a2008-07-27 21:46:04 +00006427/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner3e928bb2005-01-07 07:47:09 +00006428/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman929d3eb2008-10-01 15:07:49 +00006429/// LegalizedNodes map is filled in for any results that are not expanded, the
Chris Lattner3e928bb2005-01-07 07:47:09 +00006430/// ExpandedNodes map is filled in for any results that are expanded, and the
6431/// Lo/Hi values are returned.
Dan Gohman475871a2008-07-27 21:46:04 +00006432void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands83ec4b62008-06-06 12:08:01 +00006433 MVT VT = Op.getValueType();
6434 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greifba36cb52008-08-28 21:40:38 +00006435 SDNode *Node = Op.getNode();
Dale Johannesen8a782a22009-02-02 22:12:50 +00006436 DebugLoc dl = Node->getDebugLoc();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006437 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00006438 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands83ec4b62008-06-06 12:08:01 +00006439 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006440
Chris Lattner6fdcb252005-09-02 20:32:45 +00006441 // See if we already expanded it.
Dan Gohman475871a2008-07-27 21:46:04 +00006442 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00006443 = ExpandedNodes.find(Op);
6444 if (I != ExpandedNodes.end()) {
6445 Lo = I->second.first;
6446 Hi = I->second.second;
6447 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006448 }
6449
Chris Lattner3e928bb2005-01-07 07:47:09 +00006450 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006451 case ISD::CopyFromReg:
6452 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006453 case ISD::FP_ROUND_INREG:
6454 if (VT == MVT::ppcf128 &&
6455 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6456 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006457 SDValue SrcLo, SrcHi, Src;
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006458 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006459 Src = DAG.getNode(ISD::BUILD_PAIR, dl, VT, SrcLo, SrcHi);
Dan Gohman475871a2008-07-27 21:46:04 +00006460 SDValue Result = TLI.LowerOperation(
Dale Johannesen8a782a22009-02-02 22:12:50 +00006461 DAG.getNode(ISD::FP_ROUND_INREG, dl, VT, Src, Op.getOperand(1)), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006462 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6463 Lo = Result.getNode()->getOperand(0);
6464 Hi = Result.getNode()->getOperand(1);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006465 break;
6466 }
6467 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00006468 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006469#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006470 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006471#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00006472 assert(0 && "Do not know how to expand this operator!");
6473 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00006474 case ISD::EXTRACT_ELEMENT:
6475 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006476 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman1953ecb2008-02-27 01:52:30 +00006477 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00006478 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00006479 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen25f1d082007-10-31 00:32:36 +00006480 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6481 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6482 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006483 case ISD::UNDEF:
Dale Johannesen8a782a22009-02-02 22:12:50 +00006484 Lo = DAG.getNode(ISD::UNDEF, dl, NVT);
6485 Hi = DAG.getNode(ISD::UNDEF, dl, NVT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006486 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006487 case ISD::Constant: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006488 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman050f5502008-03-03 22:20:46 +00006489 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6490 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6491 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006492 break;
6493 }
Evan Cheng00495212006-12-12 21:32:44 +00006494 case ISD::ConstantFP: {
6495 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00006496 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen7111b022008-10-09 18:53:47 +00006497 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesena471c2e2007-10-11 18:07:22 +00006498 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6499 MVT::f64);
6500 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6501 MVT::f64);
6502 break;
6503 }
Evan Cheng279101e2006-12-12 22:19:28 +00006504 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00006505 if (getTypeAction(Lo.getValueType()) == Expand)
6506 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006507 break;
6508 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006509 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006510 // Return the operands.
6511 Lo = Node->getOperand(0);
6512 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006513 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006514
6515 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00006516 if (Node->getNumValues() == 1) {
6517 ExpandOp(Op.getOperand(0), Lo, Hi);
6518 break;
6519 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006520 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif99a6cb92008-08-26 22:36:50 +00006521 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattner27a6c732007-11-24 07:07:01 +00006522 Op.getValue(1).getValueType() == MVT::Other &&
6523 "unhandled MERGE_VALUES");
6524 ExpandOp(Op.getOperand(0), Lo, Hi);
6525 // Remember that we legalized the chain.
6526 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6527 break;
Chris Lattner58f79632005-12-12 22:27:43 +00006528
6529 case ISD::SIGN_EXTEND_INREG:
6530 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00006531 // sext_inreg the low part if needed.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006532 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Lo, Node->getOperand(1));
Chris Lattner4bdd2752006-10-06 17:34:12 +00006533
6534 // The high part gets the sign extension from the lo-part. This handles
6535 // things like sextinreg V:i64 from i8.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006536 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006537 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattner58f79632005-12-12 22:27:43 +00006538 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00006539 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006540
Nate Begemand88fc032006-01-14 03:14:10 +00006541 case ISD::BSWAP: {
6542 ExpandOp(Node->getOperand(0), Lo, Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006543 SDValue TempLo = DAG.getNode(ISD::BSWAP, dl, NVT, Hi);
6544 Hi = DAG.getNode(ISD::BSWAP, dl, NVT, Lo);
Nate Begemand88fc032006-01-14 03:14:10 +00006545 Lo = TempLo;
6546 break;
6547 }
6548
Chris Lattneredb1add2005-05-11 04:51:16 +00006549 case ISD::CTPOP:
6550 ExpandOp(Node->getOperand(0), Lo, Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006551 Lo = DAG.getNode(ISD::ADD, dl, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6552 DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
6553 DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00006554 Hi = DAG.getConstant(0, NVT);
6555 break;
6556
Chris Lattner39a8f332005-05-12 19:05:01 +00006557 case ISD::CTLZ: {
6558 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006559 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006560 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006561 SDValue HLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Hi);
6562 SDValue TopNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), HLZ,
6563 BitsC, ISD::SETNE);
6564 SDValue LowPart = DAG.getNode(ISD::CTLZ, dl, NVT, Lo);
6565 LowPart = DAG.getNode(ISD::ADD, dl, NVT, LowPart, BitsC);
Chris Lattner39a8f332005-05-12 19:05:01 +00006566
Dale Johannesen8a782a22009-02-02 22:12:50 +00006567 Lo = DAG.getNode(ISD::SELECT, dl, NVT, TopNotZero, HLZ, LowPart);
Chris Lattner39a8f332005-05-12 19:05:01 +00006568 Hi = DAG.getConstant(0, NVT);
6569 break;
6570 }
6571
6572 case ISD::CTTZ: {
6573 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006574 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006575 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006576 SDValue LTZ = DAG.getNode(ISD::CTTZ, dl, NVT, Lo);
6577 SDValue BotNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), LTZ,
6578 BitsC, ISD::SETNE);
6579 SDValue HiPart = DAG.getNode(ISD::CTTZ, dl, NVT, Hi);
6580 HiPart = DAG.getNode(ISD::ADD, dl, NVT, HiPart, BitsC);
Chris Lattner39a8f332005-05-12 19:05:01 +00006581
Dale Johannesen8a782a22009-02-02 22:12:50 +00006582 Lo = DAG.getNode(ISD::SELECT, dl, NVT, BotNotZero, LTZ, HiPart);
Chris Lattner39a8f332005-05-12 19:05:01 +00006583 Hi = DAG.getConstant(0, NVT);
6584 break;
6585 }
Chris Lattneredb1add2005-05-11 04:51:16 +00006586
Nate Begemanacc398c2006-01-25 18:21:52 +00006587 case ISD::VAARG: {
Dan Gohman475871a2008-07-27 21:46:04 +00006588 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6589 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Dale Johannesenc460ae92009-02-04 00:13:36 +00006590 Lo = DAG.getVAArg(NVT, dl, Ch, Ptr, Node->getOperand(2));
6591 Hi = DAG.getVAArg(NVT, dl, Lo.getValue(1), Ptr, Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00006592
6593 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00006594 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00006595 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00006596 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00006597 std::swap(Lo, Hi);
6598 break;
6599 }
6600
Chris Lattner3e928bb2005-01-07 07:47:09 +00006601 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00006602 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00006603 SDValue Ch = LD->getChain(); // Legalize the chain.
6604 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Cheng466685d2006-10-09 20:57:25 +00006605 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f8613e2008-08-14 20:04:46 +00006606 const Value *SV = LD->getSrcValue();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006607 int SVOffset = LD->getSrcValueOffset();
6608 unsigned Alignment = LD->getAlignment();
6609 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006610
Evan Cheng466685d2006-10-09 20:57:25 +00006611 if (ExtType == ISD::NON_EXTLOAD) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006612 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006613 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00006614 if (VT == MVT::f32 || VT == MVT::f64) {
6615 // f32->i32 or f64->i64 one to one expansion.
6616 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006617 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00006618 // Recursively expand the new load.
6619 if (getTypeAction(NVT) == Expand)
6620 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006621 break;
6622 }
Chris Lattnerec39a452005-01-19 18:02:17 +00006623
Evan Cheng466685d2006-10-09 20:57:25 +00006624 // Increment the pointer to the other half.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006625 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Dale Johannesen8a782a22009-02-02 22:12:50 +00006626 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006627 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006628 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006629 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006630 Hi = DAG.getLoad(NVT, dl, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006631 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00006632
Evan Cheng466685d2006-10-09 20:57:25 +00006633 // Build a factor node to remember that this load is independent of the
6634 // other one.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006635 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Evan Cheng466685d2006-10-09 20:57:25 +00006636 Hi.getValue(1));
6637
6638 // Remember that we legalized the chain.
6639 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00006640 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00006641 std::swap(Lo, Hi);
6642 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006643 MVT EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00006644
Dale Johannesenb6210fc2007-10-19 20:29:00 +00006645 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6646 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00006647 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dale Johannesen8a782a22009-02-02 22:12:50 +00006648 SDValue Load = DAG.getLoad(EVT, dl, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006649 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00006650 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006651 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Dale Johannesen8a782a22009-02-02 22:12:50 +00006652 ExpandOp(DAG.getNode(ISD::FP_EXTEND, dl, VT, Load), Lo, Hi);
Evan Cheng548f6112006-12-13 03:19:57 +00006653 break;
6654 }
Evan Cheng466685d2006-10-09 20:57:25 +00006655
6656 if (EVT == NVT)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006657 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006658 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006659 else
Dale Johannesen8a782a22009-02-02 22:12:50 +00006660 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006661 SVOffset, EVT, isVolatile,
6662 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006663
6664 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006665 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng466685d2006-10-09 20:57:25 +00006666
6667 if (ExtType == ISD::SEXTLOAD) {
6668 // The high part is obtained by SRA'ing all but one of the bits of the
6669 // lo part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006670 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dale Johannesen8a782a22009-02-02 22:12:50 +00006671 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
Evan Cheng466685d2006-10-09 20:57:25 +00006672 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6673 } else if (ExtType == ISD::ZEXTLOAD) {
6674 // The high part is just a zero.
6675 Hi = DAG.getConstant(0, NVT);
6676 } else /* if (ExtType == ISD::EXTLOAD) */ {
6677 // The high part is undefined.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006678 Hi = DAG.getNode(ISD::UNDEF, dl, NVT);
Evan Cheng466685d2006-10-09 20:57:25 +00006679 }
6680 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006681 break;
6682 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006683 case ISD::AND:
6684 case ISD::OR:
6685 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman475871a2008-07-27 21:46:04 +00006686 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006687 ExpandOp(Node->getOperand(0), LL, LH);
6688 ExpandOp(Node->getOperand(1), RL, RH);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006689 Lo = DAG.getNode(Node->getOpcode(), dl, NVT, LL, RL);
6690 Hi = DAG.getNode(Node->getOpcode(), dl, NVT, LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006691 break;
6692 }
6693 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006694 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006695 ExpandOp(Node->getOperand(1), LL, LH);
6696 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00006697 if (getTypeAction(NVT) == Expand)
6698 NVT = TLI.getTypeToExpandTo(NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006699 Lo = DAG.getNode(ISD::SELECT, dl, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00006700 if (VT != MVT::f32)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006701 Hi = DAG.getNode(ISD::SELECT, dl, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006702 break;
6703 }
Nate Begeman9373a812005-08-10 20:51:12 +00006704 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00006705 SDValue TL, TH, FL, FH;
Nate Begeman9373a812005-08-10 20:51:12 +00006706 ExpandOp(Node->getOperand(2), TL, TH);
6707 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00006708 if (getTypeAction(NVT) == Expand)
6709 NVT = TLI.getTypeToExpandTo(NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006710 Lo = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
Nate Begeman9373a812005-08-10 20:51:12 +00006711 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00006712 if (VT != MVT::f32)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006713 Hi = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
Evan Cheng19103b12006-12-15 22:42:55 +00006714 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006715 break;
6716 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006717 case ISD::ANY_EXTEND:
6718 // The low part is any extension of the input (which degenerates to a copy).
Dale Johannesen8a782a22009-02-02 22:12:50 +00006719 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
Chris Lattner8137c9e2006-01-28 05:07:51 +00006720 // The high part is undefined.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006721 Hi = DAG.getNode(ISD::UNDEF, dl, NVT);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006722 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006723 case ISD::SIGN_EXTEND: {
6724 // The low part is just a sign extension of the input (which degenerates to
6725 // a copy).
Dale Johannesen8a782a22009-02-02 22:12:50 +00006726 Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006727
Chris Lattner3e928bb2005-01-07 07:47:09 +00006728 // The high part is obtained by SRA'ing all but one of the bits of the lo
6729 // part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006730 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dale Johannesen8a782a22009-02-02 22:12:50 +00006731 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
Chris Lattner8137c9e2006-01-28 05:07:51 +00006732 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006733 break;
6734 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006735 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006736 // The low part is just a zero extension of the input (which degenerates to
6737 // a copy).
Dale Johannesen8a782a22009-02-02 22:12:50 +00006738 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006739
Chris Lattner3e928bb2005-01-07 07:47:09 +00006740 // The high part is just a zero.
6741 Hi = DAG.getConstant(0, NVT);
6742 break;
Chris Lattner35481892005-12-23 00:16:34 +00006743
Chris Lattner4c948eb2007-02-13 23:55:16 +00006744 case ISD::TRUNCATE: {
6745 // The input value must be larger than this value. Expand *it*.
Dan Gohman475871a2008-07-27 21:46:04 +00006746 SDValue NewLo;
Chris Lattner4c948eb2007-02-13 23:55:16 +00006747 ExpandOp(Node->getOperand(0), NewLo, Hi);
6748
6749 // The low part is now either the right size, or it is closer. If not the
6750 // right size, make an illegal truncate so we recursively expand it.
6751 if (NewLo.getValueType() != Node->getValueType(0))
Dale Johannesen8a782a22009-02-02 22:12:50 +00006752 NewLo = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), NewLo);
Chris Lattner4c948eb2007-02-13 23:55:16 +00006753 ExpandOp(NewLo, Lo, Hi);
6754 break;
6755 }
6756
Chris Lattner35481892005-12-23 00:16:34 +00006757 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006758 SDValue Tmp;
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006759 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6760 // If the target wants to, allow it to lower this itself.
6761 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6762 case Expand: assert(0 && "cannot expand FP!");
6763 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6764 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6765 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00006766 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp), DAG);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006767 }
6768
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006769 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006770 if (VT == MVT::f32 || VT == MVT::f64) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006771 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006772 if (getTypeAction(NVT) == Expand)
6773 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006774 break;
6775 }
6776
6777 // If source operand will be expanded to the same type as VT, i.e.
6778 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006779 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006780 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6781 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006782 break;
6783 }
6784
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006785 // Turn this into a load/store pair by default.
Gabor Greifba36cb52008-08-28 21:40:38 +00006786 if (Tmp.getNode() == 0)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006787 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT, dl);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006788
Chris Lattner35481892005-12-23 00:16:34 +00006789 ExpandOp(Tmp, Lo, Hi);
6790 break;
6791 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006792
Chris Lattner27a6c732007-11-24 07:07:01 +00006793 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006794 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6795 TargetLowering::Custom &&
6796 "Must custom expand ReadCycleCounter");
Dan Gohman475871a2008-07-27 21:46:04 +00006797 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006798 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattner27a6c732007-11-24 07:07:01 +00006799 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006800 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006801 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006802 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006803 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006804
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006805 case ISD::ATOMIC_CMP_SWAP: {
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006806 // This operation does not need a loop.
6807 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6808 assert(Tmp.getNode() && "Node must be custom expanded!");
6809 ExpandOp(Tmp.getValue(0), Lo, Hi);
6810 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6811 LegalizeOp(Tmp.getValue(1)));
6812 break;
6813 }
6814
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006815 case ISD::ATOMIC_LOAD_ADD:
6816 case ISD::ATOMIC_LOAD_SUB:
6817 case ISD::ATOMIC_LOAD_AND:
6818 case ISD::ATOMIC_LOAD_OR:
6819 case ISD::ATOMIC_LOAD_XOR:
6820 case ISD::ATOMIC_LOAD_NAND:
6821 case ISD::ATOMIC_SWAP: {
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006822 // These operations require a loop to be generated. We can't do that yet,
6823 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006824 SDValue In2Lo, In2Hi, In2;
6825 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006826 In2 = DAG.getNode(ISD::BUILD_PAIR, dl, VT, In2Lo, In2Hi);
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006827 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6828 SDValue Replace =
Dale Johannesen8a782a22009-02-02 22:12:50 +00006829 DAG.getAtomic(Op.getOpcode(), dl, Anode->getMemoryVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006830 Op.getOperand(0), Op.getOperand(1), In2,
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006831 Anode->getSrcValue(), Anode->getAlignment());
6832 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006833 ExpandOp(Result.getValue(0), Lo, Hi);
6834 // Remember that we legalized the chain.
6835 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006836 break;
6837 }
6838
Chris Lattner4e6c7462005-01-08 19:27:05 +00006839 // These operators cannot be expanded directly, emit them as calls to
6840 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006841 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006842 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006843 SDValue Op;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006844 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6845 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006846 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6847 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006848 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006849
Dale Johannesen8a782a22009-02-02 22:12:50 +00006850 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op), DAG);
Chris Lattnerf20d1832005-07-30 01:40:57 +00006851
Chris Lattner80a3e942005-07-29 00:33:32 +00006852 // Now that the custom expander is done, expand the result, which is still
6853 // VT.
Gabor Greifba36cb52008-08-28 21:40:38 +00006854 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006855 ExpandOp(Op, Lo, Hi);
6856 break;
6857 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006858 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006859
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006860 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6861 VT);
6862 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6863 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006864 break;
Evan Cheng56966222007-01-12 02:11:51 +00006865 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006866
Evan Cheng56966222007-01-12 02:11:51 +00006867 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006868 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006869 SDValue Op;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006870 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6871 case Expand: assert(0 && "cannot expand FP!");
6872 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6873 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6874 }
6875
Dale Johannesen8a782a22009-02-02 22:12:50 +00006876 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, dl, VT, Op), DAG);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006877
6878 // Now that the custom expander is done, expand the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00006879 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006880 ExpandOp(Op, Lo, Hi);
6881 break;
6882 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006883 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006884
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006885 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6886 VT);
6887 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6888 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006889 break;
Evan Cheng56966222007-01-12 02:11:51 +00006890 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006891
Evan Cheng05a2d562006-01-09 18:31:59 +00006892 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006893 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006894 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006895 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006896 SDValue Op = DAG.getNode(ISD::SHL, dl, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006897 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006898 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006899 // Now that the custom expander is done, expand the result, which is
6900 // still VT.
6901 ExpandOp(Op, Lo, Hi);
6902 break;
6903 }
6904 }
6905
Chris Lattner79980b02006-09-13 03:50:39 +00006906 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6907 // this X << 1 as X+X.
6908 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006909 if (ShAmt->getAPIntValue() == 1 &&
6910 TLI.isOperationLegalOrCustom(ISD::ADDC, NVT) &&
6911 TLI.isOperationLegalOrCustom(ISD::ADDE, NVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006912 SDValue LoOps[2], HiOps[3];
Chris Lattner79980b02006-09-13 03:50:39 +00006913 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6914 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6915 LoOps[1] = LoOps[0];
Dale Johannesen8a782a22009-02-02 22:12:50 +00006916 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
Chris Lattner79980b02006-09-13 03:50:39 +00006917
6918 HiOps[1] = HiOps[0];
6919 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006920 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
Chris Lattner79980b02006-09-13 03:50:39 +00006921 break;
6922 }
6923 }
6924
Chris Lattnere34b3962005-01-19 04:19:40 +00006925 // If we can emit an efficient shift operation, do so now.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006926 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
Chris Lattnere34b3962005-01-19 04:19:40 +00006927 break;
Chris Lattner47599822005-04-02 03:38:53 +00006928
6929 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006930 TargetLowering::LegalizeAction Action =
6931 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6932 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6933 Action == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006934 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0),
6935 ShiftAmt, Lo, Hi, dl);
Chris Lattner47599822005-04-02 03:38:53 +00006936 break;
6937 }
6938
Chris Lattnere34b3962005-01-19 04:19:40 +00006939 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006940 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006941 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006942 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006943
Evan Cheng05a2d562006-01-09 18:31:59 +00006944 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006945 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006946 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006947 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006948 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006949 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006950 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006951 // Now that the custom expander is done, expand the result, which is
6952 // still VT.
6953 ExpandOp(Op, Lo, Hi);
6954 break;
6955 }
6956 }
6957
Chris Lattnere34b3962005-01-19 04:19:40 +00006958 // If we can emit an efficient shift operation, do so now.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006959 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
Chris Lattnere34b3962005-01-19 04:19:40 +00006960 break;
Chris Lattner47599822005-04-02 03:38:53 +00006961
6962 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006963 TargetLowering::LegalizeAction Action =
6964 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6965 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6966 Action == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006967 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0),
6968 ShiftAmt, Lo, Hi, dl);
Chris Lattner47599822005-04-02 03:38:53 +00006969 break;
6970 }
6971
Chris Lattnere34b3962005-01-19 04:19:40 +00006972 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006973 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006974 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006975 }
6976
6977 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006978 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006979 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006980 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006981 SDValue Op = DAG.getNode(ISD::SRL, dl, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006982 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006983 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006984 // Now that the custom expander is done, expand the result, which is
6985 // still VT.
6986 ExpandOp(Op, Lo, Hi);
6987 break;
6988 }
6989 }
6990
Chris Lattnere34b3962005-01-19 04:19:40 +00006991 // If we can emit an efficient shift operation, do so now.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006992 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
Chris Lattnere34b3962005-01-19 04:19:40 +00006993 break;
Chris Lattner47599822005-04-02 03:38:53 +00006994
6995 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006996 TargetLowering::LegalizeAction Action =
6997 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6998 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6999 Action == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007000 ExpandShiftParts(ISD::SRL_PARTS,
7001 Node->getOperand(0), ShiftAmt, Lo, Hi, dl);
Chris Lattner47599822005-04-02 03:38:53 +00007002 break;
7003 }
7004
Chris Lattnere34b3962005-01-19 04:19:40 +00007005 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00007006 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00007007 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00007008 }
Chris Lattnere34b3962005-01-19 04:19:40 +00007009
Misha Brukmanedf128a2005-04-21 22:36:52 +00007010 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00007011 case ISD::SUB: {
7012 // If the target wants to custom expand this, let them.
7013 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
7014 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00007015 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007016 if (Result.getNode()) {
Duncan Sands69bfb152008-06-22 09:42:16 +00007017 ExpandOp(Result, Lo, Hi);
Chris Lattner8137c9e2006-01-28 05:07:51 +00007018 break;
7019 }
7020 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00007021 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00007022 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner8137c9e2006-01-28 05:07:51 +00007023 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7024 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman475871a2008-07-27 21:46:04 +00007025 SDValue LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00007026 LoOps[0] = LHSL;
7027 LoOps[1] = RHSL;
7028 HiOps[0] = LHSH;
7029 HiOps[1] = RHSH;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00007030
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00007031 //cascaded check to see if any smaller size has a a carry flag.
7032 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
7033 bool hasCarry = false;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00007034 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
7035 MVT AVT = MVT::getIntegerVT(BitSize);
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007036 if (TLI.isOperationLegalOrCustom(OpV, AVT)) {
Andrew Lenharth2163ca12008-10-07 18:27:23 +00007037 hasCarry = true;
7038 break;
7039 }
7040 }
7041
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00007042 if(hasCarry) {
Evan Cheng637ed032008-12-12 18:49:09 +00007043 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007044 if (Node->getOpcode() == ISD::ADD) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007045 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007046 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007047 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007048 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007049 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007050 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007051 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007052 }
7053 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00007054 } else {
Andrew Lenharth40d51392008-10-07 14:15:42 +00007055 if (Node->getOpcode() == ISD::ADD) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007056 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps, 2);
7057 Hi = DAG.getNode(ISD::ADD, dl, NVT, HiOps, 2);
7058 SDValue Cmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00007059 Lo, LoOps[0], ISD::SETULT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007060 SDValue Carry1 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp1,
Andrew Lenharth40d51392008-10-07 14:15:42 +00007061 DAG.getConstant(1, NVT),
7062 DAG.getConstant(0, NVT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00007063 SDValue Cmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00007064 Lo, LoOps[1], ISD::SETULT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007065 SDValue Carry2 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp2,
Andrew Lenharth40d51392008-10-07 14:15:42 +00007066 DAG.getConstant(1, NVT),
7067 Carry1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007068 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007069 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007070 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps, 2);
7071 Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps, 2);
7072 SDValue Cmp = DAG.getSetCC(dl, NVT, LoOps[0], LoOps[1], ISD::SETULT);
7073 SDValue Borrow = DAG.getNode(ISD::SELECT, dl, NVT, Cmp,
Andrew Lenharth40d51392008-10-07 14:15:42 +00007074 DAG.getConstant(1, NVT),
7075 DAG.getConstant(0, NVT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00007076 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007077 }
7078 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00007079 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00007080 }
Chris Lattnerb429f732007-05-17 18:15:41 +00007081
7082 case ISD::ADDC:
7083 case ISD::SUBC: {
7084 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00007085 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00007086 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7087 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7088 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00007089 SDValue LoOps[2] = { LHSL, RHSL };
7090 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00007091
7092 if (Node->getOpcode() == ISD::ADDC) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007093 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
Chris Lattnerb429f732007-05-17 18:15:41 +00007094 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007095 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
Chris Lattnerb429f732007-05-17 18:15:41 +00007096 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007097 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
Chris Lattnerb429f732007-05-17 18:15:41 +00007098 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007099 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
Chris Lattnerb429f732007-05-17 18:15:41 +00007100 }
7101 // Remember that we legalized the flag.
7102 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7103 break;
7104 }
7105 case ISD::ADDE:
7106 case ISD::SUBE: {
7107 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00007108 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00007109 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7110 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7111 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00007112 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
7113 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00007114
Dale Johannesen8a782a22009-02-02 22:12:50 +00007115 Lo = DAG.getNode(Node->getOpcode(), dl, VTList, LoOps, 3);
Chris Lattnerb429f732007-05-17 18:15:41 +00007116 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007117 Hi = DAG.getNode(Node->getOpcode(), dl, VTList, HiOps, 3);
Chris Lattnerb429f732007-05-17 18:15:41 +00007118
7119 // Remember that we legalized the flag.
7120 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7121 break;
7122 }
Nate Begemanc7c16572005-04-11 03:01:51 +00007123 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00007124 // If the target wants to custom expand this, let them.
7125 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00007126 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007127 if (New.getNode()) {
Chris Lattner8829dc82006-09-16 05:08:34 +00007128 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00007129 break;
7130 }
7131 }
7132
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007133 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, NVT);
7134 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, NVT);
7135 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, NVT);
7136 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00007137 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman475871a2008-07-27 21:46:04 +00007138 SDValue LL, LH, RL, RH;
Nate Begemanc7c16572005-04-11 03:01:51 +00007139 ExpandOp(Node->getOperand(0), LL, LH);
7140 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007141 unsigned OuterBitSize = Op.getValueSizeInBits();
7142 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00007143 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
7144 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00007145 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
7146 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
7147 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00007148 // The inputs are both zero-extended.
7149 if (HasUMUL_LOHI) {
7150 // We can emit a umul_lohi.
Dale Johannesen8a782a22009-02-02 22:12:50 +00007151 Lo = DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00007152 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00007153 break;
7154 }
7155 if (HasMULHU) {
7156 // We can emit a mulhu+mul.
Dale Johannesen8a782a22009-02-02 22:12:50 +00007157 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
7158 Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
Dan Gohman525178c2007-10-08 18:33:35 +00007159 break;
7160 }
Dan Gohman525178c2007-10-08 18:33:35 +00007161 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007162 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00007163 // The input values are both sign-extended.
7164 if (HasSMUL_LOHI) {
7165 // We can emit a smul_lohi.
Dale Johannesen8a782a22009-02-02 22:12:50 +00007166 Lo = DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00007167 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00007168 break;
7169 }
7170 if (HasMULHS) {
7171 // We can emit a mulhs+mul.
Dale Johannesen8a782a22009-02-02 22:12:50 +00007172 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
7173 Hi = DAG.getNode(ISD::MULHS, dl, NVT, LL, RL);
Dan Gohman525178c2007-10-08 18:33:35 +00007174 break;
7175 }
7176 }
7177 if (HasUMUL_LOHI) {
7178 // Lo,Hi = umul LHS, RHS.
Dale Johannesen8a782a22009-02-02 22:12:50 +00007179 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, dl,
Dan Gohman525178c2007-10-08 18:33:35 +00007180 DAG.getVTList(NVT, NVT), LL, RL);
7181 Lo = UMulLOHI;
7182 Hi = UMulLOHI.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007183 RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
7184 LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
7185 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
7186 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00007187 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00007188 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00007189 if (HasMULHU) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007190 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
7191 Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
7192 RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
7193 LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
7194 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
7195 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00007196 break;
7197 }
Nate Begemanc7c16572005-04-11 03:01:51 +00007198 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00007199
Dan Gohman525178c2007-10-08 18:33:35 +00007200 // If nothing else, we can make a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00007201 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00007202 break;
7203 }
Evan Cheng56966222007-01-12 02:11:51 +00007204 case ISD::SDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007205 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007206 break;
7207 case ISD::UDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007208 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007209 break;
7210 case ISD::SREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007211 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007212 break;
7213 case ISD::UREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007214 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007215 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00007216
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007217 case ISD::FADD:
Duncan Sands460a14e2008-04-12 17:14:18 +00007218 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7219 RTLIB::ADD_F64,
7220 RTLIB::ADD_F80,
7221 RTLIB::ADD_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007222 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007223 break;
7224 case ISD::FSUB:
Duncan Sands460a14e2008-04-12 17:14:18 +00007225 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7226 RTLIB::SUB_F64,
7227 RTLIB::SUB_F80,
7228 RTLIB::SUB_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007229 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007230 break;
7231 case ISD::FMUL:
Duncan Sands460a14e2008-04-12 17:14:18 +00007232 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7233 RTLIB::MUL_F64,
7234 RTLIB::MUL_F80,
7235 RTLIB::MUL_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007236 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007237 break;
7238 case ISD::FDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007239 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7240 RTLIB::DIV_F64,
7241 RTLIB::DIV_F80,
7242 RTLIB::DIV_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007243 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007244 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007245 case ISD::FP_EXTEND: {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007246 if (VT == MVT::ppcf128) {
7247 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7248 Node->getOperand(0).getValueType()==MVT::f64);
7249 const uint64_t zero = 0;
7250 if (Node->getOperand(0).getValueType()==MVT::f32)
Dale Johannesen8a782a22009-02-02 22:12:50 +00007251 Hi = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Node->getOperand(0));
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007252 else
7253 Hi = Node->getOperand(0);
7254 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7255 break;
7256 }
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007257 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7258 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7259 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007260 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007261 }
7262 case ISD::FP_ROUND: {
7263 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7264 VT);
7265 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7266 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007267 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007268 }
Evan Cheng4b887022008-09-09 23:02:14 +00007269 case ISD::FSQRT:
7270 case ISD::FSIN:
7271 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007272 case ISD::FLOG:
7273 case ISD::FLOG2:
7274 case ISD::FLOG10:
7275 case ISD::FEXP:
7276 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007277 case ISD::FTRUNC:
7278 case ISD::FFLOOR:
7279 case ISD::FCEIL:
7280 case ISD::FRINT:
7281 case ISD::FNEARBYINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00007282 case ISD::FPOW:
7283 case ISD::FPOWI: {
Evan Cheng56966222007-01-12 02:11:51 +00007284 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007285 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00007286 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00007287 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7288 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007289 break;
7290 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00007291 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7292 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007293 break;
7294 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00007295 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7296 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007297 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007298 case ISD::FLOG:
7299 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7300 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7301 break;
7302 case ISD::FLOG2:
7303 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7304 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7305 break;
7306 case ISD::FLOG10:
7307 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7308 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7309 break;
7310 case ISD::FEXP:
7311 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7312 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7313 break;
7314 case ISD::FEXP2:
7315 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7316 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7317 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007318 case ISD::FTRUNC:
7319 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7320 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7321 break;
7322 case ISD::FFLOOR:
7323 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7324 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7325 break;
7326 case ISD::FCEIL:
7327 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7328 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7329 break;
7330 case ISD::FRINT:
7331 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7332 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7333 break;
7334 case ISD::FNEARBYINT:
7335 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7336 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7337 break;
Evan Cheng4b887022008-09-09 23:02:14 +00007338 case ISD::FPOW:
7339 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7340 RTLIB::POW_PPCF128);
7341 break;
7342 case ISD::FPOWI:
7343 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7344 RTLIB::POWI_PPCF128);
7345 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007346 default: assert(0 && "Unreachable!");
7347 }
Duncan Sands460a14e2008-04-12 17:14:18 +00007348 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00007349 break;
7350 }
Evan Cheng966bf242006-12-16 00:52:40 +00007351 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007352 if (VT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00007353 SDValue Tmp;
Dale Johannesenf6467742007-10-12 19:02:17 +00007354 ExpandOp(Node->getOperand(0), Lo, Tmp);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007355 Hi = DAG.getNode(ISD::FABS, dl, NVT, Tmp);
Dale Johannesenf6467742007-10-12 19:02:17 +00007356 // lo = hi==fabs(hi) ? lo : -lo;
Dale Johannesen8a782a22009-02-02 22:12:50 +00007357 Lo = DAG.getNode(ISD::SELECT_CC, dl, NVT, Hi, Tmp,
7358 Lo, DAG.getNode(ISD::FNEG, dl, NVT, Lo),
Dale Johannesenf6467742007-10-12 19:02:17 +00007359 DAG.getCondCode(ISD::SETEQ));
7360 break;
7361 }
Dan Gohman475871a2008-07-27 21:46:04 +00007362 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007363 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7364 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007365 Mask = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask);
7366 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
7367 Lo = DAG.getNode(ISD::AND, dl, NVT, Lo, Mask);
Evan Cheng966bf242006-12-16 00:52:40 +00007368 if (getTypeAction(NVT) == Expand)
7369 ExpandOp(Lo, Lo, Hi);
7370 break;
7371 }
7372 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007373 if (VT == MVT::ppcf128) {
7374 ExpandOp(Node->getOperand(0), Lo, Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007375 Lo = DAG.getNode(ISD::FNEG, dl, MVT::f64, Lo);
7376 Hi = DAG.getNode(ISD::FNEG, dl, MVT::f64, Hi);
Dale Johannesenf6467742007-10-12 19:02:17 +00007377 break;
7378 }
Dan Gohman475871a2008-07-27 21:46:04 +00007379 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007380 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7381 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007382 Mask = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask);
7383 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
7384 Lo = DAG.getNode(ISD::XOR, dl, NVT, Lo, Mask);
Evan Cheng966bf242006-12-16 00:52:40 +00007385 if (getTypeAction(NVT) == Expand)
7386 ExpandOp(Lo, Lo, Hi);
7387 break;
7388 }
Evan Cheng912095b2007-01-04 21:56:39 +00007389 case ISD::FCOPYSIGN: {
7390 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7391 if (getTypeAction(NVT) == Expand)
7392 ExpandOp(Lo, Lo, Hi);
7393 break;
7394 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007395 case ISD::SINT_TO_FP:
7396 case ISD::UINT_TO_FP: {
7397 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007398 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00007399
7400 // Promote the operand if needed. Do this before checking for
7401 // ppcf128 so conversions of i16 and i8 work.
7402 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman475871a2008-07-27 21:46:04 +00007403 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesencf498192008-03-18 17:28:38 +00007404 Tmp = isSigned
Dale Johannesen8a782a22009-02-02 22:12:50 +00007405 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Tmp.getValueType(), Tmp,
Dale Johannesencf498192008-03-18 17:28:38 +00007406 DAG.getValueType(SrcVT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00007407 : DAG.getZeroExtendInReg(Tmp, dl, SrcVT);
Gabor Greifba36cb52008-08-28 21:40:38 +00007408 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesencf498192008-03-18 17:28:38 +00007409 SrcVT = Node->getOperand(0).getValueType();
7410 }
7411
Dan Gohmana2e94852008-03-10 23:03:31 +00007412 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007413 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007414 if (isSigned) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007415 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f64,
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007416 Node->getOperand(0)));
7417 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7418 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007419 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen8a782a22009-02-02 22:12:50 +00007420 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f64,
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007421 Node->getOperand(0)));
7422 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007423 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00007424 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen8a782a22009-02-02 22:12:50 +00007425 ExpandOp(DAG.getNode(ISD::SELECT_CC, dl,
7426 MVT::ppcf128, Node->getOperand(0),
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007427 DAG.getConstant(0, MVT::i32),
Dale Johannesen8a782a22009-02-02 22:12:50 +00007428 DAG.getNode(ISD::FADD, dl, MVT::ppcf128, Hi,
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007429 DAG.getConstantFP(
7430 APFloat(APInt(128, 2, TwoE32)),
7431 MVT::ppcf128)),
7432 Hi,
7433 DAG.getCondCode(ISD::SETLT)),
7434 Lo, Hi);
7435 }
7436 break;
7437 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00007438 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7439 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007440 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen8a782a22009-02-02 22:12:50 +00007441 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::ppcf128,
7442 Node->getOperand(0)), Lo, Hi);
7443 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00007444 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
Dale Johannesen8a782a22009-02-02 22:12:50 +00007445 ExpandOp(DAG.getNode(ISD::SELECT_CC, dl, MVT::ppcf128,
7446 Node->getOperand(0),
Dale Johannesen6e63e092007-10-12 17:52:03 +00007447 DAG.getConstant(0, MVT::i64),
Dale Johannesen8a782a22009-02-02 22:12:50 +00007448 DAG.getNode(ISD::FADD, dl, MVT::ppcf128, Hi,
Dale Johannesen6e63e092007-10-12 17:52:03 +00007449 DAG.getConstantFP(
7450 APFloat(APInt(128, 2, TwoE64)),
7451 MVT::ppcf128)),
7452 Hi,
7453 DAG.getCondCode(ISD::SETLT)),
7454 Lo, Hi);
7455 break;
7456 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007457
Dan Gohmana2e94852008-03-10 23:03:31 +00007458 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00007459 Node->getOperand(0), dl);
Evan Cheng110cf482008-04-01 01:50:16 +00007460 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng6ad2f932008-04-01 01:51:26 +00007461 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng110cf482008-04-01 01:50:16 +00007462 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00007463 break;
7464 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00007465 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00007466
Chris Lattner83397362005-12-21 18:02:52 +00007467 // Make sure the resultant values have been legalized themselves, unless this
7468 // is a type that requires multi-step expansion.
7469 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7470 Lo = LegalizeOp(Lo);
Gabor Greifba36cb52008-08-28 21:40:38 +00007471 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00007472 // Don't legalize the high part if it is expanded to a single node.
7473 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00007474 }
Evan Cheng05a2d562006-01-09 18:31:59 +00007475
7476 // Remember in a map if the values will be reused later.
Dan Gohman6b345ee2008-07-07 17:46:23 +00007477 bool isNew =
7478 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng05a2d562006-01-09 18:31:59 +00007479 assert(isNew && "Value already expanded?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007480 isNew = isNew;
Chris Lattner3e928bb2005-01-07 07:47:09 +00007481}
7482
Dan Gohman7f321562007-06-25 16:23:39 +00007483/// SplitVectorOp - Given an operand of vector type, break it down into
7484/// two smaller values, still of vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00007485void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7486 SDValue &Hi) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007487 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007488 SDNode *Node = Op.getNode();
Dale Johannesenbb5da912009-02-02 20:41:04 +00007489 DebugLoc dl = Node->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007490 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattnerc7029802006-03-18 01:44:44 +00007491 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00007492
Duncan Sands83ec4b62008-06-06 12:08:01 +00007493 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman5db1afb2007-11-15 21:15:26 +00007494
7495 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7496 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7497
Duncan Sands83ec4b62008-06-06 12:08:01 +00007498 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7499 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007500
Chris Lattnerc7029802006-03-18 01:44:44 +00007501 // See if we already split it.
Dan Gohman475871a2008-07-27 21:46:04 +00007502 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattnerc7029802006-03-18 01:44:44 +00007503 = SplitNodes.find(Op);
7504 if (I != SplitNodes.end()) {
7505 Lo = I->second.first;
7506 Hi = I->second.second;
7507 return;
7508 }
7509
7510 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007511 default:
7512#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007513 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007514#endif
7515 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007516 case ISD::UNDEF:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007517 Lo = DAG.getNode(ISD::UNDEF, dl, NewVT_Lo);
7518 Hi = DAG.getNode(ISD::UNDEF, dl, NewVT_Hi);
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007519 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007520 case ISD::BUILD_PAIR:
7521 Lo = Node->getOperand(0);
7522 Hi = Node->getOperand(1);
7523 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00007524 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman68679912008-04-25 18:07:40 +00007525 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7526 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007527 unsigned Index = Idx->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007528 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman68679912008-04-25 18:07:40 +00007529 if (Index < NewNumElts_Lo)
Dale Johannesenc6be1102009-02-02 22:49:46 +00007530 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVT_Lo, Lo, ScalarOp,
Nate Begeman68679912008-04-25 18:07:40 +00007531 DAG.getIntPtrConstant(Index));
7532 else
Dale Johannesenc6be1102009-02-02 22:49:46 +00007533 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVT_Hi, Hi, ScalarOp,
Nate Begeman68679912008-04-25 18:07:40 +00007534 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7535 break;
7536 }
Dan Gohman475871a2008-07-27 21:46:04 +00007537 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman68679912008-04-25 18:07:40 +00007538 Node->getOperand(1),
Dale Johannesenbb5da912009-02-02 20:41:04 +00007539 Node->getOperand(2), dl);
Nate Begeman68679912008-04-25 18:07:40 +00007540 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohman9fe46622007-09-28 23:53:40 +00007541 break;
7542 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00007543 case ISD::VECTOR_SHUFFLE: {
7544 // Build the low part.
Dan Gohman475871a2008-07-27 21:46:04 +00007545 SDValue Mask = Node->getOperand(2);
7546 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007547 MVT PtrVT = TLI.getPointerTy();
Chris Lattner6c9c6802007-11-19 21:16:54 +00007548
7549 // Insert all of the elements from the input that are needed. We use
7550 // buildvector of extractelement here because the input vectors will have
7551 // to be legalized, so this makes the code simpler.
7552 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007553 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007554 if (IdxNode.getOpcode() == ISD::UNDEF) {
Dale Johannesenc6be1102009-02-02 22:49:46 +00007555 Ops.push_back(DAG.getNode(ISD::UNDEF, dl, NewEltVT));
Nate Begeman5922f562008-03-14 00:53:31 +00007556 continue;
7557 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007558 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007559 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007560 if (Idx >= NumElements) {
7561 InVec = Node->getOperand(1);
7562 Idx -= NumElements;
7563 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007564 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewEltVT, InVec,
Chris Lattner6c9c6802007-11-19 21:16:54 +00007565 DAG.getConstant(Idx, PtrVT)));
7566 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007567 Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Lo, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007568 Ops.clear();
7569
7570 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007571 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007572 if (IdxNode.getOpcode() == ISD::UNDEF) {
Dale Johannesenc6be1102009-02-02 22:49:46 +00007573 Ops.push_back(DAG.getNode(ISD::UNDEF, dl, NewEltVT));
Nate Begeman5922f562008-03-14 00:53:31 +00007574 continue;
7575 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007576 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007577 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007578 if (Idx >= NumElements) {
7579 InVec = Node->getOperand(1);
7580 Idx -= NumElements;
7581 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007582 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewEltVT, InVec,
Chris Lattner6c9c6802007-11-19 21:16:54 +00007583 DAG.getConstant(Idx, PtrVT)));
7584 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007585 Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007586 break;
7587 }
Dan Gohman7f321562007-06-25 16:23:39 +00007588 case ISD::BUILD_VECTOR: {
Dan Gohman475871a2008-07-27 21:46:04 +00007589 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00007590 Node->op_begin()+NewNumElts_Lo);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007591 Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007592
Dan Gohman475871a2008-07-27 21:46:04 +00007593 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00007594 Node->op_end());
Dale Johannesenc6be1102009-02-02 22:49:46 +00007595 Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007596 break;
7597 }
Dan Gohman7f321562007-06-25 16:23:39 +00007598 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00007599 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00007600 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7601 if (NewNumSubvectors == 1) {
7602 Lo = Node->getOperand(0);
7603 Hi = Node->getOperand(1);
7604 } else {
Mon P Wangaeb06d22008-11-10 04:46:22 +00007605 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7606 Node->op_begin()+NewNumSubvectors);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007607 Lo = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewVT_Lo,
7608 &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00007609
Mon P Wangaeb06d22008-11-10 04:46:22 +00007610 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohman7f321562007-06-25 16:23:39 +00007611 Node->op_end());
Dale Johannesenc6be1102009-02-02 22:49:46 +00007612 Hi = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewVT_Hi,
7613 &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00007614 }
Dan Gohman65956352007-06-13 15:12:02 +00007615 break;
7616 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00007617 case ISD::EXTRACT_SUBVECTOR: {
7618 SDValue Vec = Op.getOperand(0);
7619 SDValue Idx = Op.getOperand(1);
7620 MVT IdxVT = Idx.getValueType();
7621
Dale Johannesenc6be1102009-02-02 22:49:46 +00007622 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Lo, Vec, Idx);
Mon P Wangaeb06d22008-11-10 04:46:22 +00007623 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7624 if (CIdx) {
Dale Johannesenc6be1102009-02-02 22:49:46 +00007625 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Hi, Vec,
Mon P Wangaeb06d22008-11-10 04:46:22 +00007626 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7627 IdxVT));
7628 } else {
Dale Johannesenc6be1102009-02-02 22:49:46 +00007629 Idx = DAG.getNode(ISD::ADD, dl, IdxVT, Idx,
Mon P Wangaeb06d22008-11-10 04:46:22 +00007630 DAG.getConstant(NewNumElts_Lo, IdxVT));
Dale Johannesenc6be1102009-02-02 22:49:46 +00007631 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Hi, Vec, Idx);
Mon P Wangaeb06d22008-11-10 04:46:22 +00007632 }
7633 break;
7634 }
Dan Gohmanc6230962007-10-17 14:48:28 +00007635 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007636 SDValue Cond = Node->getOperand(0);
Dan Gohmanc6230962007-10-17 14:48:28 +00007637
Dan Gohman475871a2008-07-27 21:46:04 +00007638 SDValue LL, LH, RL, RH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007639 SplitVectorOp(Node->getOperand(1), LL, LH);
7640 SplitVectorOp(Node->getOperand(2), RL, RH);
7641
Duncan Sands83ec4b62008-06-06 12:08:01 +00007642 if (Cond.getValueType().isVector()) {
Dan Gohmanc6230962007-10-17 14:48:28 +00007643 // Handle a vector merge.
Dan Gohman475871a2008-07-27 21:46:04 +00007644 SDValue CL, CH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007645 SplitVectorOp(Cond, CL, CH);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007646 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, CL, LL, RL);
7647 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007648 } else {
7649 // Handle a simple select with vector operands.
Dale Johannesenc6be1102009-02-02 22:49:46 +00007650 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, Cond, LL, RL);
7651 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007652 }
7653 break;
7654 }
Chris Lattner80c1a562008-06-30 02:43:01 +00007655 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007656 SDValue CondLHS = Node->getOperand(0);
7657 SDValue CondRHS = Node->getOperand(1);
7658 SDValue CondCode = Node->getOperand(4);
Chris Lattner80c1a562008-06-30 02:43:01 +00007659
Dan Gohman475871a2008-07-27 21:46:04 +00007660 SDValue LL, LH, RL, RH;
Chris Lattner80c1a562008-06-30 02:43:01 +00007661 SplitVectorOp(Node->getOperand(2), LL, LH);
7662 SplitVectorOp(Node->getOperand(3), RL, RH);
7663
7664 // Handle a simple select with vector operands.
Dale Johannesenc6be1102009-02-02 22:49:46 +00007665 Lo = DAG.getNode(ISD::SELECT_CC, dl, NewVT_Lo, CondLHS, CondRHS,
Chris Lattner80c1a562008-06-30 02:43:01 +00007666 LL, RL, CondCode);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007667 Hi = DAG.getNode(ISD::SELECT_CC, dl, NewVT_Hi, CondLHS, CondRHS,
Chris Lattner80c1a562008-06-30 02:43:01 +00007668 LH, RH, CondCode);
7669 break;
7670 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00007671 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007672 SDValue LL, LH, RL, RH;
Nate Begemanb43e9c12008-05-12 19:40:03 +00007673 SplitVectorOp(Node->getOperand(0), LL, LH);
7674 SplitVectorOp(Node->getOperand(1), RL, RH);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007675 Lo = DAG.getNode(ISD::VSETCC, dl, NewVT_Lo, LL, RL, Node->getOperand(2));
7676 Hi = DAG.getNode(ISD::VSETCC, dl, NewVT_Hi, LH, RH, Node->getOperand(2));
Nate Begemanb43e9c12008-05-12 19:40:03 +00007677 break;
7678 }
Dan Gohman7f321562007-06-25 16:23:39 +00007679 case ISD::ADD:
7680 case ISD::SUB:
7681 case ISD::MUL:
7682 case ISD::FADD:
7683 case ISD::FSUB:
7684 case ISD::FMUL:
7685 case ISD::SDIV:
7686 case ISD::UDIV:
7687 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00007688 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007689 case ISD::AND:
7690 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00007691 case ISD::XOR:
7692 case ISD::UREM:
7693 case ISD::SREM:
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007694 case ISD::FREM:
7695 case ISD::SHL:
7696 case ISD::SRA:
7697 case ISD::SRL: {
Dan Gohman475871a2008-07-27 21:46:04 +00007698 SDValue LL, LH, RL, RH;
Chris Lattnerc7029802006-03-18 01:44:44 +00007699 SplitVectorOp(Node->getOperand(0), LL, LH);
7700 SplitVectorOp(Node->getOperand(1), RL, RH);
7701
Dale Johannesenc6be1102009-02-02 22:49:46 +00007702 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, LL, RL);
7703 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00007704 break;
7705 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00007706 case ISD::FP_ROUND:
Dan Gohman82669522007-10-11 23:57:53 +00007707 case ISD::FPOWI: {
Dan Gohman475871a2008-07-27 21:46:04 +00007708 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007709 SplitVectorOp(Node->getOperand(0), L, H);
7710
Dale Johannesenc6be1102009-02-02 22:49:46 +00007711 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, L, Node->getOperand(1));
7712 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00007713 break;
7714 }
7715 case ISD::CTTZ:
7716 case ISD::CTLZ:
7717 case ISD::CTPOP:
7718 case ISD::FNEG:
7719 case ISD::FABS:
7720 case ISD::FSQRT:
7721 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00007722 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007723 case ISD::FLOG:
7724 case ISD::FLOG2:
7725 case ISD::FLOG10:
7726 case ISD::FEXP:
7727 case ISD::FEXP2:
Nate Begemanb348d182007-11-17 03:58:34 +00007728 case ISD::FP_TO_SINT:
7729 case ISD::FP_TO_UINT:
7730 case ISD::SINT_TO_FP:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007731 case ISD::UINT_TO_FP:
7732 case ISD::TRUNCATE:
7733 case ISD::ANY_EXTEND:
7734 case ISD::SIGN_EXTEND:
7735 case ISD::ZERO_EXTEND:
7736 case ISD::FP_EXTEND: {
Dan Gohman475871a2008-07-27 21:46:04 +00007737 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007738 SplitVectorOp(Node->getOperand(0), L, H);
7739
Dale Johannesenc6be1102009-02-02 22:49:46 +00007740 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, L);
7741 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00007742 break;
7743 }
Mon P Wang77cdf302008-11-10 20:54:11 +00007744 case ISD::CONVERT_RNDSAT: {
7745 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7746 SDValue L, H;
7747 SplitVectorOp(Node->getOperand(0), L, H);
7748 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7749 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7750 SDValue STyOpL = DAG.getValueType(L.getValueType());
7751 SDValue STyOpH = DAG.getValueType(H.getValueType());
7752
7753 SDValue RndOp = Node->getOperand(3);
7754 SDValue SatOp = Node->getOperand(4);
7755
Dale Johannesenc460ae92009-02-04 00:13:36 +00007756 Lo = DAG.getConvertRndSat(NewVT_Lo, dl, L, DTyOpL, STyOpL,
Mon P Wang77cdf302008-11-10 20:54:11 +00007757 RndOp, SatOp, CvtCode);
Dale Johannesenc460ae92009-02-04 00:13:36 +00007758 Hi = DAG.getConvertRndSat(NewVT_Hi, dl, H, DTyOpH, STyOpH,
Mon P Wang77cdf302008-11-10 20:54:11 +00007759 RndOp, SatOp, CvtCode);
7760 break;
7761 }
Dan Gohman7f321562007-06-25 16:23:39 +00007762 case ISD::LOAD: {
7763 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007764 SDValue Ch = LD->getChain();
7765 SDValue Ptr = LD->getBasePtr();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007766 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007767 const Value *SV = LD->getSrcValue();
7768 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007769 MVT MemoryVT = LD->getMemoryVT();
Dan Gohman7f321562007-06-25 16:23:39 +00007770 unsigned Alignment = LD->getAlignment();
7771 bool isVolatile = LD->isVolatile();
7772
Dan Gohman7f8613e2008-08-14 20:04:46 +00007773 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
Dale Johannesenc6be1102009-02-02 22:49:46 +00007774 SDValue Offset = DAG.getNode(ISD::UNDEF, dl, Ptr.getValueType());
Dan Gohman7f8613e2008-08-14 20:04:46 +00007775
7776 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7777 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7778 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7779
Dale Johannesenc6be1102009-02-02 22:49:46 +00007780 Lo = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
Dan Gohman7f8613e2008-08-14 20:04:46 +00007781 NewVT_Lo, Ch, Ptr, Offset,
7782 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7783 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Dale Johannesenc6be1102009-02-02 22:49:46 +00007784 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00007785 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00007786 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00007787 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007788 Hi = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
Dan Gohman7f8613e2008-08-14 20:04:46 +00007789 NewVT_Hi, Ch, Ptr, Offset,
7790 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00007791
7792 // Build a factor node to remember that this load is independent of the
7793 // other one.
Dale Johannesenc6be1102009-02-02 22:49:46 +00007794 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Chris Lattnerc7029802006-03-18 01:44:44 +00007795 Hi.getValue(1));
7796
7797 // Remember that we legalized the chain.
7798 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00007799 break;
7800 }
Dan Gohman7f321562007-06-25 16:23:39 +00007801 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00007802 // We know the result is a vector. The input may be either a vector or a
7803 // scalar value.
Dan Gohman475871a2008-07-27 21:46:04 +00007804 SDValue InOp = Node->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007805 if (!InOp.getValueType().isVector() ||
7806 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman10a7aa62007-06-29 00:09:08 +00007807 // The input is a scalar or single-element vector.
7808 // Lower to a store/load so that it can be split.
7809 // FIXME: this could be improved probably.
Mon P Wang2920d2b2008-07-15 05:28:34 +00007810 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7811 Op.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00007812 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greifba36cb52008-08-28 21:40:38 +00007813 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Chris Lattner7692eb42006-03-23 21:16:34 +00007814
Dale Johannesenc6be1102009-02-02 22:49:46 +00007815 SDValue St = DAG.getStore(DAG.getEntryNode(), dl,
Dan Gohman69de1932008-02-06 22:27:42 +00007816 InOp, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007817 PseudoSourceValue::getFixedStack(FI), 0);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007818 InOp = DAG.getLoad(Op.getValueType(), dl, St, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007819 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00007820 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00007821 // Split the vector and convert each of the pieces now.
7822 SplitVectorOp(InOp, Lo, Hi);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007823 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT_Lo, Lo);
7824 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00007825 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00007826 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007827 }
7828
7829 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00007830 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00007831 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00007832 assert(isNew && "Value already split?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007833 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007834}
7835
7836
Dan Gohman89b20c02007-06-27 14:06:22 +00007837/// ScalarizeVectorOp - Given an operand of single-element vector type
7838/// (e.g. v1f32), convert it into the equivalent operation that returns a
7839/// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +00007840SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007841 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007842 SDNode *Node = Op.getNode();
Dale Johannesenc6be1102009-02-02 22:49:46 +00007843 DebugLoc dl = Node->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007844 MVT NewVT = Op.getValueType().getVectorElementType();
7845 assert(Op.getValueType().getVectorNumElements() == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00007846
Dan Gohman7f321562007-06-25 16:23:39 +00007847 // See if we already scalarized it.
Dan Gohman475871a2008-07-27 21:46:04 +00007848 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohman7f321562007-06-25 16:23:39 +00007849 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00007850
Dan Gohman475871a2008-07-27 21:46:04 +00007851 SDValue Result;
Chris Lattnerc7029802006-03-18 01:44:44 +00007852 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00007853 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007854#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007855 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007856#endif
Dan Gohman7f321562007-06-25 16:23:39 +00007857 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7858 case ISD::ADD:
7859 case ISD::FADD:
7860 case ISD::SUB:
7861 case ISD::FSUB:
7862 case ISD::MUL:
7863 case ISD::FMUL:
7864 case ISD::SDIV:
7865 case ISD::UDIV:
7866 case ISD::FDIV:
7867 case ISD::SREM:
7868 case ISD::UREM:
7869 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00007870 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007871 case ISD::AND:
7872 case ISD::OR:
7873 case ISD::XOR:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007874 Result = DAG.getNode(Node->getOpcode(), dl,
Chris Lattnerc7029802006-03-18 01:44:44 +00007875 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007876 ScalarizeVectorOp(Node->getOperand(0)),
7877 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00007878 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007879 case ISD::FNEG:
7880 case ISD::FABS:
7881 case ISD::FSQRT:
7882 case ISD::FSIN:
7883 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007884 case ISD::FLOG:
7885 case ISD::FLOG2:
7886 case ISD::FLOG10:
7887 case ISD::FEXP:
7888 case ISD::FEXP2:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007889 case ISD::FP_TO_SINT:
7890 case ISD::FP_TO_UINT:
7891 case ISD::SINT_TO_FP:
7892 case ISD::UINT_TO_FP:
7893 case ISD::SIGN_EXTEND:
7894 case ISD::ZERO_EXTEND:
7895 case ISD::ANY_EXTEND:
7896 case ISD::TRUNCATE:
7897 case ISD::FP_EXTEND:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007898 Result = DAG.getNode(Node->getOpcode(), dl,
Dan Gohman7f321562007-06-25 16:23:39 +00007899 NewVT,
7900 ScalarizeVectorOp(Node->getOperand(0)));
7901 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00007902 case ISD::CONVERT_RNDSAT: {
7903 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
Dale Johannesenc460ae92009-02-04 00:13:36 +00007904 Result = DAG.getConvertRndSat(NewVT, dl, Op0,
Mon P Wang77cdf302008-11-10 20:54:11 +00007905 DAG.getValueType(NewVT),
7906 DAG.getValueType(Op0.getValueType()),
7907 Node->getOperand(3),
7908 Node->getOperand(4),
7909 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7910 break;
7911 }
Dan Gohman9e04c822007-10-12 14:13:46 +00007912 case ISD::FPOWI:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007913 case ISD::FP_ROUND:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007914 Result = DAG.getNode(Node->getOpcode(), dl,
Dan Gohman9e04c822007-10-12 14:13:46 +00007915 NewVT,
7916 ScalarizeVectorOp(Node->getOperand(0)),
7917 Node->getOperand(1));
7918 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007919 case ISD::LOAD: {
7920 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007921 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7922 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman7f8613e2008-08-14 20:04:46 +00007923 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007924 const Value *SV = LD->getSrcValue();
7925 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007926 MVT MemoryVT = LD->getMemoryVT();
7927 unsigned Alignment = LD->getAlignment();
7928 bool isVolatile = LD->isVolatile();
7929
7930 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
Dale Johannesenc6be1102009-02-02 22:49:46 +00007931 SDValue Offset = DAG.getNode(ISD::UNDEF, dl, Ptr.getValueType());
Dan Gohman7f8613e2008-08-14 20:04:46 +00007932
Dale Johannesenc6be1102009-02-02 22:49:46 +00007933 Result = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
Dan Gohman7f8613e2008-08-14 20:04:46 +00007934 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7935 MemoryVT.getVectorElementType(),
7936 isVolatile, Alignment);
Dan Gohman7f321562007-06-25 16:23:39 +00007937
Chris Lattnerc7029802006-03-18 01:44:44 +00007938 // Remember that we legalized the chain.
7939 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7940 break;
7941 }
Dan Gohman7f321562007-06-25 16:23:39 +00007942 case ISD::BUILD_VECTOR:
7943 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007944 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007945 case ISD::INSERT_VECTOR_ELT:
7946 // Returning the inserted scalar element.
7947 Result = Node->getOperand(1);
7948 break;
7949 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007950 assert(Node->getOperand(0).getValueType() == NewVT &&
7951 "Concat of non-legal vectors not yet supported!");
7952 Result = Node->getOperand(0);
7953 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007954 case ISD::VECTOR_SHUFFLE: {
7955 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman475871a2008-07-27 21:46:04 +00007956 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007957 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohman7f321562007-06-25 16:23:39 +00007958 Result = ScalarizeVectorOp(Node->getOperand(1));
7959 else
7960 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007961 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007962 }
7963 case ISD::EXTRACT_SUBVECTOR:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007964 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT,
7965 Node->getOperand(0), Node->getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00007966 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007967 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007968 SDValue Op0 = Op.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007969 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng446efdd2008-05-16 17:19:05 +00007970 Op0 = ScalarizeVectorOp(Op0);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007971 Result = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, Op0);
Chris Lattner5b2316e2006-03-28 20:24:43 +00007972 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007973 }
Dan Gohman7f321562007-06-25 16:23:39 +00007974 case ISD::SELECT:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007975 Result = DAG.getNode(ISD::SELECT, dl, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007976 ScalarizeVectorOp(Op.getOperand(1)),
7977 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007978 break;
Chris Lattner80c1a562008-06-30 02:43:01 +00007979 case ISD::SELECT_CC:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007980 Result = DAG.getNode(ISD::SELECT_CC, dl, NewVT, Node->getOperand(0),
Chris Lattner80c1a562008-06-30 02:43:01 +00007981 Node->getOperand(1),
7982 ScalarizeVectorOp(Op.getOperand(2)),
7983 ScalarizeVectorOp(Op.getOperand(3)),
7984 Node->getOperand(4));
7985 break;
Nate Begeman0d1704b2008-05-12 23:09:43 +00007986 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007987 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7988 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Dale Johannesenc6be1102009-02-02 22:49:46 +00007989 Result = DAG.getNode(ISD::SETCC, dl,
7990 TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00007991 Op0, Op1, Op.getOperand(2));
Dale Johannesenc6be1102009-02-02 22:49:46 +00007992 Result = DAG.getNode(ISD::SELECT, dl, NewVT, Result,
Nate Begeman0d1704b2008-05-12 23:09:43 +00007993 DAG.getConstant(-1ULL, NewVT),
7994 DAG.getConstant(0ULL, NewVT));
7995 break;
7996 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007997 }
7998
7999 if (TLI.isTypeLegal(NewVT))
8000 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00008001 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
8002 assert(isNew && "Value already scalarized?");
Evan Cheng24ac4082008-11-24 07:09:49 +00008003 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00008004 return Result;
8005}
8006
Chris Lattner3e928bb2005-01-07 07:47:09 +00008007
Mon P Wang0c397192008-10-30 08:01:45 +00008008SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
8009 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
8010 if (I != WidenNodes.end()) return I->second;
8011
8012 MVT VT = Op.getValueType();
8013 assert(VT.isVector() && "Cannot widen non-vector type!");
8014
8015 SDValue Result;
8016 SDNode *Node = Op.getNode();
Dale Johannesenc6be1102009-02-02 22:49:46 +00008017 DebugLoc dl = Node->getDebugLoc();
Mon P Wang0c397192008-10-30 08:01:45 +00008018 MVT EVT = VT.getVectorElementType();
8019
8020 unsigned NumElts = VT.getVectorNumElements();
8021 unsigned NewNumElts = WidenVT.getVectorNumElements();
8022 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
8023 assert(NewNumElts < 17);
8024
8025 // When widen is called, it is assumed that it is more efficient to use a
8026 // wide type. The default action is to widen to operation to a wider legal
8027 // vector type and then do the operation if it is legal by calling LegalizeOp
8028 // again. If there is no vector equivalent, we will unroll the operation, do
8029 // it, and rebuild the vector. If most of the operations are vectorizible to
8030 // the legal type, the resulting code will be more efficient. If this is not
8031 // the case, the resulting code will preform badly as we end up generating
8032 // code to pack/unpack the results. It is the function that calls widen
Mon P Wangf007a8b2008-11-06 05:31:54 +00008033 // that is responsible for seeing this doesn't happen.
Mon P Wang0c397192008-10-30 08:01:45 +00008034 switch (Node->getOpcode()) {
8035 default:
8036#ifndef NDEBUG
8037 Node->dump(&DAG);
8038#endif
8039 assert(0 && "Unexpected operation in WidenVectorOp!");
8040 break;
8041 case ISD::CopyFromReg:
Mon P Wang49292f12008-11-15 06:05:52 +00008042 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang0c397192008-10-30 08:01:45 +00008043 case ISD::Constant:
8044 case ISD::ConstantFP:
8045 // To build a vector of these elements, clients should call BuildVector
8046 // and with each element instead of creating a node with a vector type
8047 assert(0 && "Unexpected operation in WidenVectorOp!");
8048 case ISD::VAARG:
8049 // Variable Arguments with vector types doesn't make any sense to me
8050 assert(0 && "Unexpected operation in WidenVectorOp!");
8051 break;
Mon P Wang49292f12008-11-15 06:05:52 +00008052 case ISD::UNDEF:
Dale Johannesenc6be1102009-02-02 22:49:46 +00008053 Result = DAG.getNode(ISD::UNDEF, dl, WidenVT);
Mon P Wang49292f12008-11-15 06:05:52 +00008054 break;
Mon P Wang0c397192008-10-30 08:01:45 +00008055 case ISD::BUILD_VECTOR: {
8056 // Build a vector with undefined for the new nodes
8057 SDValueVector NewOps(Node->op_begin(), Node->op_end());
8058 for (unsigned i = NumElts; i < NewNumElts; ++i) {
Dale Johannesenc6be1102009-02-02 22:49:46 +00008059 NewOps.push_back(DAG.getNode(ISD::UNDEF, dl, EVT));
Mon P Wang0c397192008-10-30 08:01:45 +00008060 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00008061 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT,
8062 &NewOps[0], NewOps.size());
Mon P Wang0c397192008-10-30 08:01:45 +00008063 break;
8064 }
8065 case ISD::INSERT_VECTOR_ELT: {
8066 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008067 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, WidenVT, Tmp1,
Mon P Wang0c397192008-10-30 08:01:45 +00008068 Node->getOperand(1), Node->getOperand(2));
8069 break;
8070 }
8071 case ISD::VECTOR_SHUFFLE: {
8072 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8073 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8074 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
8075 // used as permutation array. We build the vector here instead of widening
8076 // because we don't want to legalize and have it turned to something else.
8077 SDValue PermOp = Node->getOperand(2);
8078 SDValueVector NewOps;
8079 MVT PVT = PermOp.getValueType().getVectorElementType();
8080 for (unsigned i = 0; i < NumElts; ++i) {
8081 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
8082 NewOps.push_back(PermOp.getOperand(i));
8083 } else {
8084 unsigned Idx =
Mon P Wange1a0b2e2008-12-13 08:15:14 +00008085 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
Mon P Wang0c397192008-10-30 08:01:45 +00008086 if (Idx < NumElts) {
8087 NewOps.push_back(PermOp.getOperand(i));
8088 }
8089 else {
8090 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
8091 PermOp.getOperand(i).getValueType()));
8092 }
8093 }
8094 }
8095 for (unsigned i = NumElts; i < NewNumElts; ++i) {
Dale Johannesenc6be1102009-02-02 22:49:46 +00008096 NewOps.push_back(DAG.getNode(ISD::UNDEF, dl, PVT));
Mon P Wang0c397192008-10-30 08:01:45 +00008097 }
8098
Dale Johannesenc6be1102009-02-02 22:49:46 +00008099 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR, dl,
Mon P Wang0c397192008-10-30 08:01:45 +00008100 MVT::getVectorVT(PVT, NewOps.size()),
8101 &NewOps[0], NewOps.size());
8102
Dale Johannesenc6be1102009-02-02 22:49:46 +00008103 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, WidenVT, Tmp1, Tmp2, Tmp3);
Mon P Wang0c397192008-10-30 08:01:45 +00008104 break;
8105 }
8106 case ISD::LOAD: {
8107 // If the load widen returns true, we can use a single load for the
8108 // vector. Otherwise, it is returning a token factor for multiple
8109 // loads.
8110 SDValue TFOp;
8111 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
8112 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
8113 else
8114 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
8115 break;
8116 }
8117
8118 case ISD::BIT_CONVERT: {
8119 SDValue Tmp1 = Node->getOperand(0);
8120 // Converts between two different types so we need to determine
8121 // the correct widen type for the input operand.
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008122 MVT InVT = Tmp1.getValueType();
8123 unsigned WidenSize = WidenVT.getSizeInBits();
8124 if (InVT.isVector()) {
8125 MVT InEltVT = InVT.getVectorElementType();
8126 unsigned InEltSize = InEltVT.getSizeInBits();
8127 assert(WidenSize % InEltSize == 0 &&
8128 "can not widen bit convert that are not multiple of element type");
8129 MVT NewInWidenVT = MVT::getVectorVT(InEltVT, WidenSize / InEltSize);
8130 Tmp1 = WidenVectorOp(Tmp1, NewInWidenVT);
8131 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
Dale Johannesenc6be1102009-02-02 22:49:46 +00008132 Result = DAG.getNode(ISD::BIT_CONVERT, dl, WidenVT, Tmp1);
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008133 } else {
8134 // If the result size is a multiple of the input size, widen the input
8135 // and then convert.
8136 unsigned InSize = InVT.getSizeInBits();
8137 assert(WidenSize % InSize == 0 &&
8138 "can not widen bit convert that are not multiple of element type");
8139 unsigned NewNumElts = WidenSize / InSize;
8140 SmallVector<SDValue, 16> Ops(NewNumElts);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008141 SDValue UndefVal = DAG.getNode(ISD::UNDEF, dl, InVT);
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008142 Ops[0] = Tmp1;
8143 for (unsigned i = 1; i < NewNumElts; ++i)
8144 Ops[i] = UndefVal;
Mon P Wang0c397192008-10-30 08:01:45 +00008145
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008146 MVT NewInVT = MVT::getVectorVT(InVT, NewNumElts);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008147 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, NewInVT, &Ops[0], NewNumElts);
8148 Result = DAG.getNode(ISD::BIT_CONVERT, dl, WidenVT, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00008149 }
8150 break;
8151 }
8152
8153 case ISD::SINT_TO_FP:
8154 case ISD::UINT_TO_FP:
8155 case ISD::FP_TO_SINT:
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008156 case ISD::FP_TO_UINT:
8157 case ISD::FP_ROUND: {
Mon P Wang0c397192008-10-30 08:01:45 +00008158 SDValue Tmp1 = Node->getOperand(0);
8159 // Converts between two different types so we need to determine
8160 // the correct widen type for the input operand.
8161 MVT TVT = Tmp1.getValueType();
8162 assert(TVT.isVector() && "can not widen non vector type");
8163 MVT TEVT = TVT.getVectorElementType();
8164 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
8165 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
8166 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008167 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00008168 break;
8169 }
8170
8171 case ISD::FP_EXTEND:
8172 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
8173 case ISD::TRUNCATE:
8174 case ISD::SIGN_EXTEND:
8175 case ISD::ZERO_EXTEND:
8176 case ISD::ANY_EXTEND:
Mon P Wang0c397192008-10-30 08:01:45 +00008177 case ISD::SIGN_EXTEND_INREG:
8178 case ISD::FABS:
8179 case ISD::FNEG:
8180 case ISD::FSQRT:
8181 case ISD::FSIN:
Mon P Wang49292f12008-11-15 06:05:52 +00008182 case ISD::FCOS:
8183 case ISD::CTPOP:
8184 case ISD::CTTZ:
8185 case ISD::CTLZ: {
Mon P Wang0c397192008-10-30 08:01:45 +00008186 // Unary op widening
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008187 SDValue Tmp1;
Mon P Wang0c397192008-10-30 08:01:45 +00008188 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8189 assert(Tmp1.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008190 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00008191 break;
8192 }
Mon P Wang77cdf302008-11-10 20:54:11 +00008193 case ISD::CONVERT_RNDSAT: {
8194 SDValue RndOp = Node->getOperand(3);
8195 SDValue SatOp = Node->getOperand(4);
Mon P Wang77cdf302008-11-10 20:54:11 +00008196 SDValue SrcOp = Node->getOperand(0);
8197
8198 // Converts between two different types so we need to determine
8199 // the correct widen type for the input operand.
8200 MVT SVT = SrcOp.getValueType();
8201 assert(SVT.isVector() && "can not widen non vector type");
8202 MVT SEVT = SVT.getVectorElementType();
8203 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8204
8205 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8206 assert(SrcOp.getValueType() == WidenVT);
8207 SDValue DTyOp = DAG.getValueType(WidenVT);
8208 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8209 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8210
Dale Johannesenc460ae92009-02-04 00:13:36 +00008211 Result = DAG.getConvertRndSat(WidenVT, dl, SrcOp, DTyOp, STyOp,
Mon P Wang77cdf302008-11-10 20:54:11 +00008212 RndOp, SatOp, CvtCode);
Mon P Wang77cdf302008-11-10 20:54:11 +00008213 break;
8214 }
Mon P Wang0c397192008-10-30 08:01:45 +00008215 case ISD::FPOW:
8216 case ISD::FPOWI:
8217 case ISD::ADD:
8218 case ISD::SUB:
8219 case ISD::MUL:
8220 case ISD::MULHS:
8221 case ISD::MULHU:
8222 case ISD::AND:
8223 case ISD::OR:
8224 case ISD::XOR:
8225 case ISD::FADD:
8226 case ISD::FSUB:
8227 case ISD::FMUL:
8228 case ISD::SDIV:
8229 case ISD::SREM:
8230 case ISD::FDIV:
8231 case ISD::FREM:
8232 case ISD::FCOPYSIGN:
8233 case ISD::UDIV:
8234 case ISD::UREM:
8235 case ISD::BSWAP: {
8236 // Binary op widening
Mon P Wang0c397192008-10-30 08:01:45 +00008237 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8238 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8239 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008240 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008241 break;
8242 }
8243
8244 case ISD::SHL:
8245 case ISD::SRA:
8246 case ISD::SRL: {
Mon P Wang0c397192008-10-30 08:01:45 +00008247 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8248 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangfb13f002008-12-02 07:35:08 +00008249 SDValue ShOp = Node->getOperand(1);
8250 MVT ShVT = ShOp.getValueType();
8251 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8252 WidenVT.getVectorNumElements());
8253 ShOp = WidenVectorOp(ShOp, NewShVT);
8254 assert(ShOp.getValueType() == NewShVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008255 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, ShOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008256 break;
8257 }
Mon P Wangfb13f002008-12-02 07:35:08 +00008258
Mon P Wang0c397192008-10-30 08:01:45 +00008259 case ISD::EXTRACT_VECTOR_ELT: {
8260 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8261 assert(Tmp1.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008262 Result = DAG.getNode(Node->getOpcode(), dl, EVT, Tmp1, Node->getOperand(1));
Mon P Wang0c397192008-10-30 08:01:45 +00008263 break;
8264 }
8265 case ISD::CONCAT_VECTORS: {
8266 // We concurrently support only widen on a multiple of the incoming vector.
8267 // We could widen on a multiple of the incoming operand if necessary.
8268 unsigned NumConcat = NewNumElts / NumElts;
8269 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Dale Johannesenc6be1102009-02-02 22:49:46 +00008270 SDValue UndefVal = DAG.getNode(ISD::UNDEF, dl, VT);
Mon P Wang0c397192008-10-30 08:01:45 +00008271 SmallVector<SDValue, 8> MOps;
8272 MOps.push_back(Op);
8273 for (unsigned i = 1; i != NumConcat; ++i) {
8274 MOps.push_back(UndefVal);
8275 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00008276 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
Mon P Wang0c397192008-10-30 08:01:45 +00008277 &MOps[0], MOps.size()));
8278 break;
8279 }
8280 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang49292f12008-11-15 06:05:52 +00008281 SDValue Tmp1 = Node->getOperand(0);
8282 SDValue Idx = Node->getOperand(1);
8283 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8284 if (CIdx && CIdx->getZExtValue() == 0) {
8285 // Since we are access the start of the vector, the incoming
8286 // vector type might be the proper.
8287 MVT Tmp1VT = Tmp1.getValueType();
8288 if (Tmp1VT == WidenVT)
8289 return Tmp1;
8290 else {
8291 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8292 if (Tmp1VTNumElts < NewNumElts)
8293 Result = WidenVectorOp(Tmp1, WidenVT);
8294 else
Dale Johannesenc6be1102009-02-02 22:49:46 +00008295 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, WidenVT, Tmp1, Idx);
Mon P Wang49292f12008-11-15 06:05:52 +00008296 }
8297 } else if (NewNumElts % NumElts == 0) {
8298 // Widen the extracted subvector.
8299 unsigned NumConcat = NewNumElts / NumElts;
Dale Johannesenc6be1102009-02-02 22:49:46 +00008300 SDValue UndefVal = DAG.getNode(ISD::UNDEF, dl, VT);
Mon P Wang49292f12008-11-15 06:05:52 +00008301 SmallVector<SDValue, 8> MOps;
8302 MOps.push_back(Op);
8303 for (unsigned i = 1; i != NumConcat; ++i) {
8304 MOps.push_back(UndefVal);
8305 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00008306 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
Mon P Wang49292f12008-11-15 06:05:52 +00008307 &MOps[0], MOps.size()));
8308 } else {
8309 assert(0 && "can not widen extract subvector");
8310 // This could be implemented using insert and build vector but I would
8311 // like to see when this happens.
8312 }
Mon P Wang0c397192008-10-30 08:01:45 +00008313 break;
8314 }
8315
8316 case ISD::SELECT: {
Mon P Wang0c397192008-10-30 08:01:45 +00008317 // Determine new condition widen type and widen
8318 SDValue Cond1 = Node->getOperand(0);
8319 MVT CondVT = Cond1.getValueType();
8320 assert(CondVT.isVector() && "can not widen non vector type");
8321 MVT CondEVT = CondVT.getVectorElementType();
8322 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8323 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8324 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8325
8326 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8327 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8328 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008329 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008330 break;
8331 }
8332
8333 case ISD::SELECT_CC: {
Mon P Wang0c397192008-10-30 08:01:45 +00008334 // Determine new condition widen type and widen
8335 SDValue Cond1 = Node->getOperand(0);
8336 SDValue Cond2 = Node->getOperand(1);
8337 MVT CondVT = Cond1.getValueType();
8338 assert(CondVT.isVector() && "can not widen non vector type");
8339 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8340 MVT CondEVT = CondVT.getVectorElementType();
8341 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8342 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8343 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8344 assert(Cond1.getValueType() == CondWidenVT &&
8345 Cond2.getValueType() == CondWidenVT && "condition not widen");
8346
8347 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8348 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8349 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8350 "operands not widen");
Dale Johannesenc6be1102009-02-02 22:49:46 +00008351 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Cond1, Cond2, Tmp1,
Mon P Wang0c397192008-10-30 08:01:45 +00008352 Tmp2, Node->getOperand(4));
Mon P Wang0c397192008-10-30 08:01:45 +00008353 break;
Mon P Wang2eb13c32008-10-30 18:21:52 +00008354 }
8355 case ISD::VSETCC: {
8356 // Determine widen for the operand
8357 SDValue Tmp1 = Node->getOperand(0);
8358 MVT TmpVT = Tmp1.getValueType();
8359 assert(TmpVT.isVector() && "can not widen non vector type");
8360 MVT TmpEVT = TmpVT.getVectorElementType();
8361 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8362 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8363 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008364 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, Tmp2,
Mon P Wang2eb13c32008-10-30 18:21:52 +00008365 Node->getOperand(2));
Mon P Wang0c397192008-10-30 08:01:45 +00008366 break;
8367 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008368 case ISD::ATOMIC_CMP_SWAP:
8369 case ISD::ATOMIC_LOAD_ADD:
8370 case ISD::ATOMIC_LOAD_SUB:
8371 case ISD::ATOMIC_LOAD_AND:
8372 case ISD::ATOMIC_LOAD_OR:
8373 case ISD::ATOMIC_LOAD_XOR:
8374 case ISD::ATOMIC_LOAD_NAND:
8375 case ISD::ATOMIC_LOAD_MIN:
8376 case ISD::ATOMIC_LOAD_MAX:
8377 case ISD::ATOMIC_LOAD_UMIN:
8378 case ISD::ATOMIC_LOAD_UMAX:
8379 case ISD::ATOMIC_SWAP: {
Mon P Wang0c397192008-10-30 08:01:45 +00008380 // For now, we assume that using vectors for these operations don't make
8381 // much sense so we just split it. We return an empty result
8382 SDValue X, Y;
8383 SplitVectorOp(Op, X, Y);
8384 return Result;
8385 break;
8386 }
8387
8388 } // end switch (Node->getOpcode())
8389
8390 assert(Result.getNode() && "Didn't set a result!");
8391 if (Result != Op)
8392 Result = LegalizeOp(Result);
8393
Mon P Wangf007a8b2008-11-06 05:31:54 +00008394 AddWidenedOperand(Op, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00008395 return Result;
8396}
8397
8398// Utility function to find a legal vector type and its associated element
8399// type from a preferred width and whose vector type must be the same size
8400// as the VVT.
8401// TLI: Target lowering used to determine legal types
8402// Width: Preferred width of element type
8403// VVT: Vector value type whose size we must match.
8404// Returns VecEVT and EVT - the vector type and its associated element type
Dan Gohman0d137d72009-01-15 16:43:02 +00008405static void FindWidenVecType(const TargetLowering &TLI, unsigned Width, MVT VVT,
Mon P Wang0c397192008-10-30 08:01:45 +00008406 MVT& EVT, MVT& VecEVT) {
8407 // We start with the preferred width, make it a power of 2 and see if
8408 // we can find a vector type of that width. If not, we reduce it by
8409 // another power of 2. If we have widen the type, a vector of bytes should
8410 // always be legal.
8411 assert(TLI.isTypeLegal(VVT));
8412 unsigned EWidth = Width + 1;
8413 do {
8414 assert(EWidth > 0);
8415 EWidth = (1 << Log2_32(EWidth-1));
8416 EVT = MVT::getIntegerVT(EWidth);
8417 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8418 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8419 } while (!TLI.isTypeLegal(VecEVT) ||
8420 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8421}
8422
8423SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8424 SDValue Chain,
8425 SDValue BasePtr,
8426 const Value *SV,
8427 int SVOffset,
8428 unsigned Alignment,
8429 bool isVolatile,
8430 unsigned LdWidth,
Dale Johannesenc6be1102009-02-02 22:49:46 +00008431 MVT ResType,
8432 DebugLoc dl) {
Mon P Wang0c397192008-10-30 08:01:45 +00008433 // We assume that we have good rules to handle loading power of two loads so
8434 // we break down the operations to power of 2 loads. The strategy is to
8435 // load the largest power of 2 that we can easily transform to a legal vector
8436 // and then insert into that vector, and the cast the result into the legal
8437 // vector that we want. This avoids unnecessary stack converts.
8438 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8439 // the load is nonvolatile, we an use a wider load for the value.
8440 // Find a vector length we can load a large chunk
8441 MVT EVT, VecEVT;
8442 unsigned EVTWidth;
8443 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8444 EVTWidth = EVT.getSizeInBits();
8445
Dale Johannesenc6be1102009-02-02 22:49:46 +00008446 SDValue LdOp = DAG.getLoad(EVT, dl, Chain, BasePtr, SV, SVOffset,
Mon P Wang0c397192008-10-30 08:01:45 +00008447 isVolatile, Alignment);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008448 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecEVT, LdOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008449 LdChain.push_back(LdOp.getValue(1));
8450
8451 // Check if we can load the element with one instruction
8452 if (LdWidth == EVTWidth) {
Dale Johannesenc6be1102009-02-02 22:49:46 +00008453 return DAG.getNode(ISD::BIT_CONVERT, dl, ResType, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008454 }
8455
8456 // The vector element order is endianness dependent.
8457 unsigned Idx = 1;
8458 LdWidth -= EVTWidth;
8459 unsigned Offset = 0;
8460
8461 while (LdWidth > 0) {
8462 unsigned Increment = EVTWidth / 8;
8463 Offset += Increment;
Dale Johannesenc6be1102009-02-02 22:49:46 +00008464 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Mon P Wang0c397192008-10-30 08:01:45 +00008465 DAG.getIntPtrConstant(Increment));
8466
8467 if (LdWidth < EVTWidth) {
8468 // Our current type we are using is too large, use a smaller size by
8469 // using a smaller power of 2
8470 unsigned oEVTWidth = EVTWidth;
8471 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8472 EVTWidth = EVT.getSizeInBits();
8473 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008474 Idx = Idx * (oEVTWidth/EVTWidth);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008475 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008476 }
8477
Dale Johannesenc6be1102009-02-02 22:49:46 +00008478 SDValue LdOp = DAG.getLoad(EVT, dl, Chain, BasePtr, SV,
Mon P Wang0c397192008-10-30 08:01:45 +00008479 SVOffset+Offset, isVolatile,
8480 MinAlign(Alignment, Offset));
8481 LdChain.push_back(LdOp.getValue(1));
Dale Johannesenc6be1102009-02-02 22:49:46 +00008482 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecEVT, VecOp, LdOp,
Mon P Wang0c397192008-10-30 08:01:45 +00008483 DAG.getIntPtrConstant(Idx++));
8484
8485 LdWidth -= EVTWidth;
8486 }
8487
Dale Johannesenc6be1102009-02-02 22:49:46 +00008488 return DAG.getNode(ISD::BIT_CONVERT, dl, ResType, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008489}
8490
8491bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8492 SDValue& TFOp,
8493 SDValue Op,
8494 MVT NVT) {
8495 // TODO: Add support for ConcatVec and the ability to load many vector
8496 // types (e.g., v4i8). This will not work when a vector register
8497 // to memory mapping is strange (e.g., vector elements are not
8498 // stored in some sequential order).
8499
8500 // It must be true that the widen vector type is bigger than where
8501 // we need to load from.
8502 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8503 MVT LdVT = LD->getMemoryVT();
Dale Johannesenc6be1102009-02-02 22:49:46 +00008504 DebugLoc dl = LD->getDebugLoc();
Mon P Wang0c397192008-10-30 08:01:45 +00008505 assert(LdVT.isVector() && NVT.isVector());
8506 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8507
8508 // Load information
8509 SDValue Chain = LD->getChain();
8510 SDValue BasePtr = LD->getBasePtr();
8511 int SVOffset = LD->getSrcValueOffset();
8512 unsigned Alignment = LD->getAlignment();
8513 bool isVolatile = LD->isVolatile();
8514 const Value *SV = LD->getSrcValue();
8515 unsigned int LdWidth = LdVT.getSizeInBits();
8516
8517 // Load value as a large register
8518 SDValueVector LdChain;
8519 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
Dale Johannesenc6be1102009-02-02 22:49:46 +00008520 Alignment, isVolatile, LdWidth, NVT, dl);
Mon P Wang0c397192008-10-30 08:01:45 +00008521
8522 if (LdChain.size() == 1) {
8523 TFOp = LdChain[0];
8524 return true;
8525 }
8526 else {
Dale Johannesenc6be1102009-02-02 22:49:46 +00008527 TFOp=DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8528 &LdChain[0], LdChain.size());
Mon P Wang0c397192008-10-30 08:01:45 +00008529 return false;
8530 }
8531}
8532
8533
8534void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8535 SDValue Chain,
8536 SDValue BasePtr,
8537 const Value *SV,
8538 int SVOffset,
8539 unsigned Alignment,
8540 bool isVolatile,
Mon P Wang49292f12008-11-15 06:05:52 +00008541 SDValue ValOp,
Dale Johannesenc6be1102009-02-02 22:49:46 +00008542 unsigned StWidth,
8543 DebugLoc dl) {
Mon P Wang0c397192008-10-30 08:01:45 +00008544 // Breaks the stores into a series of power of 2 width stores. For any
8545 // width, we convert the vector to the vector of element size that we
8546 // want to store. This avoids requiring a stack convert.
8547
8548 // Find a width of the element type we can store with
8549 MVT VVT = ValOp.getValueType();
8550 MVT EVT, VecEVT;
8551 unsigned EVTWidth;
8552 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8553 EVTWidth = EVT.getSizeInBits();
8554
Dale Johannesenc6be1102009-02-02 22:49:46 +00008555 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, ValOp);
8556 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT, VecOp,
Mon P Wange0b436a2008-11-06 22:52:21 +00008557 DAG.getIntPtrConstant(0));
Dale Johannesenc6be1102009-02-02 22:49:46 +00008558 SDValue StOp = DAG.getStore(Chain, dl, EOp, BasePtr, SV, SVOffset,
Mon P Wang0c397192008-10-30 08:01:45 +00008559 isVolatile, Alignment);
8560 StChain.push_back(StOp);
8561
8562 // Check if we are done
8563 if (StWidth == EVTWidth) {
8564 return;
8565 }
8566
8567 unsigned Idx = 1;
8568 StWidth -= EVTWidth;
8569 unsigned Offset = 0;
8570
8571 while (StWidth > 0) {
8572 unsigned Increment = EVTWidth / 8;
8573 Offset += Increment;
Dale Johannesenc6be1102009-02-02 22:49:46 +00008574 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Mon P Wang0c397192008-10-30 08:01:45 +00008575 DAG.getIntPtrConstant(Increment));
8576
8577 if (StWidth < EVTWidth) {
8578 // Our current type we are using is too large, use a smaller size by
8579 // using a smaller power of 2
8580 unsigned oEVTWidth = EVTWidth;
8581 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8582 EVTWidth = EVT.getSizeInBits();
8583 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008584 Idx = Idx * (oEVTWidth/EVTWidth);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008585 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008586 }
8587
Dale Johannesenc6be1102009-02-02 22:49:46 +00008588 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT, VecOp,
Mon P Wang49292f12008-11-15 06:05:52 +00008589 DAG.getIntPtrConstant(Idx++));
Dale Johannesenc6be1102009-02-02 22:49:46 +00008590 StChain.push_back(DAG.getStore(Chain, dl, EOp, BasePtr, SV,
Mon P Wang0c397192008-10-30 08:01:45 +00008591 SVOffset + Offset, isVolatile,
8592 MinAlign(Alignment, Offset)));
8593 StWidth -= EVTWidth;
8594 }
8595}
8596
8597
8598SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8599 SDValue Chain,
8600 SDValue BasePtr) {
8601 // TODO: It might be cleaner if we can use SplitVector and have more legal
8602 // vector types that can be stored into memory (e.g., v4xi8 can
8603 // be stored as a word). This will not work when a vector register
8604 // to memory mapping is strange (e.g., vector elements are not
8605 // stored in some sequential order).
8606
8607 MVT StVT = ST->getMemoryVT();
8608 SDValue ValOp = ST->getValue();
Dale Johannesenc6be1102009-02-02 22:49:46 +00008609 DebugLoc dl = ST->getDebugLoc();
Mon P Wang0c397192008-10-30 08:01:45 +00008610
8611 // Check if we have widen this node with another value
8612 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8613 if (I != WidenNodes.end())
8614 ValOp = I->second;
8615
8616 MVT VVT = ValOp.getValueType();
8617
8618 // It must be true that we the widen vector type is bigger than where
8619 // we need to store.
8620 assert(StVT.isVector() && VVT.isVector());
Dan Gohmanf83c81a2009-01-28 03:10:52 +00008621 assert(StVT.bitsLT(VVT));
Mon P Wang0c397192008-10-30 08:01:45 +00008622 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8623
8624 // Store value
8625 SDValueVector StChain;
8626 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8627 ST->getSrcValueOffset(), ST->getAlignment(),
Dale Johannesenc6be1102009-02-02 22:49:46 +00008628 ST->isVolatile(), ValOp, StVT.getSizeInBits(), dl);
Mon P Wang0c397192008-10-30 08:01:45 +00008629 if (StChain.size() == 1)
8630 return StChain[0];
8631 else
Dale Johannesenc6be1102009-02-02 22:49:46 +00008632 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8633 &StChain[0], StChain.size());
Mon P Wang0c397192008-10-30 08:01:45 +00008634}
8635
8636
Chris Lattner3e928bb2005-01-07 07:47:09 +00008637// SelectionDAG::Legalize - This is the entry point for the file.
8638//
Duncan Sandsb6862bb2008-12-14 09:43:15 +00008639void SelectionDAG::Legalize(bool TypesNeedLegalizing) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00008640 /// run - This is the main entry point to this class.
8641 ///
Duncan Sandsb6862bb2008-12-14 09:43:15 +00008642 SelectionDAGLegalize(*this, TypesNeedLegalizing).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00008643}
8644