blob: 01b0a82be9d666ebfd522a53f3ce80f2f331246c [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;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000559 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000560
561 // If a FP immediate is precise when represented as a float and if the
562 // target can do an extending load from float to double, we put it into
563 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000564 // double. This shrinks FP constants and canonicalizes them for targets where
565 // an FP extending load is the same cost as a normal load (such as on the x87
566 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000567 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000568 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000569 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000570 if (VT!=MVT::f64 && VT!=MVT::f32)
571 assert(0 && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000572 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000573 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000574 }
575
Duncan Sands83ec4b62008-06-06 12:08:01 +0000576 MVT OrigVT = VT;
577 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000578 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000579 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000580 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
581 // Only do this if the target has a native EXTLOAD instruction from
582 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000583 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000584 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000585 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000586 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
587 VT = SVT;
588 Extend = true;
589 }
Evan Cheng00495212006-12-12 21:32:44 +0000590 }
591
Dan Gohman475871a2008-07-27 21:46:04 +0000592 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +0000593 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000594 if (Extend)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000595 return DAG.getExtLoad(ISD::EXTLOAD, dl,
Dale Johannesen39355f92009-02-04 02:34:38 +0000596 OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000597 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000598 0, VT, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000599 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000600 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000601}
602
Chris Lattner6831a812006-02-13 09:18:02 +0000603
Evan Cheng912095b2007-01-04 21:56:39 +0000604/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
605/// operations.
606static
Dan Gohman475871a2008-07-27 21:46:04 +0000607SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
Dan Gohman0d137d72009-01-15 16:43:02 +0000608 SelectionDAG &DAG,
609 const TargetLowering &TLI) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000610 DebugLoc dl = Node->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000611 MVT VT = Node->getValueType(0);
612 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000613 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
614 "fcopysign expansion only supported for f32 and f64");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000615 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000616
Evan Cheng912095b2007-01-04 21:56:39 +0000617 // First get the sign bit of second operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000618 SDValue Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000619 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
620 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000621 Mask1 = DAG.getNode(ISD::BIT_CONVERT, dl, SrcNVT, Mask1);
622 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, dl, SrcNVT,
623 Node->getOperand(1));
624 SignBit = DAG.getNode(ISD::AND, dl, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000625 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000626 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Evan Cheng912095b2007-01-04 21:56:39 +0000627 if (SizeDiff > 0) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000628 SignBit = DAG.getNode(ISD::SRL, dl, SrcNVT, SignBit,
Evan Cheng912095b2007-01-04 21:56:39 +0000629 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000630 SignBit = DAG.getNode(ISD::TRUNCATE, dl, NVT, SignBit);
Chris Lattnerc563e1d2008-07-10 23:46:13 +0000631 } else if (SizeDiff < 0) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000632 SignBit = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, SignBit);
633 SignBit = DAG.getNode(ISD::SHL, dl, NVT, SignBit,
Chris Lattnerc563e1d2008-07-10 23:46:13 +0000634 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
635 }
Evan Cheng068c5f42007-01-05 21:31:51 +0000636
637 // Clear the sign bit of first operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000638 SDValue Mask2 = (VT == MVT::f64)
Evan Cheng068c5f42007-01-05 21:31:51 +0000639 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
640 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000641 Mask2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask2);
642 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
643 Result = DAG.getNode(ISD::AND, dl, NVT, Result, Mask2);
Evan Cheng068c5f42007-01-05 21:31:51 +0000644
645 // Or the value with the sign bit.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000646 Result = DAG.getNode(ISD::OR, dl, NVT, Result, SignBit);
Evan Cheng912095b2007-01-04 21:56:39 +0000647 return Result;
648}
649
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000650/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
651static
Dan Gohman475871a2008-07-27 21:46:04 +0000652SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000653 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000654 SDValue Chain = ST->getChain();
655 SDValue Ptr = ST->getBasePtr();
656 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000657 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000658 int Alignment = ST->getAlignment();
659 int SVOffset = ST->getSrcValueOffset();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000660 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000661 if (ST->getMemoryVT().isFloatingPoint() ||
662 ST->getMemoryVT().isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000663 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
664 if (TLI.isTypeLegal(intVT)) {
665 // Expand to a bitconvert of the value to the integer type of the
666 // same size, then a (misaligned) int store.
667 // FIXME: Does not handle truncating floating point stores!
Dale Johannesenbb5da912009-02-02 20:41:04 +0000668 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, intVT, Val);
669 return DAG.getStore(Chain, dl, Result, Ptr, ST->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000670 SVOffset, ST->isVolatile(), Alignment);
671 } else {
672 // Do a (aligned) store to a stack slot, then copy from the stack slot
673 // to the final destination using (unaligned) integer loads and stores.
674 MVT StoredVT = ST->getMemoryVT();
675 MVT RegVT =
676 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
677 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
678 unsigned RegBytes = RegVT.getSizeInBits() / 8;
679 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000680
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000681 // Make sure the stack slot is also aligned for the register type.
682 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
683
684 // Perform the original store, only redirected to the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000685 SDValue Store = DAG.getTruncStore(Chain, dl,
686 Val, StackPtr, NULL, 0,StoredVT);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000687 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
688 SmallVector<SDValue, 8> Stores;
689 unsigned Offset = 0;
690
691 // Do all but one copies using the full register width.
692 for (unsigned i = 1; i < NumRegs; i++) {
693 // Load one integer register's worth from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000694 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, NULL, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000695 // Store it to the final location. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000696 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000697 ST->getSrcValue(), SVOffset + Offset,
698 ST->isVolatile(),
699 MinAlign(ST->getAlignment(), Offset)));
700 // Increment the pointers.
701 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000702 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000703 Increment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000704 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000705 }
706
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000707 // The last store may be partial. Do a truncating store. On big-endian
708 // machines this requires an extending load from the stack slot to ensure
709 // that the bits are in the right place.
710 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000711
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000712 // Load from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000713 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000714 NULL, 0, MemVT);
715
Dale Johannesenbb5da912009-02-02 20:41:04 +0000716 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000717 ST->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000718 MemVT, ST->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000719 MinAlign(ST->getAlignment(), Offset)));
720 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000721 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000722 Stores.size());
723 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000724 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000725 assert(ST->getMemoryVT().isInteger() &&
726 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000727 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000728 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000729 MVT NewStoredVT =
730 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
731 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000732 int IncrementSize = NumBits / 8;
733
734 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000735 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
736 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000737 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000738
739 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000740 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000741 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000742 ST->getSrcValue(), SVOffset, NewStoredVT,
743 ST->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000744 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000745 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000746 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000747 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000748 ST->getSrcValue(), SVOffset + IncrementSize,
749 NewStoredVT, ST->isVolatile(), Alignment);
750
Dale Johannesenbb5da912009-02-02 20:41:04 +0000751 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000752}
753
754/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
755static
Dan Gohman475871a2008-07-27 21:46:04 +0000756SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000757 const TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000758 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000759 SDValue Chain = LD->getChain();
760 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000761 MVT VT = LD->getValueType(0);
762 MVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000763 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000764 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000765 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
766 if (TLI.isTypeLegal(intVT)) {
767 // Expand to a (misaligned) integer load of the same size,
768 // then bitconvert to floating point or vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000769 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000770 SVOffset, LD->isVolatile(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000771 LD->getAlignment());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000772 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000773 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000774 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000775
Duncan Sands05e11fa2008-12-12 21:47:02 +0000776 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000777 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000778 } else {
779 // Copy the value to a (aligned) stack slot using (unaligned) integer
780 // loads and stores, then do a (aligned) load from the stack slot.
781 MVT RegVT = TLI.getRegisterType(intVT);
782 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
783 unsigned RegBytes = RegVT.getSizeInBits() / 8;
784 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
785
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000786 // Make sure the stack slot is also aligned for the register type.
787 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
788
Duncan Sands05e11fa2008-12-12 21:47:02 +0000789 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
790 SmallVector<SDValue, 8> Stores;
791 SDValue StackPtr = StackBase;
792 unsigned Offset = 0;
793
794 // Do all but one copies using the full register width.
795 for (unsigned i = 1; i < NumRegs; i++) {
796 // Load one integer register's worth from the original location.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000797 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000798 SVOffset + Offset, LD->isVolatile(),
799 MinAlign(LD->getAlignment(), Offset));
800 // Follow the load with a store to the stack slot. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000801 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000802 NULL, 0));
803 // Increment the pointers.
804 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000805 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
806 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000807 Increment);
808 }
809
810 // The last copy may be partial. Do an extending load.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000811 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000812 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000813 LD->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000814 MemVT, LD->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000815 MinAlign(LD->getAlignment(), Offset));
816 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000817 // On big-endian machines this requires a truncating store to ensure
818 // that the bits end up in the right place.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000819 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000820 NULL, 0, MemVT));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000821
822 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000823 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000824 Stores.size());
825
826 // Finally, perform the original load only redirected to the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000827 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000828 NULL, 0, LoadedVT);
829
830 // Callers expect a MERGE_VALUES node.
831 SDValue Ops[] = { Load, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000832 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000833 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000834 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000835 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000836 "Unaligned load of unsupported type.");
837
Dale Johannesen8155d642008-02-27 22:36:00 +0000838 // Compute the new VT that is half the size of the old one. This is an
839 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000840 unsigned NumBits = LoadedVT.getSizeInBits();
841 MVT NewLoadedVT;
842 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000843 NumBits >>= 1;
844
845 unsigned Alignment = LD->getAlignment();
846 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000847 ISD::LoadExtType HiExtType = LD->getExtensionType();
848
849 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
850 if (HiExtType == ISD::NON_EXTLOAD)
851 HiExtType = ISD::ZEXTLOAD;
852
853 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000854 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000855 if (TLI.isLittleEndian()) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000856 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000857 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000858 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000859 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000860 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000861 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000862 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000863 } else {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000864 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
865 SVOffset, NewLoadedVT,LD->isVolatile(), Alignment);
866 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000867 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000868 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000869 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000870 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000871 }
872
873 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000874 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000875 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
876 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000877
Dale Johannesenbb5da912009-02-02 20:41:04 +0000878 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000879 Hi.getValue(1));
880
Dan Gohman475871a2008-07-27 21:46:04 +0000881 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000882 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000883}
Evan Cheng912095b2007-01-04 21:56:39 +0000884
Dan Gohman82669522007-10-11 23:57:53 +0000885/// UnrollVectorOp - We know that the given vector has a legal type, however
886/// the operation it performs is not legal and is an operation that we have
887/// no way of lowering. "Unroll" the vector, splitting out the scalars and
888/// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000889SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000890 MVT VT = Op.getValueType();
Dan Gohman82669522007-10-11 23:57:53 +0000891 assert(isTypeLegal(VT) &&
892 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000893 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman82669522007-10-11 23:57:53 +0000894 "Can't unroll a vector with multiple results!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000895 unsigned NE = VT.getVectorNumElements();
896 MVT EltVT = VT.getVectorElementType();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000897 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman82669522007-10-11 23:57:53 +0000898
Dan Gohman475871a2008-07-27 21:46:04 +0000899 SmallVector<SDValue, 8> Scalars;
900 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman82669522007-10-11 23:57:53 +0000901 for (unsigned i = 0; i != NE; ++i) {
902 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +0000903 SDValue Operand = Op.getOperand(j);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000904 MVT OperandVT = Operand.getValueType();
905 if (OperandVT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +0000906 // A vector operand; extract a single element.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000907 MVT OperandEltVT = OperandVT.getVectorElementType();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000908 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dan Gohman82669522007-10-11 23:57:53 +0000909 OperandEltVT,
910 Operand,
911 DAG.getConstant(i, MVT::i32));
912 } else {
913 // A scalar operand; just use it as is.
914 Operands[j] = Operand;
915 }
916 }
Mon P Wange9f10152008-12-09 05:46:39 +0000917
918 switch (Op.getOpcode()) {
919 default:
Dale Johannesenbb5da912009-02-02 20:41:04 +0000920 Scalars.push_back(DAG.getNode(Op.getOpcode(), dl, EltVT,
Mon P Wange9f10152008-12-09 05:46:39 +0000921 &Operands[0], Operands.size()));
922 break;
923 case ISD::SHL:
924 case ISD::SRA:
925 case ISD::SRL:
Duncan Sands92abc622009-01-31 15:50:11 +0000926 case ISD::ROTL:
927 case ISD::ROTR:
Dale Johannesenbb5da912009-02-02 20:41:04 +0000928 Scalars.push_back(DAG.getNode(Op.getOpcode(), dl, EltVT, Operands[0],
Duncan Sands92abc622009-01-31 15:50:11 +0000929 DAG.getShiftAmountOperand(Operands[1])));
Mon P Wange9f10152008-12-09 05:46:39 +0000930 break;
931 }
Dan Gohman82669522007-10-11 23:57:53 +0000932 }
933
Dale Johannesenbb5da912009-02-02 20:41:04 +0000934 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Scalars[0], Scalars.size());
Dan Gohman82669522007-10-11 23:57:53 +0000935}
936
Duncan Sands007f9842008-01-10 10:28:30 +0000937/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000938static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000939 RTLIB::Libcall Call_F32,
940 RTLIB::Libcall Call_F64,
941 RTLIB::Libcall Call_F80,
942 RTLIB::Libcall Call_PPCF128) {
943 return
944 VT == MVT::f32 ? Call_F32 :
945 VT == MVT::f64 ? Call_F64 :
946 VT == MVT::f80 ? Call_F80 :
947 VT == MVT::ppcf128 ? Call_PPCF128 :
948 RTLIB::UNKNOWN_LIBCALL;
949}
950
Nate Begeman68679912008-04-25 18:07:40 +0000951/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
952/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
953/// is necessary to spill the vector being inserted into to memory, perform
954/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000955SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000956PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
957 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000958 SDValue Tmp1 = Vec;
959 SDValue Tmp2 = Val;
960 SDValue Tmp3 = Idx;
Nate Begeman68679912008-04-25 18:07:40 +0000961
962 // If the target doesn't support this, we have to spill the input vector
963 // to a temporary stack slot, update the element, then reload it. This is
964 // badness. We could also load the value into a vector register (either
965 // with a "move to register" or "extload into register" instruction, then
966 // permute it into place, if the idx is a constant and if the idx is
967 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000968 MVT VT = Tmp1.getValueType();
969 MVT EltVT = VT.getVectorElementType();
970 MVT IdxVT = Tmp3.getValueType();
971 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000972 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000973
Gabor Greifba36cb52008-08-28 21:40:38 +0000974 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000975
976 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000977 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000978 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000979
980 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000981 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000982 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000983 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000984 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000985 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
986 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000987 // Store the scalar value.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000988 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000989 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000990 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000991 return DAG.getLoad(VT, dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +0000992 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000993}
994
Mon P Wange9f10152008-12-09 05:46:39 +0000995
Dan Gohmana3466152007-07-13 20:14:11 +0000996/// LegalizeOp - We know that the specified value has a legal type, and
997/// that its operands are legal. Now ensure that the operation itself
998/// is legal, recursively ensuring that the operands' operations remain
999/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +00001000SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +00001001 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
1002 return Op;
1003
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001004 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +00001005 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001006 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001007 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +00001008
Chris Lattner3e928bb2005-01-07 07:47:09 +00001009 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +00001010 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +00001011 if (Node->getNumValues() > 1) {
1012 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +00001013 if (getTypeAction(Node->getValueType(i)) != Legal) {
1014 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001015 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +00001016 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +00001017 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +00001018 }
1019 }
1020
Chris Lattner45982da2005-05-12 16:53:42 +00001021 // Note that LegalizeOp may be reentered even from single-use nodes, which
1022 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00001023 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001024 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001025
Dan Gohman475871a2008-07-27 21:46:04 +00001026 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
1027 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +00001028 bool isCustom = false;
1029
Chris Lattner3e928bb2005-01-07 07:47:09 +00001030 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +00001031 case ISD::FrameIndex:
1032 case ISD::EntryToken:
1033 case ISD::Register:
1034 case ISD::BasicBlock:
1035 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +00001036 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +00001037 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +00001038 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +00001039 case ISD::TargetConstantPool:
1040 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001041 case ISD::TargetGlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001042 case ISD::TargetExternalSymbol:
Chris Lattner948c1b12006-01-28 08:31:04 +00001043 case ISD::VALUETYPE:
1044 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +00001045 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +00001046 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +00001047 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +00001048 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00001049 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +00001050 "This must be legal!");
1051 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001052 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001053 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1054 // If this is a target node, legalize it by legalizing the operands then
1055 // passing it through.
Dan Gohman475871a2008-07-27 21:46:04 +00001056 SmallVector<SDValue, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001057 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001058 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001059
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001060 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001061
1062 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1063 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001064 return Result.getValue(Op.getResNo());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001065 }
1066 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001067#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00001068 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001069#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00001070 assert(0 && "Do not know how to legalize this operator!");
1071 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +00001072 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001073 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001074 case ISD::GlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001075 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +00001076 case ISD::ConstantPool:
1077 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001078 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1079 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +00001080 case TargetLowering::Custom:
1081 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001082 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner948c1b12006-01-28 08:31:04 +00001083 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001084 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001085 break;
1086 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001087 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +00001088 case ISD::FRAMEADDR:
1089 case ISD::RETURNADDR:
1090 // The only option for these nodes is to custom lower them. If the target
1091 // does not custom lower them, then return zero.
1092 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001093 if (Tmp1.getNode())
Nate Begemanbcc5f362007-01-29 22:58:52 +00001094 Result = Tmp1;
1095 else
1096 Result = DAG.getConstant(0, TLI.getPointerTy());
1097 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001098 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001099 MVT VT = Node->getValueType(0);
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001100 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1101 default: assert(0 && "This action is not supported yet!");
1102 case TargetLowering::Custom:
1103 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001104 if (Result.getNode()) break;
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001105 // Fall Thru
1106 case TargetLowering::Legal:
1107 Result = DAG.getConstant(0, VT);
1108 break;
1109 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001110 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001111 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001112 case ISD::EXCEPTIONADDR: {
1113 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001114 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001115 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1116 default: assert(0 && "This action is not supported yet!");
1117 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +00001118 unsigned Reg = TLI.getExceptionAddressRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +00001119 Result = DAG.getCopyFromReg(Tmp1, dl, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001120 }
1121 break;
1122 case TargetLowering::Custom:
1123 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001124 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001125 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001126 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001127 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Dale Johannesenca57b842009-02-02 23:46:53 +00001128 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001129 break;
1130 }
1131 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001132 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001133 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001134
Gabor Greifba36cb52008-08-28 21:40:38 +00001135 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001136 "Cannot return more than two values!");
1137
1138 // Since we produced two values, make sure to remember that we
1139 // legalized both of them.
1140 Tmp1 = LegalizeOp(Result);
1141 Tmp2 = LegalizeOp(Result.getValue(1));
1142 AddLegalizedOperand(Op.getValue(0), Tmp1);
1143 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001144 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +00001145 case ISD::EHSELECTION: {
1146 Tmp1 = LegalizeOp(Node->getOperand(0));
1147 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001148 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +00001149 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1150 default: assert(0 && "This action is not supported yet!");
1151 case TargetLowering::Expand: {
1152 unsigned Reg = TLI.getExceptionSelectorRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +00001153 Result = DAG.getCopyFromReg(Tmp2, dl, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +00001154 }
1155 break;
1156 case TargetLowering::Custom:
1157 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001158 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +00001159 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001160 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001161 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Dale Johannesenca57b842009-02-02 23:46:53 +00001162 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey8782d482007-02-28 20:43:58 +00001163 break;
1164 }
1165 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001166 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001167 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001168
Gabor Greifba36cb52008-08-28 21:40:38 +00001169 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001170 "Cannot return more than two values!");
1171
1172 // Since we produced two values, make sure to remember that we
1173 // legalized both of them.
1174 Tmp1 = LegalizeOp(Result);
1175 Tmp2 = LegalizeOp(Result.getValue(1));
1176 AddLegalizedOperand(Op.getValue(0), Tmp1);
1177 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001178 return Op.getResNo() ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001179 case ISD::EH_RETURN: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001180 MVT VT = Node->getValueType(0);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001181 // The only "good" option for this node is to custom lower it.
1182 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1183 default: assert(0 && "This action is not supported at all!");
1184 case TargetLowering::Custom:
1185 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001186 if (Result.getNode()) break;
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001187 // Fall Thru
1188 case TargetLowering::Legal:
1189 // Target does not know, how to lower this, lower to noop
1190 Result = LegalizeOp(Node->getOperand(0));
1191 break;
1192 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001193 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001194 break;
Chris Lattner08951a32005-09-02 01:15:01 +00001195 case ISD::AssertSext:
1196 case ISD::AssertZext:
1197 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001198 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +00001199 break;
Chris Lattner308575b2005-11-20 22:56:56 +00001200 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001201 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif99a6cb92008-08-26 22:36:50 +00001202 Result = Node->getOperand(Op.getResNo());
Chris Lattner456a93a2006-01-28 07:39:30 +00001203 break;
Chris Lattner69a52152005-01-14 22:38:01 +00001204 case ISD::CopyFromReg:
1205 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001206 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001207 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001208 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +00001209 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001210 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001211 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001212 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001213 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1214 } else {
1215 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1216 }
Chris Lattner7310fb12005-12-18 15:27:43 +00001217 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1218 }
Chris Lattner13c184d2005-01-28 06:27:38 +00001219 // Since CopyFromReg produces two values, make sure to remember that we
1220 // legalized both of them.
1221 AddLegalizedOperand(Op.getValue(0), Result);
1222 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001223 return Result.getValue(Op.getResNo());
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001224 case ISD::UNDEF: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001225 MVT VT = Op.getValueType();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001226 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +00001227 default: assert(0 && "This action is not supported yet!");
1228 case TargetLowering::Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001229 if (VT.isInteger())
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001230 Result = DAG.getConstant(0, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001231 else if (VT.isFloatingPoint())
1232 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf41db212007-09-26 17:26:49 +00001233 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001234 else
1235 assert(0 && "Unknown value type!");
1236 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001237 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001238 break;
1239 }
1240 break;
1241 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001242
Chris Lattner48b61a72006-03-28 00:40:33 +00001243 case ISD::INTRINSIC_W_CHAIN:
1244 case ISD::INTRINSIC_WO_CHAIN:
1245 case ISD::INTRINSIC_VOID: {
Dan Gohman475871a2008-07-27 21:46:04 +00001246 SmallVector<SDValue, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001247 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1248 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001249 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001250
1251 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001252 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001253 TargetLowering::Custom) {
1254 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001255 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001256 }
1257
Gabor Greifba36cb52008-08-28 21:40:38 +00001258 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001259
1260 // Must have return value and chain result.
Gabor Greifba36cb52008-08-28 21:40:38 +00001261 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattner13fc2f12006-03-27 20:28:29 +00001262 "Cannot return more than two values!");
1263
1264 // Since loads produce two values, make sure to remember that we
1265 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001266 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1267 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001268 return Result.getValue(Op.getResNo());
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001269 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001270
Dan Gohman7f460202008-06-30 20:59:49 +00001271 case ISD::DBG_STOPPOINT:
1272 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +00001273 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1274
Dan Gohman7f460202008-06-30 20:59:49 +00001275 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +00001276 case TargetLowering::Promote:
1277 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001278 case TargetLowering::Expand: {
Devang Patel83489bb2009-01-13 00:35:13 +00001279 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf560ffa2009-01-28 17:46:25 +00001280 bool useDEBUG_LOC = TLI.isOperationLegalOrCustom(ISD::DEBUG_LOC,
1281 MVT::Other);
1282 bool useLABEL = TLI.isOperationLegalOrCustom(ISD::DBG_LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001283
Dan Gohman7f460202008-06-30 20:59:49 +00001284 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Devang Patel83489bb2009-01-13 00:35:13 +00001285 GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
1286 if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
1287 DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
1288 unsigned SrcFile = DW->RecordSource(CU.getDirectory(),
1289 CU.getFilename());
1290
Dan Gohman7f460202008-06-30 20:59:49 +00001291 unsigned Line = DSP->getLine();
1292 unsigned Col = DSP->getColumn();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001293
Dale Johannesenca57b842009-02-02 23:46:53 +00001294 // A bit self-referential to have DebugLoc on Debug_Loc nodes, but
1295 // it won't hurt anything.
Jim Laskeyabf6d172006-01-05 01:25:28 +00001296 if (useDEBUG_LOC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001297 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Cheng71e86852008-07-08 20:06:39 +00001298 DAG.getConstant(Col, MVT::i32),
1299 DAG.getConstant(SrcFile, MVT::i32) };
Dale Johannesenca57b842009-02-02 23:46:53 +00001300 Result = DAG.getNode(ISD::DEBUG_LOC, dl, MVT::Other, Ops, 4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001301 } else {
Devang Patel83489bb2009-01-13 00:35:13 +00001302 unsigned ID = DW->RecordSourceLine(Line, Col, SrcFile);
Dale Johannesenca57b842009-02-02 23:46:53 +00001303 Result = DAG.getLabel(ISD::DBG_LABEL, dl, Tmp1, ID);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001304 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001305 } else {
1306 Result = Tmp1; // chain
1307 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001308 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001309 }
Evan Cheng71e86852008-07-08 20:06:39 +00001310 case TargetLowering::Legal: {
1311 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1312 if (Action == Legal && Tmp1 == Node->getOperand(0))
1313 break;
1314
Dan Gohman475871a2008-07-27 21:46:04 +00001315 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001316 Ops.push_back(Tmp1);
1317 if (Action == Legal) {
1318 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1319 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1320 } else {
1321 // Otherwise promote them.
1322 Ops.push_back(PromoteOp(Node->getOperand(1)));
1323 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner36ce6912005-11-29 06:21:05 +00001324 }
Evan Cheng71e86852008-07-08 20:06:39 +00001325 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1326 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1327 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001328 break;
1329 }
Evan Cheng71e86852008-07-08 20:06:39 +00001330 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001331 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001332
1333 case ISD::DECLARE:
1334 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1335 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1336 default: assert(0 && "This action is not supported yet!");
1337 case TargetLowering::Legal:
1338 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1339 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1340 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1341 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1342 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001343 case TargetLowering::Expand:
1344 Result = LegalizeOp(Node->getOperand(0));
1345 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001346 }
1347 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001348
1349 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001350 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001351 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001352 default: assert(0 && "This action is not supported yet!");
Evan Cheng71e86852008-07-08 20:06:39 +00001353 case TargetLowering::Legal: {
1354 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001355 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Cheng71e86852008-07-08 20:06:39 +00001356 if (Action == Legal && Tmp1 == Node->getOperand(0))
1357 break;
1358 if (Action == Legal) {
1359 Tmp2 = Node->getOperand(1);
1360 Tmp3 = Node->getOperand(2);
1361 Tmp4 = Node->getOperand(3);
1362 } else {
1363 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1364 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1365 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1366 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001367 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001368 break;
1369 }
Evan Cheng71e86852008-07-08 20:06:39 +00001370 }
Jim Laskeyabf6d172006-01-05 01:25:28 +00001371 break;
1372
Dan Gohman44066042008-07-01 00:05:16 +00001373 case ISD::DBG_LABEL:
1374 case ISD::EH_LABEL:
1375 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1376 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001377 default: assert(0 && "This action is not supported yet!");
1378 case TargetLowering::Legal:
1379 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohman44066042008-07-01 00:05:16 +00001380 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001381 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001382 case TargetLowering::Expand:
1383 Result = LegalizeOp(Node->getOperand(0));
1384 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001385 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001386 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001387
Evan Cheng27b7db52008-03-08 00:58:38 +00001388 case ISD::PREFETCH:
1389 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1390 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1391 default: assert(0 && "This action is not supported yet!");
1392 case TargetLowering::Legal:
1393 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1394 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1395 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1396 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1397 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1398 break;
1399 case TargetLowering::Expand:
1400 // It's a noop.
1401 Result = LegalizeOp(Node->getOperand(0));
1402 break;
1403 }
1404 break;
1405
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001406 case ISD::MEMBARRIER: {
1407 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001408 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1409 default: assert(0 && "This action is not supported yet!");
1410 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001411 SDValue Ops[6];
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001412 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001413 for (int x = 1; x < 6; ++x) {
1414 Ops[x] = Node->getOperand(x);
1415 if (!isTypeLegal(Ops[x].getValueType()))
1416 Ops[x] = PromoteOp(Ops[x]);
1417 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001418 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1419 break;
1420 }
1421 case TargetLowering::Expand:
1422 //There is no libgcc call for this op
1423 Result = Node->getOperand(0); // Noop
1424 break;
1425 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001426 break;
1427 }
1428
Dan Gohman0b1d4a72008-12-23 21:37:04 +00001429 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang63307c32008-05-05 19:05:59 +00001430 unsigned int num_operands = 4;
1431 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001432 SDValue Ops[4];
Mon P Wang63307c32008-05-05 19:05:59 +00001433 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001434 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang63307c32008-05-05 19:05:59 +00001435 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1436
1437 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1438 default: assert(0 && "This action is not supported yet!");
1439 case TargetLowering::Custom:
1440 Result = TLI.LowerOperation(Result, DAG);
1441 break;
1442 case TargetLowering::Legal:
1443 break;
1444 }
Dan Gohman475871a2008-07-27 21:46:04 +00001445 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1446 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001447 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001448 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00001449 case ISD::ATOMIC_LOAD_ADD:
1450 case ISD::ATOMIC_LOAD_SUB:
1451 case ISD::ATOMIC_LOAD_AND:
1452 case ISD::ATOMIC_LOAD_OR:
1453 case ISD::ATOMIC_LOAD_XOR:
1454 case ISD::ATOMIC_LOAD_NAND:
1455 case ISD::ATOMIC_LOAD_MIN:
1456 case ISD::ATOMIC_LOAD_MAX:
1457 case ISD::ATOMIC_LOAD_UMIN:
1458 case ISD::ATOMIC_LOAD_UMAX:
1459 case ISD::ATOMIC_SWAP: {
Mon P Wang63307c32008-05-05 19:05:59 +00001460 unsigned int num_operands = 3;
1461 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001462 SDValue Ops[3];
Mon P Wang63307c32008-05-05 19:05:59 +00001463 for (unsigned int x = 0; x < num_operands; ++x)
1464 Ops[x] = LegalizeOp(Node->getOperand(x));
1465 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands126d9072008-07-04 11:47:58 +00001466
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001467 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001468 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001469 case TargetLowering::Custom:
1470 Result = TLI.LowerOperation(Result, DAG);
1471 break;
1472 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001473 break;
1474 }
Dan Gohman475871a2008-07-27 21:46:04 +00001475 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1476 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001477 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001478 }
Scott Michelc1513d22007-08-08 23:23:31 +00001479 case ISD::Constant: {
1480 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1481 unsigned opAction =
1482 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1483
Chris Lattner3e928bb2005-01-07 07:47:09 +00001484 // We know we don't need to expand constants here, constants only have one
1485 // value and we check that it is fine above.
1486
Scott Michelc1513d22007-08-08 23:23:31 +00001487 if (opAction == TargetLowering::Custom) {
1488 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001489 if (Tmp1.getNode())
Scott Michelc1513d22007-08-08 23:23:31 +00001490 Result = Tmp1;
1491 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001492 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001493 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001494 case ISD::ConstantFP: {
1495 // Spill FP immediates to the constant pool if the target cannot directly
1496 // codegen them. Targets often have some immediate values that can be
1497 // efficiently generated into an FP register without a load. We explicitly
1498 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001499 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1500
Chris Lattner3181a772006-01-29 06:26:56 +00001501 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1502 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001503 case TargetLowering::Legal:
1504 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001505 case TargetLowering::Custom:
1506 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001507 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001508 Result = Tmp3;
1509 break;
1510 }
1511 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001512 case TargetLowering::Expand: {
1513 // Check to see if this FP immediate is already legal.
1514 bool isLegal = false;
1515 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1516 E = TLI.legal_fpimm_end(); I != E; ++I) {
1517 if (CFP->isExactlyValue(*I)) {
1518 isLegal = true;
1519 break;
1520 }
1521 }
1522 // If this is a legal constant, turn it into a TargetConstantFP node.
1523 if (isLegal)
1524 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001525 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001526 }
Nate Begemane1795842008-02-14 08:57:00 +00001527 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001528 break;
1529 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001530 case ISD::TokenFactor:
1531 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001532 Tmp1 = LegalizeOp(Node->getOperand(0));
1533 Tmp2 = LegalizeOp(Node->getOperand(1));
1534 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1535 } else if (Node->getNumOperands() == 3) {
1536 Tmp1 = LegalizeOp(Node->getOperand(0));
1537 Tmp2 = LegalizeOp(Node->getOperand(1));
1538 Tmp3 = LegalizeOp(Node->getOperand(2));
1539 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001540 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001541 SmallVector<SDValue, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001542 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001543 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1544 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001545 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001546 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001547 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001548
1549 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001550 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001551 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001552 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001553 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001554 // A call within a calling sequence must be legalized to something
1555 // other than the normal CALLSEQ_END. Violating this gets Legalize
1556 // into an infinite loop.
1557 assert ((!IsLegalizingCall ||
1558 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001559 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001560 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001561
1562 // The number of incoming and outgoing values should match; unless the final
1563 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001564 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1565 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1566 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001567 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001568 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001569
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001570 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001571 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001572 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1573 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001574 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001575 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001576 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001577 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001578 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001579 }
1580 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001581 case ISD::EXTRACT_SUBREG: {
1582 Tmp1 = LegalizeOp(Node->getOperand(0));
1583 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1584 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001585 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001586 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1587 }
1588 break;
1589 case ISD::INSERT_SUBREG: {
1590 Tmp1 = LegalizeOp(Node->getOperand(0));
1591 Tmp2 = LegalizeOp(Node->getOperand(1));
1592 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1593 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001594 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001595 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1596 }
1597 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001598 case ISD::BUILD_VECTOR:
1599 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001600 default: assert(0 && "This action is not supported yet!");
1601 case TargetLowering::Custom:
1602 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001603 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001604 Result = Tmp3;
1605 break;
1606 }
1607 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001608 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001609 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001610 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001611 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001612 break;
1613 case ISD::INSERT_VECTOR_ELT:
1614 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001615 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001616
1617 // The type of the value to insert may not be legal, even though the vector
1618 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1619 // here.
1620 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1621 default: assert(0 && "Cannot expand insert element operand");
1622 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1623 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang0c397192008-10-30 08:01:45 +00001624 case Expand:
1625 // FIXME: An alternative would be to check to see if the target is not
1626 // going to custom lower this operation, we could bitcast to half elt
1627 // width and perform two inserts at that width, if that is legal.
1628 Tmp2 = Node->getOperand(1);
1629 break;
Nate Begeman0325d902008-02-13 06:43:04 +00001630 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001631 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1632
1633 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1634 Node->getValueType(0))) {
1635 default: assert(0 && "This action is not supported yet!");
1636 case TargetLowering::Legal:
1637 break;
1638 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001639 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001640 if (Tmp4.getNode()) {
Nate Begeman2281a992008-01-05 20:47:37 +00001641 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001642 break;
1643 }
1644 // FALLTHROUGH
Mon P Wang0c397192008-10-30 08:01:45 +00001645 case TargetLowering::Promote:
1646 // Fall thru for vector case
Chris Lattner2332b9f2006-03-19 01:17:20 +00001647 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001648 // If the insert index is a constant, codegen this as a scalar_to_vector,
1649 // then a shuffle that inserts it into the right position in the vector.
1650 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001651 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1652 // match the element type of the vector being created.
1653 if (Tmp2.getValueType() ==
Duncan Sands83ec4b62008-06-06 12:08:01 +00001654 Op.getValueType().getVectorElementType()) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001655 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Nate Begeman0325d902008-02-13 06:43:04 +00001656 Tmp1.getValueType(), Tmp2);
1657
Duncan Sands83ec4b62008-06-06 12:08:01 +00001658 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1659 MVT ShufMaskVT =
1660 MVT::getIntVectorWithNumElements(NumElts);
1661 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman0325d902008-02-13 06:43:04 +00001662
1663 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1664 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1665 // elt 0 of the RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00001666 SmallVector<SDValue, 8> ShufOps;
Nate Begeman0325d902008-02-13 06:43:04 +00001667 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001668 if (i != InsertPos->getZExtValue())
Nate Begeman0325d902008-02-13 06:43:04 +00001669 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1670 else
1671 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1672 }
Dale Johannesenca57b842009-02-02 23:46:53 +00001673 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, ShufMaskVT,
Nate Begeman0325d902008-02-13 06:43:04 +00001674 &ShufOps[0], ShufOps.size());
1675
Dale Johannesenca57b842009-02-02 23:46:53 +00001676 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, Tmp1.getValueType(),
Nate Begeman0325d902008-02-13 06:43:04 +00001677 Tmp1, ScVec, ShufMask);
1678 Result = LegalizeOp(Result);
1679 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001680 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001681 }
Dale Johannesenbb5da912009-02-02 20:41:04 +00001682 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3, dl);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001683 break;
1684 }
1685 }
1686 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001687 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001688 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1689 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1690 break;
1691 }
1692
Chris Lattnerce872152006-03-19 06:31:19 +00001693 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1694 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1695 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1696 Node->getValueType(0))) {
1697 default: assert(0 && "This action is not supported yet!");
1698 case TargetLowering::Legal:
1699 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001700 case TargetLowering::Custom:
1701 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001702 if (Tmp3.getNode()) {
Chris Lattner4d3abee2006-03-19 06:47:21 +00001703 Result = Tmp3;
1704 break;
1705 }
1706 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001707 case TargetLowering::Expand:
1708 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001709 break;
1710 }
Chris Lattnerce872152006-03-19 06:31:19 +00001711 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001712 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001713 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1714 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1715 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1716
1717 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001718 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1719 default: assert(0 && "Unknown operation action!");
1720 case TargetLowering::Legal:
1721 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1722 "vector shuffle should not be created if not legal!");
1723 break;
1724 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001725 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001726 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001727 Result = Tmp3;
1728 break;
1729 }
1730 // FALLTHROUGH
1731 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001732 MVT VT = Node->getValueType(0);
1733 MVT EltVT = VT.getVectorElementType();
1734 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001735 SDValue Mask = Node->getOperand(2);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001736 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001737 SmallVector<SDValue,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001738 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001739 SDValue Arg = Mask.getOperand(i);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001740 if (Arg.getOpcode() == ISD::UNDEF) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001741 Ops.push_back(DAG.getNode(ISD::UNDEF, dl, EltVT));
Evan Cheng18dd6d02006-04-05 06:07:11 +00001742 } else {
1743 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001744 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng18dd6d02006-04-05 06:07:11 +00001745 if (Idx < NumElems)
Dale Johannesenca57b842009-02-02 23:46:53 +00001746 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp1,
Evan Cheng18dd6d02006-04-05 06:07:11 +00001747 DAG.getConstant(Idx, PtrVT)));
1748 else
Dale Johannesenca57b842009-02-02 23:46:53 +00001749 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp2,
Evan Cheng18dd6d02006-04-05 06:07:11 +00001750 DAG.getConstant(Idx - NumElems, PtrVT)));
1751 }
1752 }
Dale Johannesenca57b842009-02-02 23:46:53 +00001753 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001754 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001755 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001756 case TargetLowering::Promote: {
1757 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001758 MVT OVT = Node->getValueType(0);
1759 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001760
1761 // Cast the two input vectors.
Dale Johannesenca57b842009-02-02 23:46:53 +00001762 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
1763 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
Chris Lattner4352cc92006-04-04 17:23:26 +00001764
1765 // Convert the shuffle mask to the right # elements.
Dan Gohman475871a2008-07-27 21:46:04 +00001766 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001767 assert(Tmp3.getNode() && "Shuffle not legal?");
Dale Johannesenca57b842009-02-02 23:46:53 +00001768 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, NVT, Tmp1, Tmp2, Tmp3);
1769 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Chris Lattner4352cc92006-04-04 17:23:26 +00001770 break;
1771 }
Chris Lattner87100e02006-03-20 01:52:29 +00001772 }
1773 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001774
1775 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001776 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001777 Tmp2 = LegalizeOp(Node->getOperand(1));
1778 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001779 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001780 break;
1781
Dan Gohman7f321562007-06-25 16:23:39 +00001782 case ISD::EXTRACT_SUBVECTOR:
1783 Tmp1 = Node->getOperand(0);
1784 Tmp2 = LegalizeOp(Node->getOperand(1));
1785 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1786 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001787 break;
1788
Mon P Wang0c397192008-10-30 08:01:45 +00001789 case ISD::CONCAT_VECTORS: {
1790 // Use extract/insert/build vector for now. We might try to be
1791 // more clever later.
1792 MVT PtrVT = TLI.getPointerTy();
1793 SmallVector<SDValue, 8> Ops;
1794 unsigned NumOperands = Node->getNumOperands();
1795 for (unsigned i=0; i < NumOperands; ++i) {
1796 SDValue SubOp = Node->getOperand(i);
1797 MVT VVT = SubOp.getNode()->getValueType(0);
1798 MVT EltVT = VVT.getVectorElementType();
1799 unsigned NumSubElem = VVT.getVectorNumElements();
1800 for (unsigned j=0; j < NumSubElem; ++j) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001801 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
Mon P Wang0c397192008-10-30 08:01:45 +00001802 DAG.getConstant(j, PtrVT)));
1803 }
1804 }
Dale Johannesenca57b842009-02-02 23:46:53 +00001805 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0),
Mon P Wang0c397192008-10-30 08:01:45 +00001806 &Ops[0], Ops.size()));
1807 }
1808
Chris Lattner6831a812006-02-13 09:18:02 +00001809 case ISD::CALLSEQ_START: {
1810 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1811
1812 // Recursively Legalize all of the inputs of the call end that do not lead
1813 // to this call start. This ensures that any libcalls that need be inserted
1814 // are inserted *before* the CALLSEQ_START.
Mon P Wange5ab34e2009-02-04 19:38:14 +00001815 IsLegalizingCallArgs = true;
Chris Lattner00755df2007-02-04 00:27:56 +00001816 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001817 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001818 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001819 NodesLeadingTo);
1820 }
Mon P Wange5ab34e2009-02-04 19:38:14 +00001821 IsLegalizingCallArgs = false;
Chris Lattner6831a812006-02-13 09:18:02 +00001822
1823 // Now that we legalized all of the inputs (which may have inserted
1824 // libcalls) create the new CALLSEQ_START node.
1825 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1826
1827 // Merge in the last call, to ensure that this call start after the last
1828 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001829 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001830 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1831 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001832 Tmp1 = LegalizeOp(Tmp1);
1833 }
Chris Lattner6831a812006-02-13 09:18:02 +00001834
1835 // Do not try to legalize the target-specific arguments (#1+).
1836 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001837 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001838 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001839 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001840 }
1841
1842 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001843 AddLegalizedOperand(Op.getValue(0), Result);
1844 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1845 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1846
Chris Lattner6831a812006-02-13 09:18:02 +00001847 // Now that the callseq_start and all of the non-call nodes above this call
1848 // sequence have been legalized, legalize the call itself. During this
1849 // process, no libcalls can/will be inserted, guaranteeing that no calls
1850 // can overlap.
1851 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001852 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001853 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001854 IsLegalizingCall = true;
1855
1856 // Legalize the call, starting from the CALLSEQ_END.
1857 LegalizeOp(LastCALLSEQ_END);
1858 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1859 return Result;
1860 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001861 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001862 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1863 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001864 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001865 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1866 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001867 assert(I != LegalizedNodes.end() &&
1868 "Legalizing the call start should have legalized this node!");
1869 return I->second;
1870 }
1871
1872 // Otherwise, the call start has been legalized and everything is going
1873 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001874 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001875 // Do not try to legalize the target-specific arguments (#1+), except for
1876 // an optional flag input.
1877 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1878 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001879 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001880 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001881 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001882 }
1883 } else {
1884 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1885 if (Tmp1 != Node->getOperand(0) ||
1886 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001887 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001888 Ops[0] = Tmp1;
1889 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001890 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001891 }
Chris Lattner6a542892006-01-24 05:48:21 +00001892 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001893 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001894 // This finishes up call legalization.
1895 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001896
1897 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001898 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001899 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001900 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001901 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001902 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001903 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001904 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1905 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1906 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001907 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001908
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001909 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001910 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001911 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001912 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001913 case TargetLowering::Expand: {
1914 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1915 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1916 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001917 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001918
1919 // Chain the dynamic stack allocation so that it doesn't modify the stack
1920 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001921 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001922
Dan Gohman475871a2008-07-27 21:46:04 +00001923 SDValue Size = Tmp2.getOperand(1);
Dale Johannesenc460ae92009-02-04 00:13:36 +00001924 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001925 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001926 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001927 unsigned StackAlign =
1928 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1929 if (Align > StackAlign)
Dale Johannesenca57b842009-02-02 23:46:53 +00001930 SP = DAG.getNode(ISD::AND, dl, VT, SP,
Evan Cheng3e20bba2007-08-17 18:02:22 +00001931 DAG.getConstant(-(uint64_t)Align, VT));
Dale Johannesenca57b842009-02-02 23:46:53 +00001932 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenc460ae92009-02-04 00:13:36 +00001933 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001934
Dale Johannesenca57b842009-02-02 23:46:53 +00001935 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
Chris Lattnere563bbc2008-10-11 22:08:30 +00001936 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001937
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001938 Tmp1 = LegalizeOp(Tmp1);
1939 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001940 break;
1941 }
1942 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001943 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001944 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001945 Tmp1 = LegalizeOp(Tmp3);
1946 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001947 }
Chris Lattner903d2782006-01-15 08:54:32 +00001948 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001949 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001950 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001951 }
Chris Lattner903d2782006-01-15 08:54:32 +00001952 // Since this op produce two values, make sure to remember that we
1953 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001954 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1955 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001956 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001957 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001958 case ISD::INLINEASM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001959 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001960 bool Changed = false;
1961 // Legalize all of the operands of the inline asm, in case they are nodes
1962 // that need to be expanded or something. Note we skip the asm string and
1963 // all of the TargetConstant flags.
Dan Gohman475871a2008-07-27 21:46:04 +00001964 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001965 Changed = Op != Ops[0];
1966 Ops[0] = Op;
1967
1968 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1969 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001970 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Chris Lattner25a022c2006-07-11 01:40:09 +00001971 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman475871a2008-07-27 21:46:04 +00001972 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001973 if (Op != Ops[i]) {
1974 Changed = true;
1975 Ops[i] = Op;
1976 }
1977 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001978 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001979
1980 if (HasInFlag) {
1981 Op = LegalizeOp(Ops.back());
1982 Changed |= Op != Ops.back();
1983 Ops.back() = Op;
1984 }
1985
1986 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001987 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001988
1989 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman475871a2008-07-27 21:46:04 +00001990 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1991 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001992 return Result.getValue(Op.getResNo());
Chris Lattner25a022c2006-07-11 01:40:09 +00001993 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001994 case ISD::BR:
1995 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001996 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001997 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001998 Tmp1 = LegalizeOp(Tmp1);
1999 LastCALLSEQ_END = DAG.getEntryNode();
2000
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002001 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00002002 break;
Nate Begeman37efe672006-04-22 18:53:45 +00002003 case ISD::BRIND:
2004 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2005 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00002006 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Nate Begeman37efe672006-04-22 18:53:45 +00002007 Tmp1 = LegalizeOp(Tmp1);
2008 LastCALLSEQ_END = DAG.getEntryNode();
2009
2010 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2011 default: assert(0 && "Indirect target must be legal type (pointer)!");
2012 case Legal:
2013 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2014 break;
2015 }
2016 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2017 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00002018 case ISD::BR_JT:
2019 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2020 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00002021 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002022 Tmp1 = LegalizeOp(Tmp1);
2023 LastCALLSEQ_END = DAG.getEntryNode();
2024
2025 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
2026 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2027
2028 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
2029 default: assert(0 && "This action is not supported yet!");
2030 case TargetLowering::Legal: break;
2031 case TargetLowering::Custom:
2032 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002033 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00002034 break;
2035 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002036 SDValue Chain = Result.getOperand(0);
2037 SDValue Table = Result.getOperand(1);
2038 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002039
Duncan Sands83ec4b62008-06-06 12:08:01 +00002040 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002041 MachineFunction &MF = DAG.getMachineFunction();
2042 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Dale Johannesenca57b842009-02-02 23:46:53 +00002043 Index= DAG.getNode(ISD::MUL, dl, PTy,
2044 Index, DAG.getConstant(EntrySize, PTy));
2045 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002046
Duncan Sands712f7b32008-12-12 08:13:38 +00002047 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
Dale Johannesenca57b842009-02-02 23:46:53 +00002048 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Duncan Sands712f7b32008-12-12 08:13:38 +00002049 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00002050 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002051 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00002052 // For PIC, the sequence is:
2053 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00002054 // RelocBase can be JumpTable, GOT or some sort of global base.
Dale Johannesenca57b842009-02-02 23:46:53 +00002055 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
Evan Chengcc415862007-11-09 01:32:10 +00002056 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00002057 }
Dale Johannesenca57b842009-02-02 23:46:53 +00002058 Result = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002059 }
2060 }
2061 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002062 case ISD::BRCOND:
2063 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002064 // Ensure that libcalls are emitted before a return.
Dale Johannesenca57b842009-02-02 23:46:53 +00002065 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00002066 Tmp1 = LegalizeOp(Tmp1);
2067 LastCALLSEQ_END = DAG.getEntryNode();
2068
Chris Lattner47e92232005-01-18 19:27:06 +00002069 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2070 case Expand: assert(0 && "It's impossible to expand bools");
2071 case Legal:
2072 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2073 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002074 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00002075 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00002076
2077 // The top bits of the promoted condition are not necessarily zero, ensure
2078 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002079 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002080 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002081 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dale Johannesenca57b842009-02-02 23:46:53 +00002082 Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002083 break;
2084 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002085 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002086
2087 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002088 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00002089
2090 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
2091 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002092 case TargetLowering::Legal: break;
2093 case TargetLowering::Custom:
2094 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002095 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002096 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002097 case TargetLowering::Expand:
2098 // Expand brcond's setcc into its constituent parts and create a BR_CC
2099 // Node.
2100 if (Tmp2.getOpcode() == ISD::SETCC) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002101 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
2102 Tmp1, Tmp2.getOperand(2),
Nate Begeman7cbd5252005-08-16 19:49:35 +00002103 Tmp2.getOperand(0), Tmp2.getOperand(1),
2104 Node->getOperand(2));
2105 } else {
Dale Johannesenca57b842009-02-02 23:46:53 +00002106 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Nate Begeman7cbd5252005-08-16 19:49:35 +00002107 DAG.getCondCode(ISD::SETNE), Tmp2,
2108 DAG.getConstant(0, Tmp2.getValueType()),
2109 Node->getOperand(2));
2110 }
2111 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002112 }
2113 break;
2114 case ISD::BR_CC:
2115 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002116 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00002117 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00002118 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002119 Tmp2 = Node->getOperand(2); // LHS
2120 Tmp3 = Node->getOperand(3); // RHS
2121 Tmp4 = Node->getOperand(1); // CC
2122
Dale Johannesenbb5da912009-02-02 20:41:04 +00002123 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()),
2124 Tmp2, Tmp3, Tmp4, dl);
Evan Cheng722cb362006-12-18 22:55:34 +00002125 LastCALLSEQ_END = DAG.getEntryNode();
2126
Evan Cheng7f042682008-10-15 02:05:31 +00002127 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002128 // the LHS is a legal SETCC itself. In this case, we need to compare
2129 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002130 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002131 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2132 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00002133 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00002134
2135 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2136 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002137
Chris Lattner181b7a32005-12-17 23:46:46 +00002138 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2139 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002140 case TargetLowering::Legal: break;
2141 case TargetLowering::Custom:
2142 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002143 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00002144 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002145 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002146 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002147 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00002148 LoadSDNode *LD = cast<LoadSDNode>(Node);
2149 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2150 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002151
Evan Cheng466685d2006-10-09 20:57:25 +00002152 ISD::LoadExtType ExtType = LD->getExtensionType();
2153 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002154 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00002155 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2156 Tmp3 = Result.getValue(0);
2157 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002158
Evan Cheng466685d2006-10-09 20:57:25 +00002159 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2160 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002161 case TargetLowering::Legal:
2162 // If this is an unaligned load and the target doesn't support it,
2163 // expand it.
2164 if (!TLI.allowsUnalignedMemoryAccesses()) {
2165 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002166 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002167 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002168 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002169 TLI);
2170 Tmp3 = Result.getOperand(0);
2171 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00002172 Tmp3 = LegalizeOp(Tmp3);
2173 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002174 }
2175 }
2176 break;
Evan Cheng466685d2006-10-09 20:57:25 +00002177 case TargetLowering::Custom:
2178 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002179 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002180 Tmp3 = LegalizeOp(Tmp1);
2181 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00002182 }
Evan Cheng466685d2006-10-09 20:57:25 +00002183 break;
2184 case TargetLowering::Promote: {
2185 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002186 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00002187 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002188 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002189
Dale Johannesenca57b842009-02-02 23:46:53 +00002190 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002191 LD->getSrcValueOffset(),
2192 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00002193 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2194 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002195 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00002196 }
Evan Cheng466685d2006-10-09 20:57:25 +00002197 }
2198 // Since loads produce two values, make sure to remember that we
2199 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002200 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2201 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002202 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00002203 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002204 MVT SrcVT = LD->getMemoryVT();
2205 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002206 int SVOffset = LD->getSrcValueOffset();
2207 unsigned Alignment = LD->getAlignment();
2208 bool isVolatile = LD->isVolatile();
2209
Duncan Sands83ec4b62008-06-06 12:08:01 +00002210 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002211 // Some targets pretend to have an i1 loading operation, and actually
2212 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2213 // bits are guaranteed to be zero; it helps the optimizers understand
2214 // that these bits are zero. It is also useful for EXTLOAD, since it
2215 // tells the optimizers that those bits are undefined. It would be
2216 // nice to have an effective generic way of getting these benefits...
2217 // Until such a way is found, don't insist on promoting i1 here.
2218 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00002219 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002220 // Promote to a byte-sized load if not loading an integral number of
2221 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002222 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2223 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002224 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002225
2226 // The extra bits are guaranteed to be zero, since we stored them that
2227 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2228
2229 ISD::LoadExtType NewExtType =
2230 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2231
Dale Johannesenca57b842009-02-02 23:46:53 +00002232 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002233 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2234 NVT, isVolatile, Alignment);
2235
2236 Ch = Result.getValue(1); // The chain.
2237
2238 if (ExtType == ISD::SEXTLOAD)
2239 // Having the top bits zero doesn't help when sign extending.
Dale Johannesenca57b842009-02-02 23:46:53 +00002240 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
2241 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002242 Result, DAG.getValueType(SrcVT));
2243 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2244 // All the top bits are guaranteed to be zero - inform the optimizers.
Dale Johannesenca57b842009-02-02 23:46:53 +00002245 Result = DAG.getNode(ISD::AssertZext, dl,
2246 Result.getValueType(), Result,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002247 DAG.getValueType(SrcVT));
2248
2249 Tmp1 = LegalizeOp(Result);
2250 Tmp2 = LegalizeOp(Ch);
2251 } else if (SrcWidth & (SrcWidth - 1)) {
2252 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002253 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002254 "Unsupported extload!");
2255 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2256 assert(RoundWidth < SrcWidth);
2257 unsigned ExtraWidth = SrcWidth - RoundWidth;
2258 assert(ExtraWidth < RoundWidth);
2259 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2260 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002261 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2262 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002263 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002264 unsigned IncrementSize;
2265
2266 if (TLI.isLittleEndian()) {
2267 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2268 // Load the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00002269 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
2270 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002271 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2272 Alignment);
2273
2274 // Load the remaining ExtraWidth bits.
2275 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00002276 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002277 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00002278 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002279 LD->getSrcValue(), SVOffset + IncrementSize,
2280 ExtraVT, isVolatile,
2281 MinAlign(Alignment, IncrementSize));
2282
2283 // Build a factor node to remember that this load is independent of the
2284 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00002285 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002286 Hi.getValue(1));
2287
2288 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00002289 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002290 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2291
2292 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00002293 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002294 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002295 // Big endian - avoid unaligned loads.
2296 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2297 // Load the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00002298 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002299 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2300 Alignment);
2301
2302 // Load the remaining ExtraWidth bits.
2303 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00002304 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002305 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00002306 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
2307 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002308 LD->getSrcValue(), SVOffset + IncrementSize,
2309 ExtraVT, isVolatile,
2310 MinAlign(Alignment, IncrementSize));
2311
2312 // Build a factor node to remember that this load is independent of the
2313 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00002314 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002315 Hi.getValue(1));
2316
2317 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00002318 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002319 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2320
2321 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00002322 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002323 }
2324
2325 Tmp1 = LegalizeOp(Result);
2326 Tmp2 = LegalizeOp(Ch);
2327 } else {
Evan Cheng03294662008-10-14 21:26:46 +00002328 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002329 default: assert(0 && "This action is not supported yet!");
2330 case TargetLowering::Custom:
2331 isCustom = true;
2332 // FALLTHROUGH
2333 case TargetLowering::Legal:
2334 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2335 Tmp1 = Result.getValue(0);
2336 Tmp2 = Result.getValue(1);
2337
2338 if (isCustom) {
2339 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002340 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002341 Tmp1 = LegalizeOp(Tmp3);
2342 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2343 }
2344 } else {
2345 // If this is an unaligned load and the target doesn't support it,
2346 // expand it.
2347 if (!TLI.allowsUnalignedMemoryAccesses()) {
2348 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002349 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002350 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002351 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002352 TLI);
2353 Tmp1 = Result.getOperand(0);
2354 Tmp2 = Result.getOperand(1);
2355 Tmp1 = LegalizeOp(Tmp1);
2356 Tmp2 = LegalizeOp(Tmp2);
2357 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002358 }
2359 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002360 break;
2361 case TargetLowering::Expand:
2362 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2363 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002364 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002365 LD->getSrcValueOffset(),
2366 LD->isVolatile(), LD->getAlignment());
Dale Johannesenca57b842009-02-02 23:46:53 +00002367 Result = DAG.getNode(ISD::FP_EXTEND, dl,
2368 Node->getValueType(0), Load);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002369 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2370 Tmp2 = LegalizeOp(Load.getValue(1));
2371 break;
2372 }
2373 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2374 // Turn the unsupported load into an EXTLOAD followed by an explicit
2375 // zero/sign extend inreg.
Dale Johannesenca57b842009-02-02 23:46:53 +00002376 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002377 Tmp1, Tmp2, LD->getSrcValue(),
2378 LD->getSrcValueOffset(), SrcVT,
2379 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00002380 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002381 if (ExtType == ISD::SEXTLOAD)
Dale Johannesenca57b842009-02-02 23:46:53 +00002382 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
2383 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002384 Result, DAG.getValueType(SrcVT));
2385 else
Dale Johannesenca57b842009-02-02 23:46:53 +00002386 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002387 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2388 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002389 break;
2390 }
Evan Cheng466685d2006-10-09 20:57:25 +00002391 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002392
Evan Cheng466685d2006-10-09 20:57:25 +00002393 // Since loads produce two values, make sure to remember that we legalized
2394 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002395 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2396 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002397 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002398 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002399 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002400 case ISD::EXTRACT_ELEMENT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002401 MVT OpTy = Node->getOperand(0).getValueType();
Nate Begeman5dc897b2005-10-19 00:06:56 +00002402 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002403 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002404 case Legal:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002405 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Nate Begeman5dc897b2005-10-19 00:06:56 +00002406 // 1 -> Hi
Dale Johannesenca57b842009-02-02 23:46:53 +00002407 Result = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002408 DAG.getConstant(OpTy.getSizeInBits()/2,
Nate Begeman5dc897b2005-10-19 00:06:56 +00002409 TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002410 Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result);
Nate Begeman5dc897b2005-10-19 00:06:56 +00002411 } else {
2412 // 0 -> Lo
Dale Johannesenca57b842009-02-02 23:46:53 +00002413 Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
Nate Begeman5dc897b2005-10-19 00:06:56 +00002414 Node->getOperand(0));
2415 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002416 break;
2417 case Expand:
2418 // Get both the low and high parts.
2419 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002420 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Nate Begeman5dc897b2005-10-19 00:06:56 +00002421 Result = Tmp2; // 1 -> Hi
2422 else
2423 Result = Tmp1; // 0 -> Lo
2424 break;
2425 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002426 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002427 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002428
2429 case ISD::CopyToReg:
2430 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002431
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002432 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002433 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002434 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002435 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002436 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002437 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002438 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002439 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002440 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002441 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002442 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2443 Tmp3);
2444 } else {
2445 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002446 }
2447
2448 // Since this produces two values, make sure to remember that we legalized
2449 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002450 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2451 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002452 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002453 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002454 break;
2455
2456 case ISD::RET:
2457 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002458
2459 // Ensure that libcalls are emitted before a return.
Dale Johannesenca57b842009-02-02 23:46:53 +00002460 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00002461 Tmp1 = LegalizeOp(Tmp1);
2462 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002463
Chris Lattner3e928bb2005-01-07 07:47:09 +00002464 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002465 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002466 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002467 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002468 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002469 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002470 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002471 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002472 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002473 if (!Tmp2.getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002474 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002475 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002476
2477 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002478 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002479 std::swap(Lo, Hi);
2480
Gabor Greifba36cb52008-08-28 21:40:38 +00002481 if (Hi.getNode())
Dale Johannesenca57b842009-02-02 23:46:53 +00002482 Result = DAG.getNode(ISD::RET, dl, MVT::Other,
2483 Tmp1, Lo, Tmp3, Hi,Tmp3);
Evan Cheng13acce32006-12-11 19:27:14 +00002484 else
Dale Johannesenca57b842009-02-02 23:46:53 +00002485 Result = DAG.getNode(ISD::RET, dl, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002486 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002487 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00002488 SDNode *InVal = Tmp2.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002489 int InIx = Tmp2.getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002490 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2491 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002492
Dan Gohman7f321562007-06-25 16:23:39 +00002493 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002494 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002495 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002496 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002497 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002498 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002499 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002500 } else if (NumElems == 1) {
2501 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002502 Tmp2 = ScalarizeVectorOp(Tmp2);
2503 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002504 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002505
2506 // FIXME: Returns of gcc generic vectors smaller than a legal type
2507 // should be returned in integer registers!
2508
Chris Lattnerf87324e2006-04-11 01:31:51 +00002509 // The scalarized value type may not be legal, e.g. it might require
2510 // promotion or expansion. Relegalize the return.
2511 Result = LegalizeOp(Result);
2512 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002513 // FIXME: Returns of gcc generic vectors larger than a legal vector
2514 // type should be returned by reference!
Dan Gohman475871a2008-07-27 21:46:04 +00002515 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002516 SplitVectorOp(Tmp2, Lo, Hi);
Dale Johannesenca57b842009-02-02 23:46:53 +00002517 Result = DAG.getNode(ISD::RET, dl, MVT::Other,
2518 Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002519 Result = LegalizeOp(Result);
2520 }
2521 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002522 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002523 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002524 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002525 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002526 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002527 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002528 }
2529 break;
2530 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002531 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002532 break;
2533 default: { // ret <values>
Dan Gohman475871a2008-07-27 21:46:04 +00002534 SmallVector<SDValue, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002535 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002536 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002537 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2538 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002539 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002540 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002541 break;
2542 case Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002543 SDValue Lo, Hi;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002544 assert(!Node->getOperand(i).getValueType().isExtended() &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002545 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002546 ExpandOp(Node->getOperand(i), Lo, Hi);
2547 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002548 NewValues.push_back(Node->getOperand(i+1));
Gabor Greifba36cb52008-08-28 21:40:38 +00002549 if (Hi.getNode()) {
Evan Cheng13acce32006-12-11 19:27:14 +00002550 NewValues.push_back(Hi);
2551 NewValues.push_back(Node->getOperand(i+1));
2552 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002553 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002554 }
2555 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002556 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002557 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002558
2559 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002560 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002561 else
Dale Johannesenca57b842009-02-02 23:46:53 +00002562 Result = DAG.getNode(ISD::RET, dl, MVT::Other,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002563 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002564 break;
2565 }
2566 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002567
Chris Lattner6862dbc2006-01-29 21:02:23 +00002568 if (Result.getOpcode() == ISD::RET) {
2569 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2570 default: assert(0 && "This action is not supported yet!");
2571 case TargetLowering::Legal: break;
2572 case TargetLowering::Custom:
2573 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002574 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner6862dbc2006-01-29 21:02:23 +00002575 break;
2576 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002577 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002578 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002579 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002580 StoreSDNode *ST = cast<StoreSDNode>(Node);
2581 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2582 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002583 int SVOffset = ST->getSrcValueOffset();
2584 unsigned Alignment = ST->getAlignment();
2585 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002586
Evan Cheng8b2794a2006-10-13 21:14:26 +00002587 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002588 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2589 // FIXME: We shouldn't do this for TargetConstantFP's.
2590 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2591 // to phase ordering between legalized code and the dag combiner. This
2592 // probably means that we need to integrate dag combiner and legalizer
2593 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002594 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002595 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002596 if (CFP->getValueType(0) == MVT::f32 &&
2597 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002598 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00002599 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002600 MVT::i32);
Dale Johannesenca57b842009-02-02 23:46:53 +00002601 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002602 SVOffset, isVolatile, Alignment);
2603 break;
2604 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002605 // If this target supports 64-bit registers, do a single 64-bit store.
2606 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00002607 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002608 zextOrTrunc(64), MVT::i64);
Dale Johannesenca57b842009-02-02 23:46:53 +00002609 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00002610 SVOffset, isVolatile, Alignment);
2611 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002612 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002613 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2614 // stores. If the target supports neither 32- nor 64-bits, this
2615 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00002616 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00002617 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2618 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002619 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002620
Dale Johannesenca57b842009-02-02 23:46:53 +00002621 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00002622 SVOffset, isVolatile, Alignment);
Dale Johannesenca57b842009-02-02 23:46:53 +00002623 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002624 DAG.getIntPtrConstant(4));
Dale Johannesenca57b842009-02-02 23:46:53 +00002625 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002626 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002627
Dale Johannesenca57b842009-02-02 23:46:53 +00002628 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002629 break;
2630 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002631 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002632 }
2633
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002634 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002635 case Legal: {
2636 Tmp3 = LegalizeOp(ST->getValue());
2637 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2638 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002639
Duncan Sands83ec4b62008-06-06 12:08:01 +00002640 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002641 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2642 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002643 case TargetLowering::Legal:
2644 // If this is an unaligned store and the target doesn't support it,
2645 // expand it.
2646 if (!TLI.allowsUnalignedMemoryAccesses()) {
2647 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002648 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002649 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002650 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002651 TLI);
2652 }
2653 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002654 case TargetLowering::Custom:
2655 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002656 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002657 break;
2658 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002659 assert(VT.isVector() && "Unknown legal promote case!");
Dale Johannesenca57b842009-02-02 23:46:53 +00002660 Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002661 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00002662 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002663 ST->getSrcValue(), SVOffset, isVolatile,
2664 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002665 break;
2666 }
2667 break;
2668 }
2669 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002670 if (!ST->getMemoryVT().isVector()) {
2671 // Truncate the value and store the result.
2672 Tmp3 = PromoteOp(ST->getValue());
Dale Johannesenca57b842009-02-02 23:46:53 +00002673 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Mon P Wang0c397192008-10-30 08:01:45 +00002674 SVOffset, ST->getMemoryVT(),
2675 isVolatile, Alignment);
2676 break;
2677 }
2678 // Fall thru to expand for vector
2679 case Expand: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002680 unsigned IncrementSize = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002681 SDValue Lo, Hi;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002682
2683 // If this is a vector type, then we have to calculate the increment as
2684 // the product of the element size in bytes, and the number of elements
2685 // in the high half of the vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002686 if (ST->getValue().getValueType().isVector()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002687 SDNode *InVal = ST->getValue().getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002688 int InIx = ST->getValue().getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002689 MVT InVT = InVal->getValueType(InIx);
2690 unsigned NumElems = InVT.getVectorNumElements();
2691 MVT EVT = InVT.getVectorElementType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002692
Dan Gohman7f321562007-06-25 16:23:39 +00002693 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002694 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002695 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002696 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002697 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002698 Tmp3 = LegalizeOp(ST->getValue());
Dale Johannesenca57b842009-02-02 23:46:53 +00002699 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002700 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002701 Result = LegalizeOp(Result);
2702 break;
2703 } else if (NumElems == 1) {
2704 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002705 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dale Johannesenca57b842009-02-02 23:46:53 +00002706 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002707 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002708 // The scalarized value type may not be legal, e.g. it might require
2709 // promotion or expansion. Relegalize the scalar store.
2710 Result = LegalizeOp(Result);
2711 break;
2712 } else {
Mon P Wang0c397192008-10-30 08:01:45 +00002713 // Check if we have widen this node with another value
2714 std::map<SDValue, SDValue>::iterator I =
2715 WidenNodes.find(ST->getValue());
2716 if (I != WidenNodes.end()) {
2717 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2718 break;
2719 }
2720 else {
2721 SplitVectorOp(ST->getValue(), Lo, Hi);
2722 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2723 EVT.getSizeInBits()/8;
2724 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002725 }
2726 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002727 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greifba36cb52008-08-28 21:40:38 +00002728 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002729
Richard Pennington4b052dc2008-09-25 16:15:10 +00002730 if (Hi.getNode() && TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002731 std::swap(Lo, Hi);
2732 }
2733
Dale Johannesenca57b842009-02-02 23:46:53 +00002734 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002735 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002736
Gabor Greifba36cb52008-08-28 21:40:38 +00002737 if (Hi.getNode() == NULL) {
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002738 // Must be int <-> float one-to-one expansion.
2739 Result = Lo;
2740 break;
2741 }
2742
Dale Johannesenca57b842009-02-02 23:46:53 +00002743 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002744 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002745 assert(isTypeLegal(Tmp2.getValueType()) &&
2746 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002747 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002748 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenca57b842009-02-02 23:46:53 +00002749 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002750 SVOffset, isVolatile, Alignment);
Dale Johannesenca57b842009-02-02 23:46:53 +00002751 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002752 break;
Mon P Wang0c397192008-10-30 08:01:45 +00002753 } // case Expand
Evan Cheng8b2794a2006-10-13 21:14:26 +00002754 }
2755 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002756 switch (getTypeAction(ST->getValue().getValueType())) {
2757 case Legal:
2758 Tmp3 = LegalizeOp(ST->getValue());
2759 break;
2760 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002761 if (!ST->getValue().getValueType().isVector()) {
2762 // We can promote the value, the truncstore will still take care of it.
2763 Tmp3 = PromoteOp(ST->getValue());
2764 break;
2765 }
2766 // Vector case falls through to expand
Chris Lattnerddf89562008-01-17 19:59:44 +00002767 case Expand:
2768 // Just store the low part. This may become a non-trunc store, so make
2769 // sure to use getTruncStore, not UpdateNodeOperands below.
2770 ExpandOp(ST->getValue(), Tmp3, Tmp4);
Dale Johannesenca57b842009-02-02 23:46:53 +00002771 return DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Chris Lattnerddf89562008-01-17 19:59:44 +00002772 SVOffset, MVT::i8, isVolatile, Alignment);
2773 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002774
Duncan Sands83ec4b62008-06-06 12:08:01 +00002775 MVT StVT = ST->getMemoryVT();
2776 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00002777
Duncan Sands83ec4b62008-06-06 12:08:01 +00002778 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00002779 // Promote to a byte-sized store with upper bits zero if not
2780 // storing an integral number of bytes. For example, promote
2781 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002782 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00002783 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
2784 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002785 SVOffset, NVT, isVolatile, Alignment);
2786 } else if (StWidth & (StWidth - 1)) {
2787 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002788 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00002789 "Unsupported truncstore!");
2790 unsigned RoundWidth = 1 << Log2_32(StWidth);
2791 assert(RoundWidth < StWidth);
2792 unsigned ExtraWidth = StWidth - RoundWidth;
2793 assert(ExtraWidth < RoundWidth);
2794 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2795 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002796 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2797 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002798 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00002799 unsigned IncrementSize;
2800
2801 if (TLI.isLittleEndian()) {
2802 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2803 // Store the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00002804 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002805 SVOffset, RoundVT,
2806 isVolatile, Alignment);
2807
2808 // Store the remaining ExtraWidth bits.
2809 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00002810 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00002811 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00002812 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00002813 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002814 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002815 SVOffset + IncrementSize, ExtraVT, isVolatile,
2816 MinAlign(Alignment, IncrementSize));
2817 } else {
2818 // Big endian - avoid unaligned stores.
2819 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2820 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00002821 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00002822 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002823 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
2824 SVOffset, RoundVT, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00002825
2826 // Store the remaining ExtraWidth bits.
2827 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00002828 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00002829 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00002830 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002831 SVOffset + IncrementSize, ExtraVT, isVolatile,
2832 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002833 }
Duncan Sands7e857202008-01-22 07:17:34 +00002834
2835 // The order of the stores doesn't matter.
Dale Johannesenca57b842009-02-02 23:46:53 +00002836 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00002837 } else {
2838 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2839 Tmp2 != ST->getBasePtr())
2840 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2841 ST->getOffset());
2842
2843 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2844 default: assert(0 && "This action is not supported yet!");
2845 case TargetLowering::Legal:
2846 // If this is an unaligned store and the target doesn't support it,
2847 // expand it.
2848 if (!TLI.allowsUnalignedMemoryAccesses()) {
2849 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002850 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00002851 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002852 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00002853 TLI);
2854 }
2855 break;
2856 case TargetLowering::Custom:
2857 Result = TLI.LowerOperation(Result, DAG);
2858 break;
2859 case Expand:
2860 // TRUNCSTORE:i16 i32 -> STORE i16
2861 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00002862 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
2863 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
2864 SVOffset, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00002865 break;
2866 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002867 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002868 }
2869 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002870 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002871 case ISD::PCMARKER:
2872 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002873 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002874 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002875 case ISD::STACKSAVE:
2876 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002877 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2878 Tmp1 = Result.getValue(0);
2879 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002880
Chris Lattner140d53c2006-01-13 02:50:02 +00002881 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2882 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002883 case TargetLowering::Legal: break;
2884 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002885 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002886 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002887 Tmp1 = LegalizeOp(Tmp3);
2888 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002889 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002890 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002891 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002892 // Expand to CopyFromReg if the target set
2893 // StackPointerRegisterToSaveRestore.
2894 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Dale Johannesenc460ae92009-02-04 00:13:36 +00002895 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), dl, SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002896 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002897 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002898 } else {
Dale Johannesenca57b842009-02-02 23:46:53 +00002899 Tmp1 = DAG.getNode(ISD::UNDEF, dl, Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002900 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002901 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002902 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002903 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002904
2905 // Since stacksave produce two values, make sure to remember that we
2906 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002907 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2908 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002909 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002910
Chris Lattner140d53c2006-01-13 02:50:02 +00002911 case ISD::STACKRESTORE:
2912 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2913 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002914 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002915
2916 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2917 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002918 case TargetLowering::Legal: break;
2919 case TargetLowering::Custom:
2920 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002921 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002922 break;
2923 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002924 // Expand to CopyToReg if the target set
2925 // StackPointerRegisterToSaveRestore.
2926 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Dale Johannesenc460ae92009-02-04 00:13:36 +00002927 Result = DAG.getCopyToReg(Tmp1, dl, SP, Tmp2);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002928 } else {
2929 Result = Tmp1;
2930 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002931 break;
2932 }
2933 break;
2934
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002935 case ISD::READCYCLECOUNTER:
2936 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002937 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002938 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2939 Node->getValueType(0))) {
2940 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002941 case TargetLowering::Legal:
2942 Tmp1 = Result.getValue(0);
2943 Tmp2 = Result.getValue(1);
2944 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002945 case TargetLowering::Custom:
2946 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002947 Tmp1 = LegalizeOp(Result.getValue(0));
2948 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002949 break;
2950 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002951
2952 // Since rdcc produce two values, make sure to remember that we legalized
2953 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002954 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2955 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002956 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002957
Chris Lattner2ee743f2005-01-14 22:08:15 +00002958 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002959 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2960 case Expand: assert(0 && "It's impossible to expand bools");
2961 case Legal:
2962 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2963 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002964 case Promote: {
Mon P Wang0c397192008-10-30 08:01:45 +00002965 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Chris Lattner47e92232005-01-18 19:27:06 +00002966 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002967 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002968 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002969 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002970 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dale Johannesenca57b842009-02-02 23:46:53 +00002971 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002972 break;
2973 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002974 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002975 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002976 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002977
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002978 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002979
Nate Begemanb942a3d2005-08-23 04:29:48 +00002980 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002981 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002982 case TargetLowering::Legal: break;
2983 case TargetLowering::Custom: {
2984 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002985 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002986 break;
2987 }
Nate Begeman9373a812005-08-10 20:51:12 +00002988 case TargetLowering::Expand:
2989 if (Tmp1.getOpcode() == ISD::SETCC) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002990 Result = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
Nate Begeman9373a812005-08-10 20:51:12 +00002991 Tmp2, Tmp3,
2992 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2993 } else {
Dale Johannesenca57b842009-02-02 23:46:53 +00002994 Result = DAG.getSelectCC(dl, Tmp1,
Nate Begeman9373a812005-08-10 20:51:12 +00002995 DAG.getConstant(0, Tmp1.getValueType()),
2996 Tmp2, Tmp3, ISD::SETNE);
2997 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002998 break;
2999 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003000 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003001 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
3002 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003003 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00003004 ExtOp = ISD::BIT_CONVERT;
3005 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003006 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003007 ExtOp = ISD::ANY_EXTEND;
3008 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003009 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00003010 ExtOp = ISD::FP_EXTEND;
3011 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003012 }
3013 // Promote each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00003014 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Tmp2);
3015 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Tmp3);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003016 // Perform the larger operation, then round down.
Dale Johannesenca57b842009-02-02 23:46:53 +00003017 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00003018 if (TruncOp != ISD::FP_ROUND)
Dale Johannesenca57b842009-02-02 23:46:53 +00003019 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00003020 else
Dale Johannesenca57b842009-02-02 23:46:53 +00003021 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result,
Chris Lattner0bd48932008-01-17 07:00:52 +00003022 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003023 break;
3024 }
3025 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003026 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00003027 case ISD::SELECT_CC: {
3028 Tmp1 = Node->getOperand(0); // LHS
3029 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00003030 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
3031 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00003032 SDValue CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00003033
Dale Johannesenbb5da912009-02-02 20:41:04 +00003034 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
3035 Tmp1, Tmp2, CC, dl);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003036
Evan Cheng7f042682008-10-15 02:05:31 +00003037 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00003038 // the LHS is a legal SETCC itself. In this case, we need to compare
3039 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00003040 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003041 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3042 CC = DAG.getCondCode(ISD::SETNE);
3043 }
3044 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
3045
3046 // Everything is legal, see if we should expand this op or something.
3047 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
3048 default: assert(0 && "This action is not supported yet!");
3049 case TargetLowering::Legal: break;
3050 case TargetLowering::Custom:
3051 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003052 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00003053 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003054 }
3055 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00003056 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003057 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00003058 Tmp1 = Node->getOperand(0);
3059 Tmp2 = Node->getOperand(1);
3060 Tmp3 = Node->getOperand(2);
Dale Johannesenbb5da912009-02-02 20:41:04 +00003061 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003062
3063 // If we had to Expand the SetCC operands into a SELECT node, then it may
3064 // not always be possible to return a true LHS & RHS. In this case, just
3065 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003066 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003067 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003068 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003069 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003070
Chris Lattner73e142f2006-01-30 22:43:50 +00003071 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003072 default: assert(0 && "Cannot handle this action for SETCC yet!");
3073 case TargetLowering::Custom:
3074 isCustom = true;
3075 // FALLTHROUGH.
3076 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00003077 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00003078 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00003079 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003080 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00003081 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003082 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00003083 case TargetLowering::Promote: {
3084 // First step, figure out the appropriate operation to use.
3085 // Allow SETCC to not be supported for all legal data types
3086 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00003087 MVT NewInTy = Node->getOperand(0).getValueType();
3088 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00003089
3090 // Scan for the appropriate larger type to use.
3091 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003092 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00003093
Duncan Sands83ec4b62008-06-06 12:08:01 +00003094 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003095 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003096 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003097 "Fell off of the edge of the floating point world");
3098
3099 // If the target supports SETCC of this type, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003100 if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00003101 break;
3102 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003103 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00003104 assert(0 && "Cannot promote Legal Integer SETCC yet");
3105 else {
Dale Johannesenca57b842009-02-02 23:46:53 +00003106 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp1);
3107 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp2);
Andrew Lenharthae355752005-11-30 17:12:26 +00003108 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003109 Tmp1 = LegalizeOp(Tmp1);
3110 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00003111 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00003112 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00003113 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00003114 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003115 case TargetLowering::Expand:
3116 // Expand a setcc node into a select_cc of the same condition, lhs, and
3117 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003118 MVT VT = Node->getValueType(0);
Dale Johannesenca57b842009-02-02 23:46:53 +00003119 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
Nate Begemanb942a3d2005-08-23 04:29:48 +00003120 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00003121 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003122 break;
3123 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003124 break;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003125 case ISD::VSETCC: {
3126 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3127 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman475871a2008-07-27 21:46:04 +00003128 SDValue CC = Node->getOperand(2);
Nate Begemanb43e9c12008-05-12 19:40:03 +00003129
3130 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3131
3132 // Everything is legal, see if we should expand this op or something.
3133 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3134 default: assert(0 && "This action is not supported yet!");
3135 case TargetLowering::Legal: break;
3136 case TargetLowering::Custom:
3137 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003138 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003139 break;
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003140 case TargetLowering::Expand: {
3141 // Unroll into a nasty set of scalar code for now.
3142 MVT VT = Node->getValueType(0);
3143 unsigned NumElems = VT.getVectorNumElements();
3144 MVT EltVT = VT.getVectorElementType();
3145 MVT TmpEltVT = Tmp1.getValueType().getVectorElementType();
3146 SmallVector<SDValue, 8> Ops(NumElems);
3147 for (unsigned i = 0; i < NumElems; ++i) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003148 SDValue In1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT,
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003149 Tmp1, DAG.getIntPtrConstant(i));
Dale Johannesenca57b842009-02-02 23:46:53 +00003150 Ops[i] = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(TmpEltVT),
3151 In1, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3152 TmpEltVT, Tmp2,
3153 DAG.getIntPtrConstant(i)),
Mon P Wang84aff842008-12-17 08:49:47 +00003154 CC);
Dale Johannesenca57b842009-02-02 23:46:53 +00003155 Ops[i] = DAG.getNode(ISD::SELECT, dl, EltVT, Ops[i], DAG.getConstant(
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003156 APInt::getAllOnesValue(EltVT.getSizeInBits()),
3157 EltVT), DAG.getConstant(0, EltVT));
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003158 }
Dale Johannesenca57b842009-02-02 23:46:53 +00003159 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElems);
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003160 break;
3161 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00003162 }
3163 break;
3164 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00003165
Chris Lattner5b359c62005-04-02 04:00:59 +00003166 case ISD::SHL_PARTS:
3167 case ISD::SRA_PARTS:
3168 case ISD::SRL_PARTS: {
Dan Gohman475871a2008-07-27 21:46:04 +00003169 SmallVector<SDValue, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00003170 bool Changed = false;
Duncan Sands92abc622009-01-31 15:50:11 +00003171 unsigned N = Node->getNumOperands();
3172 for (unsigned i = 0; i + 1 < N; ++i) {
Chris Lattner84f67882005-01-20 18:52:28 +00003173 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3174 Changed |= Ops.back() != Node->getOperand(i);
3175 }
Duncan Sands92abc622009-01-31 15:50:11 +00003176 Ops.push_back(LegalizeOp(DAG.getShiftAmountOperand(Node->getOperand(N-1))));
3177 Changed |= Ops.back() != Node->getOperand(N-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003178 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003179 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00003180
Evan Cheng05a2d562006-01-09 18:31:59 +00003181 switch (TLI.getOperationAction(Node->getOpcode(),
3182 Node->getValueType(0))) {
3183 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003184 case TargetLowering::Legal: break;
3185 case TargetLowering::Custom:
3186 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003187 if (Tmp1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003188 SDValue Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00003189 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003190 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman475871a2008-07-27 21:46:04 +00003191 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003192 if (i == Op.getResNo())
Evan Cheng12f22742006-01-19 04:54:52 +00003193 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00003194 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003195 assert(RetVal.getNode() && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00003196 return RetVal;
3197 }
Evan Cheng05a2d562006-01-09 18:31:59 +00003198 break;
3199 }
3200
Chris Lattner2c8086f2005-04-02 05:00:07 +00003201 // Since these produce multiple values, make sure to remember that we
3202 // legalized all of them.
3203 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +00003204 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00003205 return Result.getValue(Op.getResNo());
Chris Lattner84f67882005-01-20 18:52:28 +00003206 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003207
3208 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00003209 case ISD::ADD:
3210 case ISD::SUB:
3211 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00003212 case ISD::MULHS:
3213 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003214 case ISD::UDIV:
3215 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003216 case ISD::AND:
3217 case ISD::OR:
3218 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00003219 case ISD::SHL:
3220 case ISD::SRL:
3221 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00003222 case ISD::FADD:
3223 case ISD::FSUB:
3224 case ISD::FMUL:
3225 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00003226 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003227 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands92abc622009-01-31 15:50:11 +00003228 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003229
3230 if ((Node->getOpcode() == ISD::SHL ||
3231 Node->getOpcode() == ISD::SRL ||
3232 Node->getOpcode() == ISD::SRA) &&
Duncan Sands92abc622009-01-31 15:50:11 +00003233 !Node->getValueType(0).isVector())
3234 Tmp2 = DAG.getShiftAmountOperand(Tmp2);
3235
3236 switch (getTypeAction(Tmp2.getValueType())) {
3237 case Expand: assert(0 && "Not possible");
3238 case Legal:
3239 Tmp2 = LegalizeOp(Tmp2); // Legalize the RHS.
3240 break;
3241 case Promote:
3242 Tmp2 = PromoteOp(Tmp2); // Promote the RHS.
3243 break;
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003244 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003245
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003246 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003247
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003248 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003249 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00003250 case TargetLowering::Legal: break;
3251 case TargetLowering::Custom:
3252 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003253 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003254 Result = Tmp1;
3255 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00003256 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003257 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003258 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003259 MVT VT = Op.getValueType();
Mon P Wang0c397192008-10-30 08:01:45 +00003260
Dan Gohman525178c2007-10-08 18:33:35 +00003261 // See if multiply or divide can be lowered using two-result operations.
3262 SDVTList VTs = DAG.getVTList(VT, VT);
3263 if (Node->getOpcode() == ISD::MUL) {
3264 // We just need the low half of the multiply; try both the signed
3265 // and unsigned forms. If the target supports both SMUL_LOHI and
3266 // UMUL_LOHI, form a preference by checking which forms of plain
3267 // MULH it supports.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003268 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3269 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3270 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3271 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
Dan Gohman525178c2007-10-08 18:33:35 +00003272 unsigned OpToUse = 0;
3273 if (HasSMUL_LOHI && !HasMULHS) {
3274 OpToUse = ISD::SMUL_LOHI;
3275 } else if (HasUMUL_LOHI && !HasMULHU) {
3276 OpToUse = ISD::UMUL_LOHI;
3277 } else if (HasSMUL_LOHI) {
3278 OpToUse = ISD::SMUL_LOHI;
3279 } else if (HasUMUL_LOHI) {
3280 OpToUse = ISD::UMUL_LOHI;
3281 }
3282 if (OpToUse) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003283 Result = SDValue(DAG.getNode(OpToUse, dl, VTs, Tmp1, Tmp2).getNode(),
3284 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003285 break;
3286 }
3287 }
3288 if (Node->getOpcode() == ISD::MULHS &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003289 TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003290 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl,
3291 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00003292 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003293 break;
3294 }
3295 if (Node->getOpcode() == ISD::MULHU &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003296 TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003297 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl,
3298 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00003299 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003300 break;
3301 }
3302 if (Node->getOpcode() == ISD::SDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003303 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003304 Result = SDValue(DAG.getNode(ISD::SDIVREM, dl,
3305 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00003306 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003307 break;
3308 }
3309 if (Node->getOpcode() == ISD::UDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003310 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003311 Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
3312 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00003313 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003314 break;
3315 }
Mon P Wang87c8a8f2008-12-18 20:03:17 +00003316
Dan Gohman82669522007-10-11 23:57:53 +00003317 // Check to see if we have a libcall for this operator.
3318 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3319 bool isSigned = false;
3320 switch (Node->getOpcode()) {
3321 case ISD::UDIV:
3322 case ISD::SDIV:
3323 if (VT == MVT::i32) {
3324 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang0c397192008-10-30 08:01:45 +00003325 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00003326 isSigned = Node->getOpcode() == ISD::SDIV;
3327 }
3328 break;
Chris Lattner31d71612008-10-04 21:27:46 +00003329 case ISD::MUL:
3330 if (VT == MVT::i32)
3331 LC = RTLIB::MUL_I32;
Nate Begeman9b994852009-01-24 22:12:48 +00003332 else if (VT == MVT::i64)
Scott Michel845145f2008-12-29 03:21:37 +00003333 LC = RTLIB::MUL_I64;
Chris Lattner31d71612008-10-04 21:27:46 +00003334 break;
Dan Gohman82669522007-10-11 23:57:53 +00003335 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003336 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3337 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003338 break;
Scott Micheld1e8d9c2009-01-21 04:58:48 +00003339 case ISD::FDIV:
3340 LC = GetFPLibCall(VT, RTLIB::DIV_F32, RTLIB::DIV_F64, RTLIB::DIV_F80,
3341 RTLIB::DIV_PPCF128);
3342 break;
Dan Gohman82669522007-10-11 23:57:53 +00003343 default: break;
3344 }
3345 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00003346 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003347 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003348 break;
3349 }
Mon P Wang0c397192008-10-30 08:01:45 +00003350
Duncan Sands83ec4b62008-06-06 12:08:01 +00003351 assert(Node->getValueType(0).isVector() &&
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003352 "Cannot expand this binary operator!");
3353 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003354 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003355 break;
3356 }
Evan Chengcc987612006-04-12 21:20:24 +00003357 case TargetLowering::Promote: {
3358 switch (Node->getOpcode()) {
3359 default: assert(0 && "Do not know how to promote this BinOp!");
3360 case ISD::AND:
3361 case ISD::OR:
3362 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003363 MVT OVT = Node->getValueType(0);
3364 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3365 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00003366 // Bit convert each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00003367 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
3368 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
3369 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Evan Chengcc987612006-04-12 21:20:24 +00003370 // Bit convert the result back the original type.
Dale Johannesenca57b842009-02-02 23:46:53 +00003371 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Evan Chengcc987612006-04-12 21:20:24 +00003372 break;
3373 }
3374 }
3375 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003376 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003377 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003378
Dan Gohmane14ea862007-10-05 14:17:22 +00003379 case ISD::SMUL_LOHI:
3380 case ISD::UMUL_LOHI:
3381 case ISD::SDIVREM:
3382 case ISD::UDIVREM:
3383 // These nodes will only be produced by target-specific lowering, so
3384 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003385 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003386 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003387
3388 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3389 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3390 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003391 break;
3392
Chris Lattnera09f8482006-03-05 05:09:38 +00003393 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3394 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3395 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3396 case Expand: assert(0 && "Not possible");
3397 case Legal:
3398 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3399 break;
3400 case Promote:
3401 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3402 break;
3403 }
3404
3405 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3406
3407 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3408 default: assert(0 && "Operation not supported");
3409 case TargetLowering::Custom:
3410 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003411 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003412 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003413 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003414 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003415 // If this target supports fabs/fneg natively and select is cheap,
3416 // do this efficiently.
3417 if (!TLI.isSelectExpensive() &&
3418 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3419 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003420 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003421 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003422 // Get the sign bit of the RHS.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003423 MVT IVT =
Chris Lattner8f4191d2006-03-13 06:08:38 +00003424 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dale Johannesenca57b842009-02-02 23:46:53 +00003425 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2);
3426 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(IVT),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003427 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3428 // Get the absolute value of the result.
Dale Johannesenca57b842009-02-02 23:46:53 +00003429 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
Chris Lattner8f4191d2006-03-13 06:08:38 +00003430 // Select between the nabs and abs value based on the sign bit of
3431 // the input.
Dale Johannesenca57b842009-02-02 23:46:53 +00003432 Result = DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
Chris Lattner8f4191d2006-03-13 06:08:38 +00003433 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3434 AbsVal),
3435 AbsVal);
3436 Result = LegalizeOp(Result);
3437 break;
3438 }
3439
3440 // Otherwise, do bitwise ops!
Duncan Sands83ec4b62008-06-06 12:08:01 +00003441 MVT NVT =
Evan Cheng912095b2007-01-04 21:56:39 +00003442 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3443 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
Dale Johannesenca57b842009-02-02 23:46:53 +00003444 Result = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0), Result);
Evan Cheng912095b2007-01-04 21:56:39 +00003445 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003446 break;
3447 }
Evan Cheng912095b2007-01-04 21:56:39 +00003448 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003449 break;
3450
Nate Begeman551bf3f2006-02-17 05:43:56 +00003451 case ISD::ADDC:
3452 case ISD::SUBC:
3453 Tmp1 = LegalizeOp(Node->getOperand(0));
3454 Tmp2 = LegalizeOp(Node->getOperand(1));
3455 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003456 Tmp3 = Result.getValue(0);
3457 Tmp4 = Result.getValue(1);
3458
3459 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3460 default: assert(0 && "This action is not supported yet!");
3461 case TargetLowering::Legal:
3462 break;
3463 case TargetLowering::Custom:
3464 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3465 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003466 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003467 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3468 }
3469 break;
3470 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003471 // Since this produces two values, make sure to remember that we legalized
3472 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003473 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3474 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3475 return Op.getResNo() ? Tmp4 : Tmp3;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003476
Nate Begeman551bf3f2006-02-17 05:43:56 +00003477 case ISD::ADDE:
3478 case ISD::SUBE:
3479 Tmp1 = LegalizeOp(Node->getOperand(0));
3480 Tmp2 = LegalizeOp(Node->getOperand(1));
3481 Tmp3 = LegalizeOp(Node->getOperand(2));
3482 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003483 Tmp3 = Result.getValue(0);
3484 Tmp4 = Result.getValue(1);
3485
3486 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3487 default: assert(0 && "This action is not supported yet!");
3488 case TargetLowering::Legal:
3489 break;
3490 case TargetLowering::Custom:
3491 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3492 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003493 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003494 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3495 }
3496 break;
3497 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003498 // Since this produces two values, make sure to remember that we legalized
3499 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003500 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3501 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3502 return Op.getResNo() ? Tmp4 : Tmp3;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003503
Nate Begeman419f8b62005-10-18 00:27:41 +00003504 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003505 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00003506 // TODO: handle the case where the Lo and Hi operands are not of legal type
3507 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3508 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3509 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003510 case TargetLowering::Promote:
3511 case TargetLowering::Custom:
3512 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003513 case TargetLowering::Legal:
3514 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Dale Johannesenca57b842009-02-02 23:46:53 +00003515 Result = DAG.getNode(ISD::BUILD_PAIR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003516 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003517 case TargetLowering::Expand:
Dale Johannesenca57b842009-02-02 23:46:53 +00003518 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Tmp1);
3519 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Tmp2);
3520 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003521 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00003522 TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00003523 Result = DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003524 break;
3525 }
3526 break;
3527 }
3528
Nate Begemanc105e192005-04-06 00:23:54 +00003529 case ISD::UREM:
3530 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003531 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003532 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3533 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003534
Nate Begemanc105e192005-04-06 00:23:54 +00003535 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003536 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3537 case TargetLowering::Custom:
3538 isCustom = true;
3539 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003540 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003541 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003542 if (isCustom) {
3543 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003544 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003545 }
Nate Begemanc105e192005-04-06 00:23:54 +00003546 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003547 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003548 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003549 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003550 MVT VT = Node->getValueType(0);
Dan Gohman525178c2007-10-08 18:33:35 +00003551
3552 // See if remainder can be lowered using two-result operations.
3553 SDVTList VTs = DAG.getVTList(VT, VT);
3554 if (Node->getOpcode() == ISD::SREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003555 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003556 Result = SDValue(DAG.getNode(ISD::SDIVREM, dl,
3557 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003558 break;
3559 }
3560 if (Node->getOpcode() == ISD::UREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003561 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00003562 Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
3563 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003564 break;
3565 }
3566
Duncan Sands83ec4b62008-06-06 12:08:01 +00003567 if (VT.isInteger()) {
Dan Gohman525178c2007-10-08 18:33:35 +00003568 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003569 TargetLowering::Legal) {
3570 // X % Y -> X-X/Y*Y
Dale Johannesenca57b842009-02-02 23:46:53 +00003571 Result = DAG.getNode(DivOpc, dl, VT, Tmp1, Tmp2);
3572 Result = DAG.getNode(ISD::MUL, dl, VT, Result, Tmp2);
3573 Result = DAG.getNode(ISD::SUB, dl, VT, Tmp1, Result);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003574 } else if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003575 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003576 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003577 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003578 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003579 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3580 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman475871a2008-07-27 21:46:04 +00003581 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003582 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003583 }
Dan Gohman0d974262007-11-06 22:11:54 +00003584 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003585 assert(VT.isFloatingPoint() &&
Dan Gohman0d974262007-11-06 22:11:54 +00003586 "remainder op must have integer or floating-point type");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003587 if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003588 Result = LegalizeOp(UnrollVectorOp(Op));
3589 } else {
3590 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003591 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3592 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003593 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003594 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman80176312007-11-05 23:35:22 +00003595 }
Nate Begemanc105e192005-04-06 00:23:54 +00003596 }
3597 break;
3598 }
Dan Gohman525178c2007-10-08 18:33:35 +00003599 }
Nate Begemanc105e192005-04-06 00:23:54 +00003600 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003601 case ISD::VAARG: {
3602 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3603 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3604
Duncan Sands83ec4b62008-06-06 12:08:01 +00003605 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003606 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3607 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003608 case TargetLowering::Custom:
3609 isCustom = true;
3610 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003611 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003612 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3613 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003614 Tmp1 = Result.getValue(1);
3615
3616 if (isCustom) {
3617 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003618 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003619 Result = LegalizeOp(Tmp2);
3620 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3621 }
3622 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003623 break;
3624 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003625 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00003626 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003627 // Increment the pointer, VAList, to the next vaarg
Dale Johannesenca57b842009-02-02 23:46:53 +00003628 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00003629 DAG.getConstant(TLI.getTargetData()->
3630 getTypePaddedSize(VT.getTypeForMVT()),
3631 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00003632 // Store the incremented VAList to the legalized pointer
Dale Johannesenca57b842009-02-02 23:46:53 +00003633 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003634 // Load the actual argument out of the pointer VAList
Dale Johannesenca57b842009-02-02 23:46:53 +00003635 Result = DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003636 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003637 Result = LegalizeOp(Result);
3638 break;
3639 }
3640 }
3641 // Since VAARG produces two values, make sure to remember that we
3642 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003643 AddLegalizedOperand(SDValue(Node, 0), Result);
3644 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003645 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003646 }
3647
3648 case ISD::VACOPY:
3649 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3650 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3651 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3652
3653 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3654 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003655 case TargetLowering::Custom:
3656 isCustom = true;
3657 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003658 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003659 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3660 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003661 if (isCustom) {
3662 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003663 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003664 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003665 break;
3666 case TargetLowering::Expand:
3667 // This defaults to loading a pointer from the input and storing it to the
3668 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003669 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3670 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00003671 Tmp4 = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp3, VS, 0);
3672 Result = DAG.getStore(Tmp4.getValue(1), dl, Tmp4, Tmp2, VD, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003673 break;
3674 }
3675 break;
3676
3677 case ISD::VAEND:
3678 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3679 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3680
3681 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3682 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003683 case TargetLowering::Custom:
3684 isCustom = true;
3685 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003686 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003687 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003688 if (isCustom) {
3689 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003690 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003691 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003692 break;
3693 case TargetLowering::Expand:
3694 Result = Tmp1; // Default to a no-op, return the chain
3695 break;
3696 }
3697 break;
3698
3699 case ISD::VASTART:
3700 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3701 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3702
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003703 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3704
Nate Begemanacc398c2006-01-25 18:21:52 +00003705 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3706 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003707 case TargetLowering::Legal: break;
3708 case TargetLowering::Custom:
3709 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003710 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003711 break;
3712 }
3713 break;
3714
Nate Begeman35ef9132006-01-11 21:21:00 +00003715 case ISD::ROTL:
3716 case ISD::ROTR:
3717 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands92abc622009-01-31 15:50:11 +00003718 Tmp2 = LegalizeOp(DAG.getShiftAmountOperand(Node->getOperand(1))); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003719 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003720 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3721 default:
3722 assert(0 && "ROTL/ROTR legalize operation not supported");
3723 break;
3724 case TargetLowering::Legal:
3725 break;
3726 case TargetLowering::Custom:
3727 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003728 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelc9dc1142007-04-02 21:36:32 +00003729 break;
3730 case TargetLowering::Promote:
3731 assert(0 && "Do not know how to promote ROTL/ROTR");
3732 break;
3733 case TargetLowering::Expand:
3734 assert(0 && "Do not know how to expand ROTL/ROTR");
3735 break;
3736 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003737 break;
3738
Nate Begemand88fc032006-01-14 03:14:10 +00003739 case ISD::BSWAP:
3740 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3741 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003742 case TargetLowering::Custom:
3743 assert(0 && "Cannot custom legalize this yet!");
3744 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003745 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003746 break;
3747 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003748 MVT OVT = Tmp1.getValueType();
3749 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3750 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begemand88fc032006-01-14 03:14:10 +00003751
Dale Johannesenca57b842009-02-02 23:46:53 +00003752 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
3753 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3754 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Chris Lattner456a93a2006-01-28 07:39:30 +00003755 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3756 break;
3757 }
3758 case TargetLowering::Expand:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003759 Result = ExpandBSWAP(Tmp1, dl);
Chris Lattner456a93a2006-01-28 07:39:30 +00003760 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003761 }
3762 break;
3763
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003764 case ISD::CTPOP:
3765 case ISD::CTTZ:
3766 case ISD::CTLZ:
3767 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3768 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003769 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003770 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003771 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003772 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003773 TargetLowering::Custom) {
3774 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003775 if (Tmp1.getNode()) {
Scott Michel335f4f72007-08-02 02:22:46 +00003776 Result = Tmp1;
3777 }
Scott Michel910b66d2007-07-30 21:00:31 +00003778 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003779 break;
3780 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003781 MVT OVT = Tmp1.getValueType();
3782 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003783
3784 // Zero extend the argument.
Dale Johannesenca57b842009-02-02 23:46:53 +00003785 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003786 // Perform the larger operation, then subtract if needed.
Dale Johannesenca57b842009-02-02 23:46:53 +00003787 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003788 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003789 case ISD::CTPOP:
3790 Result = Tmp1;
3791 break;
3792 case ISD::CTTZ:
3793 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Dale Johannesenca57b842009-02-02 23:46:53 +00003794 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3795 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003796 ISD::SETEQ);
Dale Johannesenca57b842009-02-02 23:46:53 +00003797 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003798 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003799 break;
3800 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003801 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Dale Johannesenca57b842009-02-02 23:46:53 +00003802 Result = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003803 DAG.getConstant(NVT.getSizeInBits() -
3804 OVT.getSizeInBits(), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003805 break;
3806 }
3807 break;
3808 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003809 case TargetLowering::Expand:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003810 Result = ExpandBitCount(Node->getOpcode(), Tmp1, dl);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003811 break;
3812 }
3813 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003814
Chris Lattner2c8086f2005-04-02 05:00:07 +00003815 // Unary operators
3816 case ISD::FABS:
3817 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003818 case ISD::FSQRT:
3819 case ISD::FSIN:
3820 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003821 case ISD::FLOG:
3822 case ISD::FLOG2:
3823 case ISD::FLOG10:
3824 case ISD::FEXP:
3825 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00003826 case ISD::FTRUNC:
3827 case ISD::FFLOOR:
3828 case ISD::FCEIL:
3829 case ISD::FRINT:
3830 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003831 Tmp1 = LegalizeOp(Node->getOperand(0));
3832 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003833 case TargetLowering::Promote:
3834 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003835 isCustom = true;
3836 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003837 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003838 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003839 if (isCustom) {
3840 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003841 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00003842 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003843 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003844 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003845 switch (Node->getOpcode()) {
3846 default: assert(0 && "Unreachable!");
3847 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003848 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3849 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00003850 Result = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003851 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003852 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003853 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003854 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003855 Tmp2 = DAG.getConstantFP(0.0, VT);
Dale Johannesenca57b842009-02-02 23:46:53 +00003856 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00003857 Tmp1, Tmp2, ISD::SETUGT);
Dale Johannesenca57b842009-02-02 23:46:53 +00003858 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
3859 Result = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003860 break;
3861 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00003862 case ISD::FSQRT:
3863 case ISD::FSIN:
3864 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003865 case ISD::FLOG:
3866 case ISD::FLOG2:
3867 case ISD::FLOG10:
3868 case ISD::FEXP:
3869 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003870 case ISD::FTRUNC:
3871 case ISD::FFLOOR:
3872 case ISD::FCEIL:
3873 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00003874 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003875 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003876
3877 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003878 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003879 Result = LegalizeOp(UnrollVectorOp(Op));
3880 break;
3881 }
3882
Evan Cheng56966222007-01-12 02:11:51 +00003883 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003884 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003885 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003886 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3887 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003888 break;
3889 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003890 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3891 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003892 break;
3893 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003894 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3895 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003896 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003897 case ISD::FLOG:
3898 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3899 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3900 break;
3901 case ISD::FLOG2:
3902 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3903 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3904 break;
3905 case ISD::FLOG10:
3906 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3907 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3908 break;
3909 case ISD::FEXP:
3910 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3911 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3912 break;
3913 case ISD::FEXP2:
3914 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3915 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3916 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003917 case ISD::FTRUNC:
3918 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3919 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3920 break;
3921 case ISD::FFLOOR:
3922 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3923 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3924 break;
3925 case ISD::FCEIL:
3926 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3927 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3928 break;
3929 case ISD::FRINT:
3930 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3931 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3932 break;
3933 case ISD::FNEARBYINT:
3934 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3935 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3936 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00003937 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003938 default: assert(0 && "Unreachable!");
3939 }
Dan Gohman475871a2008-07-27 21:46:04 +00003940 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003941 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003942 break;
3943 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003944 }
3945 break;
3946 }
3947 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003948 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003949 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003950
3951 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003952 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003953 Result = LegalizeOp(UnrollVectorOp(Op));
3954 break;
3955 }
3956
3957 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003958 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3959 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003960 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003961 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003962 break;
3963 }
Chris Lattner35481892005-12-23 00:16:34 +00003964 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003965 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003966 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003967 Node->getValueType(0), dl);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003968 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003969 // The input has to be a vector type, we have to either scalarize it, pack
3970 // it, or convert it based on whether the input vector type is legal.
Gabor Greifba36cb52008-08-28 21:40:38 +00003971 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00003972 int InIx = Node->getOperand(0).getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003973 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3974 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohman7f321562007-06-25 16:23:39 +00003975
3976 // Figure out if there is a simple type corresponding to this Vector
3977 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003978 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00003979 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003980 // Turn this into a bit convert of the vector input.
Dale Johannesenca57b842009-02-02 23:46:53 +00003981 Result = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0),
Dan Gohman7f321562007-06-25 16:23:39 +00003982 LegalizeOp(Node->getOperand(0)));
3983 break;
3984 } else if (NumElems == 1) {
3985 // Turn this into a bit convert of the scalar input.
Dale Johannesenca57b842009-02-02 23:46:53 +00003986 Result = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0),
Dan Gohman7f321562007-06-25 16:23:39 +00003987 ScalarizeVectorOp(Node->getOperand(0)));
3988 break;
3989 } else {
3990 // FIXME: UNIMP! Store then reload
3991 assert(0 && "Cast from unsupported vector type not implemented yet!");
3992 }
Chris Lattner67993f72006-01-23 07:30:46 +00003993 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003994 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3995 Node->getOperand(0).getValueType())) {
3996 default: assert(0 && "Unknown operation action!");
3997 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003998 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003999 Node->getValueType(0), dl);
Chris Lattner35481892005-12-23 00:16:34 +00004000 break;
4001 case TargetLowering::Legal:
4002 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004003 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00004004 break;
4005 }
4006 }
4007 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00004008 case ISD::CONVERT_RNDSAT: {
4009 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4010 switch (CvtCode) {
4011 default: assert(0 && "Unknown cvt code!");
4012 case ISD::CVT_SF:
4013 case ISD::CVT_UF:
Mon P Wang77cdf302008-11-10 20:54:11 +00004014 case ISD::CVT_FF:
Mon P Wang1cd46bb2008-12-09 07:27:39 +00004015 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00004016 case ISD::CVT_FS:
4017 case ISD::CVT_FU:
4018 case ISD::CVT_SS:
4019 case ISD::CVT_SU:
4020 case ISD::CVT_US:
4021 case ISD::CVT_UU: {
4022 SDValue DTyOp = Node->getOperand(1);
4023 SDValue STyOp = Node->getOperand(2);
4024 SDValue RndOp = Node->getOperand(3);
4025 SDValue SatOp = Node->getOperand(4);
4026 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4027 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4028 case Legal:
4029 Tmp1 = LegalizeOp(Node->getOperand(0));
4030 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
4031 RndOp, SatOp);
4032 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4033 TargetLowering::Custom) {
4034 Tmp1 = TLI.LowerOperation(Result, DAG);
4035 if (Tmp1.getNode()) Result = Tmp1;
4036 }
4037 break;
4038 case Promote:
4039 Result = PromoteOp(Node->getOperand(0));
4040 // For FP, make Op1 a i32
4041
Dale Johannesenc460ae92009-02-04 00:13:36 +00004042 Result = DAG.getConvertRndSat(Op.getValueType(), dl, Result,
Mon P Wang77cdf302008-11-10 20:54:11 +00004043 DTyOp, STyOp, RndOp, SatOp, CvtCode);
4044 break;
4045 }
4046 break;
4047 }
4048 } // end switch CvtCode
4049 break;
4050 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00004051 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00004052 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004053 case ISD::UINT_TO_FP: {
4054 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman7f8613e2008-08-14 20:04:46 +00004055 Result = LegalizeINT_TO_FP(Result, isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +00004056 Node->getValueType(0), Node->getOperand(0), dl);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004057 break;
4058 }
4059 case ISD::TRUNCATE:
4060 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4061 case Legal:
4062 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel546d7b52008-12-02 19:55:08 +00004063 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
4064 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
4065 case TargetLowering::Custom:
Mon P Wangf67303d2008-12-11 00:44:22 +00004066 isCustom = true;
4067 // FALLTHROUGH
Scott Michel546d7b52008-12-02 19:55:08 +00004068 case TargetLowering::Legal:
Mon P Wangf67303d2008-12-11 00:44:22 +00004069 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4070 if (isCustom) {
4071 Tmp1 = TLI.LowerOperation(Result, DAG);
4072 if (Tmp1.getNode()) Result = Tmp1;
4073 }
4074 break;
Mon P Wang9e5ecb82008-12-12 01:25:51 +00004075 case TargetLowering::Expand:
4076 assert(Result.getValueType().isVector() && "must be vector type");
4077 // Unroll the truncate. We should do better.
4078 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Schellerb0a5cdd2008-12-02 12:12:25 +00004079 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004080 break;
4081 case Expand:
4082 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4083
4084 // Since the result is legal, we should just be able to truncate the low
4085 // part of the source.
Dale Johannesenca57b842009-02-02 23:46:53 +00004086 Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004087 break;
4088 case Promote:
4089 Result = PromoteOp(Node->getOperand(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00004090 Result = DAG.getNode(ISD::TRUNCATE, dl, Op.getValueType(), Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004091 break;
4092 }
4093 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004094
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004095 case ISD::FP_TO_SINT:
4096 case ISD::FP_TO_UINT:
4097 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4098 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00004099 Tmp1 = LegalizeOp(Node->getOperand(0));
4100
Chris Lattner1618beb2005-07-29 00:11:56 +00004101 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
4102 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004103 case TargetLowering::Custom:
4104 isCustom = true;
4105 // FALLTHROUGH
4106 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004107 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004108 if (isCustom) {
4109 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004110 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00004111 }
4112 break;
4113 case TargetLowering::Promote:
4114 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Dale Johannesenaf435272009-02-02 19:03:57 +00004115 Node->getOpcode() == ISD::FP_TO_SINT,
4116 dl);
Chris Lattner456a93a2006-01-28 07:39:30 +00004117 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00004118 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00004119 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004120 SDValue True, False;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004121 MVT VT = Node->getOperand(0).getValueType();
4122 MVT NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00004123 const uint64_t zero[] = {0, 0};
Duncan Sands83ec4b62008-06-06 12:08:01 +00004124 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
4125 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohmanc7773bf2008-02-29 01:44:25 +00004126 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00004127 Tmp2 = DAG.getConstantFP(apf, VT);
Dale Johannesenaf435272009-02-02 19:03:57 +00004128 Tmp3 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
4129 Node->getOperand(0),
Duncan Sands5480c042009-01-01 15:52:00 +00004130 Tmp2, ISD::SETLT);
Dale Johannesenaf435272009-02-02 19:03:57 +00004131 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
4132 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
4133 DAG.getNode(ISD::FSUB, dl, VT,
4134 Node->getOperand(0), Tmp2));
4135 False = DAG.getNode(ISD::XOR, dl, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00004136 DAG.getConstant(x, NVT));
Dale Johannesenaf435272009-02-02 19:03:57 +00004137 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp3, True, False);
Chris Lattner456a93a2006-01-28 07:39:30 +00004138 break;
Nate Begemand2558e32005-08-14 01:20:53 +00004139 } else {
4140 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
4141 }
4142 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00004143 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004144 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00004145 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004146 MVT VT = Op.getValueType();
4147 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004148 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004149 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004150 if (Node->getOpcode() == ISD::FP_TO_SINT) {
Dale Johannesenaf435272009-02-02 19:03:57 +00004151 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
Chris Lattner0bd48932008-01-17 07:00:52 +00004152 Node->getOperand(0), DAG.getValueType(MVT::f64));
Dale Johannesenaf435272009-02-02 19:03:57 +00004153 Result = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Result,
Chris Lattner0bd48932008-01-17 07:00:52 +00004154 DAG.getIntPtrConstant(1));
Dale Johannesenaf435272009-02-02 19:03:57 +00004155 Result = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00004156 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004157 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4158 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4159 Tmp2 = DAG.getConstantFP(apf, OVT);
4160 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4161 // FIXME: generated code sucks.
Dale Johannesenaf435272009-02-02 19:03:57 +00004162 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Node->getOperand(0),
4163 Tmp2,
4164 DAG.getNode(ISD::ADD, dl, MVT::i32,
4165 DAG.getNode(ISD::FP_TO_SINT, dl, VT,
4166 DAG.getNode(ISD::FSUB, dl, OVT,
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004167 Node->getOperand(0), Tmp2)),
4168 DAG.getConstant(0x80000000, MVT::i32)),
Dale Johannesenaf435272009-02-02 19:03:57 +00004169 DAG.getNode(ISD::FP_TO_SINT, dl, VT,
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004170 Node->getOperand(0)),
4171 DAG.getCondCode(ISD::SETGE));
4172 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004173 break;
4174 }
Dan Gohmana2e94852008-03-10 23:03:31 +00004175 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsb2ff8852008-07-17 02:36:29 +00004176 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4177 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4178 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman475871a2008-07-27 21:46:04 +00004179 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00004180 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00004181 break;
4182 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004183 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004184 Tmp1 = PromoteOp(Node->getOperand(0));
4185 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4186 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004187 break;
4188 }
4189 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004190
Chris Lattnerf2670a82008-01-16 06:57:07 +00004191 case ISD::FP_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004192 MVT DstVT = Op.getValueType();
4193 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004194 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4195 // The only other way we can lower this is to turn it into a STORE,
4196 // LOAD pair, targetting a temporary location (a stack slot).
Dale Johannesen8a782a22009-02-02 22:12:50 +00004197 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT, dl);
Chris Lattner0bd48932008-01-17 07:00:52 +00004198 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00004199 }
4200 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4201 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4202 case Legal:
4203 Tmp1 = LegalizeOp(Node->getOperand(0));
4204 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4205 break;
4206 case Promote:
4207 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00004208 Result = DAG.getNode(ISD::FP_EXTEND, dl, Op.getValueType(), Tmp1);
Chris Lattnerf2670a82008-01-16 06:57:07 +00004209 break;
4210 }
4211 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004212 }
Dale Johannesen5411a392007-08-09 01:04:01 +00004213 case ISD::FP_ROUND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004214 MVT DstVT = Op.getValueType();
4215 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004216 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4217 if (SrcVT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00004218 SDValue Lo;
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004219 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00004220 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004221 if (DstVT!=MVT::f64)
Dale Johannesenca57b842009-02-02 23:46:53 +00004222 Result = DAG.getNode(ISD::FP_ROUND, dl,
4223 DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00004224 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00004225 }
Chris Lattner0bd48932008-01-17 07:00:52 +00004226 // The only other way we can lower this is to turn it into a STORE,
4227 // LOAD pair, targetting a temporary location (a stack slot).
Dale Johannesen8a782a22009-02-02 22:12:50 +00004228 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT, dl);
Chris Lattner0bd48932008-01-17 07:00:52 +00004229 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00004230 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00004231 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4232 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4233 case Legal:
4234 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004235 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004236 break;
4237 case Promote:
4238 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00004239 Result = DAG.getNode(ISD::FP_ROUND, dl, Op.getValueType(), Tmp1,
Chris Lattner0bd48932008-01-17 07:00:52 +00004240 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004241 break;
4242 }
4243 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004244 }
Chris Lattner13c78e22005-09-02 00:18:10 +00004245 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004246 case ISD::ZERO_EXTEND:
4247 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004248 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00004249 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004250 case Legal:
4251 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel82747a52008-04-30 00:26:38 +00004252 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel0123b7d2008-02-15 23:05:48 +00004253 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4254 TargetLowering::Custom) {
Scott Michel82747a52008-04-30 00:26:38 +00004255 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004256 if (Tmp1.getNode()) Result = Tmp1;
Scott Michel0123b7d2008-02-15 23:05:48 +00004257 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004258 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004259 case Promote:
4260 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00004261 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004262 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00004263 Result = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00004264 break;
Chris Lattner1713e732005-01-16 00:38:00 +00004265 case ISD::ZERO_EXTEND:
4266 Result = PromoteOp(Node->getOperand(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00004267 Result = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), Result);
4268 Result = DAG.getZeroExtendInReg(Result, dl,
Chris Lattner23993562005-04-13 02:38:47 +00004269 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00004270 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004271 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00004272 Result = PromoteOp(Node->getOperand(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00004273 Result = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), Result);
4274 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004275 Result,
4276 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00004277 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004278 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004279 }
4280 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00004281 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00004282 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00004283 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004284 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00004285
4286 // If this operation is not supported, convert it to a shl/shr or load/store
4287 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004288 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4289 default: assert(0 && "This action not supported for this op yet!");
4290 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004291 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004292 break;
4293 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00004294 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00004295 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00004296 // NOTE: we could fall back on load/store here too for targets without
4297 // SAR. However, it is doubtful that any exist.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004298 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4299 ExtraVT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00004300 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Dale Johannesenca57b842009-02-02 23:46:53 +00004301 Result = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
Chris Lattner45b8caf2005-01-15 07:15:18 +00004302 Node->getOperand(0), ShiftCst);
Dale Johannesenca57b842009-02-02 23:46:53 +00004303 Result = DAG.getNode(ISD::SRA, dl, Node->getValueType(0),
Chris Lattner45b8caf2005-01-15 07:15:18 +00004304 Result, ShiftCst);
4305 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00004306 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00004307 // EXTLOAD pair, targetting a temporary location (a stack slot).
4308
4309 // NOTE: there is a choice here between constantly creating new stack
4310 // slots and always reusing the same one. We currently always create
4311 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00004312 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00004313 Node->getValueType(0), dl);
Chris Lattner45b8caf2005-01-15 07:15:18 +00004314 } else {
4315 assert(0 && "Unknown op");
4316 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004317 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00004318 }
Chris Lattner0f69b292005-01-15 06:18:18 +00004319 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004320 }
Duncan Sands36397f52007-07-27 12:58:54 +00004321 case ISD::TRAMPOLINE: {
Dan Gohman475871a2008-07-27 21:46:04 +00004322 SDValue Ops[6];
Duncan Sands36397f52007-07-27 12:58:54 +00004323 for (unsigned i = 0; i != 6; ++i)
4324 Ops[i] = LegalizeOp(Node->getOperand(i));
4325 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4326 // The only option for this node is to custom lower it.
4327 Result = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004328 assert(Result.getNode() && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00004329
4330 // Since trampoline produces two values, make sure to remember that we
4331 // legalized both of them.
4332 Tmp1 = LegalizeOp(Result.getValue(1));
4333 Result = LegalizeOp(Result);
Dan Gohman475871a2008-07-27 21:46:04 +00004334 AddLegalizedOperand(SDValue(Node, 0), Result);
4335 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00004336 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00004337 }
Dan Gohman9c78a392008-05-14 00:43:10 +00004338 case ISD::FLT_ROUNDS_: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004339 MVT VT = Node->getValueType(0);
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004340 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4341 default: assert(0 && "This action not supported for this op yet!");
4342 case TargetLowering::Custom:
4343 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004344 if (Result.getNode()) break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004345 // Fall Thru
4346 case TargetLowering::Legal:
4347 // If this operation is not supported, lower it to constant 1
4348 Result = DAG.getConstant(1, VT);
4349 break;
4350 }
Dan Gohman9ab9ee82008-05-12 16:07:15 +00004351 break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004352 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004353 case ISD::TRAP: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004354 MVT VT = Node->getValueType(0);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004355 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4356 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00004357 case TargetLowering::Legal:
4358 Tmp1 = LegalizeOp(Node->getOperand(0));
4359 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4360 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004361 case TargetLowering::Custom:
4362 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004363 if (Result.getNode()) break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004364 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00004365 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004366 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00004367 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004368 TargetLowering::ArgListTy Args;
Dan Gohman475871a2008-07-27 21:46:04 +00004369 std::pair<SDValue,SDValue> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00004370 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00004371 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00004372 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Dale Johannesen7d2ad622009-01-30 23:10:59 +00004373 Args, DAG, dl);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004374 Result = CallResult.second;
4375 break;
4376 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004377 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004378 }
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004379
Bill Wendling74c37652008-12-09 22:08:41 +00004380 case ISD::SADDO:
4381 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004382 MVT VT = Node->getValueType(0);
4383 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4384 default: assert(0 && "This action not supported for this op yet!");
4385 case TargetLowering::Custom:
4386 Result = TLI.LowerOperation(Op, DAG);
4387 if (Result.getNode()) break;
4388 // FALLTHROUGH
4389 case TargetLowering::Legal: {
4390 SDValue LHS = LegalizeOp(Node->getOperand(0));
4391 SDValue RHS = LegalizeOp(Node->getOperand(1));
4392
Bill Wendling74c37652008-12-09 22:08:41 +00004393 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00004394 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00004395 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004396 MVT OType = Node->getValueType(1);
4397
Bill Wendlinga6af91a2008-11-25 08:19:22 +00004398 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004399
Bill Wendling740464e2008-11-25 19:40:17 +00004400 // LHSSign -> LHS >= 0
4401 // RHSSign -> RHS >= 0
4402 // SumSign -> Sum >= 0
4403 //
Bill Wendling74c37652008-12-09 22:08:41 +00004404 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00004405 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00004406 // Sub:
4407 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00004408 //
Dale Johannesenca57b842009-02-02 23:46:53 +00004409 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
4410 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
4411 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
Bill Wendling74c37652008-12-09 22:08:41 +00004412 Node->getOpcode() == ISD::SADDO ?
4413 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004414
Dale Johannesenca57b842009-02-02 23:46:53 +00004415 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
4416 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004417
Dale Johannesenca57b842009-02-02 23:46:53 +00004418 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004419
4420 MVT ValueVTs[] = { LHS.getValueType(), OType };
4421 SDValue Ops[] = { Sum, Cmp };
4422
Dale Johannesenca57b842009-02-02 23:46:53 +00004423 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
4424 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00004425 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004426 SDNode *RNode = Result.getNode();
4427 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4428 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4429 break;
4430 }
4431 }
4432
4433 break;
4434 }
Bill Wendling74c37652008-12-09 22:08:41 +00004435 case ISD::UADDO:
4436 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00004437 MVT VT = Node->getValueType(0);
4438 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4439 default: assert(0 && "This action not supported for this op yet!");
4440 case TargetLowering::Custom:
4441 Result = TLI.LowerOperation(Op, DAG);
4442 if (Result.getNode()) break;
4443 // FALLTHROUGH
4444 case TargetLowering::Legal: {
4445 SDValue LHS = LegalizeOp(Node->getOperand(0));
4446 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004447
Bill Wendling74c37652008-12-09 22:08:41 +00004448 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00004449 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00004450 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004451 MVT OType = Node->getValueType(1);
Dale Johannesenca57b842009-02-02 23:46:53 +00004452 SDValue Cmp = DAG.getSetCC(dl, OType, Sum, LHS,
Bill Wendling74c37652008-12-09 22:08:41 +00004453 Node->getOpcode () == ISD::UADDO ?
4454 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004455
Bill Wendling41ea7e72008-11-24 19:21:46 +00004456 MVT ValueVTs[] = { LHS.getValueType(), OType };
4457 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004458
Dale Johannesenca57b842009-02-02 23:46:53 +00004459 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
4460 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00004461 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004462 SDNode *RNode = Result.getNode();
4463 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4464 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4465 break;
4466 }
4467 }
4468
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004469 break;
4470 }
Bill Wendling74c37652008-12-09 22:08:41 +00004471 case ISD::SMULO:
4472 case ISD::UMULO: {
4473 MVT VT = Node->getValueType(0);
4474 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4475 default: assert(0 && "This action is not supported at all!");
4476 case TargetLowering::Custom:
4477 Result = TLI.LowerOperation(Op, DAG);
4478 if (Result.getNode()) break;
4479 // Fall Thru
4480 case TargetLowering::Legal:
4481 // FIXME: According to Hacker's Delight, this can be implemented in
4482 // target independent lowering, but it would be inefficient, since it
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00004483 // requires a division + a branch.
Bill Wendling74c37652008-12-09 22:08:41 +00004484 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4485 break;
4486 }
4487 break;
4488 }
4489
Chris Lattner45b8caf2005-01-15 07:15:18 +00004490 }
Chris Lattner456a93a2006-01-28 07:39:30 +00004491
Chris Lattner4ddd2832006-04-08 04:13:17 +00004492 assert(Result.getValueType() == Op.getValueType() &&
4493 "Bad legalization!");
4494
Chris Lattner456a93a2006-01-28 07:39:30 +00004495 // Make sure that the generated code is itself legal.
4496 if (Result != Op)
4497 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004498
Chris Lattner45982da2005-05-12 16:53:42 +00004499 // Note that LegalizeOp may be reentered even from single-use nodes, which
4500 // means that we always must cache transformed nodes.
4501 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004502 return Result;
4503}
4504
Chris Lattner8b6fa222005-01-15 22:16:26 +00004505/// PromoteOp - Given an operation that produces a value in an invalid type,
4506/// promote it to compute the value into a larger type. The produced value will
4507/// have the correct bits for the low portion of the register, but no guarantee
4508/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman475871a2008-07-27 21:46:04 +00004509SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004510 MVT VT = Op.getValueType();
4511 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004512 assert(getTypeAction(VT) == Promote &&
4513 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004514 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner03c85462005-01-15 05:21:40 +00004515 "Cannot promote to smaller type!");
4516
Dan Gohman475871a2008-07-27 21:46:04 +00004517 SDValue Tmp1, Tmp2, Tmp3;
4518 SDValue Result;
Gabor Greifba36cb52008-08-28 21:40:38 +00004519 SDNode *Node = Op.getNode();
Dale Johannesen8a782a22009-02-02 22:12:50 +00004520 DebugLoc dl = Node->getDebugLoc();
Chris Lattner03c85462005-01-15 05:21:40 +00004521
Dan Gohman475871a2008-07-27 21:46:04 +00004522 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004523 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004524
Chris Lattner03c85462005-01-15 05:21:40 +00004525 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004526 case ISD::CopyFromReg:
4527 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004528 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004529#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004530 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004531#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004532 assert(0 && "Do not know how to promote this operator!");
4533 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004534 case ISD::UNDEF:
Dale Johannesen8a782a22009-02-02 22:12:50 +00004535 Result = DAG.getNode(ISD::UNDEF, dl, NVT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004536 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004537 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004538 if (VT != MVT::i1)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004539 Result = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, Op);
Chris Lattnerec176e32005-08-30 16:56:19 +00004540 else
Dale Johannesen8a782a22009-02-02 22:12:50 +00004541 Result = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004542 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4543 break;
4544 case ISD::ConstantFP:
Dale Johannesen8a782a22009-02-02 22:12:50 +00004545 Result = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004546 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4547 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004548
Duncan Sands5480c042009-01-01 15:52:00 +00004549 case ISD::SETCC: {
4550 MVT VT0 = Node->getOperand(0).getValueType();
4551 assert(isTypeLegal(TLI.getSetCCResultType(VT0))
Nate Begeman5922f562008-03-14 00:53:31 +00004552 && "SetCC type is not legal??");
Dale Johannesen8a782a22009-02-02 22:12:50 +00004553 Result = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(VT0),
Nate Begeman5922f562008-03-14 00:53:31 +00004554 Node->getOperand(0), Node->getOperand(1),
4555 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004556 break;
Duncan Sands5480c042009-01-01 15:52:00 +00004557 }
Chris Lattner03c85462005-01-15 05:21:40 +00004558 case ISD::TRUNCATE:
4559 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4560 case Legal:
4561 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004562 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner03c85462005-01-15 05:21:40 +00004563 "This truncation doesn't make sense!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004564 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Dale Johannesen8a782a22009-02-02 22:12:50 +00004565 Result = DAG.getNode(ISD::TRUNCATE, dl, NVT, Result);
Chris Lattner03c85462005-01-15 05:21:40 +00004566 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004567 case Promote:
4568 // The truncation is not required, because we don't guarantee anything
4569 // about high bits anyway.
4570 Result = PromoteOp(Node->getOperand(0));
4571 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004572 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004573 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4574 // Truncate the low part of the expanded value to the result type
Dale Johannesen8a782a22009-02-02 22:12:50 +00004575 Result = DAG.getNode(ISD::TRUNCATE, dl, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004576 }
4577 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004578 case ISD::SIGN_EXTEND:
4579 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004580 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004581 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4582 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4583 case Legal:
4584 // Input is legal? Just do extend all the way to the larger type.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004585 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004586 break;
4587 case Promote:
4588 // Promote the reg if it's smaller.
4589 Result = PromoteOp(Node->getOperand(0));
4590 // The high bits are not guaranteed to be anything. Insert an extend.
4591 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004592 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004593 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004594 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004595 Result = DAG.getZeroExtendInReg(Result, dl,
Chris Lattner23993562005-04-13 02:38:47 +00004596 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004597 break;
4598 }
4599 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00004600 case ISD::CONVERT_RNDSAT: {
4601 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4602 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4603 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4604 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4605 "can only promote integers");
Dale Johannesenc460ae92009-02-04 00:13:36 +00004606 Result = DAG.getConvertRndSat(NVT, dl, Node->getOperand(0),
Mon P Wang77cdf302008-11-10 20:54:11 +00004607 Node->getOperand(1), Node->getOperand(2),
4608 Node->getOperand(3), Node->getOperand(4),
4609 CvtCode);
4610 break;
4611
4612 }
Chris Lattner35481892005-12-23 00:16:34 +00004613 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004614 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
Dale Johannesen8a782a22009-02-02 22:12:50 +00004615 Node->getValueType(0), dl);
Chris Lattner35481892005-12-23 00:16:34 +00004616 Result = PromoteOp(Result);
4617 break;
4618
Chris Lattner8b6fa222005-01-15 22:16:26 +00004619 case ISD::FP_EXTEND:
4620 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4621 case ISD::FP_ROUND:
4622 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4623 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4624 case Promote: assert(0 && "Unreachable with 2 FP types!");
4625 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004626 if (Node->getConstantOperandVal(1) == 0) {
4627 // Input is legal? Do an FP_ROUND_INREG.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004628 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Node->getOperand(0),
Chris Lattner0bd48932008-01-17 07:00:52 +00004629 DAG.getValueType(VT));
4630 } else {
4631 // Just remove the truncate, it isn't affecting the value.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004632 Result = DAG.getNode(ISD::FP_ROUND, dl, NVT, Node->getOperand(0),
Chris Lattner0bd48932008-01-17 07:00:52 +00004633 Node->getOperand(1));
4634 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004635 break;
4636 }
4637 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004638 case ISD::SINT_TO_FP:
4639 case ISD::UINT_TO_FP:
4640 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4641 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004642 // No extra round required here.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004643 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004644 break;
4645
4646 case Promote:
4647 Result = PromoteOp(Node->getOperand(0));
4648 if (Node->getOpcode() == ISD::SINT_TO_FP)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004649 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004650 Result,
4651 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004652 else
Dale Johannesen8a782a22009-02-02 22:12:50 +00004653 Result = DAG.getZeroExtendInReg(Result, dl,
Chris Lattner23993562005-04-13 02:38:47 +00004654 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004655 // No extra round required here.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004656 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004657 break;
4658 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004659 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00004660 Node->getOperand(0), dl);
Chris Lattner77e77a62005-01-21 06:05:23 +00004661 // Round if we cannot tolerate excess precision.
4662 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004663 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004664 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004665 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004666 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004667 break;
4668
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004669 case ISD::SIGN_EXTEND_INREG:
4670 Result = PromoteOp(Node->getOperand(0));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004671 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Result,
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004672 Node->getOperand(1));
4673 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004674 case ISD::FP_TO_SINT:
4675 case ISD::FP_TO_UINT:
4676 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4677 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004678 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004679 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004680 break;
4681 case Promote:
4682 // The input result is prerounded, so we don't have to do anything
4683 // special.
4684 Tmp1 = PromoteOp(Node->getOperand(0));
4685 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004686 }
Nate Begemand2558e32005-08-14 01:20:53 +00004687 // If we're promoting a UINT to a larger size, check to see if the new node
4688 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4689 // we can use that instead. This allows us to generate better code for
4690 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4691 // legal, such as PowerPC.
4692 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004693 !TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NVT) &&
4694 (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT) ||
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004695 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Dale Johannesen8a782a22009-02-02 22:12:50 +00004696 Result = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Tmp1);
Nate Begemand2558e32005-08-14 01:20:53 +00004697 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00004698 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Nate Begemand2558e32005-08-14 01:20:53 +00004699 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004700 break;
4701
Chris Lattner2c8086f2005-04-02 05:00:07 +00004702 case ISD::FABS:
4703 case ISD::FNEG:
4704 Tmp1 = PromoteOp(Node->getOperand(0));
4705 assert(Tmp1.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004706 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Chris Lattner2c8086f2005-04-02 05:00:07 +00004707 // NOTE: we do not have to do any extra rounding here for
4708 // NoExcessFPPrecision, because we know the input will have the appropriate
4709 // precision, and these operations don't modify precision at all.
4710 break;
4711
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004712 case ISD::FLOG:
4713 case ISD::FLOG2:
4714 case ISD::FLOG10:
4715 case ISD::FEXP:
4716 case ISD::FEXP2:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004717 case ISD::FSQRT:
4718 case ISD::FSIN:
4719 case ISD::FCOS:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00004720 case ISD::FTRUNC:
4721 case ISD::FFLOOR:
4722 case ISD::FCEIL:
4723 case ISD::FRINT:
4724 case ISD::FNEARBYINT:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004725 Tmp1 = PromoteOp(Node->getOperand(0));
4726 assert(Tmp1.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004727 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004728 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004729 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004730 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004731 break;
4732
Evan Cheng9d24ac52008-09-09 23:35:53 +00004733 case ISD::FPOW:
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004734 case ISD::FPOWI: {
Evan Cheng9d24ac52008-09-09 23:35:53 +00004735 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004736 // directly as well, which may be better.
4737 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng9d24ac52008-09-09 23:35:53 +00004738 Tmp2 = Node->getOperand(1);
4739 if (Node->getOpcode() == ISD::FPOW)
4740 Tmp2 = PromoteOp(Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004741 assert(Tmp1.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004742 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004743 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004744 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004745 DAG.getValueType(VT));
4746 break;
4747 }
4748
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004749 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang28873102008-06-25 08:15:39 +00004750 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004751 Tmp2 = PromoteOp(Node->getOperand(2));
4752 Tmp3 = PromoteOp(Node->getOperand(3));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004753 Result = DAG.getAtomic(Node->getOpcode(), dl, AtomNode->getMemoryVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004754 AtomNode->getChain(),
Mon P Wang28873102008-06-25 08:15:39 +00004755 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004756 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004757 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004758 // Remember that we legalized the chain.
4759 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4760 break;
4761 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004762 case ISD::ATOMIC_LOAD_ADD:
4763 case ISD::ATOMIC_LOAD_SUB:
4764 case ISD::ATOMIC_LOAD_AND:
4765 case ISD::ATOMIC_LOAD_OR:
4766 case ISD::ATOMIC_LOAD_XOR:
4767 case ISD::ATOMIC_LOAD_NAND:
4768 case ISD::ATOMIC_LOAD_MIN:
4769 case ISD::ATOMIC_LOAD_MAX:
4770 case ISD::ATOMIC_LOAD_UMIN:
4771 case ISD::ATOMIC_LOAD_UMAX:
4772 case ISD::ATOMIC_SWAP: {
Mon P Wang28873102008-06-25 08:15:39 +00004773 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004774 Tmp2 = PromoteOp(Node->getOperand(2));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004775 Result = DAG.getAtomic(Node->getOpcode(), dl, AtomNode->getMemoryVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004776 AtomNode->getChain(),
Mon P Wang28873102008-06-25 08:15:39 +00004777 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004778 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004779 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004780 // Remember that we legalized the chain.
4781 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4782 break;
4783 }
4784
Chris Lattner03c85462005-01-15 05:21:40 +00004785 case ISD::AND:
4786 case ISD::OR:
4787 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004788 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004789 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004790 case ISD::MUL:
4791 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004792 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004793 // that too is okay if they are integer operations.
4794 Tmp1 = PromoteOp(Node->getOperand(0));
4795 Tmp2 = PromoteOp(Node->getOperand(1));
4796 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004797 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004798 break;
4799 case ISD::FADD:
4800 case ISD::FSUB:
4801 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004802 Tmp1 = PromoteOp(Node->getOperand(0));
4803 Tmp2 = PromoteOp(Node->getOperand(1));
4804 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004805 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004806
4807 // Floating point operations will give excess precision that we may not be
4808 // able to tolerate. If we DO allow excess precision, just leave it,
4809 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004810 // FIXME: Why would we need to round FP ops more than integer ones?
4811 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004812 if (NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004813 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004814 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004815 break;
4816
Chris Lattner8b6fa222005-01-15 22:16:26 +00004817 case ISD::SDIV:
4818 case ISD::SREM:
4819 // These operators require that their input be sign extended.
4820 Tmp1 = PromoteOp(Node->getOperand(0));
4821 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004822 if (NVT.isInteger()) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00004823 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
Chris Lattner15e4b012005-07-10 00:07:11 +00004824 DAG.getValueType(VT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004825 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp2,
Chris Lattner15e4b012005-07-10 00:07:11 +00004826 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004827 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00004828 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004829
4830 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004831 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004832 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004833 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004834 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004835 case ISD::FDIV:
4836 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004837 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004838 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004839 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004840 case Expand: assert(0 && "not implemented");
4841 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4842 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004843 }
4844 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004845 case Expand: assert(0 && "not implemented");
4846 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4847 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004848 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00004849 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004850
4851 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004852 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004853 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Chris Lattner01b3d732005-09-28 22:28:18 +00004854 DAG.getValueType(VT));
4855 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004856
4857 case ISD::UDIV:
4858 case ISD::UREM:
4859 // These operators require that their input be zero extended.
4860 Tmp1 = PromoteOp(Node->getOperand(0));
4861 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004862 assert(NVT.isInteger() && "Operators don't apply to FP!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00004863 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
4864 Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, VT);
4865 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004866 break;
4867
4868 case ISD::SHL:
4869 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004870 Result = DAG.getNode(ISD::SHL, dl, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004871 break;
4872 case ISD::SRA:
4873 // The input value must be properly sign extended.
4874 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004875 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
Chris Lattner15e4b012005-07-10 00:07:11 +00004876 DAG.getValueType(VT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004877 Result = DAG.getNode(ISD::SRA, dl, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004878 break;
4879 case ISD::SRL:
4880 // The input value must be properly zero extended.
4881 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesen8a782a22009-02-02 22:12:50 +00004882 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
4883 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004884 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004885
4886 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004887 Tmp1 = Node->getOperand(0); // Get the chain.
4888 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004889 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
Dale Johannesenc460ae92009-02-04 00:13:36 +00004890 Tmp3 = DAG.getVAArg(VT, dl, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands126d9072008-07-04 11:47:58 +00004891 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman0aed7842006-01-28 03:14:31 +00004892 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004893 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenc460ae92009-02-04 00:13:36 +00004894 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004895 // Increment the pointer, VAList, to the next vaarg
Dale Johannesen8a782a22009-02-02 22:12:50 +00004896 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004897 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman0aed7842006-01-28 03:14:31 +00004898 TLI.getPointerTy()));
4899 // Store the incremented VAList to the legalized pointer
Dale Johannesen8a782a22009-02-02 22:12:50 +00004900 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004901 // Load the actual argument out of the pointer VAList
Dale Johannesen8a782a22009-02-02 22:12:50 +00004902 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004903 }
4904 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004905 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004906 break;
4907
Evan Cheng466685d2006-10-09 20:57:25 +00004908 case ISD::LOAD: {
4909 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004910 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4911 ? ISD::EXTLOAD : LD->getExtensionType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00004912 Result = DAG.getExtLoad(ExtType, dl, NVT,
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004913 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004914 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004915 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004916 LD->isVolatile(),
4917 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004918 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004919 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004920 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004921 }
Scott Michel8bf61e82008-06-02 22:18:03 +00004922 case ISD::SELECT: {
Chris Lattner03c85462005-01-15 05:21:40 +00004923 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4924 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel8bf61e82008-06-02 22:18:03 +00004925
Duncan Sands83ec4b62008-06-06 12:08:01 +00004926 MVT VT2 = Tmp2.getValueType();
Scott Michel8bf61e82008-06-02 22:18:03 +00004927 assert(VT2 == Tmp3.getValueType()
Scott Michelba12f572008-06-03 19:13:20 +00004928 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4929 // Ensure that the resulting node is at least the same size as the operands'
4930 // value types, because we cannot assume that TLI.getSetCCValueType() is
4931 // constant.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004932 Result = DAG.getNode(ISD::SELECT, dl, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004933 break;
Scott Michel8bf61e82008-06-02 22:18:03 +00004934 }
Nate Begeman9373a812005-08-10 20:51:12 +00004935 case ISD::SELECT_CC:
4936 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4937 Tmp3 = PromoteOp(Node->getOperand(3)); // False
Dale Johannesen8a782a22009-02-02 22:12:50 +00004938 Result = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004939 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004940 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004941 case ISD::BSWAP:
4942 Tmp1 = Node->getOperand(0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004943 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
4944 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
4945 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004946 DAG.getConstant(NVT.getSizeInBits() -
4947 VT.getSizeInBits(),
Nate Begemand88fc032006-01-14 03:14:10 +00004948 TLI.getShiftAmountTy()));
4949 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004950 case ISD::CTPOP:
4951 case ISD::CTTZ:
4952 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004953 // Zero extend the argument
Dale Johannesen8a782a22009-02-02 22:12:50 +00004954 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004955 // Perform the larger operation, then subtract if needed.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004956 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004957 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004958 case ISD::CTPOP:
4959 Result = Tmp1;
4960 break;
4961 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004962 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Dale Johannesen8a782a22009-02-02 22:12:50 +00004963 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004964 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanb55757e2007-05-18 17:52:13 +00004965 ISD::SETEQ);
Dale Johannesen8a782a22009-02-02 22:12:50 +00004966 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004967 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004968 break;
4969 case ISD::CTLZ:
4970 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00004971 Result = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004972 DAG.getConstant(NVT.getSizeInBits() -
4973 VT.getSizeInBits(), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004974 break;
4975 }
4976 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004977 case ISD::EXTRACT_SUBVECTOR:
4978 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004979 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004980 case ISD::EXTRACT_VECTOR_ELT:
4981 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4982 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004983 }
4984
Gabor Greifba36cb52008-08-28 21:40:38 +00004985 assert(Result.getNode() && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004986
4987 // Make sure the result is itself legal.
4988 Result = LegalizeOp(Result);
4989
4990 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004991 AddPromotedOperand(Op, Result);
4992 return Result;
4993}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004994
Dan Gohman7f321562007-06-25 16:23:39 +00004995/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4996/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4997/// based on the vector type. The return type of this matches the element type
4998/// of the vector, which may not be legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00004999SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner15972212006-03-31 17:55:51 +00005000 // We know that operand #0 is the Vec vector. If the index is a constant
5001 // or if the invec is a supported hardware type, we can use it. Otherwise,
5002 // lower to a store then an indexed load.
Dan Gohman475871a2008-07-27 21:46:04 +00005003 SDValue Vec = Op.getOperand(0);
5004 SDValue Idx = Op.getOperand(1);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005005 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattner15972212006-03-31 17:55:51 +00005006
Duncan Sands83ec4b62008-06-06 12:08:01 +00005007 MVT TVT = Vec.getValueType();
5008 unsigned NumElems = TVT.getVectorNumElements();
Chris Lattner15972212006-03-31 17:55:51 +00005009
Dan Gohman7f321562007-06-25 16:23:39 +00005010 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
5011 default: assert(0 && "This action is not supported yet!");
5012 case TargetLowering::Custom: {
5013 Vec = LegalizeOp(Vec);
5014 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00005015 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005016 if (Tmp3.getNode())
Dan Gohman7f321562007-06-25 16:23:39 +00005017 return Tmp3;
5018 break;
5019 }
5020 case TargetLowering::Legal:
5021 if (isTypeLegal(TVT)) {
5022 Vec = LegalizeOp(Vec);
5023 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00005024 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00005025 }
5026 break;
Mon P Wang0c397192008-10-30 08:01:45 +00005027 case TargetLowering::Promote:
5028 assert(TVT.isVector() && "not vector type");
5029 // fall thru to expand since vectors are by default are promote
Dan Gohman7f321562007-06-25 16:23:39 +00005030 case TargetLowering::Expand:
5031 break;
5032 }
5033
5034 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00005035 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00005036 Op = ScalarizeVectorOp(Vec);
5037 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00005038 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00005039 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00005040 SDValue Lo, Hi;
Chris Lattner15972212006-03-31 17:55:51 +00005041 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005042 if (CIdx->getZExtValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00005043 Vec = Lo;
5044 } else {
5045 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005046 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00005047 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00005048 }
Dan Gohman7f321562007-06-25 16:23:39 +00005049
Chris Lattner15972212006-03-31 17:55:51 +00005050 // It's now an extract from the appropriate high or low part. Recurse.
5051 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00005052 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00005053 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00005054 // Store the value to a temporary stack slot, then LOAD the scalar
5055 // element back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005056 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dale Johannesenbb5da912009-02-02 20:41:04 +00005057 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0);
Dan Gohman7f321562007-06-25 16:23:39 +00005058
5059 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005060 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +00005061 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
Dan Gohman7f321562007-06-25 16:23:39 +00005062 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005063
Duncan Sands8e4eb092008-06-08 20:54:56 +00005064 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Dale Johannesenbb5da912009-02-02 20:41:04 +00005065 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005066 else
Dale Johannesenbb5da912009-02-02 20:41:04 +00005067 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005068
Dale Johannesenbb5da912009-02-02 20:41:04 +00005069 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
Dan Gohman7f321562007-06-25 16:23:39 +00005070
Dale Johannesenbb5da912009-02-02 20:41:04 +00005071 Op = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00005072 }
Dan Gohman7f321562007-06-25 16:23:39 +00005073 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00005074}
5075
Dan Gohman7f321562007-06-25 16:23:39 +00005076/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00005077/// we assume the operation can be split if it is not already legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005078SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman65956352007-06-13 15:12:02 +00005079 // We know that operand #0 is the Vec vector. For now we assume the index
5080 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman475871a2008-07-27 21:46:04 +00005081 SDValue Vec = Op.getOperand(0);
5082 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00005083
Duncan Sands83ec4b62008-06-06 12:08:01 +00005084 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohman65956352007-06-13 15:12:02 +00005085
Duncan Sands83ec4b62008-06-06 12:08:01 +00005086 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman65956352007-06-13 15:12:02 +00005087 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00005088 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00005089 }
5090
5091 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00005092 SDValue Lo, Hi;
Dan Gohman65956352007-06-13 15:12:02 +00005093 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005094 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohman65956352007-06-13 15:12:02 +00005095 Vec = Lo;
5096 } else {
5097 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005098 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
5099 Idx.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00005100 }
5101
5102 // It's now an extract from the appropriate high or low part. Recurse.
5103 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00005104 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00005105}
5106
Nate Begeman750ac1b2006-02-01 07:19:44 +00005107/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
5108/// with condition CC on the current target. This usually involves legalizing
5109/// or promoting the arguments. In the case where LHS and RHS must be expanded,
5110/// there may be no choice but to create a new SetCC node to represent the
5111/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00005112/// LHS, and the SDValue returned in RHS has a nil SDNode value.
5113void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
5114 SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00005115 SDValue &CC,
5116 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00005117 SDValue Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005118
5119 switch (getTypeAction(LHS.getValueType())) {
5120 case Legal:
5121 Tmp1 = LegalizeOp(LHS); // LHS
5122 Tmp2 = LegalizeOp(RHS); // RHS
5123 break;
5124 case Promote:
5125 Tmp1 = PromoteOp(LHS); // LHS
5126 Tmp2 = PromoteOp(RHS); // RHS
5127
5128 // If this is an FP compare, the operands have already been extended.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005129 if (LHS.getValueType().isInteger()) {
5130 MVT VT = LHS.getValueType();
5131 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00005132
5133 // Otherwise, we have to insert explicit sign or zero extends. Note
5134 // that we could insert sign extends for ALL conditions, but zero extend
5135 // is cheaper on many machines (an AND instead of two shifts), so prefer
5136 // it.
5137 switch (cast<CondCodeSDNode>(CC)->get()) {
5138 default: assert(0 && "Unknown integer comparison!");
5139 case ISD::SETEQ:
5140 case ISD::SETNE:
5141 case ISD::SETUGE:
5142 case ISD::SETUGT:
5143 case ISD::SETULE:
5144 case ISD::SETULT:
5145 // ALL of these operations will work if we either sign or zero extend
5146 // the operands (including the unsigned comparisons!). Zero extend is
5147 // usually a simpler/cheaper operation, so prefer it.
Dale Johannesenbb5da912009-02-02 20:41:04 +00005148 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
5149 Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00005150 break;
5151 case ISD::SETGE:
5152 case ISD::SETGT:
5153 case ISD::SETLT:
5154 case ISD::SETLE:
Dale Johannesenbb5da912009-02-02 20:41:04 +00005155 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
Nate Begeman750ac1b2006-02-01 07:19:44 +00005156 DAG.getValueType(VT));
Dale Johannesenbb5da912009-02-02 20:41:04 +00005157 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp2,
Nate Begeman750ac1b2006-02-01 07:19:44 +00005158 DAG.getValueType(VT));
Evan Chengefa53392008-10-13 18:46:18 +00005159 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5160 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Nate Begeman750ac1b2006-02-01 07:19:44 +00005161 break;
5162 }
5163 }
5164 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005165 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005166 MVT VT = LHS.getValueType();
Evan Cheng2b49c502006-12-15 02:59:56 +00005167 if (VT == MVT::f32 || VT == MVT::f64) {
5168 // Expand into one or more soft-fp libcall(s).
Evan Cheng6518c6e2008-07-01 21:35:46 +00005169 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00005170 switch (cast<CondCodeSDNode>(CC)->get()) {
5171 case ISD::SETEQ:
5172 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00005173 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005174 break;
5175 case ISD::SETNE:
5176 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00005177 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005178 break;
5179 case ISD::SETGE:
5180 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00005181 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005182 break;
5183 case ISD::SETLT:
5184 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00005185 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005186 break;
5187 case ISD::SETLE:
5188 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00005189 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005190 break;
5191 case ISD::SETGT:
5192 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00005193 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005194 break;
5195 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00005196 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5197 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005198 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00005199 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005200 break;
5201 default:
Evan Cheng56966222007-01-12 02:11:51 +00005202 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005203 switch (cast<CondCodeSDNode>(CC)->get()) {
5204 case ISD::SETONE:
5205 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00005206 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005207 // Fallthrough
5208 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00005209 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005210 break;
5211 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00005212 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005213 break;
5214 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00005215 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005216 break;
5217 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00005218 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005219 break;
Evan Cheng56966222007-01-12 02:11:51 +00005220 case ISD::SETUEQ:
5221 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00005222 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005223 default: assert(0 && "Unsupported FP setcc!");
5224 }
5225 }
Duncan Sandsf9516202008-06-30 10:19:09 +00005226
Dan Gohman475871a2008-07-27 21:46:04 +00005227 SDValue Dummy;
5228 SDValue Ops[2] = { LHS, RHS };
Dale Johannesenbb5da912009-02-02 20:41:04 +00005229 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2, dl).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005230 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00005231 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00005232 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00005233 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Dale Johannesenbb5da912009-02-02 20:41:04 +00005234 Tmp1 = DAG.getNode(ISD::SETCC, dl,
Duncan Sands5480c042009-01-01 15:52:00 +00005235 TLI.getSetCCResultType(Tmp1.getValueType()),
5236 Tmp1, Tmp2, CC);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005237 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2, dl).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005238 false /*sign irrelevant*/, Dummy);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005239 Tmp2 = DAG.getNode(ISD::SETCC, dl,
Duncan Sands5480c042009-01-01 15:52:00 +00005240 TLI.getSetCCResultType(LHS.getValueType()), LHS,
5241 Tmp2, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Dale Johannesenbb5da912009-02-02 20:41:04 +00005242 Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman475871a2008-07-27 21:46:04 +00005243 Tmp2 = SDValue();
Evan Cheng2b49c502006-12-15 02:59:56 +00005244 }
Evan Cheng21cacc42008-07-07 07:18:09 +00005245 LHS = LegalizeOp(Tmp1);
Evan Cheng2b49c502006-12-15 02:59:56 +00005246 RHS = Tmp2;
5247 return;
5248 }
5249
Dan Gohman475871a2008-07-27 21:46:04 +00005250 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005251 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005252 ExpandOp(RHS, RHSLo, RHSHi);
5253 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5254
5255 if (VT==MVT::ppcf128) {
5256 // FIXME: This generated code sucks. We want to generate
Dale Johannesene2f20832008-09-12 00:30:56 +00005257 // FCMPU crN, hi1, hi2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005258 // BNE crN, L:
Dale Johannesene2f20832008-09-12 00:30:56 +00005259 // FCMPU crN, lo1, lo2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005260 // The following can be improved, but not that much.
Dale Johannesenbb5da912009-02-02 20:41:04 +00005261 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005262 LHSHi, RHSHi, ISD::SETOEQ);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005263 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005264 LHSLo, RHSLo, CCCode);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005265 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
5266 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005267 LHSHi, RHSHi, ISD::SETUNE);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005268 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005269 LHSHi, RHSHi, CCCode);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005270 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
5271 Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00005272 Tmp2 = SDValue();
Dale Johannesen638ccd52007-10-06 01:24:11 +00005273 break;
5274 }
5275
5276 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005277 case ISD::SETEQ:
5278 case ISD::SETNE:
5279 if (RHSLo == RHSHi)
5280 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5281 if (RHSCST->isAllOnesValue()) {
5282 // Comparison to -1.
Dale Johannesenbb5da912009-02-02 20:41:04 +00005283 Tmp1 = DAG.getNode(ISD::AND, dl,LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00005284 Tmp2 = RHSLo;
5285 break;
5286 }
5287
Dale Johannesenbb5da912009-02-02 20:41:04 +00005288 Tmp1 = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
5289 Tmp2 = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
5290 Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00005291 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5292 break;
5293 default:
5294 // If this is a comparison of the sign bit, just look at the top part.
5295 // X > -1, x < 0
5296 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5297 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005298 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00005299 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5300 CST->isAllOnesValue())) { // X > -1
5301 Tmp1 = LHSHi;
5302 Tmp2 = RHSHi;
5303 break;
5304 }
5305
5306 // FIXME: This generated code sucks.
5307 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00005308 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005309 default: assert(0 && "Unknown integer setcc!");
5310 case ISD::SETLT:
5311 case ISD::SETULT: LowCC = ISD::SETULT; break;
5312 case ISD::SETGT:
5313 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5314 case ISD::SETLE:
5315 case ISD::SETULE: LowCC = ISD::SETULE; break;
5316 case ISD::SETGE:
5317 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5318 }
5319
5320 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5321 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5322 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5323
5324 // NOTE: on targets without efficient SELECT of bools, we can always use
5325 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00005326 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Duncan Sands5480c042009-01-01 15:52:00 +00005327 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00005328 LHSLo, RHSLo, LowCC, false, DagCombineInfo, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00005329 if (!Tmp1.getNode())
Dale Johannesenbb5da912009-02-02 20:41:04 +00005330 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005331 LHSLo, RHSLo, LowCC);
5332 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00005333 LHSHi, RHSHi, CCCode, false, DagCombineInfo, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00005334 if (!Tmp2.getNode())
Dale Johannesenbb5da912009-02-02 20:41:04 +00005335 Tmp2 = DAG.getNode(ISD::SETCC, dl,
Duncan Sands5480c042009-01-01 15:52:00 +00005336 TLI.getSetCCResultType(LHSHi.getValueType()),
5337 LHSHi, RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00005338
Gabor Greifba36cb52008-08-28 21:40:38 +00005339 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5340 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman002e5d02008-03-13 22:13:53 +00005341 if ((Tmp1C && Tmp1C->isNullValue()) ||
5342 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00005343 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5344 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00005345 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00005346 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5347 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5348 // low part is known false, returns high part.
5349 // For LE / GE, if high part is known false, ignore the low part.
5350 // For LT / GT, if high part is known true, ignore the low part.
5351 Tmp1 = Tmp2;
Dan Gohman475871a2008-07-27 21:46:04 +00005352 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005353 } else {
Duncan Sands5480c042009-01-01 15:52:00 +00005354 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5355 LHSHi, RHSHi, ISD::SETEQ, false,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00005356 DagCombineInfo, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00005357 if (!Result.getNode())
Dale Johannesenbb5da912009-02-02 20:41:04 +00005358 Result=DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00005359 LHSHi, RHSHi, ISD::SETEQ);
Dale Johannesenbb5da912009-02-02 20:41:04 +00005360 Result = LegalizeOp(DAG.getNode(ISD::SELECT, dl, Tmp1.getValueType(),
Evan Cheng2e677812007-02-08 22:16:19 +00005361 Result, Tmp1, Tmp2));
5362 Tmp1 = Result;
Dan Gohman475871a2008-07-27 21:46:04 +00005363 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005364 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005365 }
5366 }
Evan Cheng2b49c502006-12-15 02:59:56 +00005367 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005368 LHS = Tmp1;
5369 RHS = Tmp2;
5370}
5371
Evan Cheng7f042682008-10-15 02:05:31 +00005372/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5373/// condition code CC on the current target. This routine assumes LHS and rHS
5374/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5375/// illegal condition code into AND / OR of multiple SETCC values.
5376void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5377 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00005378 SDValue &CC,
5379 DebugLoc dl) {
Evan Cheng7f042682008-10-15 02:05:31 +00005380 MVT OpVT = LHS.getValueType();
5381 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5382 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5383 default: assert(0 && "Unknown condition code action!");
5384 case TargetLowering::Legal:
5385 // Nothing to do.
5386 break;
5387 case TargetLowering::Expand: {
5388 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5389 unsigned Opc = 0;
5390 switch (CCCode) {
5391 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00005392 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5393 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5394 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5395 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5396 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5397 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5398 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5399 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5400 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5401 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5402 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5403 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00005404 // FIXME: Implement more expansions.
5405 }
5406
Dale Johannesenbb5da912009-02-02 20:41:04 +00005407 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
5408 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
5409 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00005410 RHS = SDValue();
5411 CC = SDValue();
5412 break;
5413 }
5414 }
5415}
5416
Chris Lattner1401d152008-01-16 07:45:30 +00005417/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5418/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5419/// a load from the stack slot to DestVT, extending it if needed.
5420/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005421SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5422 MVT SlotVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005423 MVT DestVT,
5424 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00005425 // Create the stack frame object.
Mon P Wang364d73d2008-07-05 20:40:31 +00005426 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5427 SrcOp.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00005428 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang364d73d2008-07-05 20:40:31 +00005429
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005430 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005431 int SPFI = StackPtrFI->getIndex();
Dan Gohman15b38302009-01-29 21:02:43 +00005432 const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
5433
Duncan Sands83ec4b62008-06-06 12:08:01 +00005434 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5435 unsigned SlotSize = SlotVT.getSizeInBits();
5436 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang364d73d2008-07-05 20:40:31 +00005437 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5438 DestVT.getTypeForMVT());
Chris Lattner35481892005-12-23 00:16:34 +00005439
Chris Lattner1401d152008-01-16 07:45:30 +00005440 // Emit a store to the stack slot. Use a truncstore if the input value is
5441 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00005442 SDValue Store;
Mon P Wang364d73d2008-07-05 20:40:31 +00005443
Chris Lattner1401d152008-01-16 07:45:30 +00005444 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00005445 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00005446 SV, 0, SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005447 else {
5448 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00005449 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00005450 SV, 0, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005451 }
5452
Chris Lattner35481892005-12-23 00:16:34 +00005453 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00005454 if (SlotSize == DestSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00005455 return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, DestAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005456
5457 assert(SlotSize < DestSize && "Unknown extension!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00005458 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT,
Mon P Wang364d73d2008-07-05 20:40:31 +00005459 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00005460}
5461
Dan Gohman475871a2008-07-27 21:46:04 +00005462SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005463 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00005464 // Create a vector sized/aligned stack slot, store the value to element #0,
5465 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005466 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00005467
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005468 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005469 int SPFI = StackPtrFI->getIndex();
5470
Dale Johannesen8a782a22009-02-02 22:12:50 +00005471 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(0),
5472 StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005473 PseudoSourceValue::getFixedStack(SPFI), 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00005474 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005475 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00005476}
5477
5478
Chris Lattnerce872152006-03-19 06:31:19 +00005479/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00005480/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00005481SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Chris Lattnerce872152006-03-19 06:31:19 +00005482
5483 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00005484 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00005485 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00005486 bool isOnlyLowElement = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005487 SDValue SplatValue = Node->getOperand(0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00005488 DebugLoc dl = Node->getDebugLoc();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005489
Dan Gohman475871a2008-07-27 21:46:04 +00005490 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005491 // and use a bitmask instead of a list of elements.
Dan Gohman475871a2008-07-27 21:46:04 +00005492 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00005493 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005494 bool isConstant = true;
5495 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5496 SplatValue.getOpcode() != ISD::UNDEF)
5497 isConstant = false;
5498
Evan Cheng033e6812006-03-24 01:17:21 +00005499 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005500 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00005501 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00005502 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00005503 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00005504 if (SplatValue != V)
Dan Gohman475871a2008-07-27 21:46:04 +00005505 SplatValue = SDValue(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005506
5507 // If this isn't a constant element or an undef, we can't use a constant
5508 // pool load.
5509 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5510 V.getOpcode() != ISD::UNDEF)
5511 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00005512 }
5513
5514 if (isOnlyLowElement) {
5515 // If the low element is an undef too, then this whole things is an undef.
5516 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
Dale Johannesen8a782a22009-02-02 22:12:50 +00005517 return DAG.getNode(ISD::UNDEF, dl, Node->getValueType(0));
Chris Lattnerce872152006-03-19 06:31:19 +00005518 // Otherwise, turn this into a scalar_to_vector node.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005519 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, Node->getValueType(0),
Chris Lattnerce872152006-03-19 06:31:19 +00005520 Node->getOperand(0));
5521 }
5522
Chris Lattner2eb86532006-03-24 07:29:17 +00005523 // If all elements are constants, create a load from the constant pool.
5524 if (isConstant) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005525 MVT VT = Node->getValueType(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005526 std::vector<Constant*> CV;
5527 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5528 if (ConstantFPSDNode *V =
5529 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005530 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005531 } else if (ConstantSDNode *V =
Chris Lattner02a260a2008-04-20 00:41:09 +00005532 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005533 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005534 } else {
5535 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner02a260a2008-04-20 00:41:09 +00005536 const Type *OpNTy =
Duncan Sands83ec4b62008-06-06 12:08:01 +00005537 Node->getOperand(0).getValueType().getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00005538 CV.push_back(UndefValue::get(OpNTy));
5539 }
5540 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00005541 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005542 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005543 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00005544 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005545 PseudoSourceValue::getConstantPool(), 0,
5546 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00005547 }
5548
Gabor Greifba36cb52008-08-28 21:40:38 +00005549 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00005550 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005551 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman475871a2008-07-27 21:46:04 +00005552 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5553 std::vector<SDValue> ZeroVec(NumElems, Zero);
Dale Johannesen8a782a22009-02-02 22:12:50 +00005554 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005555 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00005556
5557 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005558 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00005559 // Get the splatted value into the low element of a vector register.
Dan Gohman475871a2008-07-27 21:46:04 +00005560 SDValue LowValVec =
Dale Johannesen8a782a22009-02-02 22:12:50 +00005561 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5562 Node->getValueType(0), SplatValue);
Chris Lattner87100e02006-03-20 01:52:29 +00005563
5564 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Dale Johannesen8a782a22009-02-02 22:12:50 +00005565 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl,
5566 Node->getValueType(0), LowValVec,
Chris Lattner87100e02006-03-20 01:52:29 +00005567 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5568 SplatMask);
5569 }
5570 }
5571
Evan Cheng033e6812006-03-24 01:17:21 +00005572 // If there are only two unique elements, we may be able to turn this into a
5573 // vector shuffle.
5574 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005575 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00005576 SDValue Val1 = Node->getOperand(1);
5577 SDValue Val2;
5578 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005579 if (MI->first != Val1)
5580 Val2 = MI->first;
5581 else
5582 Val2 = (++MI)->first;
5583
5584 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5585 // vector shuffle has the undef vector on the RHS.
5586 if (Val1.getOpcode() == ISD::UNDEF)
5587 std::swap(Val1, Val2);
5588
Evan Cheng033e6812006-03-24 01:17:21 +00005589 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005590 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5591 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005592 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005593
5594 // Set elements of the shuffle mask for Val1.
5595 std::vector<unsigned> &Val1Elts = Values[Val1];
5596 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5597 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5598
5599 // Set elements of the shuffle mask for Val2.
5600 std::vector<unsigned> &Val2Elts = Values[Val2];
5601 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5602 if (Val2.getOpcode() != ISD::UNDEF)
5603 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5604 else
Dale Johannesen8a782a22009-02-02 22:12:50 +00005605 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, dl, MaskEltVT);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005606
Dale Johannesen8a782a22009-02-02 22:12:50 +00005607 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005608 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005609
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005610 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005611 if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR,
5612 Node->getValueType(0)) &&
Chris Lattner4352cc92006-04-04 17:23:26 +00005613 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005614 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,Node->getValueType(0), Val1);
5615 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,Node->getValueType(0), Val2);
Dan Gohman475871a2008-07-27 21:46:04 +00005616 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng033e6812006-03-24 01:17:21 +00005617
5618 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Dale Johannesen8a782a22009-02-02 22:12:50 +00005619 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl,Node->getValueType(0), Ops, 3);
Evan Cheng033e6812006-03-24 01:17:21 +00005620 }
5621 }
Chris Lattnerce872152006-03-19 06:31:19 +00005622
5623 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5624 // aligned object on the stack, store each element into it, then load
5625 // the result as a vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005626 MVT VT = Node->getValueType(0);
Chris Lattnerce872152006-03-19 06:31:19 +00005627 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00005628 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohman15b38302009-01-29 21:02:43 +00005629 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
5630 const Value *SV = PseudoSourceValue::getFixedStack(FI);
5631
Chris Lattnerce872152006-03-19 06:31:19 +00005632 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00005633 SmallVector<SDValue, 8> Stores;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005634 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005635 // Store (in the right endianness) the elements to memory.
5636 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5637 // Ignore undef elements.
5638 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5639
Chris Lattner841c8822006-03-22 01:46:54 +00005640 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005641
Dan Gohman475871a2008-07-27 21:46:04 +00005642 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00005643 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
Chris Lattnerce872152006-03-19 06:31:19 +00005644
Dale Johannesen8a782a22009-02-02 22:12:50 +00005645 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
5646 Idx, SV, Offset));
Chris Lattnerce872152006-03-19 06:31:19 +00005647 }
5648
Dan Gohman475871a2008-07-27 21:46:04 +00005649 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00005650 if (!Stores.empty()) // Not all undef elements?
Dale Johannesen8a782a22009-02-02 22:12:50 +00005651 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005652 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005653 else
5654 StoreChain = DAG.getEntryNode();
5655
5656 // Result is a load from the stack slot.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005657 return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005658}
5659
Chris Lattner5b359c62005-04-02 04:00:59 +00005660void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman475871a2008-07-27 21:46:04 +00005661 SDValue Op, SDValue Amt,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005662 SDValue &Lo, SDValue &Hi,
5663 DebugLoc dl) {
Chris Lattner5b359c62005-04-02 04:00:59 +00005664 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00005665 SDValue LHSL, LHSH;
Chris Lattner5b359c62005-04-02 04:00:59 +00005666 ExpandOp(Op, LHSL, LHSH);
5667
Dan Gohman475871a2008-07-27 21:46:04 +00005668 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005669 MVT VT = LHSL.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00005670 Lo = DAG.getNode(NodeOp, dl, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005671 Hi = Lo.getValue(1);
5672}
5673
5674
Chris Lattnere34b3962005-01-19 04:19:40 +00005675/// ExpandShift - Try to find a clever way to expand this shift operation out to
5676/// smaller elements. If we can't find a way that is more efficient than a
5677/// libcall on this target, return false. Otherwise, return true with the
5678/// low-parts expanded into Lo and Hi.
Dan Gohman475871a2008-07-27 21:46:04 +00005679bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
Dale Johannesen8a782a22009-02-02 22:12:50 +00005680 SDValue &Lo, SDValue &Hi,
5681 DebugLoc dl) {
Chris Lattnere34b3962005-01-19 04:19:40 +00005682 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5683 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005684
Duncan Sands83ec4b62008-06-06 12:08:01 +00005685 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman475871a2008-07-27 21:46:04 +00005686 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005687 MVT ShTy = ShAmt.getValueType();
5688 unsigned ShBits = ShTy.getSizeInBits();
5689 unsigned VTBits = Op.getValueType().getSizeInBits();
5690 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005691
Chris Lattner7a3c8552007-10-14 20:35:12 +00005692 // Handle the case when Amt is an immediate.
Gabor Greifba36cb52008-08-28 21:40:38 +00005693 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005694 unsigned Cst = CN->getZExtValue();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005695 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005696 SDValue InL, InH;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005697 ExpandOp(Op, InL, InH);
5698 switch(Opc) {
5699 case ISD::SHL:
5700 if (Cst > VTBits) {
5701 Lo = DAG.getConstant(0, NVT);
5702 Hi = DAG.getConstant(0, NVT);
5703 } else if (Cst > NVTBits) {
5704 Lo = DAG.getConstant(0, NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00005705 Hi = DAG.getNode(ISD::SHL, dl,
5706 NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005707 } else if (Cst == NVTBits) {
5708 Lo = DAG.getConstant(0, NVT);
5709 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005710 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005711 Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, DAG.getConstant(Cst, ShTy));
5712 Hi = DAG.getNode(ISD::OR, dl, NVT,
5713 DAG.getNode(ISD::SHL, dl, NVT, InH, DAG.getConstant(Cst, ShTy)),
5714 DAG.getNode(ISD::SRL, dl, NVT, InL,
5715 DAG.getConstant(NVTBits-Cst, ShTy)));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005716 }
5717 return true;
5718 case ISD::SRL:
5719 if (Cst > VTBits) {
5720 Lo = DAG.getConstant(0, NVT);
5721 Hi = DAG.getConstant(0, NVT);
5722 } else if (Cst > NVTBits) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005723 Lo = DAG.getNode(ISD::SRL, dl, NVT,
5724 InH, DAG.getConstant(Cst-NVTBits,ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005725 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005726 } else if (Cst == NVTBits) {
5727 Lo = InH;
5728 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005729 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005730 Lo = DAG.getNode(ISD::OR, dl, NVT,
5731 DAG.getNode(ISD::SRL, dl, NVT, InL, DAG.getConstant(Cst, ShTy)),
5732 DAG.getNode(ISD::SHL, dl, NVT, InH,
5733 DAG.getConstant(NVTBits-Cst, ShTy)));
5734 Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, DAG.getConstant(Cst, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005735 }
5736 return true;
5737 case ISD::SRA:
5738 if (Cst > VTBits) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005739 Hi = Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005740 DAG.getConstant(NVTBits-1, ShTy));
5741 } else if (Cst > NVTBits) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005742 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005743 DAG.getConstant(Cst-NVTBits, ShTy));
Dale Johannesen8a782a22009-02-02 22:12:50 +00005744 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005745 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005746 } else if (Cst == NVTBits) {
5747 Lo = InH;
Dale Johannesen8a782a22009-02-02 22:12:50 +00005748 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005749 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005750 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00005751 Lo = DAG.getNode(ISD::OR, dl, NVT,
5752 DAG.getNode(ISD::SRL, dl, NVT, InL, DAG.getConstant(Cst, ShTy)),
5753 DAG.getNode(ISD::SHL, dl,
5754 NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5755 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, DAG.getConstant(Cst, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005756 }
5757 return true;
5758 }
5759 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005760
5761 // Okay, the shift amount isn't constant. However, if we can tell that it is
5762 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005763 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5764 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005765 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005766
Dan Gohman9e255b72008-02-22 01:12:31 +00005767 // If we know that if any of the high bits of the shift amount are one, then
5768 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005769 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005770 // Mask out the high bit, which we know is set.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005771 Amt = DAG.getNode(ISD::AND, dl, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005772 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005773
5774 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005775 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005776 ExpandOp(Op, InL, InH);
5777 switch(Opc) {
5778 case ISD::SHL:
5779 Lo = DAG.getConstant(0, NVT); // Low part is zero.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005780 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005781 return true;
5782 case ISD::SRL:
5783 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005784 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005785 return true;
5786 case ISD::SRA:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005787 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005788 DAG.getConstant(NVTBits-1, Amt.getValueType()));
Dale Johannesen8a782a22009-02-02 22:12:50 +00005789 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005790 return true;
5791 }
5792 }
5793
Dan Gohman9e255b72008-02-22 01:12:31 +00005794 // If we know that the high bits of the shift amount are all zero, then we can
5795 // do this as a couple of simple shifts.
5796 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005797 // Compute 32-amt.
Dale Johannesen8a782a22009-02-02 22:12:50 +00005798 SDValue Amt2 = DAG.getNode(ISD::SUB, dl, Amt.getValueType(),
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005799 DAG.getConstant(NVTBits, Amt.getValueType()),
5800 Amt);
5801
5802 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005803 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005804 ExpandOp(Op, InL, InH);
5805 switch(Opc) {
5806 case ISD::SHL:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005807 Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
5808 Hi = DAG.getNode(ISD::OR, dl, NVT,
5809 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
5810 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt2));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005811 return true;
5812 case ISD::SRL:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005813 Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
5814 Lo = DAG.getNode(ISD::OR, dl, NVT,
5815 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
5816 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005817 return true;
5818 case ISD::SRA:
Dale Johannesen8a782a22009-02-02 22:12:50 +00005819 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
5820 Lo = DAG.getNode(ISD::OR, dl, NVT,
5821 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
5822 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005823 return true;
5824 }
5825 }
5826
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005827 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005828}
Chris Lattner77e77a62005-01-21 06:05:23 +00005829
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005830
Chris Lattner77e77a62005-01-21 06:05:23 +00005831// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5832// does not fit into a register, return the lo part and set the hi part to the
5833// by-reg argument. If it does fit into a single register, return the result
5834// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00005835SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5836 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005837 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5838 // The input chain to this libcall is the entry node of the function.
5839 // Legalizing the call will automatically add the previous call to the
5840 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00005841 SDValue InChain = DAG.getEntryNode();
Chris Lattner6831a812006-02-13 09:18:02 +00005842
Chris Lattner77e77a62005-01-21 06:05:23 +00005843 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005844 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005845 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005846 MVT ArgVT = Node->getOperand(i).getValueType();
5847 const Type *ArgTy = ArgVT.getTypeForMVT();
Reid Spencer47857812006-12-31 05:55:36 +00005848 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005849 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005850 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005851 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005852 }
Bill Wendling056292f2008-09-16 21:48:12 +00005853 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00005854 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005855
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005856 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005857 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman475871a2008-07-27 21:46:04 +00005858 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005859 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005860 CallingConv::C, false, Callee, Args, DAG,
5861 Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005862
Chris Lattner6831a812006-02-13 09:18:02 +00005863 // Legalize the call sequence, starting with the chain. This will advance
5864 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5865 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5866 LegalizeOp(CallInfo.second);
Dan Gohman475871a2008-07-27 21:46:04 +00005867 SDValue Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005868 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005869 default: assert(0 && "Unknown thing");
5870 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005871 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005872 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005873 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005874 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005875 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005876 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005877 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005878}
5879
Dan Gohman7f8613e2008-08-14 20:04:46 +00005880/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5881///
5882SDValue SelectionDAGLegalize::
Dale Johannesenaf435272009-02-02 19:03:57 +00005883LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op,
5884 DebugLoc dl) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00005885 bool isCustom = false;
5886 SDValue Tmp1;
5887 switch (getTypeAction(Op.getValueType())) {
5888 case Legal:
5889 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5890 Op.getValueType())) {
5891 default: assert(0 && "Unknown operation action!");
5892 case TargetLowering::Custom:
5893 isCustom = true;
5894 // FALLTHROUGH
5895 case TargetLowering::Legal:
5896 Tmp1 = LegalizeOp(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00005897 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005898 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5899 else
Dale Johannesenaf435272009-02-02 19:03:57 +00005900 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, dl,
Dan Gohman7f8613e2008-08-14 20:04:46 +00005901 DestTy, Tmp1);
5902 if (isCustom) {
5903 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005904 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005905 }
5906 break;
5907 case TargetLowering::Expand:
Dale Johannesenaf435272009-02-02 19:03:57 +00005908 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy, dl);
Dan Gohman7f8613e2008-08-14 20:04:46 +00005909 break;
5910 case TargetLowering::Promote:
Dale Johannesenaf435272009-02-02 19:03:57 +00005911 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned, dl);
Dan Gohman7f8613e2008-08-14 20:04:46 +00005912 break;
5913 }
5914 break;
5915 case Expand:
Dale Johannesenaf435272009-02-02 19:03:57 +00005916 Result = ExpandIntToFP(isSigned, DestTy, Op, dl) ;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005917 break;
5918 case Promote:
5919 Tmp1 = PromoteOp(Op);
5920 if (isSigned) {
Dale Johannesenaf435272009-02-02 19:03:57 +00005921 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Tmp1.getValueType(),
Dan Gohman7f8613e2008-08-14 20:04:46 +00005922 Tmp1, DAG.getValueType(Op.getValueType()));
5923 } else {
Dale Johannesenaf435272009-02-02 19:03:57 +00005924 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl,
Dan Gohman7f8613e2008-08-14 20:04:46 +00005925 Op.getValueType());
5926 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005927 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005928 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5929 else
Dale Johannesenaf435272009-02-02 19:03:57 +00005930 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, dl,
Dan Gohman7f8613e2008-08-14 20:04:46 +00005931 DestTy, Tmp1);
5932 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5933 break;
5934 }
5935 return Result;
5936}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005937
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005938/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5939///
Dan Gohman475871a2008-07-27 21:46:04 +00005940SDValue SelectionDAGLegalize::
Dale Johannesenaf435272009-02-02 19:03:57 +00005941ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005942 MVT SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005943 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005944
Dan Gohman7f8613e2008-08-14 20:04:46 +00005945 // Expand unsupported int-to-fp vector casts by unrolling them.
5946 if (DestTy.isVector()) {
5947 if (!ExpandSource)
5948 return LegalizeOp(UnrollVectorOp(Source));
5949 MVT DestEltTy = DestTy.getVectorElementType();
5950 if (DestTy.getVectorNumElements() == 1) {
5951 SDValue Scalar = ScalarizeVectorOp(Source);
5952 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +00005953 DestEltTy, Scalar, dl);
5954 return DAG.getNode(ISD::BUILD_VECTOR, dl, DestTy, Result);
Dan Gohman7f8613e2008-08-14 20:04:46 +00005955 }
5956 SDValue Lo, Hi;
5957 SplitVectorOp(Source, Lo, Hi);
5958 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5959 DestTy.getVectorNumElements() / 2);
Dale Johannesenaf435272009-02-02 19:03:57 +00005960 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy,
5961 Lo, dl);
5962 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy,
5963 Hi, dl);
5964 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, DestTy, LoResult,
Evan Chengefa53392008-10-13 18:46:18 +00005965 HiResult));
Dan Gohman7f8613e2008-08-14 20:04:46 +00005966 }
5967
Evan Cheng9845eb52008-04-01 02:18:22 +00005968 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5969 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005970 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005971 // incoming integer is set. To handle this, we dynamically test to see if
5972 // it is set, and, if so, add a fudge factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005973 SDValue Hi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005974 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005975 SDValue Lo;
Dan Gohman034f60e2008-03-11 01:59:03 +00005976 ExpandOp(Source, Lo, Hi);
Dale Johannesenaf435272009-02-02 19:03:57 +00005977 Source = DAG.getNode(ISD::BUILD_PAIR, dl, SourceVT, Lo, Hi);
Dan Gohman034f60e2008-03-11 01:59:03 +00005978 } else {
5979 // The comparison for the sign bit will use the entire operand.
5980 Hi = Source;
5981 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005982
Dale Johannesen53997b02008-11-04 20:52:49 +00005983 // Check to see if the target has a custom way to lower this. If so, use
5984 // it. (Note we've already expanded the operand in this case.)
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005985 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5986 default: assert(0 && "This action not implemented for this operation!");
5987 case TargetLowering::Legal:
5988 case TargetLowering::Expand:
5989 break; // This case is handled below.
5990 case TargetLowering::Custom: {
5991 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5992 Source), DAG);
5993 if (NV.getNode())
5994 return LegalizeOp(NV);
5995 break; // The target decided this was legal after all
5996 }
5997 }
5998
Chris Lattner66de05b2005-05-13 04:45:13 +00005999 // If this is unsigned, and not supported, first perform the conversion to
6000 // signed, then adjust the result if the sign bit is set.
Dale Johannesenaf435272009-02-02 19:03:57 +00006001 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source, dl);
Chris Lattner66de05b2005-05-13 04:45:13 +00006002
Dale Johannesenaf435272009-02-02 19:03:57 +00006003 SDValue SignSet = DAG.getSetCC(dl,
6004 TLI.getSetCCResultType(Hi.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00006005 Hi, DAG.getConstant(0, Hi.getValueType()),
6006 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00006007 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00006008 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattnere9c35e72005-04-13 05:09:42 +00006009 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00006010 uint64_t FF = 0x5f800000ULL;
6011 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohman34bc1782008-03-05 02:07:31 +00006012 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00006013
Dan Gohman475871a2008-07-27 21:46:04 +00006014 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00006015 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00006016 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00006017 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00006018 SDValue FudgeInReg;
Chris Lattnere9c35e72005-04-13 05:09:42 +00006019 if (DestTy == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00006020 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00006021 PseudoSourceValue::getConstantPool(), 0,
6022 false, Alignment);
Duncan Sands8e4eb092008-06-08 20:54:56 +00006023 else if (DestTy.bitsGT(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006024 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenaf435272009-02-02 19:03:57 +00006025 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, dl, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00006026 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00006027 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00006028 MVT::f32, false, Alignment);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006029 else
6030 assert(0 && "Unexpected conversion");
6031
Duncan Sands83ec4b62008-06-06 12:08:01 +00006032 MVT SCVT = SignedConv.getValueType();
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006033 if (SCVT != DestTy) {
6034 // Destination type needs to be expanded as well. The FADD now we are
6035 // constructing will be expanded into a libcall.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006036 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
6037 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dale Johannesenaf435272009-02-02 19:03:57 +00006038 SignedConv = DAG.getNode(ISD::BUILD_PAIR, dl, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006039 SignedConv, SignedConv.getValue(1));
6040 }
Dale Johannesenaf435272009-02-02 19:03:57 +00006041 SignedConv = DAG.getNode(ISD::BIT_CONVERT, dl, DestTy, SignedConv);
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006042 }
Dale Johannesenaf435272009-02-02 19:03:57 +00006043 return DAG.getNode(ISD::FADD, dl, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00006044 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00006045
Chris Lattnera88a2602005-05-14 05:33:54 +00006046 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00006047 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00006048 default: assert(0 && "This action not implemented for this operation!");
6049 case TargetLowering::Legal:
6050 case TargetLowering::Expand:
6051 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00006052 case TargetLowering::Custom: {
Dale Johannesenaf435272009-02-02 19:03:57 +00006053 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, dl, DestTy,
Chris Lattner07dffd62005-08-26 00:14:16 +00006054 Source), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006055 if (NV.getNode())
Chris Lattner07dffd62005-08-26 00:14:16 +00006056 return LegalizeOp(NV);
6057 break; // The target decided this was legal after all
6058 }
Chris Lattnera88a2602005-05-14 05:33:54 +00006059 }
6060
Chris Lattner13689e22005-05-12 07:00:44 +00006061 // Expand the source, then glue it back together for the call. We must expand
6062 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00006063 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00006064 SDValue SrcLo, SrcHi;
Dan Gohman034f60e2008-03-11 01:59:03 +00006065 ExpandOp(Source, SrcLo, SrcHi);
Dale Johannesenaf435272009-02-02 19:03:57 +00006066 Source = DAG.getNode(ISD::BUILD_PAIR, dl, SourceVT, SrcLo, SrcHi);
Dan Gohman034f60e2008-03-11 01:59:03 +00006067 }
Chris Lattner13689e22005-05-12 07:00:44 +00006068
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006069 RTLIB::Libcall LC = isSigned ?
6070 RTLIB::getSINTTOFP(SourceVT, DestTy) :
6071 RTLIB::getUINTTOFP(SourceVT, DestTy);
6072 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
6073
Dale Johannesenaf435272009-02-02 19:03:57 +00006074 Source = DAG.getNode(ISD::SINT_TO_FP, dl, DestTy, Source);
Dan Gohman475871a2008-07-27 21:46:04 +00006075 SDValue HiPart;
Gabor Greifba36cb52008-08-28 21:40:38 +00006076 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
6077 if (Result.getValueType() != DestTy && HiPart.getNode())
Dale Johannesenaf435272009-02-02 19:03:57 +00006078 Result = DAG.getNode(ISD::BUILD_PAIR, dl, DestTy, Result, HiPart);
Dan Gohmana2e94852008-03-10 23:03:31 +00006079 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00006080}
Misha Brukmanedf128a2005-04-21 22:36:52 +00006081
Chris Lattner22cde6a2006-01-28 08:25:58 +00006082/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
6083/// INT_TO_FP operation of the specified operand when the target requests that
6084/// we expand it. At this point, we know that the result and operand types are
6085/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00006086SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
6087 SDValue Op0,
Dale Johannesenaf435272009-02-02 19:03:57 +00006088 MVT DestVT,
6089 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006090 if (Op0.getValueType() == MVT::i32) {
6091 // simple 32-bit [signed|unsigned] integer to float/double expansion
6092
Chris Lattner23594d42008-01-16 07:03:22 +00006093 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00006094 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner23594d42008-01-16 07:03:22 +00006095
Chris Lattner22cde6a2006-01-28 08:25:58 +00006096 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00006097 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00006098 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00006099 SDValue Hi = StackSlot;
Dale Johannesenaf435272009-02-02 19:03:57 +00006100 SDValue Lo = DAG.getNode(ISD::ADD, dl,
6101 TLI.getPointerTy(), StackSlot,WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00006102 if (TLI.isLittleEndian())
6103 std::swap(Hi, Lo);
6104
Chris Lattner22cde6a2006-01-28 08:25:58 +00006105 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00006106 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006107 if (isSigned) {
6108 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00006109 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dale Johannesenaf435272009-02-02 19:03:57 +00006110 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006111 } else {
6112 Op0Mapped = Op0;
6113 }
6114 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00006115 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00006116 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006117 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00006118 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006119 // store the hi of the constructed double - biased exponent
Dale Johannesenaf435272009-02-02 19:03:57 +00006120 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006121 // load the constructed double
Dale Johannesenaf435272009-02-02 19:03:57 +00006122 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006123 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00006124 SDValue Bias = DAG.getConstantFP(isSigned ?
Chris Lattner22cde6a2006-01-28 08:25:58 +00006125 BitsToDouble(0x4330000080000000ULL)
6126 : BitsToDouble(0x4330000000000000ULL),
6127 MVT::f64);
6128 // subtract the bias
Dale Johannesenaf435272009-02-02 19:03:57 +00006129 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006130 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00006131 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006132 // handle final rounding
6133 if (DestVT == MVT::f64) {
6134 // do nothing
6135 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006136 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00006137 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00006138 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00006139 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00006140 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006141 }
6142 return Result;
6143 }
6144 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesenaf435272009-02-02 19:03:57 +00006145 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006146
Dale Johannesenaf435272009-02-02 19:03:57 +00006147 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00006148 Op0, DAG.getConstant(0, Op0.getValueType()),
6149 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00006150 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00006151 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006152 SignSet, Four, Zero);
6153
6154 // If the sign bit of the integer is set, the large number will be treated
6155 // as a negative number. To counteract this, the dynamic code adds an
6156 // offset depending on the data type.
6157 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006158 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006159 default: assert(0 && "Unsupported integer type!");
6160 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6161 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6162 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6163 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6164 }
6165 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00006166 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006167
Dan Gohman475871a2008-07-27 21:46:04 +00006168 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00006169 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00006170 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00006171 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00006172 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006173 if (DestVT == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00006174 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00006175 PseudoSourceValue::getConstantPool(), 0,
6176 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006177 else {
Dan Gohman69de1932008-02-06 22:27:42 +00006178 FudgeInReg =
Dale Johannesenaf435272009-02-02 19:03:57 +00006179 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohman69de1932008-02-06 22:27:42 +00006180 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00006181 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00006182 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006183 }
6184
Dale Johannesenaf435272009-02-02 19:03:57 +00006185 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006186}
6187
6188/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6189/// *INT_TO_FP operation of the specified operand when the target requests that
6190/// we promote it. At this point, we know that the result and operand types are
6191/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6192/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00006193SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6194 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00006195 bool isSigned,
6196 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006197 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006198 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006199
6200 unsigned OpToUse = 0;
6201
6202 // Scan for the appropriate larger type to use.
6203 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006204 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6205 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006206
6207 // If the target supports SINT_TO_FP of this type, use it.
6208 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6209 default: break;
6210 case TargetLowering::Legal:
6211 if (!TLI.isTypeLegal(NewInTy))
6212 break; // Can't use this datatype.
6213 // FALL THROUGH.
6214 case TargetLowering::Custom:
6215 OpToUse = ISD::SINT_TO_FP;
6216 break;
6217 }
6218 if (OpToUse) break;
6219 if (isSigned) continue;
6220
6221 // If the target supports UINT_TO_FP of this type, use it.
6222 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6223 default: break;
6224 case TargetLowering::Legal:
6225 if (!TLI.isTypeLegal(NewInTy))
6226 break; // Can't use this datatype.
6227 // FALL THROUGH.
6228 case TargetLowering::Custom:
6229 OpToUse = ISD::UINT_TO_FP;
6230 break;
6231 }
6232 if (OpToUse) break;
6233
6234 // Otherwise, try a larger type.
6235 }
6236
6237 // Okay, we found the operation and type to use. Zero extend our input to the
6238 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00006239 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00006240 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00006241 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006242}
6243
6244/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6245/// FP_TO_*INT operation of the specified operand when the target requests that
6246/// we promote it. At this point, we know that the result and operand types are
6247/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6248/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00006249SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6250 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00006251 bool isSigned,
6252 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006253 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006254 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006255
6256 unsigned OpToUse = 0;
6257
6258 // Scan for the appropriate larger type to use.
6259 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006260 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6261 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006262
6263 // If the target supports FP_TO_SINT returning this type, use it.
6264 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6265 default: break;
6266 case TargetLowering::Legal:
6267 if (!TLI.isTypeLegal(NewOutTy))
6268 break; // Can't use this datatype.
6269 // FALL THROUGH.
6270 case TargetLowering::Custom:
6271 OpToUse = ISD::FP_TO_SINT;
6272 break;
6273 }
6274 if (OpToUse) break;
6275
6276 // If the target supports FP_TO_UINT of this type, use it.
6277 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6278 default: break;
6279 case TargetLowering::Legal:
6280 if (!TLI.isTypeLegal(NewOutTy))
6281 break; // Can't use this datatype.
6282 // FALL THROUGH.
6283 case TargetLowering::Custom:
6284 OpToUse = ISD::FP_TO_UINT;
6285 break;
6286 }
6287 if (OpToUse) break;
6288
6289 // Otherwise, try a larger type.
6290 }
6291
Chris Lattner27a6c732007-11-24 07:07:01 +00006292
6293 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00006294 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00006295
Chris Lattner27a6c732007-11-24 07:07:01 +00006296 // If the operation produces an invalid type, it must be custom lowered. Use
6297 // the target lowering hooks to expand it. Just keep the low part of the
6298 // expanded operation, we know that we're truncating anyway.
6299 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00006300 SmallVector<SDValue, 2> Results;
6301 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6302 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6303 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00006304 }
Duncan Sands126d9072008-07-04 11:47:58 +00006305
Chris Lattner27a6c732007-11-24 07:07:01 +00006306 // Truncate the result of the extended FP_TO_*INT operation to the desired
6307 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00006308 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006309}
6310
6311/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6312///
Dale Johannesen8a782a22009-02-02 22:12:50 +00006313SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006314 MVT VT = Op.getValueType();
6315 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00006316 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006317 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006318 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6319 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00006320 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
6321 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
6322 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006323 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00006324 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
6325 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
6326 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
6327 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
6328 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6329 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6330 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
6331 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
6332 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006333 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00006334 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
6335 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
6336 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
6337 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
6338 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
6339 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
6340 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
6341 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
6342 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6343 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6344 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6345 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6346 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6347 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6348 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
6349 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
6350 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
6351 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
6352 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
6353 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
6354 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006355 }
6356}
6357
6358/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6359///
Dale Johannesen8a782a22009-02-02 22:12:50 +00006360SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
6361 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006362 switch (Opc) {
6363 default: assert(0 && "Cannot expand this yet!");
6364 case ISD::CTPOP: {
6365 static const uint64_t mask[6] = {
6366 0x5555555555555555ULL, 0x3333333333333333ULL,
6367 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6368 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6369 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00006370 MVT VT = Op.getValueType();
6371 MVT ShVT = TLI.getShiftAmountTy();
6372 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006373 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6374 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006375 unsigned EltSize = VT.isVector() ?
6376 VT.getVectorElementType().getSizeInBits() : len;
6377 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
Dan Gohman475871a2008-07-27 21:46:04 +00006378 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dale Johannesene72c5962009-02-06 21:55:48 +00006379 Op = DAG.getNode(ISD::ADD, dl, VT,
6380 DAG.getNode(ISD::AND, dl, VT, Op, Tmp2),
Dale Johannesen8a782a22009-02-02 22:12:50 +00006381 DAG.getNode(ISD::AND, dl, VT,
6382 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3),
6383 Tmp2));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006384 }
6385 return Op;
6386 }
6387 case ISD::CTLZ: {
6388 // for now, we do this:
6389 // x = x | (x >> 1);
6390 // x = x | (x >> 2);
6391 // ...
6392 // x = x | (x >>16);
6393 // x = x | (x >>32); // for 64-bit input
6394 // return popcount(~x);
6395 //
6396 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006397 MVT VT = Op.getValueType();
6398 MVT ShVT = TLI.getShiftAmountTy();
6399 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006400 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006401 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006402 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00006403 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006404 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00006405 Op = DAG.getNOT(dl, Op, VT);
6406 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006407 }
6408 case ISD::CTTZ: {
6409 // for now, we use: { return popcount(~x & (x - 1)); }
6410 // unless the target has ctlz but not ctpop, in which case we use:
6411 // { return 32 - nlz(~x & (x-1)); }
6412 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006413 MVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00006414 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
6415 DAG.getNOT(dl, Op, VT),
6416 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00006417 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006418 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006419 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
6420 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00006421 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006422 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00006423 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
6424 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006425 }
6426 }
6427}
Chris Lattnere34b3962005-01-19 04:19:40 +00006428
Dan Gohman475871a2008-07-27 21:46:04 +00006429/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner3e928bb2005-01-07 07:47:09 +00006430/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman929d3eb2008-10-01 15:07:49 +00006431/// LegalizedNodes map is filled in for any results that are not expanded, the
Chris Lattner3e928bb2005-01-07 07:47:09 +00006432/// ExpandedNodes map is filled in for any results that are expanded, and the
6433/// Lo/Hi values are returned.
Dan Gohman475871a2008-07-27 21:46:04 +00006434void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands83ec4b62008-06-06 12:08:01 +00006435 MVT VT = Op.getValueType();
6436 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greifba36cb52008-08-28 21:40:38 +00006437 SDNode *Node = Op.getNode();
Dale Johannesen8a782a22009-02-02 22:12:50 +00006438 DebugLoc dl = Node->getDebugLoc();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006439 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00006440 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands83ec4b62008-06-06 12:08:01 +00006441 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006442
Chris Lattner6fdcb252005-09-02 20:32:45 +00006443 // See if we already expanded it.
Dan Gohman475871a2008-07-27 21:46:04 +00006444 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00006445 = ExpandedNodes.find(Op);
6446 if (I != ExpandedNodes.end()) {
6447 Lo = I->second.first;
6448 Hi = I->second.second;
6449 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006450 }
6451
Chris Lattner3e928bb2005-01-07 07:47:09 +00006452 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006453 case ISD::CopyFromReg:
6454 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006455 case ISD::FP_ROUND_INREG:
6456 if (VT == MVT::ppcf128 &&
6457 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6458 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006459 SDValue SrcLo, SrcHi, Src;
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006460 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006461 Src = DAG.getNode(ISD::BUILD_PAIR, dl, VT, SrcLo, SrcHi);
Dan Gohman475871a2008-07-27 21:46:04 +00006462 SDValue Result = TLI.LowerOperation(
Dale Johannesen8a782a22009-02-02 22:12:50 +00006463 DAG.getNode(ISD::FP_ROUND_INREG, dl, VT, Src, Op.getOperand(1)), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006464 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6465 Lo = Result.getNode()->getOperand(0);
6466 Hi = Result.getNode()->getOperand(1);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006467 break;
6468 }
6469 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00006470 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006471#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006472 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006473#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00006474 assert(0 && "Do not know how to expand this operator!");
6475 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00006476 case ISD::EXTRACT_ELEMENT:
6477 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006478 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman1953ecb2008-02-27 01:52:30 +00006479 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00006480 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00006481 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen25f1d082007-10-31 00:32:36 +00006482 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6483 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6484 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006485 case ISD::UNDEF:
Dale Johannesen8a782a22009-02-02 22:12:50 +00006486 Lo = DAG.getNode(ISD::UNDEF, dl, NVT);
6487 Hi = DAG.getNode(ISD::UNDEF, dl, NVT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006488 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006489 case ISD::Constant: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006490 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman050f5502008-03-03 22:20:46 +00006491 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6492 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6493 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006494 break;
6495 }
Evan Cheng00495212006-12-12 21:32:44 +00006496 case ISD::ConstantFP: {
6497 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00006498 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen7111b022008-10-09 18:53:47 +00006499 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesena471c2e2007-10-11 18:07:22 +00006500 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6501 MVT::f64);
6502 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6503 MVT::f64);
6504 break;
6505 }
Evan Cheng279101e2006-12-12 22:19:28 +00006506 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00006507 if (getTypeAction(Lo.getValueType()) == Expand)
6508 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006509 break;
6510 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006511 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006512 // Return the operands.
6513 Lo = Node->getOperand(0);
6514 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006515 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006516
6517 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00006518 if (Node->getNumValues() == 1) {
6519 ExpandOp(Op.getOperand(0), Lo, Hi);
6520 break;
6521 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006522 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif99a6cb92008-08-26 22:36:50 +00006523 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattner27a6c732007-11-24 07:07:01 +00006524 Op.getValue(1).getValueType() == MVT::Other &&
6525 "unhandled MERGE_VALUES");
6526 ExpandOp(Op.getOperand(0), Lo, Hi);
6527 // Remember that we legalized the chain.
6528 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6529 break;
Chris Lattner58f79632005-12-12 22:27:43 +00006530
6531 case ISD::SIGN_EXTEND_INREG:
6532 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00006533 // sext_inreg the low part if needed.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006534 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Lo, Node->getOperand(1));
Chris Lattner4bdd2752006-10-06 17:34:12 +00006535
6536 // The high part gets the sign extension from the lo-part. This handles
6537 // things like sextinreg V:i64 from i8.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006538 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006539 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattner58f79632005-12-12 22:27:43 +00006540 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00006541 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006542
Nate Begemand88fc032006-01-14 03:14:10 +00006543 case ISD::BSWAP: {
6544 ExpandOp(Node->getOperand(0), Lo, Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006545 SDValue TempLo = DAG.getNode(ISD::BSWAP, dl, NVT, Hi);
6546 Hi = DAG.getNode(ISD::BSWAP, dl, NVT, Lo);
Nate Begemand88fc032006-01-14 03:14:10 +00006547 Lo = TempLo;
6548 break;
6549 }
6550
Chris Lattneredb1add2005-05-11 04:51:16 +00006551 case ISD::CTPOP:
6552 ExpandOp(Node->getOperand(0), Lo, Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006553 Lo = DAG.getNode(ISD::ADD, dl, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6554 DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
6555 DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00006556 Hi = DAG.getConstant(0, NVT);
6557 break;
6558
Chris Lattner39a8f332005-05-12 19:05:01 +00006559 case ISD::CTLZ: {
6560 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006561 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006562 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006563 SDValue HLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Hi);
6564 SDValue TopNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), HLZ,
6565 BitsC, ISD::SETNE);
6566 SDValue LowPart = DAG.getNode(ISD::CTLZ, dl, NVT, Lo);
6567 LowPart = DAG.getNode(ISD::ADD, dl, NVT, LowPart, BitsC);
Chris Lattner39a8f332005-05-12 19:05:01 +00006568
Dale Johannesen8a782a22009-02-02 22:12:50 +00006569 Lo = DAG.getNode(ISD::SELECT, dl, NVT, TopNotZero, HLZ, LowPart);
Chris Lattner39a8f332005-05-12 19:05:01 +00006570 Hi = DAG.getConstant(0, NVT);
6571 break;
6572 }
6573
6574 case ISD::CTTZ: {
6575 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006576 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006577 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006578 SDValue LTZ = DAG.getNode(ISD::CTTZ, dl, NVT, Lo);
6579 SDValue BotNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), LTZ,
6580 BitsC, ISD::SETNE);
6581 SDValue HiPart = DAG.getNode(ISD::CTTZ, dl, NVT, Hi);
6582 HiPart = DAG.getNode(ISD::ADD, dl, NVT, HiPart, BitsC);
Chris Lattner39a8f332005-05-12 19:05:01 +00006583
Dale Johannesen8a782a22009-02-02 22:12:50 +00006584 Lo = DAG.getNode(ISD::SELECT, dl, NVT, BotNotZero, LTZ, HiPart);
Chris Lattner39a8f332005-05-12 19:05:01 +00006585 Hi = DAG.getConstant(0, NVT);
6586 break;
6587 }
Chris Lattneredb1add2005-05-11 04:51:16 +00006588
Nate Begemanacc398c2006-01-25 18:21:52 +00006589 case ISD::VAARG: {
Dan Gohman475871a2008-07-27 21:46:04 +00006590 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6591 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Dale Johannesenc460ae92009-02-04 00:13:36 +00006592 Lo = DAG.getVAArg(NVT, dl, Ch, Ptr, Node->getOperand(2));
6593 Hi = DAG.getVAArg(NVT, dl, Lo.getValue(1), Ptr, Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00006594
6595 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00006596 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00006597 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00006598 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00006599 std::swap(Lo, Hi);
6600 break;
6601 }
6602
Chris Lattner3e928bb2005-01-07 07:47:09 +00006603 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00006604 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00006605 SDValue Ch = LD->getChain(); // Legalize the chain.
6606 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Cheng466685d2006-10-09 20:57:25 +00006607 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f8613e2008-08-14 20:04:46 +00006608 const Value *SV = LD->getSrcValue();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006609 int SVOffset = LD->getSrcValueOffset();
6610 unsigned Alignment = LD->getAlignment();
6611 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006612
Evan Cheng466685d2006-10-09 20:57:25 +00006613 if (ExtType == ISD::NON_EXTLOAD) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006614 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006615 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00006616 if (VT == MVT::f32 || VT == MVT::f64) {
6617 // f32->i32 or f64->i64 one to one expansion.
6618 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006619 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00006620 // Recursively expand the new load.
6621 if (getTypeAction(NVT) == Expand)
6622 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006623 break;
6624 }
Chris Lattnerec39a452005-01-19 18:02:17 +00006625
Evan Cheng466685d2006-10-09 20:57:25 +00006626 // Increment the pointer to the other half.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006627 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Dale Johannesen8a782a22009-02-02 22:12:50 +00006628 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006629 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006630 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006631 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006632 Hi = DAG.getLoad(NVT, dl, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006633 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00006634
Evan Cheng466685d2006-10-09 20:57:25 +00006635 // Build a factor node to remember that this load is independent of the
6636 // other one.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006637 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Evan Cheng466685d2006-10-09 20:57:25 +00006638 Hi.getValue(1));
6639
6640 // Remember that we legalized the chain.
6641 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00006642 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00006643 std::swap(Lo, Hi);
6644 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006645 MVT EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00006646
Dale Johannesenb6210fc2007-10-19 20:29:00 +00006647 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6648 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00006649 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dale Johannesen8a782a22009-02-02 22:12:50 +00006650 SDValue Load = DAG.getLoad(EVT, dl, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006651 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00006652 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006653 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Dale Johannesen8a782a22009-02-02 22:12:50 +00006654 ExpandOp(DAG.getNode(ISD::FP_EXTEND, dl, VT, Load), Lo, Hi);
Evan Cheng548f6112006-12-13 03:19:57 +00006655 break;
6656 }
Evan Cheng466685d2006-10-09 20:57:25 +00006657
6658 if (EVT == NVT)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006659 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006660 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006661 else
Dale Johannesen8a782a22009-02-02 22:12:50 +00006662 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006663 SVOffset, EVT, isVolatile,
6664 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006665
6666 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006667 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng466685d2006-10-09 20:57:25 +00006668
6669 if (ExtType == ISD::SEXTLOAD) {
6670 // The high part is obtained by SRA'ing all but one of the bits of the
6671 // lo part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006672 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dale Johannesen8a782a22009-02-02 22:12:50 +00006673 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
Evan Cheng466685d2006-10-09 20:57:25 +00006674 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6675 } else if (ExtType == ISD::ZEXTLOAD) {
6676 // The high part is just a zero.
6677 Hi = DAG.getConstant(0, NVT);
6678 } else /* if (ExtType == ISD::EXTLOAD) */ {
6679 // The high part is undefined.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006680 Hi = DAG.getNode(ISD::UNDEF, dl, NVT);
Evan Cheng466685d2006-10-09 20:57:25 +00006681 }
6682 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006683 break;
6684 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006685 case ISD::AND:
6686 case ISD::OR:
6687 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman475871a2008-07-27 21:46:04 +00006688 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006689 ExpandOp(Node->getOperand(0), LL, LH);
6690 ExpandOp(Node->getOperand(1), RL, RH);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006691 Lo = DAG.getNode(Node->getOpcode(), dl, NVT, LL, RL);
6692 Hi = DAG.getNode(Node->getOpcode(), dl, NVT, LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006693 break;
6694 }
6695 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006696 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006697 ExpandOp(Node->getOperand(1), LL, LH);
6698 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00006699 if (getTypeAction(NVT) == Expand)
6700 NVT = TLI.getTypeToExpandTo(NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006701 Lo = DAG.getNode(ISD::SELECT, dl, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00006702 if (VT != MVT::f32)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006703 Hi = DAG.getNode(ISD::SELECT, dl, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006704 break;
6705 }
Nate Begeman9373a812005-08-10 20:51:12 +00006706 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00006707 SDValue TL, TH, FL, FH;
Nate Begeman9373a812005-08-10 20:51:12 +00006708 ExpandOp(Node->getOperand(2), TL, TH);
6709 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00006710 if (getTypeAction(NVT) == Expand)
6711 NVT = TLI.getTypeToExpandTo(NVT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006712 Lo = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
Nate Begeman9373a812005-08-10 20:51:12 +00006713 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00006714 if (VT != MVT::f32)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006715 Hi = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
Evan Cheng19103b12006-12-15 22:42:55 +00006716 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006717 break;
6718 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006719 case ISD::ANY_EXTEND:
6720 // The low part is any extension of the input (which degenerates to a copy).
Dale Johannesen8a782a22009-02-02 22:12:50 +00006721 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
Chris Lattner8137c9e2006-01-28 05:07:51 +00006722 // The high part is undefined.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006723 Hi = DAG.getNode(ISD::UNDEF, dl, NVT);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006724 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006725 case ISD::SIGN_EXTEND: {
6726 // The low part is just a sign extension of the input (which degenerates to
6727 // a copy).
Dale Johannesen8a782a22009-02-02 22:12:50 +00006728 Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006729
Chris Lattner3e928bb2005-01-07 07:47:09 +00006730 // The high part is obtained by SRA'ing all but one of the bits of the lo
6731 // part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006732 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dale Johannesen8a782a22009-02-02 22:12:50 +00006733 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
Chris Lattner8137c9e2006-01-28 05:07:51 +00006734 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006735 break;
6736 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006737 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006738 // The low part is just a zero extension of the input (which degenerates to
6739 // a copy).
Dale Johannesen8a782a22009-02-02 22:12:50 +00006740 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006741
Chris Lattner3e928bb2005-01-07 07:47:09 +00006742 // The high part is just a zero.
6743 Hi = DAG.getConstant(0, NVT);
6744 break;
Chris Lattner35481892005-12-23 00:16:34 +00006745
Chris Lattner4c948eb2007-02-13 23:55:16 +00006746 case ISD::TRUNCATE: {
6747 // The input value must be larger than this value. Expand *it*.
Dan Gohman475871a2008-07-27 21:46:04 +00006748 SDValue NewLo;
Chris Lattner4c948eb2007-02-13 23:55:16 +00006749 ExpandOp(Node->getOperand(0), NewLo, Hi);
6750
6751 // The low part is now either the right size, or it is closer. If not the
6752 // right size, make an illegal truncate so we recursively expand it.
6753 if (NewLo.getValueType() != Node->getValueType(0))
Dale Johannesen8a782a22009-02-02 22:12:50 +00006754 NewLo = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), NewLo);
Chris Lattner4c948eb2007-02-13 23:55:16 +00006755 ExpandOp(NewLo, Lo, Hi);
6756 break;
6757 }
6758
Chris Lattner35481892005-12-23 00:16:34 +00006759 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006760 SDValue Tmp;
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006761 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6762 // If the target wants to, allow it to lower this itself.
6763 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6764 case Expand: assert(0 && "cannot expand FP!");
6765 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6766 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6767 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00006768 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp), DAG);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006769 }
6770
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006771 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006772 if (VT == MVT::f32 || VT == MVT::f64) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006773 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006774 if (getTypeAction(NVT) == Expand)
6775 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006776 break;
6777 }
6778
6779 // If source operand will be expanded to the same type as VT, i.e.
6780 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006781 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006782 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6783 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006784 break;
6785 }
6786
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006787 // Turn this into a load/store pair by default.
Gabor Greifba36cb52008-08-28 21:40:38 +00006788 if (Tmp.getNode() == 0)
Dale Johannesen8a782a22009-02-02 22:12:50 +00006789 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT, dl);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006790
Chris Lattner35481892005-12-23 00:16:34 +00006791 ExpandOp(Tmp, Lo, Hi);
6792 break;
6793 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006794
Chris Lattner27a6c732007-11-24 07:07:01 +00006795 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006796 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6797 TargetLowering::Custom &&
6798 "Must custom expand ReadCycleCounter");
Dan Gohman475871a2008-07-27 21:46:04 +00006799 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006800 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattner27a6c732007-11-24 07:07:01 +00006801 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006802 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006803 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006804 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006805 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006806
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006807 case ISD::ATOMIC_CMP_SWAP: {
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006808 // This operation does not need a loop.
6809 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6810 assert(Tmp.getNode() && "Node must be custom expanded!");
6811 ExpandOp(Tmp.getValue(0), Lo, Hi);
6812 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6813 LegalizeOp(Tmp.getValue(1)));
6814 break;
6815 }
6816
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006817 case ISD::ATOMIC_LOAD_ADD:
6818 case ISD::ATOMIC_LOAD_SUB:
6819 case ISD::ATOMIC_LOAD_AND:
6820 case ISD::ATOMIC_LOAD_OR:
6821 case ISD::ATOMIC_LOAD_XOR:
6822 case ISD::ATOMIC_LOAD_NAND:
6823 case ISD::ATOMIC_SWAP: {
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006824 // These operations require a loop to be generated. We can't do that yet,
6825 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006826 SDValue In2Lo, In2Hi, In2;
6827 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006828 In2 = DAG.getNode(ISD::BUILD_PAIR, dl, VT, In2Lo, In2Hi);
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006829 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6830 SDValue Replace =
Dale Johannesen8a782a22009-02-02 22:12:50 +00006831 DAG.getAtomic(Op.getOpcode(), dl, Anode->getMemoryVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006832 Op.getOperand(0), Op.getOperand(1), In2,
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006833 Anode->getSrcValue(), Anode->getAlignment());
6834 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006835 ExpandOp(Result.getValue(0), Lo, Hi);
6836 // Remember that we legalized the chain.
6837 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006838 break;
6839 }
6840
Chris Lattner4e6c7462005-01-08 19:27:05 +00006841 // These operators cannot be expanded directly, emit them as calls to
6842 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006843 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006844 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006845 SDValue Op;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006846 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6847 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006848 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6849 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006850 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006851
Dale Johannesen8a782a22009-02-02 22:12:50 +00006852 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op), DAG);
Chris Lattnerf20d1832005-07-30 01:40:57 +00006853
Chris Lattner80a3e942005-07-29 00:33:32 +00006854 // Now that the custom expander is done, expand the result, which is still
6855 // VT.
Gabor Greifba36cb52008-08-28 21:40:38 +00006856 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006857 ExpandOp(Op, Lo, Hi);
6858 break;
6859 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006860 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006861
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006862 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6863 VT);
6864 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6865 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006866 break;
Evan Cheng56966222007-01-12 02:11:51 +00006867 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006868
Evan Cheng56966222007-01-12 02:11:51 +00006869 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006870 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006871 SDValue Op;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006872 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6873 case Expand: assert(0 && "cannot expand FP!");
6874 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6875 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6876 }
6877
Dale Johannesen8a782a22009-02-02 22:12:50 +00006878 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, dl, VT, Op), DAG);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006879
6880 // Now that the custom expander is done, expand the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00006881 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006882 ExpandOp(Op, Lo, Hi);
6883 break;
6884 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006885 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006886
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006887 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6888 VT);
6889 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6890 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006891 break;
Evan Cheng56966222007-01-12 02:11:51 +00006892 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006893
Evan Cheng05a2d562006-01-09 18:31:59 +00006894 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006895 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006896 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006897 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006898 SDValue Op = DAG.getNode(ISD::SHL, dl, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006899 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006900 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006901 // Now that the custom expander is done, expand the result, which is
6902 // still VT.
6903 ExpandOp(Op, Lo, Hi);
6904 break;
6905 }
6906 }
6907
Chris Lattner79980b02006-09-13 03:50:39 +00006908 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6909 // this X << 1 as X+X.
6910 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006911 if (ShAmt->getAPIntValue() == 1 &&
6912 TLI.isOperationLegalOrCustom(ISD::ADDC, NVT) &&
6913 TLI.isOperationLegalOrCustom(ISD::ADDE, NVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006914 SDValue LoOps[2], HiOps[3];
Chris Lattner79980b02006-09-13 03:50:39 +00006915 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6916 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6917 LoOps[1] = LoOps[0];
Dale Johannesen8a782a22009-02-02 22:12:50 +00006918 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
Chris Lattner79980b02006-09-13 03:50:39 +00006919
6920 HiOps[1] = HiOps[0];
6921 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00006922 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
Chris Lattner79980b02006-09-13 03:50:39 +00006923 break;
6924 }
6925 }
6926
Chris Lattnere34b3962005-01-19 04:19:40 +00006927 // If we can emit an efficient shift operation, do so now.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006928 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
Chris Lattnere34b3962005-01-19 04:19:40 +00006929 break;
Chris Lattner47599822005-04-02 03:38:53 +00006930
6931 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006932 TargetLowering::LegalizeAction Action =
6933 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6934 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6935 Action == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006936 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0),
6937 ShiftAmt, Lo, Hi, dl);
Chris Lattner47599822005-04-02 03:38:53 +00006938 break;
6939 }
6940
Chris Lattnere34b3962005-01-19 04:19:40 +00006941 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006942 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006943 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006944 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006945
Evan Cheng05a2d562006-01-09 18:31:59 +00006946 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006947 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006948 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006949 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dale Johannesene72c5962009-02-06 21:55:48 +00006950 SDValue Op = DAG.getNode(ISD::SRA, dl, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006951 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006952 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006953 // Now that the custom expander is done, expand the result, which is
6954 // still VT.
6955 ExpandOp(Op, Lo, Hi);
6956 break;
6957 }
6958 }
6959
Chris Lattnere34b3962005-01-19 04:19:40 +00006960 // If we can emit an efficient shift operation, do so now.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006961 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
Chris Lattnere34b3962005-01-19 04:19:40 +00006962 break;
Chris Lattner47599822005-04-02 03:38:53 +00006963
6964 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006965 TargetLowering::LegalizeAction Action =
6966 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6967 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6968 Action == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006969 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0),
6970 ShiftAmt, Lo, Hi, dl);
Chris Lattner47599822005-04-02 03:38:53 +00006971 break;
6972 }
6973
Chris Lattnere34b3962005-01-19 04:19:40 +00006974 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006975 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006976 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006977 }
6978
6979 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006980 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006981 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006982 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00006983 SDValue Op = DAG.getNode(ISD::SRL, dl, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006984 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006985 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006986 // Now that the custom expander is done, expand the result, which is
6987 // still VT.
6988 ExpandOp(Op, Lo, Hi);
6989 break;
6990 }
6991 }
6992
Chris Lattnere34b3962005-01-19 04:19:40 +00006993 // If we can emit an efficient shift operation, do so now.
Dale Johannesen8a782a22009-02-02 22:12:50 +00006994 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
Chris Lattnere34b3962005-01-19 04:19:40 +00006995 break;
Chris Lattner47599822005-04-02 03:38:53 +00006996
6997 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006998 TargetLowering::LegalizeAction Action =
6999 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
7000 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
7001 Action == TargetLowering::Custom) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007002 ExpandShiftParts(ISD::SRL_PARTS,
7003 Node->getOperand(0), ShiftAmt, Lo, Hi, dl);
Chris Lattner47599822005-04-02 03:38:53 +00007004 break;
7005 }
7006
Chris Lattnere34b3962005-01-19 04:19:40 +00007007 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00007008 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00007009 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00007010 }
Chris Lattnere34b3962005-01-19 04:19:40 +00007011
Misha Brukmanedf128a2005-04-21 22:36:52 +00007012 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00007013 case ISD::SUB: {
7014 // If the target wants to custom expand this, let them.
7015 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
7016 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00007017 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007018 if (Result.getNode()) {
Duncan Sands69bfb152008-06-22 09:42:16 +00007019 ExpandOp(Result, Lo, Hi);
Chris Lattner8137c9e2006-01-28 05:07:51 +00007020 break;
7021 }
7022 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00007023 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00007024 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner8137c9e2006-01-28 05:07:51 +00007025 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7026 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman475871a2008-07-27 21:46:04 +00007027 SDValue LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00007028 LoOps[0] = LHSL;
7029 LoOps[1] = RHSL;
7030 HiOps[0] = LHSH;
7031 HiOps[1] = RHSH;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00007032
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00007033 //cascaded check to see if any smaller size has a a carry flag.
7034 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
7035 bool hasCarry = false;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00007036 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
7037 MVT AVT = MVT::getIntegerVT(BitSize);
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007038 if (TLI.isOperationLegalOrCustom(OpV, AVT)) {
Andrew Lenharth2163ca12008-10-07 18:27:23 +00007039 hasCarry = true;
7040 break;
7041 }
7042 }
7043
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00007044 if(hasCarry) {
Evan Cheng637ed032008-12-12 18:49:09 +00007045 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007046 if (Node->getOpcode() == ISD::ADD) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007047 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007048 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007049 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007050 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007051 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007052 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007053 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007054 }
7055 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00007056 } else {
Andrew Lenharth40d51392008-10-07 14:15:42 +00007057 if (Node->getOpcode() == ISD::ADD) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007058 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps, 2);
7059 Hi = DAG.getNode(ISD::ADD, dl, NVT, HiOps, 2);
7060 SDValue Cmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00007061 Lo, LoOps[0], ISD::SETULT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007062 SDValue Carry1 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp1,
Andrew Lenharth40d51392008-10-07 14:15:42 +00007063 DAG.getConstant(1, NVT),
7064 DAG.getConstant(0, NVT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00007065 SDValue Cmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00007066 Lo, LoOps[1], ISD::SETULT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007067 SDValue Carry2 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp2,
Andrew Lenharth40d51392008-10-07 14:15:42 +00007068 DAG.getConstant(1, NVT),
7069 Carry1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007070 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007071 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007072 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps, 2);
7073 Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps, 2);
7074 SDValue Cmp = DAG.getSetCC(dl, NVT, LoOps[0], LoOps[1], ISD::SETULT);
7075 SDValue Borrow = DAG.getNode(ISD::SELECT, dl, NVT, Cmp,
Andrew Lenharth40d51392008-10-07 14:15:42 +00007076 DAG.getConstant(1, NVT),
7077 DAG.getConstant(0, NVT));
Dale Johannesen8a782a22009-02-02 22:12:50 +00007078 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007079 }
7080 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00007081 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00007082 }
Chris Lattnerb429f732007-05-17 18:15:41 +00007083
7084 case ISD::ADDC:
7085 case ISD::SUBC: {
7086 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00007087 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00007088 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7089 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7090 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00007091 SDValue LoOps[2] = { LHSL, RHSL };
7092 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00007093
7094 if (Node->getOpcode() == ISD::ADDC) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007095 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
Chris Lattnerb429f732007-05-17 18:15:41 +00007096 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007097 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
Chris Lattnerb429f732007-05-17 18:15:41 +00007098 } else {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007099 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
Chris Lattnerb429f732007-05-17 18:15:41 +00007100 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007101 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
Chris Lattnerb429f732007-05-17 18:15:41 +00007102 }
7103 // Remember that we legalized the flag.
7104 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7105 break;
7106 }
7107 case ISD::ADDE:
7108 case ISD::SUBE: {
7109 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00007110 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00007111 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7112 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7113 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00007114 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
7115 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00007116
Dale Johannesen8a782a22009-02-02 22:12:50 +00007117 Lo = DAG.getNode(Node->getOpcode(), dl, VTList, LoOps, 3);
Chris Lattnerb429f732007-05-17 18:15:41 +00007118 HiOps[2] = Lo.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007119 Hi = DAG.getNode(Node->getOpcode(), dl, VTList, HiOps, 3);
Chris Lattnerb429f732007-05-17 18:15:41 +00007120
7121 // Remember that we legalized the flag.
7122 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7123 break;
7124 }
Nate Begemanc7c16572005-04-11 03:01:51 +00007125 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00007126 // If the target wants to custom expand this, let them.
7127 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00007128 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007129 if (New.getNode()) {
Chris Lattner8829dc82006-09-16 05:08:34 +00007130 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00007131 break;
7132 }
7133 }
7134
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007135 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, NVT);
7136 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, NVT);
7137 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, NVT);
7138 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00007139 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman475871a2008-07-27 21:46:04 +00007140 SDValue LL, LH, RL, RH;
Nate Begemanc7c16572005-04-11 03:01:51 +00007141 ExpandOp(Node->getOperand(0), LL, LH);
7142 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007143 unsigned OuterBitSize = Op.getValueSizeInBits();
7144 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00007145 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
7146 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00007147 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
7148 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
7149 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00007150 // The inputs are both zero-extended.
7151 if (HasUMUL_LOHI) {
7152 // We can emit a umul_lohi.
Dale Johannesen8a782a22009-02-02 22:12:50 +00007153 Lo = DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00007154 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00007155 break;
7156 }
7157 if (HasMULHU) {
7158 // We can emit a mulhu+mul.
Dale Johannesen8a782a22009-02-02 22:12:50 +00007159 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
7160 Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
Dan Gohman525178c2007-10-08 18:33:35 +00007161 break;
7162 }
Dan Gohman525178c2007-10-08 18:33:35 +00007163 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007164 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00007165 // The input values are both sign-extended.
7166 if (HasSMUL_LOHI) {
7167 // We can emit a smul_lohi.
Dale Johannesen8a782a22009-02-02 22:12:50 +00007168 Lo = DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00007169 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00007170 break;
7171 }
7172 if (HasMULHS) {
7173 // We can emit a mulhs+mul.
Dale Johannesen8a782a22009-02-02 22:12:50 +00007174 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
7175 Hi = DAG.getNode(ISD::MULHS, dl, NVT, LL, RL);
Dan Gohman525178c2007-10-08 18:33:35 +00007176 break;
7177 }
7178 }
7179 if (HasUMUL_LOHI) {
7180 // Lo,Hi = umul LHS, RHS.
Dale Johannesen8a782a22009-02-02 22:12:50 +00007181 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, dl,
Dan Gohman525178c2007-10-08 18:33:35 +00007182 DAG.getVTList(NVT, NVT), LL, RL);
7183 Lo = UMulLOHI;
7184 Hi = UMulLOHI.getValue(1);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007185 RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
7186 LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
7187 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
7188 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00007189 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00007190 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00007191 if (HasMULHU) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007192 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
7193 Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
7194 RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
7195 LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
7196 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
7197 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00007198 break;
7199 }
Nate Begemanc7c16572005-04-11 03:01:51 +00007200 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00007201
Dan Gohman525178c2007-10-08 18:33:35 +00007202 // If nothing else, we can make a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00007203 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00007204 break;
7205 }
Evan Cheng56966222007-01-12 02:11:51 +00007206 case ISD::SDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007207 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007208 break;
7209 case ISD::UDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007210 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007211 break;
7212 case ISD::SREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007213 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007214 break;
7215 case ISD::UREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007216 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007217 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00007218
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007219 case ISD::FADD:
Duncan Sands460a14e2008-04-12 17:14:18 +00007220 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7221 RTLIB::ADD_F64,
7222 RTLIB::ADD_F80,
7223 RTLIB::ADD_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007224 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007225 break;
7226 case ISD::FSUB:
Duncan Sands460a14e2008-04-12 17:14:18 +00007227 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7228 RTLIB::SUB_F64,
7229 RTLIB::SUB_F80,
7230 RTLIB::SUB_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007231 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007232 break;
7233 case ISD::FMUL:
Duncan Sands460a14e2008-04-12 17:14:18 +00007234 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7235 RTLIB::MUL_F64,
7236 RTLIB::MUL_F80,
7237 RTLIB::MUL_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007238 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007239 break;
7240 case ISD::FDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007241 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7242 RTLIB::DIV_F64,
7243 RTLIB::DIV_F80,
7244 RTLIB::DIV_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007245 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007246 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007247 case ISD::FP_EXTEND: {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007248 if (VT == MVT::ppcf128) {
7249 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7250 Node->getOperand(0).getValueType()==MVT::f64);
7251 const uint64_t zero = 0;
7252 if (Node->getOperand(0).getValueType()==MVT::f32)
Dale Johannesen8a782a22009-02-02 22:12:50 +00007253 Hi = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Node->getOperand(0));
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007254 else
7255 Hi = Node->getOperand(0);
7256 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7257 break;
7258 }
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007259 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7260 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7261 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007262 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007263 }
7264 case ISD::FP_ROUND: {
7265 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7266 VT);
7267 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7268 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007269 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007270 }
Evan Cheng4b887022008-09-09 23:02:14 +00007271 case ISD::FSQRT:
7272 case ISD::FSIN:
7273 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007274 case ISD::FLOG:
7275 case ISD::FLOG2:
7276 case ISD::FLOG10:
7277 case ISD::FEXP:
7278 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007279 case ISD::FTRUNC:
7280 case ISD::FFLOOR:
7281 case ISD::FCEIL:
7282 case ISD::FRINT:
7283 case ISD::FNEARBYINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00007284 case ISD::FPOW:
7285 case ISD::FPOWI: {
Evan Cheng56966222007-01-12 02:11:51 +00007286 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007287 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00007288 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00007289 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7290 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007291 break;
7292 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00007293 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7294 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007295 break;
7296 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00007297 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7298 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007299 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007300 case ISD::FLOG:
7301 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7302 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7303 break;
7304 case ISD::FLOG2:
7305 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7306 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7307 break;
7308 case ISD::FLOG10:
7309 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7310 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7311 break;
7312 case ISD::FEXP:
7313 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7314 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7315 break;
7316 case ISD::FEXP2:
7317 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7318 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7319 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007320 case ISD::FTRUNC:
7321 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7322 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7323 break;
7324 case ISD::FFLOOR:
7325 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7326 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7327 break;
7328 case ISD::FCEIL:
7329 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7330 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7331 break;
7332 case ISD::FRINT:
7333 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7334 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7335 break;
7336 case ISD::FNEARBYINT:
7337 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7338 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7339 break;
Evan Cheng4b887022008-09-09 23:02:14 +00007340 case ISD::FPOW:
7341 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7342 RTLIB::POW_PPCF128);
7343 break;
7344 case ISD::FPOWI:
7345 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7346 RTLIB::POWI_PPCF128);
7347 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007348 default: assert(0 && "Unreachable!");
7349 }
Duncan Sands460a14e2008-04-12 17:14:18 +00007350 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00007351 break;
7352 }
Evan Cheng966bf242006-12-16 00:52:40 +00007353 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007354 if (VT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00007355 SDValue Tmp;
Dale Johannesenf6467742007-10-12 19:02:17 +00007356 ExpandOp(Node->getOperand(0), Lo, Tmp);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007357 Hi = DAG.getNode(ISD::FABS, dl, NVT, Tmp);
Dale Johannesenf6467742007-10-12 19:02:17 +00007358 // lo = hi==fabs(hi) ? lo : -lo;
Dale Johannesen8a782a22009-02-02 22:12:50 +00007359 Lo = DAG.getNode(ISD::SELECT_CC, dl, NVT, Hi, Tmp,
7360 Lo, DAG.getNode(ISD::FNEG, dl, NVT, Lo),
Dale Johannesenf6467742007-10-12 19:02:17 +00007361 DAG.getCondCode(ISD::SETEQ));
7362 break;
7363 }
Dan Gohman475871a2008-07-27 21:46:04 +00007364 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007365 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7366 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007367 Mask = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask);
7368 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
7369 Lo = DAG.getNode(ISD::AND, dl, NVT, Lo, Mask);
Evan Cheng966bf242006-12-16 00:52:40 +00007370 if (getTypeAction(NVT) == Expand)
7371 ExpandOp(Lo, Lo, Hi);
7372 break;
7373 }
7374 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007375 if (VT == MVT::ppcf128) {
7376 ExpandOp(Node->getOperand(0), Lo, Hi);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007377 Lo = DAG.getNode(ISD::FNEG, dl, MVT::f64, Lo);
7378 Hi = DAG.getNode(ISD::FNEG, dl, MVT::f64, Hi);
Dale Johannesenf6467742007-10-12 19:02:17 +00007379 break;
7380 }
Dan Gohman475871a2008-07-27 21:46:04 +00007381 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007382 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7383 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007384 Mask = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask);
7385 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
7386 Lo = DAG.getNode(ISD::XOR, dl, NVT, Lo, Mask);
Evan Cheng966bf242006-12-16 00:52:40 +00007387 if (getTypeAction(NVT) == Expand)
7388 ExpandOp(Lo, Lo, Hi);
7389 break;
7390 }
Evan Cheng912095b2007-01-04 21:56:39 +00007391 case ISD::FCOPYSIGN: {
7392 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7393 if (getTypeAction(NVT) == Expand)
7394 ExpandOp(Lo, Lo, Hi);
7395 break;
7396 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007397 case ISD::SINT_TO_FP:
7398 case ISD::UINT_TO_FP: {
7399 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007400 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00007401
7402 // Promote the operand if needed. Do this before checking for
7403 // ppcf128 so conversions of i16 and i8 work.
7404 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman475871a2008-07-27 21:46:04 +00007405 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesencf498192008-03-18 17:28:38 +00007406 Tmp = isSigned
Dale Johannesen8a782a22009-02-02 22:12:50 +00007407 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Tmp.getValueType(), Tmp,
Dale Johannesencf498192008-03-18 17:28:38 +00007408 DAG.getValueType(SrcVT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00007409 : DAG.getZeroExtendInReg(Tmp, dl, SrcVT);
Gabor Greifba36cb52008-08-28 21:40:38 +00007410 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesencf498192008-03-18 17:28:38 +00007411 SrcVT = Node->getOperand(0).getValueType();
7412 }
7413
Dan Gohmana2e94852008-03-10 23:03:31 +00007414 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007415 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007416 if (isSigned) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00007417 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f64,
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007418 Node->getOperand(0)));
7419 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7420 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007421 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen8a782a22009-02-02 22:12:50 +00007422 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f64,
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007423 Node->getOperand(0)));
7424 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
Dale Johannesen8a782a22009-02-02 22:12:50 +00007425 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00007426 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen8a782a22009-02-02 22:12:50 +00007427 ExpandOp(DAG.getNode(ISD::SELECT_CC, dl,
7428 MVT::ppcf128, Node->getOperand(0),
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007429 DAG.getConstant(0, MVT::i32),
Dale Johannesen8a782a22009-02-02 22:12:50 +00007430 DAG.getNode(ISD::FADD, dl, MVT::ppcf128, Hi,
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007431 DAG.getConstantFP(
7432 APFloat(APInt(128, 2, TwoE32)),
7433 MVT::ppcf128)),
7434 Hi,
7435 DAG.getCondCode(ISD::SETLT)),
7436 Lo, Hi);
7437 }
7438 break;
7439 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00007440 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7441 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007442 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen8a782a22009-02-02 22:12:50 +00007443 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::ppcf128,
7444 Node->getOperand(0)), Lo, Hi);
7445 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00007446 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
Dale Johannesen8a782a22009-02-02 22:12:50 +00007447 ExpandOp(DAG.getNode(ISD::SELECT_CC, dl, MVT::ppcf128,
7448 Node->getOperand(0),
Dale Johannesen6e63e092007-10-12 17:52:03 +00007449 DAG.getConstant(0, MVT::i64),
Dale Johannesen8a782a22009-02-02 22:12:50 +00007450 DAG.getNode(ISD::FADD, dl, MVT::ppcf128, Hi,
Dale Johannesen6e63e092007-10-12 17:52:03 +00007451 DAG.getConstantFP(
7452 APFloat(APInt(128, 2, TwoE64)),
7453 MVT::ppcf128)),
7454 Hi,
7455 DAG.getCondCode(ISD::SETLT)),
7456 Lo, Hi);
7457 break;
7458 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007459
Dan Gohmana2e94852008-03-10 23:03:31 +00007460 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00007461 Node->getOperand(0), dl);
Evan Cheng110cf482008-04-01 01:50:16 +00007462 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng6ad2f932008-04-01 01:51:26 +00007463 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng110cf482008-04-01 01:50:16 +00007464 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00007465 break;
7466 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00007467 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00007468
Chris Lattner83397362005-12-21 18:02:52 +00007469 // Make sure the resultant values have been legalized themselves, unless this
7470 // is a type that requires multi-step expansion.
7471 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7472 Lo = LegalizeOp(Lo);
Gabor Greifba36cb52008-08-28 21:40:38 +00007473 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00007474 // Don't legalize the high part if it is expanded to a single node.
7475 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00007476 }
Evan Cheng05a2d562006-01-09 18:31:59 +00007477
7478 // Remember in a map if the values will be reused later.
Dan Gohman6b345ee2008-07-07 17:46:23 +00007479 bool isNew =
7480 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng05a2d562006-01-09 18:31:59 +00007481 assert(isNew && "Value already expanded?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007482 isNew = isNew;
Chris Lattner3e928bb2005-01-07 07:47:09 +00007483}
7484
Dan Gohman7f321562007-06-25 16:23:39 +00007485/// SplitVectorOp - Given an operand of vector type, break it down into
7486/// two smaller values, still of vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00007487void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7488 SDValue &Hi) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007489 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007490 SDNode *Node = Op.getNode();
Dale Johannesenbb5da912009-02-02 20:41:04 +00007491 DebugLoc dl = Node->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007492 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattnerc7029802006-03-18 01:44:44 +00007493 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00007494
Duncan Sands83ec4b62008-06-06 12:08:01 +00007495 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman5db1afb2007-11-15 21:15:26 +00007496
7497 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7498 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7499
Duncan Sands83ec4b62008-06-06 12:08:01 +00007500 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7501 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007502
Chris Lattnerc7029802006-03-18 01:44:44 +00007503 // See if we already split it.
Dan Gohman475871a2008-07-27 21:46:04 +00007504 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattnerc7029802006-03-18 01:44:44 +00007505 = SplitNodes.find(Op);
7506 if (I != SplitNodes.end()) {
7507 Lo = I->second.first;
7508 Hi = I->second.second;
7509 return;
7510 }
7511
7512 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007513 default:
7514#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007515 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007516#endif
7517 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007518 case ISD::UNDEF:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007519 Lo = DAG.getNode(ISD::UNDEF, dl, NewVT_Lo);
7520 Hi = DAG.getNode(ISD::UNDEF, dl, NewVT_Hi);
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007521 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007522 case ISD::BUILD_PAIR:
7523 Lo = Node->getOperand(0);
7524 Hi = Node->getOperand(1);
7525 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00007526 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman68679912008-04-25 18:07:40 +00007527 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7528 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007529 unsigned Index = Idx->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007530 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman68679912008-04-25 18:07:40 +00007531 if (Index < NewNumElts_Lo)
Dale Johannesenc6be1102009-02-02 22:49:46 +00007532 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVT_Lo, Lo, ScalarOp,
Nate Begeman68679912008-04-25 18:07:40 +00007533 DAG.getIntPtrConstant(Index));
7534 else
Dale Johannesenc6be1102009-02-02 22:49:46 +00007535 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVT_Hi, Hi, ScalarOp,
Nate Begeman68679912008-04-25 18:07:40 +00007536 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7537 break;
7538 }
Dan Gohman475871a2008-07-27 21:46:04 +00007539 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman68679912008-04-25 18:07:40 +00007540 Node->getOperand(1),
Dale Johannesenbb5da912009-02-02 20:41:04 +00007541 Node->getOperand(2), dl);
Nate Begeman68679912008-04-25 18:07:40 +00007542 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohman9fe46622007-09-28 23:53:40 +00007543 break;
7544 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00007545 case ISD::VECTOR_SHUFFLE: {
7546 // Build the low part.
Dan Gohman475871a2008-07-27 21:46:04 +00007547 SDValue Mask = Node->getOperand(2);
7548 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007549 MVT PtrVT = TLI.getPointerTy();
Chris Lattner6c9c6802007-11-19 21:16:54 +00007550
7551 // Insert all of the elements from the input that are needed. We use
7552 // buildvector of extractelement here because the input vectors will have
7553 // to be legalized, so this makes the code simpler.
7554 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007555 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007556 if (IdxNode.getOpcode() == ISD::UNDEF) {
Dale Johannesenc6be1102009-02-02 22:49:46 +00007557 Ops.push_back(DAG.getNode(ISD::UNDEF, dl, NewEltVT));
Nate Begeman5922f562008-03-14 00:53:31 +00007558 continue;
7559 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007560 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007561 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007562 if (Idx >= NumElements) {
7563 InVec = Node->getOperand(1);
7564 Idx -= NumElements;
7565 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007566 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewEltVT, InVec,
Chris Lattner6c9c6802007-11-19 21:16:54 +00007567 DAG.getConstant(Idx, PtrVT)));
7568 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007569 Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Lo, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007570 Ops.clear();
7571
7572 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007573 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007574 if (IdxNode.getOpcode() == ISD::UNDEF) {
Dale Johannesenc6be1102009-02-02 22:49:46 +00007575 Ops.push_back(DAG.getNode(ISD::UNDEF, dl, NewEltVT));
Nate Begeman5922f562008-03-14 00:53:31 +00007576 continue;
7577 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007578 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007579 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007580 if (Idx >= NumElements) {
7581 InVec = Node->getOperand(1);
7582 Idx -= NumElements;
7583 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007584 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewEltVT, InVec,
Chris Lattner6c9c6802007-11-19 21:16:54 +00007585 DAG.getConstant(Idx, PtrVT)));
7586 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00007587 Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007588 break;
7589 }
Dan Gohman7f321562007-06-25 16:23:39 +00007590 case ISD::BUILD_VECTOR: {
Dan Gohman475871a2008-07-27 21:46:04 +00007591 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00007592 Node->op_begin()+NewNumElts_Lo);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007593 Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007594
Dan Gohman475871a2008-07-27 21:46:04 +00007595 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00007596 Node->op_end());
Dale Johannesenc6be1102009-02-02 22:49:46 +00007597 Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007598 break;
7599 }
Dan Gohman7f321562007-06-25 16:23:39 +00007600 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00007601 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00007602 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7603 if (NewNumSubvectors == 1) {
7604 Lo = Node->getOperand(0);
7605 Hi = Node->getOperand(1);
7606 } else {
Mon P Wangaeb06d22008-11-10 04:46:22 +00007607 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7608 Node->op_begin()+NewNumSubvectors);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007609 Lo = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewVT_Lo,
7610 &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00007611
Mon P Wangaeb06d22008-11-10 04:46:22 +00007612 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohman7f321562007-06-25 16:23:39 +00007613 Node->op_end());
Dale Johannesenc6be1102009-02-02 22:49:46 +00007614 Hi = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewVT_Hi,
7615 &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00007616 }
Dan Gohman65956352007-06-13 15:12:02 +00007617 break;
7618 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00007619 case ISD::EXTRACT_SUBVECTOR: {
7620 SDValue Vec = Op.getOperand(0);
7621 SDValue Idx = Op.getOperand(1);
7622 MVT IdxVT = Idx.getValueType();
7623
Dale Johannesenc6be1102009-02-02 22:49:46 +00007624 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Lo, Vec, Idx);
Mon P Wangaeb06d22008-11-10 04:46:22 +00007625 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7626 if (CIdx) {
Dale Johannesenc6be1102009-02-02 22:49:46 +00007627 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Hi, Vec,
Mon P Wangaeb06d22008-11-10 04:46:22 +00007628 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7629 IdxVT));
7630 } else {
Dale Johannesenc6be1102009-02-02 22:49:46 +00007631 Idx = DAG.getNode(ISD::ADD, dl, IdxVT, Idx,
Mon P Wangaeb06d22008-11-10 04:46:22 +00007632 DAG.getConstant(NewNumElts_Lo, IdxVT));
Dale Johannesenc6be1102009-02-02 22:49:46 +00007633 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Hi, Vec, Idx);
Mon P Wangaeb06d22008-11-10 04:46:22 +00007634 }
7635 break;
7636 }
Dan Gohmanc6230962007-10-17 14:48:28 +00007637 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007638 SDValue Cond = Node->getOperand(0);
Dan Gohmanc6230962007-10-17 14:48:28 +00007639
Dan Gohman475871a2008-07-27 21:46:04 +00007640 SDValue LL, LH, RL, RH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007641 SplitVectorOp(Node->getOperand(1), LL, LH);
7642 SplitVectorOp(Node->getOperand(2), RL, RH);
7643
Duncan Sands83ec4b62008-06-06 12:08:01 +00007644 if (Cond.getValueType().isVector()) {
Dan Gohmanc6230962007-10-17 14:48:28 +00007645 // Handle a vector merge.
Dan Gohman475871a2008-07-27 21:46:04 +00007646 SDValue CL, CH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007647 SplitVectorOp(Cond, CL, CH);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007648 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, CL, LL, RL);
7649 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007650 } else {
7651 // Handle a simple select with vector operands.
Dale Johannesenc6be1102009-02-02 22:49:46 +00007652 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, Cond, LL, RL);
7653 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007654 }
7655 break;
7656 }
Chris Lattner80c1a562008-06-30 02:43:01 +00007657 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007658 SDValue CondLHS = Node->getOperand(0);
7659 SDValue CondRHS = Node->getOperand(1);
7660 SDValue CondCode = Node->getOperand(4);
Chris Lattner80c1a562008-06-30 02:43:01 +00007661
Dan Gohman475871a2008-07-27 21:46:04 +00007662 SDValue LL, LH, RL, RH;
Chris Lattner80c1a562008-06-30 02:43:01 +00007663 SplitVectorOp(Node->getOperand(2), LL, LH);
7664 SplitVectorOp(Node->getOperand(3), RL, RH);
7665
7666 // Handle a simple select with vector operands.
Dale Johannesenc6be1102009-02-02 22:49:46 +00007667 Lo = DAG.getNode(ISD::SELECT_CC, dl, NewVT_Lo, CondLHS, CondRHS,
Chris Lattner80c1a562008-06-30 02:43:01 +00007668 LL, RL, CondCode);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007669 Hi = DAG.getNode(ISD::SELECT_CC, dl, NewVT_Hi, CondLHS, CondRHS,
Chris Lattner80c1a562008-06-30 02:43:01 +00007670 LH, RH, CondCode);
7671 break;
7672 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00007673 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007674 SDValue LL, LH, RL, RH;
Nate Begemanb43e9c12008-05-12 19:40:03 +00007675 SplitVectorOp(Node->getOperand(0), LL, LH);
7676 SplitVectorOp(Node->getOperand(1), RL, RH);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007677 Lo = DAG.getNode(ISD::VSETCC, dl, NewVT_Lo, LL, RL, Node->getOperand(2));
7678 Hi = DAG.getNode(ISD::VSETCC, dl, NewVT_Hi, LH, RH, Node->getOperand(2));
Nate Begemanb43e9c12008-05-12 19:40:03 +00007679 break;
7680 }
Dan Gohman7f321562007-06-25 16:23:39 +00007681 case ISD::ADD:
7682 case ISD::SUB:
7683 case ISD::MUL:
7684 case ISD::FADD:
7685 case ISD::FSUB:
7686 case ISD::FMUL:
7687 case ISD::SDIV:
7688 case ISD::UDIV:
7689 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00007690 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007691 case ISD::AND:
7692 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00007693 case ISD::XOR:
7694 case ISD::UREM:
7695 case ISD::SREM:
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007696 case ISD::FREM:
7697 case ISD::SHL:
7698 case ISD::SRA:
7699 case ISD::SRL: {
Dan Gohman475871a2008-07-27 21:46:04 +00007700 SDValue LL, LH, RL, RH;
Chris Lattnerc7029802006-03-18 01:44:44 +00007701 SplitVectorOp(Node->getOperand(0), LL, LH);
7702 SplitVectorOp(Node->getOperand(1), RL, RH);
7703
Dale Johannesenc6be1102009-02-02 22:49:46 +00007704 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, LL, RL);
7705 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00007706 break;
7707 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00007708 case ISD::FP_ROUND:
Dan Gohman82669522007-10-11 23:57:53 +00007709 case ISD::FPOWI: {
Dan Gohman475871a2008-07-27 21:46:04 +00007710 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007711 SplitVectorOp(Node->getOperand(0), L, H);
7712
Dale Johannesenc6be1102009-02-02 22:49:46 +00007713 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, L, Node->getOperand(1));
7714 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00007715 break;
7716 }
7717 case ISD::CTTZ:
7718 case ISD::CTLZ:
7719 case ISD::CTPOP:
7720 case ISD::FNEG:
7721 case ISD::FABS:
7722 case ISD::FSQRT:
7723 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00007724 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007725 case ISD::FLOG:
7726 case ISD::FLOG2:
7727 case ISD::FLOG10:
7728 case ISD::FEXP:
7729 case ISD::FEXP2:
Nate Begemanb348d182007-11-17 03:58:34 +00007730 case ISD::FP_TO_SINT:
7731 case ISD::FP_TO_UINT:
7732 case ISD::SINT_TO_FP:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007733 case ISD::UINT_TO_FP:
7734 case ISD::TRUNCATE:
7735 case ISD::ANY_EXTEND:
7736 case ISD::SIGN_EXTEND:
7737 case ISD::ZERO_EXTEND:
7738 case ISD::FP_EXTEND: {
Dan Gohman475871a2008-07-27 21:46:04 +00007739 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007740 SplitVectorOp(Node->getOperand(0), L, H);
7741
Dale Johannesenc6be1102009-02-02 22:49:46 +00007742 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, L);
7743 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00007744 break;
7745 }
Mon P Wang77cdf302008-11-10 20:54:11 +00007746 case ISD::CONVERT_RNDSAT: {
7747 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7748 SDValue L, H;
7749 SplitVectorOp(Node->getOperand(0), L, H);
7750 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7751 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7752 SDValue STyOpL = DAG.getValueType(L.getValueType());
7753 SDValue STyOpH = DAG.getValueType(H.getValueType());
7754
7755 SDValue RndOp = Node->getOperand(3);
7756 SDValue SatOp = Node->getOperand(4);
7757
Dale Johannesenc460ae92009-02-04 00:13:36 +00007758 Lo = DAG.getConvertRndSat(NewVT_Lo, dl, L, DTyOpL, STyOpL,
Mon P Wang77cdf302008-11-10 20:54:11 +00007759 RndOp, SatOp, CvtCode);
Dale Johannesenc460ae92009-02-04 00:13:36 +00007760 Hi = DAG.getConvertRndSat(NewVT_Hi, dl, H, DTyOpH, STyOpH,
Mon P Wang77cdf302008-11-10 20:54:11 +00007761 RndOp, SatOp, CvtCode);
7762 break;
7763 }
Dan Gohman7f321562007-06-25 16:23:39 +00007764 case ISD::LOAD: {
7765 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007766 SDValue Ch = LD->getChain();
7767 SDValue Ptr = LD->getBasePtr();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007768 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007769 const Value *SV = LD->getSrcValue();
7770 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007771 MVT MemoryVT = LD->getMemoryVT();
Dan Gohman7f321562007-06-25 16:23:39 +00007772 unsigned Alignment = LD->getAlignment();
7773 bool isVolatile = LD->isVolatile();
7774
Dan Gohman7f8613e2008-08-14 20:04:46 +00007775 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
Dale Johannesenc6be1102009-02-02 22:49:46 +00007776 SDValue Offset = DAG.getNode(ISD::UNDEF, dl, Ptr.getValueType());
Dan Gohman7f8613e2008-08-14 20:04:46 +00007777
7778 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7779 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7780 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7781
Dale Johannesenc6be1102009-02-02 22:49:46 +00007782 Lo = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
Dan Gohman7f8613e2008-08-14 20:04:46 +00007783 NewVT_Lo, Ch, Ptr, Offset,
7784 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7785 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Dale Johannesenc6be1102009-02-02 22:49:46 +00007786 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00007787 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00007788 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00007789 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007790 Hi = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
Dan Gohman7f8613e2008-08-14 20:04:46 +00007791 NewVT_Hi, Ch, Ptr, Offset,
7792 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00007793
7794 // Build a factor node to remember that this load is independent of the
7795 // other one.
Dale Johannesenc6be1102009-02-02 22:49:46 +00007796 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Chris Lattnerc7029802006-03-18 01:44:44 +00007797 Hi.getValue(1));
7798
7799 // Remember that we legalized the chain.
7800 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00007801 break;
7802 }
Dan Gohman7f321562007-06-25 16:23:39 +00007803 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00007804 // We know the result is a vector. The input may be either a vector or a
7805 // scalar value.
Dan Gohman475871a2008-07-27 21:46:04 +00007806 SDValue InOp = Node->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007807 if (!InOp.getValueType().isVector() ||
7808 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman10a7aa62007-06-29 00:09:08 +00007809 // The input is a scalar or single-element vector.
7810 // Lower to a store/load so that it can be split.
7811 // FIXME: this could be improved probably.
Mon P Wang2920d2b2008-07-15 05:28:34 +00007812 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7813 Op.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00007814 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greifba36cb52008-08-28 21:40:38 +00007815 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Chris Lattner7692eb42006-03-23 21:16:34 +00007816
Dale Johannesenc6be1102009-02-02 22:49:46 +00007817 SDValue St = DAG.getStore(DAG.getEntryNode(), dl,
Dan Gohman69de1932008-02-06 22:27:42 +00007818 InOp, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007819 PseudoSourceValue::getFixedStack(FI), 0);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007820 InOp = DAG.getLoad(Op.getValueType(), dl, St, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007821 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00007822 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00007823 // Split the vector and convert each of the pieces now.
7824 SplitVectorOp(InOp, Lo, Hi);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007825 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT_Lo, Lo);
7826 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00007827 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00007828 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007829 }
7830
7831 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00007832 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00007833 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00007834 assert(isNew && "Value already split?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007835 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007836}
7837
7838
Dan Gohman89b20c02007-06-27 14:06:22 +00007839/// ScalarizeVectorOp - Given an operand of single-element vector type
7840/// (e.g. v1f32), convert it into the equivalent operation that returns a
7841/// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +00007842SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007843 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007844 SDNode *Node = Op.getNode();
Dale Johannesenc6be1102009-02-02 22:49:46 +00007845 DebugLoc dl = Node->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007846 MVT NewVT = Op.getValueType().getVectorElementType();
7847 assert(Op.getValueType().getVectorNumElements() == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00007848
Dan Gohman7f321562007-06-25 16:23:39 +00007849 // See if we already scalarized it.
Dan Gohman475871a2008-07-27 21:46:04 +00007850 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohman7f321562007-06-25 16:23:39 +00007851 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00007852
Dan Gohman475871a2008-07-27 21:46:04 +00007853 SDValue Result;
Chris Lattnerc7029802006-03-18 01:44:44 +00007854 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00007855 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007856#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007857 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007858#endif
Dan Gohman7f321562007-06-25 16:23:39 +00007859 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7860 case ISD::ADD:
7861 case ISD::FADD:
7862 case ISD::SUB:
7863 case ISD::FSUB:
7864 case ISD::MUL:
7865 case ISD::FMUL:
7866 case ISD::SDIV:
7867 case ISD::UDIV:
7868 case ISD::FDIV:
7869 case ISD::SREM:
7870 case ISD::UREM:
7871 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00007872 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007873 case ISD::AND:
7874 case ISD::OR:
7875 case ISD::XOR:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007876 Result = DAG.getNode(Node->getOpcode(), dl,
Chris Lattnerc7029802006-03-18 01:44:44 +00007877 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007878 ScalarizeVectorOp(Node->getOperand(0)),
7879 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00007880 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007881 case ISD::FNEG:
7882 case ISD::FABS:
7883 case ISD::FSQRT:
7884 case ISD::FSIN:
7885 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007886 case ISD::FLOG:
7887 case ISD::FLOG2:
7888 case ISD::FLOG10:
7889 case ISD::FEXP:
7890 case ISD::FEXP2:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007891 case ISD::FP_TO_SINT:
7892 case ISD::FP_TO_UINT:
7893 case ISD::SINT_TO_FP:
7894 case ISD::UINT_TO_FP:
7895 case ISD::SIGN_EXTEND:
7896 case ISD::ZERO_EXTEND:
7897 case ISD::ANY_EXTEND:
7898 case ISD::TRUNCATE:
7899 case ISD::FP_EXTEND:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007900 Result = DAG.getNode(Node->getOpcode(), dl,
Dan Gohman7f321562007-06-25 16:23:39 +00007901 NewVT,
7902 ScalarizeVectorOp(Node->getOperand(0)));
7903 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00007904 case ISD::CONVERT_RNDSAT: {
7905 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
Dale Johannesenc460ae92009-02-04 00:13:36 +00007906 Result = DAG.getConvertRndSat(NewVT, dl, Op0,
Mon P Wang77cdf302008-11-10 20:54:11 +00007907 DAG.getValueType(NewVT),
7908 DAG.getValueType(Op0.getValueType()),
7909 Node->getOperand(3),
7910 Node->getOperand(4),
7911 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7912 break;
7913 }
Dan Gohman9e04c822007-10-12 14:13:46 +00007914 case ISD::FPOWI:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007915 case ISD::FP_ROUND:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007916 Result = DAG.getNode(Node->getOpcode(), dl,
Dan Gohman9e04c822007-10-12 14:13:46 +00007917 NewVT,
7918 ScalarizeVectorOp(Node->getOperand(0)),
7919 Node->getOperand(1));
7920 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007921 case ISD::LOAD: {
7922 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007923 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7924 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman7f8613e2008-08-14 20:04:46 +00007925 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007926 const Value *SV = LD->getSrcValue();
7927 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007928 MVT MemoryVT = LD->getMemoryVT();
7929 unsigned Alignment = LD->getAlignment();
7930 bool isVolatile = LD->isVolatile();
7931
7932 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
Dale Johannesenc6be1102009-02-02 22:49:46 +00007933 SDValue Offset = DAG.getNode(ISD::UNDEF, dl, Ptr.getValueType());
Dan Gohman7f8613e2008-08-14 20:04:46 +00007934
Dale Johannesenc6be1102009-02-02 22:49:46 +00007935 Result = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
Dan Gohman7f8613e2008-08-14 20:04:46 +00007936 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7937 MemoryVT.getVectorElementType(),
7938 isVolatile, Alignment);
Dan Gohman7f321562007-06-25 16:23:39 +00007939
Chris Lattnerc7029802006-03-18 01:44:44 +00007940 // Remember that we legalized the chain.
7941 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7942 break;
7943 }
Dan Gohman7f321562007-06-25 16:23:39 +00007944 case ISD::BUILD_VECTOR:
7945 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007946 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007947 case ISD::INSERT_VECTOR_ELT:
7948 // Returning the inserted scalar element.
7949 Result = Node->getOperand(1);
7950 break;
7951 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007952 assert(Node->getOperand(0).getValueType() == NewVT &&
7953 "Concat of non-legal vectors not yet supported!");
7954 Result = Node->getOperand(0);
7955 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007956 case ISD::VECTOR_SHUFFLE: {
7957 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman475871a2008-07-27 21:46:04 +00007958 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007959 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohman7f321562007-06-25 16:23:39 +00007960 Result = ScalarizeVectorOp(Node->getOperand(1));
7961 else
7962 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007963 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007964 }
7965 case ISD::EXTRACT_SUBVECTOR:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007966 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT,
7967 Node->getOperand(0), Node->getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00007968 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007969 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007970 SDValue Op0 = Op.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007971 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng446efdd2008-05-16 17:19:05 +00007972 Op0 = ScalarizeVectorOp(Op0);
Dale Johannesenc6be1102009-02-02 22:49:46 +00007973 Result = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, Op0);
Chris Lattner5b2316e2006-03-28 20:24:43 +00007974 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007975 }
Dan Gohman7f321562007-06-25 16:23:39 +00007976 case ISD::SELECT:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007977 Result = DAG.getNode(ISD::SELECT, dl, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007978 ScalarizeVectorOp(Op.getOperand(1)),
7979 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007980 break;
Chris Lattner80c1a562008-06-30 02:43:01 +00007981 case ISD::SELECT_CC:
Dale Johannesenc6be1102009-02-02 22:49:46 +00007982 Result = DAG.getNode(ISD::SELECT_CC, dl, NewVT, Node->getOperand(0),
Chris Lattner80c1a562008-06-30 02:43:01 +00007983 Node->getOperand(1),
7984 ScalarizeVectorOp(Op.getOperand(2)),
7985 ScalarizeVectorOp(Op.getOperand(3)),
7986 Node->getOperand(4));
7987 break;
Nate Begeman0d1704b2008-05-12 23:09:43 +00007988 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007989 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7990 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Dale Johannesenc6be1102009-02-02 22:49:46 +00007991 Result = DAG.getNode(ISD::SETCC, dl,
7992 TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00007993 Op0, Op1, Op.getOperand(2));
Dale Johannesenc6be1102009-02-02 22:49:46 +00007994 Result = DAG.getNode(ISD::SELECT, dl, NewVT, Result,
Nate Begeman0d1704b2008-05-12 23:09:43 +00007995 DAG.getConstant(-1ULL, NewVT),
7996 DAG.getConstant(0ULL, NewVT));
7997 break;
7998 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007999 }
8000
8001 if (TLI.isTypeLegal(NewVT))
8002 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00008003 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
8004 assert(isNew && "Value already scalarized?");
Evan Cheng24ac4082008-11-24 07:09:49 +00008005 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00008006 return Result;
8007}
8008
Chris Lattner3e928bb2005-01-07 07:47:09 +00008009
Mon P Wang0c397192008-10-30 08:01:45 +00008010SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
8011 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
8012 if (I != WidenNodes.end()) return I->second;
8013
8014 MVT VT = Op.getValueType();
8015 assert(VT.isVector() && "Cannot widen non-vector type!");
8016
8017 SDValue Result;
8018 SDNode *Node = Op.getNode();
Dale Johannesenc6be1102009-02-02 22:49:46 +00008019 DebugLoc dl = Node->getDebugLoc();
Mon P Wang0c397192008-10-30 08:01:45 +00008020 MVT EVT = VT.getVectorElementType();
8021
8022 unsigned NumElts = VT.getVectorNumElements();
8023 unsigned NewNumElts = WidenVT.getVectorNumElements();
8024 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
8025 assert(NewNumElts < 17);
8026
8027 // When widen is called, it is assumed that it is more efficient to use a
8028 // wide type. The default action is to widen to operation to a wider legal
8029 // vector type and then do the operation if it is legal by calling LegalizeOp
8030 // again. If there is no vector equivalent, we will unroll the operation, do
8031 // it, and rebuild the vector. If most of the operations are vectorizible to
8032 // the legal type, the resulting code will be more efficient. If this is not
8033 // the case, the resulting code will preform badly as we end up generating
8034 // code to pack/unpack the results. It is the function that calls widen
Mon P Wangf007a8b2008-11-06 05:31:54 +00008035 // that is responsible for seeing this doesn't happen.
Mon P Wang0c397192008-10-30 08:01:45 +00008036 switch (Node->getOpcode()) {
8037 default:
8038#ifndef NDEBUG
8039 Node->dump(&DAG);
8040#endif
8041 assert(0 && "Unexpected operation in WidenVectorOp!");
8042 break;
8043 case ISD::CopyFromReg:
Mon P Wang49292f12008-11-15 06:05:52 +00008044 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang0c397192008-10-30 08:01:45 +00008045 case ISD::Constant:
8046 case ISD::ConstantFP:
8047 // To build a vector of these elements, clients should call BuildVector
8048 // and with each element instead of creating a node with a vector type
8049 assert(0 && "Unexpected operation in WidenVectorOp!");
8050 case ISD::VAARG:
8051 // Variable Arguments with vector types doesn't make any sense to me
8052 assert(0 && "Unexpected operation in WidenVectorOp!");
8053 break;
Mon P Wang49292f12008-11-15 06:05:52 +00008054 case ISD::UNDEF:
Dale Johannesenc6be1102009-02-02 22:49:46 +00008055 Result = DAG.getNode(ISD::UNDEF, dl, WidenVT);
Mon P Wang49292f12008-11-15 06:05:52 +00008056 break;
Mon P Wang0c397192008-10-30 08:01:45 +00008057 case ISD::BUILD_VECTOR: {
8058 // Build a vector with undefined for the new nodes
8059 SDValueVector NewOps(Node->op_begin(), Node->op_end());
8060 for (unsigned i = NumElts; i < NewNumElts; ++i) {
Dale Johannesenc6be1102009-02-02 22:49:46 +00008061 NewOps.push_back(DAG.getNode(ISD::UNDEF, dl, EVT));
Mon P Wang0c397192008-10-30 08:01:45 +00008062 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00008063 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT,
8064 &NewOps[0], NewOps.size());
Mon P Wang0c397192008-10-30 08:01:45 +00008065 break;
8066 }
8067 case ISD::INSERT_VECTOR_ELT: {
8068 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008069 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, WidenVT, Tmp1,
Mon P Wang0c397192008-10-30 08:01:45 +00008070 Node->getOperand(1), Node->getOperand(2));
8071 break;
8072 }
8073 case ISD::VECTOR_SHUFFLE: {
8074 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8075 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8076 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
8077 // used as permutation array. We build the vector here instead of widening
8078 // because we don't want to legalize and have it turned to something else.
8079 SDValue PermOp = Node->getOperand(2);
8080 SDValueVector NewOps;
8081 MVT PVT = PermOp.getValueType().getVectorElementType();
8082 for (unsigned i = 0; i < NumElts; ++i) {
8083 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
8084 NewOps.push_back(PermOp.getOperand(i));
8085 } else {
8086 unsigned Idx =
Mon P Wange1a0b2e2008-12-13 08:15:14 +00008087 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
Mon P Wang0c397192008-10-30 08:01:45 +00008088 if (Idx < NumElts) {
8089 NewOps.push_back(PermOp.getOperand(i));
8090 }
8091 else {
8092 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
8093 PermOp.getOperand(i).getValueType()));
8094 }
8095 }
8096 }
8097 for (unsigned i = NumElts; i < NewNumElts; ++i) {
Dale Johannesenc6be1102009-02-02 22:49:46 +00008098 NewOps.push_back(DAG.getNode(ISD::UNDEF, dl, PVT));
Mon P Wang0c397192008-10-30 08:01:45 +00008099 }
8100
Dale Johannesenc6be1102009-02-02 22:49:46 +00008101 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR, dl,
Mon P Wang0c397192008-10-30 08:01:45 +00008102 MVT::getVectorVT(PVT, NewOps.size()),
8103 &NewOps[0], NewOps.size());
8104
Dale Johannesenc6be1102009-02-02 22:49:46 +00008105 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, WidenVT, Tmp1, Tmp2, Tmp3);
Mon P Wang0c397192008-10-30 08:01:45 +00008106 break;
8107 }
8108 case ISD::LOAD: {
8109 // If the load widen returns true, we can use a single load for the
8110 // vector. Otherwise, it is returning a token factor for multiple
8111 // loads.
8112 SDValue TFOp;
8113 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
8114 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
8115 else
8116 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
8117 break;
8118 }
8119
8120 case ISD::BIT_CONVERT: {
8121 SDValue Tmp1 = Node->getOperand(0);
8122 // Converts between two different types so we need to determine
8123 // the correct widen type for the input operand.
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008124 MVT InVT = Tmp1.getValueType();
8125 unsigned WidenSize = WidenVT.getSizeInBits();
8126 if (InVT.isVector()) {
8127 MVT InEltVT = InVT.getVectorElementType();
8128 unsigned InEltSize = InEltVT.getSizeInBits();
8129 assert(WidenSize % InEltSize == 0 &&
8130 "can not widen bit convert that are not multiple of element type");
8131 MVT NewInWidenVT = MVT::getVectorVT(InEltVT, WidenSize / InEltSize);
8132 Tmp1 = WidenVectorOp(Tmp1, NewInWidenVT);
8133 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
Dale Johannesenc6be1102009-02-02 22:49:46 +00008134 Result = DAG.getNode(ISD::BIT_CONVERT, dl, WidenVT, Tmp1);
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008135 } else {
8136 // If the result size is a multiple of the input size, widen the input
8137 // and then convert.
8138 unsigned InSize = InVT.getSizeInBits();
8139 assert(WidenSize % InSize == 0 &&
8140 "can not widen bit convert that are not multiple of element type");
8141 unsigned NewNumElts = WidenSize / InSize;
8142 SmallVector<SDValue, 16> Ops(NewNumElts);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008143 SDValue UndefVal = DAG.getNode(ISD::UNDEF, dl, InVT);
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008144 Ops[0] = Tmp1;
8145 for (unsigned i = 1; i < NewNumElts; ++i)
8146 Ops[i] = UndefVal;
Mon P Wang0c397192008-10-30 08:01:45 +00008147
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008148 MVT NewInVT = MVT::getVectorVT(InVT, NewNumElts);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008149 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, NewInVT, &Ops[0], NewNumElts);
8150 Result = DAG.getNode(ISD::BIT_CONVERT, dl, WidenVT, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00008151 }
8152 break;
8153 }
8154
8155 case ISD::SINT_TO_FP:
8156 case ISD::UINT_TO_FP:
8157 case ISD::FP_TO_SINT:
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008158 case ISD::FP_TO_UINT:
8159 case ISD::FP_ROUND: {
Mon P Wang0c397192008-10-30 08:01:45 +00008160 SDValue Tmp1 = Node->getOperand(0);
8161 // Converts between two different types so we need to determine
8162 // the correct widen type for the input operand.
8163 MVT TVT = Tmp1.getValueType();
8164 assert(TVT.isVector() && "can not widen non vector type");
8165 MVT TEVT = TVT.getVectorElementType();
8166 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
8167 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
8168 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008169 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00008170 break;
8171 }
8172
8173 case ISD::FP_EXTEND:
8174 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
8175 case ISD::TRUNCATE:
8176 case ISD::SIGN_EXTEND:
8177 case ISD::ZERO_EXTEND:
8178 case ISD::ANY_EXTEND:
Mon P Wang0c397192008-10-30 08:01:45 +00008179 case ISD::SIGN_EXTEND_INREG:
8180 case ISD::FABS:
8181 case ISD::FNEG:
8182 case ISD::FSQRT:
8183 case ISD::FSIN:
Mon P Wang49292f12008-11-15 06:05:52 +00008184 case ISD::FCOS:
8185 case ISD::CTPOP:
8186 case ISD::CTTZ:
8187 case ISD::CTLZ: {
Mon P Wang0c397192008-10-30 08:01:45 +00008188 // Unary op widening
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008189 SDValue Tmp1;
Mon P Wang0c397192008-10-30 08:01:45 +00008190 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8191 assert(Tmp1.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008192 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00008193 break;
8194 }
Mon P Wang77cdf302008-11-10 20:54:11 +00008195 case ISD::CONVERT_RNDSAT: {
8196 SDValue RndOp = Node->getOperand(3);
8197 SDValue SatOp = Node->getOperand(4);
Mon P Wang77cdf302008-11-10 20:54:11 +00008198 SDValue SrcOp = Node->getOperand(0);
8199
8200 // Converts between two different types so we need to determine
8201 // the correct widen type for the input operand.
8202 MVT SVT = SrcOp.getValueType();
8203 assert(SVT.isVector() && "can not widen non vector type");
8204 MVT SEVT = SVT.getVectorElementType();
8205 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8206
8207 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8208 assert(SrcOp.getValueType() == WidenVT);
8209 SDValue DTyOp = DAG.getValueType(WidenVT);
8210 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8211 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8212
Dale Johannesenc460ae92009-02-04 00:13:36 +00008213 Result = DAG.getConvertRndSat(WidenVT, dl, SrcOp, DTyOp, STyOp,
Mon P Wang77cdf302008-11-10 20:54:11 +00008214 RndOp, SatOp, CvtCode);
Mon P Wang77cdf302008-11-10 20:54:11 +00008215 break;
8216 }
Mon P Wang0c397192008-10-30 08:01:45 +00008217 case ISD::FPOW:
8218 case ISD::FPOWI:
8219 case ISD::ADD:
8220 case ISD::SUB:
8221 case ISD::MUL:
8222 case ISD::MULHS:
8223 case ISD::MULHU:
8224 case ISD::AND:
8225 case ISD::OR:
8226 case ISD::XOR:
8227 case ISD::FADD:
8228 case ISD::FSUB:
8229 case ISD::FMUL:
8230 case ISD::SDIV:
8231 case ISD::SREM:
8232 case ISD::FDIV:
8233 case ISD::FREM:
8234 case ISD::FCOPYSIGN:
8235 case ISD::UDIV:
8236 case ISD::UREM:
8237 case ISD::BSWAP: {
8238 // Binary op widening
Mon P Wang0c397192008-10-30 08:01:45 +00008239 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8240 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8241 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008242 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008243 break;
8244 }
8245
8246 case ISD::SHL:
8247 case ISD::SRA:
8248 case ISD::SRL: {
Mon P Wang0c397192008-10-30 08:01:45 +00008249 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8250 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangfb13f002008-12-02 07:35:08 +00008251 SDValue ShOp = Node->getOperand(1);
8252 MVT ShVT = ShOp.getValueType();
8253 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8254 WidenVT.getVectorNumElements());
8255 ShOp = WidenVectorOp(ShOp, NewShVT);
8256 assert(ShOp.getValueType() == NewShVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008257 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, ShOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008258 break;
8259 }
Mon P Wangfb13f002008-12-02 07:35:08 +00008260
Mon P Wang0c397192008-10-30 08:01:45 +00008261 case ISD::EXTRACT_VECTOR_ELT: {
8262 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8263 assert(Tmp1.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008264 Result = DAG.getNode(Node->getOpcode(), dl, EVT, Tmp1, Node->getOperand(1));
Mon P Wang0c397192008-10-30 08:01:45 +00008265 break;
8266 }
8267 case ISD::CONCAT_VECTORS: {
8268 // We concurrently support only widen on a multiple of the incoming vector.
8269 // We could widen on a multiple of the incoming operand if necessary.
8270 unsigned NumConcat = NewNumElts / NumElts;
8271 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Dale Johannesenc6be1102009-02-02 22:49:46 +00008272 SDValue UndefVal = DAG.getNode(ISD::UNDEF, dl, VT);
Mon P Wang0c397192008-10-30 08:01:45 +00008273 SmallVector<SDValue, 8> MOps;
8274 MOps.push_back(Op);
8275 for (unsigned i = 1; i != NumConcat; ++i) {
8276 MOps.push_back(UndefVal);
8277 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00008278 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
Mon P Wang0c397192008-10-30 08:01:45 +00008279 &MOps[0], MOps.size()));
8280 break;
8281 }
8282 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang49292f12008-11-15 06:05:52 +00008283 SDValue Tmp1 = Node->getOperand(0);
8284 SDValue Idx = Node->getOperand(1);
8285 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8286 if (CIdx && CIdx->getZExtValue() == 0) {
8287 // Since we are access the start of the vector, the incoming
8288 // vector type might be the proper.
8289 MVT Tmp1VT = Tmp1.getValueType();
8290 if (Tmp1VT == WidenVT)
8291 return Tmp1;
8292 else {
8293 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8294 if (Tmp1VTNumElts < NewNumElts)
8295 Result = WidenVectorOp(Tmp1, WidenVT);
8296 else
Dale Johannesenc6be1102009-02-02 22:49:46 +00008297 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, WidenVT, Tmp1, Idx);
Mon P Wang49292f12008-11-15 06:05:52 +00008298 }
8299 } else if (NewNumElts % NumElts == 0) {
8300 // Widen the extracted subvector.
8301 unsigned NumConcat = NewNumElts / NumElts;
Dale Johannesenc6be1102009-02-02 22:49:46 +00008302 SDValue UndefVal = DAG.getNode(ISD::UNDEF, dl, VT);
Mon P Wang49292f12008-11-15 06:05:52 +00008303 SmallVector<SDValue, 8> MOps;
8304 MOps.push_back(Op);
8305 for (unsigned i = 1; i != NumConcat; ++i) {
8306 MOps.push_back(UndefVal);
8307 }
Dale Johannesenc6be1102009-02-02 22:49:46 +00008308 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
Mon P Wang49292f12008-11-15 06:05:52 +00008309 &MOps[0], MOps.size()));
8310 } else {
8311 assert(0 && "can not widen extract subvector");
8312 // This could be implemented using insert and build vector but I would
8313 // like to see when this happens.
8314 }
Mon P Wang0c397192008-10-30 08:01:45 +00008315 break;
8316 }
8317
8318 case ISD::SELECT: {
Mon P Wang0c397192008-10-30 08:01:45 +00008319 // Determine new condition widen type and widen
8320 SDValue Cond1 = Node->getOperand(0);
8321 MVT CondVT = Cond1.getValueType();
8322 assert(CondVT.isVector() && "can not widen non vector type");
8323 MVT CondEVT = CondVT.getVectorElementType();
8324 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8325 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8326 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8327
8328 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8329 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8330 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008331 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008332 break;
8333 }
8334
8335 case ISD::SELECT_CC: {
Mon P Wang0c397192008-10-30 08:01:45 +00008336 // Determine new condition widen type and widen
8337 SDValue Cond1 = Node->getOperand(0);
8338 SDValue Cond2 = Node->getOperand(1);
8339 MVT CondVT = Cond1.getValueType();
8340 assert(CondVT.isVector() && "can not widen non vector type");
8341 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8342 MVT CondEVT = CondVT.getVectorElementType();
8343 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8344 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8345 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8346 assert(Cond1.getValueType() == CondWidenVT &&
8347 Cond2.getValueType() == CondWidenVT && "condition not widen");
8348
8349 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8350 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8351 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8352 "operands not widen");
Dale Johannesenc6be1102009-02-02 22:49:46 +00008353 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Cond1, Cond2, Tmp1,
Mon P Wang0c397192008-10-30 08:01:45 +00008354 Tmp2, Node->getOperand(4));
Mon P Wang0c397192008-10-30 08:01:45 +00008355 break;
Mon P Wang2eb13c32008-10-30 18:21:52 +00008356 }
8357 case ISD::VSETCC: {
8358 // Determine widen for the operand
8359 SDValue Tmp1 = Node->getOperand(0);
8360 MVT TmpVT = Tmp1.getValueType();
8361 assert(TmpVT.isVector() && "can not widen non vector type");
8362 MVT TmpEVT = TmpVT.getVectorElementType();
8363 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8364 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8365 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008366 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, Tmp2,
Mon P Wang2eb13c32008-10-30 18:21:52 +00008367 Node->getOperand(2));
Mon P Wang0c397192008-10-30 08:01:45 +00008368 break;
8369 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008370 case ISD::ATOMIC_CMP_SWAP:
8371 case ISD::ATOMIC_LOAD_ADD:
8372 case ISD::ATOMIC_LOAD_SUB:
8373 case ISD::ATOMIC_LOAD_AND:
8374 case ISD::ATOMIC_LOAD_OR:
8375 case ISD::ATOMIC_LOAD_XOR:
8376 case ISD::ATOMIC_LOAD_NAND:
8377 case ISD::ATOMIC_LOAD_MIN:
8378 case ISD::ATOMIC_LOAD_MAX:
8379 case ISD::ATOMIC_LOAD_UMIN:
8380 case ISD::ATOMIC_LOAD_UMAX:
8381 case ISD::ATOMIC_SWAP: {
Mon P Wang0c397192008-10-30 08:01:45 +00008382 // For now, we assume that using vectors for these operations don't make
8383 // much sense so we just split it. We return an empty result
8384 SDValue X, Y;
8385 SplitVectorOp(Op, X, Y);
8386 return Result;
8387 break;
8388 }
8389
8390 } // end switch (Node->getOpcode())
8391
8392 assert(Result.getNode() && "Didn't set a result!");
8393 if (Result != Op)
8394 Result = LegalizeOp(Result);
8395
Mon P Wangf007a8b2008-11-06 05:31:54 +00008396 AddWidenedOperand(Op, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00008397 return Result;
8398}
8399
8400// Utility function to find a legal vector type and its associated element
8401// type from a preferred width and whose vector type must be the same size
8402// as the VVT.
8403// TLI: Target lowering used to determine legal types
8404// Width: Preferred width of element type
8405// VVT: Vector value type whose size we must match.
8406// Returns VecEVT and EVT - the vector type and its associated element type
Dan Gohman0d137d72009-01-15 16:43:02 +00008407static void FindWidenVecType(const TargetLowering &TLI, unsigned Width, MVT VVT,
Mon P Wang0c397192008-10-30 08:01:45 +00008408 MVT& EVT, MVT& VecEVT) {
8409 // We start with the preferred width, make it a power of 2 and see if
8410 // we can find a vector type of that width. If not, we reduce it by
8411 // another power of 2. If we have widen the type, a vector of bytes should
8412 // always be legal.
8413 assert(TLI.isTypeLegal(VVT));
8414 unsigned EWidth = Width + 1;
8415 do {
8416 assert(EWidth > 0);
8417 EWidth = (1 << Log2_32(EWidth-1));
8418 EVT = MVT::getIntegerVT(EWidth);
8419 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8420 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8421 } while (!TLI.isTypeLegal(VecEVT) ||
8422 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8423}
8424
8425SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8426 SDValue Chain,
8427 SDValue BasePtr,
8428 const Value *SV,
8429 int SVOffset,
8430 unsigned Alignment,
8431 bool isVolatile,
8432 unsigned LdWidth,
Dale Johannesenc6be1102009-02-02 22:49:46 +00008433 MVT ResType,
8434 DebugLoc dl) {
Mon P Wang0c397192008-10-30 08:01:45 +00008435 // We assume that we have good rules to handle loading power of two loads so
8436 // we break down the operations to power of 2 loads. The strategy is to
8437 // load the largest power of 2 that we can easily transform to a legal vector
8438 // and then insert into that vector, and the cast the result into the legal
8439 // vector that we want. This avoids unnecessary stack converts.
8440 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8441 // the load is nonvolatile, we an use a wider load for the value.
8442 // Find a vector length we can load a large chunk
8443 MVT EVT, VecEVT;
8444 unsigned EVTWidth;
8445 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8446 EVTWidth = EVT.getSizeInBits();
8447
Dale Johannesenc6be1102009-02-02 22:49:46 +00008448 SDValue LdOp = DAG.getLoad(EVT, dl, Chain, BasePtr, SV, SVOffset,
Mon P Wang0c397192008-10-30 08:01:45 +00008449 isVolatile, Alignment);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008450 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecEVT, LdOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008451 LdChain.push_back(LdOp.getValue(1));
8452
8453 // Check if we can load the element with one instruction
8454 if (LdWidth == EVTWidth) {
Dale Johannesenc6be1102009-02-02 22:49:46 +00008455 return DAG.getNode(ISD::BIT_CONVERT, dl, ResType, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008456 }
8457
8458 // The vector element order is endianness dependent.
8459 unsigned Idx = 1;
8460 LdWidth -= EVTWidth;
8461 unsigned Offset = 0;
8462
8463 while (LdWidth > 0) {
8464 unsigned Increment = EVTWidth / 8;
8465 Offset += Increment;
Dale Johannesenc6be1102009-02-02 22:49:46 +00008466 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Mon P Wang0c397192008-10-30 08:01:45 +00008467 DAG.getIntPtrConstant(Increment));
8468
8469 if (LdWidth < EVTWidth) {
8470 // Our current type we are using is too large, use a smaller size by
8471 // using a smaller power of 2
8472 unsigned oEVTWidth = EVTWidth;
8473 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8474 EVTWidth = EVT.getSizeInBits();
8475 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008476 Idx = Idx * (oEVTWidth/EVTWidth);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008477 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008478 }
8479
Dale Johannesenc6be1102009-02-02 22:49:46 +00008480 SDValue LdOp = DAG.getLoad(EVT, dl, Chain, BasePtr, SV,
Mon P Wang0c397192008-10-30 08:01:45 +00008481 SVOffset+Offset, isVolatile,
8482 MinAlign(Alignment, Offset));
8483 LdChain.push_back(LdOp.getValue(1));
Dale Johannesenc6be1102009-02-02 22:49:46 +00008484 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecEVT, VecOp, LdOp,
Mon P Wang0c397192008-10-30 08:01:45 +00008485 DAG.getIntPtrConstant(Idx++));
8486
8487 LdWidth -= EVTWidth;
8488 }
8489
Dale Johannesenc6be1102009-02-02 22:49:46 +00008490 return DAG.getNode(ISD::BIT_CONVERT, dl, ResType, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008491}
8492
8493bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8494 SDValue& TFOp,
8495 SDValue Op,
8496 MVT NVT) {
8497 // TODO: Add support for ConcatVec and the ability to load many vector
8498 // types (e.g., v4i8). This will not work when a vector register
8499 // to memory mapping is strange (e.g., vector elements are not
8500 // stored in some sequential order).
8501
8502 // It must be true that the widen vector type is bigger than where
8503 // we need to load from.
8504 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8505 MVT LdVT = LD->getMemoryVT();
Dale Johannesenc6be1102009-02-02 22:49:46 +00008506 DebugLoc dl = LD->getDebugLoc();
Mon P Wang0c397192008-10-30 08:01:45 +00008507 assert(LdVT.isVector() && NVT.isVector());
8508 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8509
8510 // Load information
8511 SDValue Chain = LD->getChain();
8512 SDValue BasePtr = LD->getBasePtr();
8513 int SVOffset = LD->getSrcValueOffset();
8514 unsigned Alignment = LD->getAlignment();
8515 bool isVolatile = LD->isVolatile();
8516 const Value *SV = LD->getSrcValue();
8517 unsigned int LdWidth = LdVT.getSizeInBits();
8518
8519 // Load value as a large register
8520 SDValueVector LdChain;
8521 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
Dale Johannesenc6be1102009-02-02 22:49:46 +00008522 Alignment, isVolatile, LdWidth, NVT, dl);
Mon P Wang0c397192008-10-30 08:01:45 +00008523
8524 if (LdChain.size() == 1) {
8525 TFOp = LdChain[0];
8526 return true;
8527 }
8528 else {
Dale Johannesenc6be1102009-02-02 22:49:46 +00008529 TFOp=DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8530 &LdChain[0], LdChain.size());
Mon P Wang0c397192008-10-30 08:01:45 +00008531 return false;
8532 }
8533}
8534
8535
8536void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8537 SDValue Chain,
8538 SDValue BasePtr,
8539 const Value *SV,
8540 int SVOffset,
8541 unsigned Alignment,
8542 bool isVolatile,
Mon P Wang49292f12008-11-15 06:05:52 +00008543 SDValue ValOp,
Dale Johannesenc6be1102009-02-02 22:49:46 +00008544 unsigned StWidth,
8545 DebugLoc dl) {
Mon P Wang0c397192008-10-30 08:01:45 +00008546 // Breaks the stores into a series of power of 2 width stores. For any
8547 // width, we convert the vector to the vector of element size that we
8548 // want to store. This avoids requiring a stack convert.
8549
8550 // Find a width of the element type we can store with
8551 MVT VVT = ValOp.getValueType();
8552 MVT EVT, VecEVT;
8553 unsigned EVTWidth;
8554 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8555 EVTWidth = EVT.getSizeInBits();
8556
Dale Johannesenc6be1102009-02-02 22:49:46 +00008557 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, ValOp);
8558 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT, VecOp,
Mon P Wange0b436a2008-11-06 22:52:21 +00008559 DAG.getIntPtrConstant(0));
Dale Johannesenc6be1102009-02-02 22:49:46 +00008560 SDValue StOp = DAG.getStore(Chain, dl, EOp, BasePtr, SV, SVOffset,
Mon P Wang0c397192008-10-30 08:01:45 +00008561 isVolatile, Alignment);
8562 StChain.push_back(StOp);
8563
8564 // Check if we are done
8565 if (StWidth == EVTWidth) {
8566 return;
8567 }
8568
8569 unsigned Idx = 1;
8570 StWidth -= EVTWidth;
8571 unsigned Offset = 0;
8572
8573 while (StWidth > 0) {
8574 unsigned Increment = EVTWidth / 8;
8575 Offset += Increment;
Dale Johannesenc6be1102009-02-02 22:49:46 +00008576 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Mon P Wang0c397192008-10-30 08:01:45 +00008577 DAG.getIntPtrConstant(Increment));
8578
8579 if (StWidth < EVTWidth) {
8580 // Our current type we are using is too large, use a smaller size by
8581 // using a smaller power of 2
8582 unsigned oEVTWidth = EVTWidth;
8583 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8584 EVTWidth = EVT.getSizeInBits();
8585 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008586 Idx = Idx * (oEVTWidth/EVTWidth);
Dale Johannesenc6be1102009-02-02 22:49:46 +00008587 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, VecOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008588 }
8589
Dale Johannesenc6be1102009-02-02 22:49:46 +00008590 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT, VecOp,
Mon P Wang49292f12008-11-15 06:05:52 +00008591 DAG.getIntPtrConstant(Idx++));
Dale Johannesenc6be1102009-02-02 22:49:46 +00008592 StChain.push_back(DAG.getStore(Chain, dl, EOp, BasePtr, SV,
Mon P Wang0c397192008-10-30 08:01:45 +00008593 SVOffset + Offset, isVolatile,
8594 MinAlign(Alignment, Offset)));
8595 StWidth -= EVTWidth;
8596 }
8597}
8598
8599
8600SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8601 SDValue Chain,
8602 SDValue BasePtr) {
8603 // TODO: It might be cleaner if we can use SplitVector and have more legal
8604 // vector types that can be stored into memory (e.g., v4xi8 can
8605 // be stored as a word). This will not work when a vector register
8606 // to memory mapping is strange (e.g., vector elements are not
8607 // stored in some sequential order).
8608
8609 MVT StVT = ST->getMemoryVT();
8610 SDValue ValOp = ST->getValue();
Dale Johannesenc6be1102009-02-02 22:49:46 +00008611 DebugLoc dl = ST->getDebugLoc();
Mon P Wang0c397192008-10-30 08:01:45 +00008612
8613 // Check if we have widen this node with another value
8614 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8615 if (I != WidenNodes.end())
8616 ValOp = I->second;
8617
8618 MVT VVT = ValOp.getValueType();
8619
8620 // It must be true that we the widen vector type is bigger than where
8621 // we need to store.
8622 assert(StVT.isVector() && VVT.isVector());
Dan Gohmanf83c81a2009-01-28 03:10:52 +00008623 assert(StVT.bitsLT(VVT));
Mon P Wang0c397192008-10-30 08:01:45 +00008624 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8625
8626 // Store value
8627 SDValueVector StChain;
8628 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8629 ST->getSrcValueOffset(), ST->getAlignment(),
Dale Johannesenc6be1102009-02-02 22:49:46 +00008630 ST->isVolatile(), ValOp, StVT.getSizeInBits(), dl);
Mon P Wang0c397192008-10-30 08:01:45 +00008631 if (StChain.size() == 1)
8632 return StChain[0];
8633 else
Dale Johannesenc6be1102009-02-02 22:49:46 +00008634 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8635 &StChain[0], StChain.size());
Mon P Wang0c397192008-10-30 08:01:45 +00008636}
8637
8638
Chris Lattner3e928bb2005-01-07 07:47:09 +00008639// SelectionDAG::Legalize - This is the entry point for the file.
8640//
Duncan Sandsb6862bb2008-12-14 09:43:15 +00008641void SelectionDAG::Legalize(bool TypesNeedLegalizing) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00008642 /// run - This is the main entry point to this class.
8643 ///
Duncan Sandsb6862bb2008-12-14 09:43:15 +00008644 SelectionDAGLegalize(*this, TypesNeedLegalizing).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00008645}
8646