blob: 5b8d18ce21418e3fe0a5458eb2c3fb35fc0779da [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patel83489bb2009-01-13 00:35:13 +000019#include "llvm/CodeGen/DwarfWriter.h"
20#include "llvm/Analysis/DebugInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000021#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng61bbbab2007-08-16 23:50:06 +000022#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000023#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000024#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000026#include "llvm/Target/TargetOptions.h"
Dan Gohman707e0182008-04-12 04:36:06 +000027#include "llvm/Target/TargetSubtarget.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000028#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000029#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000030#include "llvm/DerivedTypes.h"
Devang Patel83489bb2009-01-13 00:35:13 +000031#include "llvm/GlobalVariable.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000032#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000033#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000034#include "llvm/Support/MathExtras.h"
Chris Lattner79715142007-02-03 01:12:36 +000035#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000036#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000037#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000038#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000039using namespace llvm;
40
41//===----------------------------------------------------------------------===//
42/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
43/// hacks on it until the target machine can handle it. This involves
44/// eliminating value sizes the machine cannot handle (promoting small sizes to
45/// large sizes or splitting up large values into small values) as well as
46/// eliminating operations the machine cannot handle.
47///
48/// This code also does a small amount of optimization and recognition of idioms
49/// as part of its processing. For example, if a target does not support a
50/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
51/// will attempt merge setcc and brc instructions into brcc's.
52///
53namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000054class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000055 TargetLowering &TLI;
56 SelectionDAG &DAG;
Duncan Sandsb6862bb2008-12-14 09:43:15 +000057 bool TypesNeedLegalizing;
Chris Lattner3e928bb2005-01-07 07:47:09 +000058
Chris Lattner6831a812006-02-13 09:18:02 +000059 // Libcall insertion helpers.
60
61 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
62 /// legalized. We use this to ensure that calls are properly serialized
63 /// against each other, including inserted libcalls.
Dan Gohman475871a2008-07-27 21:46:04 +000064 SDValue LastCALLSEQ_END;
Chris Lattner6831a812006-02-13 09:18:02 +000065
66 /// IsLegalizingCall - This member is used *only* for purposes of providing
67 /// helpful assertions that a libcall isn't created while another call is
68 /// being legalized (which could lead to non-serialized call sequences).
69 bool IsLegalizingCall;
70
Chris Lattner3e928bb2005-01-07 07:47:09 +000071 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000072 Legal, // The target natively supports this operation.
73 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000074 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000075 };
Chris Lattner6831a812006-02-13 09:18:02 +000076
Chris Lattner3e928bb2005-01-07 07:47:09 +000077 /// ValueTypeActions - This is a bitvector that contains two bits for each
78 /// value type, where the two bits correspond to the LegalizeAction enum.
79 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000080 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000081
Chris Lattner3e928bb2005-01-07 07:47:09 +000082 /// LegalizedNodes - For nodes that are of legal width, and that have more
83 /// than one use, this map indicates what regularized operand to use. This
84 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000085 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000086
Chris Lattner03c85462005-01-15 05:21:40 +000087 /// PromotedNodes - For nodes that are below legal width, and that have more
88 /// than one use, this map indicates what promoted value to use. This allows
89 /// us to avoid promoting the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000090 DenseMap<SDValue, SDValue> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000091
Chris Lattnerc7029802006-03-18 01:44:44 +000092 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +000093 /// which operands are the expanded version of the input. This allows
Chris Lattnerc7029802006-03-18 01:44:44 +000094 /// us to avoid expanding the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000095 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000096
Chris Lattnerc7029802006-03-18 01:44:44 +000097 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +000098 /// which operands are the split version of the input. This allows us
Chris Lattnerc7029802006-03-18 01:44:44 +000099 /// to avoid splitting the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +0000100 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000101
Dan Gohman7f321562007-06-25 16:23:39 +0000102 /// ScalarizedNodes - For nodes that need to be converted from vector types to
103 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000104 /// processed to the result.
Dan Gohman475871a2008-07-27 21:46:04 +0000105 std::map<SDValue, SDValue> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000106
Mon P Wangf007a8b2008-11-06 05:31:54 +0000107 /// WidenNodes - For nodes that need to be widened from one vector type to
108 /// another, this contains the mapping of those that we have already widen.
109 /// This allows us to avoid widening more than once.
Mon P Wang0c397192008-10-30 08:01:45 +0000110 std::map<SDValue, SDValue> WidenNodes;
111
Dan Gohman475871a2008-07-27 21:46:04 +0000112 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000113 LegalizedNodes.insert(std::make_pair(From, To));
114 // If someone requests legalization of the new node, return itself.
115 if (From != To)
116 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000117 }
Dan Gohman475871a2008-07-27 21:46:04 +0000118 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman6b345ee2008-07-07 17:46:23 +0000119 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Chris Lattner03c85462005-01-15 05:21:40 +0000120 assert(isNew && "Got into the map somehow?");
Evan Cheng24ac4082008-11-24 07:09:49 +0000121 isNew = isNew;
Chris Lattner69a889e2005-12-20 00:53:54 +0000122 // If someone requests legalization of the new node, return itself.
123 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000124 }
Mon P Wangf007a8b2008-11-06 05:31:54 +0000125 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang0c397192008-10-30 08:01:45 +0000126 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
127 assert(isNew && "Got into the map somehow?");
Evan Cheng24ac4082008-11-24 07:09:49 +0000128 isNew = isNew;
Mon P Wang0c397192008-10-30 08:01:45 +0000129 // If someone requests legalization of the new node, return itself.
130 LegalizedNodes.insert(std::make_pair(To, To));
131 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000132
Chris Lattner3e928bb2005-01-07 07:47:09 +0000133public:
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000134 explicit SelectionDAGLegalize(SelectionDAG &DAG, bool TypesNeedLegalizing);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000135
Chris Lattner3e928bb2005-01-07 07:47:09 +0000136 /// getTypeAction - Return how we should legalize values of this type, either
137 /// it is already legal or we need to expand it into multiple registers of
138 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000139 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000140 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000141 }
142
143 /// isTypeLegal - Return true if this type is legal on this target.
144 ///
Duncan Sands83ec4b62008-06-06 12:08:01 +0000145 bool isTypeLegal(MVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000146 return getTypeAction(VT) == Legal;
147 }
148
Chris Lattner3e928bb2005-01-07 07:47:09 +0000149 void LegalizeDAG();
150
Chris Lattner456a93a2006-01-28 07:39:30 +0000151private:
Dan Gohman7f321562007-06-25 16:23:39 +0000152 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000153 /// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000154 void HandleOp(SDValue Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000155
156 /// LegalizeOp - We know that the specified value has a legal type.
157 /// Recursively ensure that the operands have legal types, then return the
158 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000159 SDValue LegalizeOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000160
Dan Gohman82669522007-10-11 23:57:53 +0000161 /// UnrollVectorOp - We know that the given vector has a legal type, however
162 /// the operation it performs is not legal and is an operation that we have
163 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
164 /// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000165 SDValue UnrollVectorOp(SDValue O);
Nate Begeman68679912008-04-25 18:07:40 +0000166
167 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
168 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
169 /// is necessary to spill the vector being inserted into to memory, perform
170 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000171 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
172 SDValue Idx);
Dan Gohman82669522007-10-11 23:57:53 +0000173
Chris Lattnerc7029802006-03-18 01:44:44 +0000174 /// PromoteOp - Given an operation that produces a value in an invalid type,
175 /// promote it to compute the value into a larger type. The produced value
176 /// will have the correct bits for the low portion of the register, but no
177 /// guarantee is made about the top bits: it may be zero, sign-extended, or
178 /// garbage.
Dan Gohman475871a2008-07-27 21:46:04 +0000179 SDValue PromoteOp(SDValue O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000180
Dan Gohman475871a2008-07-27 21:46:04 +0000181 /// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattnerc7029802006-03-18 01:44:44 +0000182 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman929d3eb2008-10-01 15:07:49 +0000183 /// the LegalizedNodes map is filled in for any results that are not expanded,
Chris Lattnerc7029802006-03-18 01:44:44 +0000184 /// the ExpandedNodes map is filled in for any results that are expanded, and
185 /// the Lo/Hi values are returned. This applies to integer types and Vector
186 /// types.
Dan Gohman475871a2008-07-27 21:46:04 +0000187 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000188
Mon P Wangf007a8b2008-11-06 05:31:54 +0000189 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
190 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
191 /// for the existing elements but no guarantee is made about the new elements
192 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
193 /// when we have an instruction operating on an illegal vector type and we
194 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang0c397192008-10-30 08:01:45 +0000195 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
196
Dan Gohman7f321562007-06-25 16:23:39 +0000197 /// SplitVectorOp - Given an operand of vector type, break it down into
198 /// two smaller values.
Dan Gohman475871a2008-07-27 21:46:04 +0000199 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000200
Dan Gohman89b20c02007-06-27 14:06:22 +0000201 /// ScalarizeVectorOp - Given an operand of single-element vector type
202 /// (e.g. v1f32), convert it into the equivalent operation that returns a
203 /// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +0000204 SDValue ScalarizeVectorOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000205
Mon P Wangf007a8b2008-11-06 05:31:54 +0000206 /// Useful 16 element vector type that is used to pass operands for widening.
Mon P Wang0c397192008-10-30 08:01:45 +0000207 typedef SmallVector<SDValue, 16> SDValueVector;
208
209 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
210 /// the LdChain contains a single load and false if it contains a token
211 /// factor for multiple loads. It takes
212 /// Result: location to return the result
213 /// LdChain: location to return the load chain
214 /// Op: load operation to widen
215 /// NVT: widen vector result type we want for the load
216 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
217 SDValue Op, MVT NVT);
218
219 /// Helper genWidenVectorLoads - Helper function to generate a set of
220 /// loads to load a vector with a resulting wider type. It takes
221 /// LdChain: list of chains for the load we have generated
222 /// Chain: incoming chain for the ld vector
223 /// BasePtr: base pointer to load from
224 /// SV: memory disambiguation source value
225 /// SVOffset: memory disambiugation offset
226 /// Alignment: alignment of the memory
227 /// isVolatile: volatile load
228 /// LdWidth: width of memory that we want to load
229 /// ResType: the wider result result type for the resulting loaded vector
230 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
231 SDValue BasePtr, const Value *SV,
232 int SVOffset, unsigned Alignment,
233 bool isVolatile, unsigned LdWidth,
234 MVT ResType);
235
236 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
237 /// location. It takes
238 /// ST: store node that we want to replace
239 /// Chain: incoming store chain
240 /// BasePtr: base address of where we want to store into
241 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
242 SDValue BasePtr);
243
244 /// Helper genWidenVectorStores - Helper function to generate a set of
245 /// stores to store a widen vector into non widen memory
246 // It takes
247 // StChain: list of chains for the stores we have generated
248 // Chain: incoming chain for the ld vector
249 // BasePtr: base pointer to load from
250 // SV: memory disambiguation source value
251 // SVOffset: memory disambiugation offset
252 // Alignment: alignment of the memory
253 // isVolatile: volatile lod
254 // ValOp: value to store
255 // StWidth: width of memory that we want to store
256 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
257 SDValue BasePtr, const Value *SV,
258 int SVOffset, unsigned Alignment,
259 bool isVolatile, SDValue ValOp,
260 unsigned StWidth);
261
Duncan Sandsd038e042008-07-21 10:20:31 +0000262 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Chris Lattner4352cc92006-04-04 17:23:26 +0000263 /// specified mask and type. Targets can specify exactly which masks they
264 /// support and the code generator is tasked with not creating illegal masks.
265 ///
266 /// Note that this will also return true for shuffles that are promoted to a
267 /// different type.
268 ///
269 /// If this is a legal shuffle, this method returns the (possibly promoted)
270 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman475871a2008-07-27 21:46:04 +0000271 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Chris Lattner4352cc92006-04-04 17:23:26 +0000272
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000273 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000274 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000275
Dan Gohman475871a2008-07-27 21:46:04 +0000276 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Evan Cheng7f042682008-10-15 02:05:31 +0000277 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC);
278 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC) {
279 LegalizeSetCCOperands(LHS, RHS, CC);
280 LegalizeSetCCCondCode(VT, LHS, RHS, CC);
281 }
Nate Begeman750ac1b2006-02-01 07:19:44 +0000282
Dan Gohman475871a2008-07-27 21:46:04 +0000283 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
284 SDValue &Hi);
285 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000286
Dan Gohman475871a2008-07-27 21:46:04 +0000287 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
288 SDValue ExpandBUILD_VECTOR(SDNode *Node);
289 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dan Gohman7f8613e2008-08-14 20:04:46 +0000290 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000291 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
292 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
293 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000294
Dan Gohman475871a2008-07-27 21:46:04 +0000295 SDValue ExpandBSWAP(SDValue Op);
296 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
297 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
298 SDValue &Lo, SDValue &Hi);
299 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
300 SDValue &Lo, SDValue &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000301
Dan Gohman475871a2008-07-27 21:46:04 +0000302 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
303 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Mon P Wange9f10152008-12-09 05:46:39 +0000304
305 // Returns the legalized (truncated or extended) shift amount.
306 SDValue LegalizeShiftAmount(SDValue ShiftAmt);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000307};
308}
309
Chris Lattner4352cc92006-04-04 17:23:26 +0000310/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
311/// specified mask and type. Targets can specify exactly which masks they
312/// support and the code generator is tasked with not creating illegal masks.
313///
314/// Note that this will also return true for shuffles that are promoted to a
315/// different type.
Dan Gohman475871a2008-07-27 21:46:04 +0000316SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Chris Lattner4352cc92006-04-04 17:23:26 +0000317 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
318 default: return 0;
319 case TargetLowering::Legal:
320 case TargetLowering::Custom:
321 break;
322 case TargetLowering::Promote: {
323 // If this is promoted to a different type, convert the shuffle mask and
324 // ask if it is legal in the promoted type!
Duncan Sands83ec4b62008-06-06 12:08:01 +0000325 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd038e042008-07-21 10:20:31 +0000326 MVT EltVT = NVT.getVectorElementType();
Chris Lattner4352cc92006-04-04 17:23:26 +0000327
328 // If we changed # elements, change the shuffle mask.
329 unsigned NumEltsGrowth =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000330 NVT.getVectorNumElements() / VT.getVectorNumElements();
Chris Lattner4352cc92006-04-04 17:23:26 +0000331 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
332 if (NumEltsGrowth > 1) {
333 // Renumber the elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000334 SmallVector<SDValue, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000335 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000336 SDValue InOp = Mask.getOperand(i);
Chris Lattner4352cc92006-04-04 17:23:26 +0000337 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
338 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd038e042008-07-21 10:20:31 +0000339 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000340 else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000341 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd038e042008-07-21 10:20:31 +0000342 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000343 }
344 }
345 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000346 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000347 }
348 VT = NVT;
349 break;
350 }
351 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000352 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Chris Lattner4352cc92006-04-04 17:23:26 +0000353}
354
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000355SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag, bool types)
356 : TLI(dag.getTargetLoweringInfo()), DAG(dag), TypesNeedLegalizing(types),
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000357 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000358 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000359 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000360}
361
Chris Lattner3e928bb2005-01-07 07:47:09 +0000362void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000363 LastCALLSEQ_END = DAG.getEntryNode();
364 IsLegalizingCall = false;
365
Chris Lattnerab510a72005-10-02 17:49:46 +0000366 // The legalize process is inherently a bottom-up recursive process (users
367 // legalize their uses before themselves). Given infinite stack space, we
368 // could just start legalizing on the root and traverse the whole graph. In
369 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000370 // blocks. To avoid this problem, compute an ordering of the nodes where each
371 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000372 DAG.AssignTopologicalOrder();
373 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
374 E = prior(DAG.allnodes_end()); I != next(E); ++I)
375 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000376
377 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000378 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000379 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
380 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000381
382 ExpandedNodes.clear();
383 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000384 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000385 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000386 ScalarizedNodes.clear();
Mon P Wang0c397192008-10-30 08:01:45 +0000387 WidenNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000388
389 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000390 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000391}
392
Chris Lattner6831a812006-02-13 09:18:02 +0000393
394/// FindCallEndFromCallStart - Given a chained node that is part of a call
395/// sequence, find the CALLSEQ_END node that terminates the call sequence.
396static SDNode *FindCallEndFromCallStart(SDNode *Node) {
397 if (Node->getOpcode() == ISD::CALLSEQ_END)
398 return Node;
399 if (Node->use_empty())
400 return 0; // No CallSeqEnd
401
402 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000403 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000404 if (TheChain.getValueType() != MVT::Other) {
405 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000406 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000407 if (TheChain.getValueType() != MVT::Other) {
408 // Otherwise, hunt for it.
409 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
410 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000411 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000412 break;
413 }
414
415 // Otherwise, we walked into a node without a chain.
416 if (TheChain.getValueType() != MVT::Other)
417 return 0;
418 }
419 }
420
421 for (SDNode::use_iterator UI = Node->use_begin(),
422 E = Node->use_end(); UI != E; ++UI) {
423
424 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000425 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000426 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
427 if (User->getOperand(i) == TheChain)
428 if (SDNode *Result = FindCallEndFromCallStart(User))
429 return Result;
430 }
431 return 0;
432}
433
434/// FindCallStartFromCallEnd - Given a chained node that is part of a call
435/// sequence, find the CALLSEQ_START node that initiates the call sequence.
436static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
437 assert(Node && "Didn't find callseq_start for a call??");
438 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
439
440 assert(Node->getOperand(0).getValueType() == MVT::Other &&
441 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000442 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000443}
444
445/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
446/// see if any uses can reach Dest. If no dest operands can get to dest,
447/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000448///
449/// Keep track of the nodes we fine that actually do lead to Dest in
450/// NodesLeadingTo. This avoids retraversing them exponential number of times.
451///
452bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000453 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000454 if (N == Dest) return true; // N certainly leads to Dest :)
455
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000456 // If we've already processed this node and it does lead to Dest, there is no
457 // need to reprocess it.
458 if (NodesLeadingTo.count(N)) return true;
459
Chris Lattner6831a812006-02-13 09:18:02 +0000460 // If the first result of this node has been already legalized, then it cannot
461 // reach N.
462 switch (getTypeAction(N->getValueType(0))) {
463 case Legal:
Dan Gohman475871a2008-07-27 21:46:04 +0000464 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000465 break;
466 case Promote:
Dan Gohman475871a2008-07-27 21:46:04 +0000467 if (PromotedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000468 break;
469 case Expand:
Dan Gohman475871a2008-07-27 21:46:04 +0000470 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000471 break;
472 }
473
474 // Okay, this node has not already been legalized. Check and legalize all
475 // operands. If none lead to Dest, then we can legalize this node.
476 bool OperandsLeadToDest = false;
477 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
478 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000479 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000480
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000481 if (OperandsLeadToDest) {
482 NodesLeadingTo.insert(N);
483 return true;
484 }
Chris Lattner6831a812006-02-13 09:18:02 +0000485
486 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000487 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000488 return false;
489}
490
Mon P Wang0c397192008-10-30 08:01:45 +0000491/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000492/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000493void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000494 MVT VT = Op.getValueType();
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000495 // If the type legalizer was run then we should never see any illegal result
496 // types here except for target constants (the type legalizer does not touch
Mon P Wang87c8a8f2008-12-18 20:03:17 +0000497 // those) or for build vector used as a mask for a vector shuffle.
498 // FIXME: We can removed the BUILD_VECTOR case when we fix PR2957.
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000499 assert((TypesNeedLegalizing || getTypeAction(VT) == Legal ||
Mon P Wang87c8a8f2008-12-18 20:03:17 +0000500 Op.getOpcode() == ISD::TargetConstant ||
501 Op.getOpcode() == ISD::BUILD_VECTOR) &&
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000502 "Illegal type introduced after type legalization?");
Dan Gohman7f321562007-06-25 16:23:39 +0000503 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000504 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000505 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang0c397192008-10-30 08:01:45 +0000506 case Promote:
507 if (!VT.isVector()) {
508 (void)PromoteOp(Op);
509 break;
510 }
511 else {
512 // See if we can widen otherwise use Expand to either scalarize or split
513 MVT WidenVT = TLI.getWidenVectorType(VT);
514 if (WidenVT != MVT::Other) {
515 (void) WidenVectorOp(Op, WidenVT);
516 break;
517 }
518 // else fall thru to expand since we can't widen the vector
519 }
Chris Lattnerc7029802006-03-18 01:44:44 +0000520 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000521 if (!VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000522 // If this is an illegal scalar, expand it into its two component
523 // pieces.
Dan Gohman475871a2008-07-27 21:46:04 +0000524 SDValue X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000525 if (Op.getOpcode() == ISD::TargetConstant)
526 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000527 ExpandOp(Op, X, Y);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000528 } else if (VT.getVectorNumElements() == 1) {
Dan Gohman7f321562007-06-25 16:23:39 +0000529 // If this is an illegal single element vector, convert it to a
530 // scalar operation.
531 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000532 } else {
Mon P Wang0c397192008-10-30 08:01:45 +0000533 // This is an illegal multiple element vector.
Dan Gohman7f321562007-06-25 16:23:39 +0000534 // Split it in half and legalize both parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000535 SDValue X, Y;
Dan Gohman7f321562007-06-25 16:23:39 +0000536 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000537 }
538 break;
539 }
540}
Chris Lattner6831a812006-02-13 09:18:02 +0000541
Evan Cheng9f877882006-12-13 20:57:08 +0000542/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
543/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000544static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000545 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000546 bool Extend = false;
547
548 // If a FP immediate is precise when represented as a float and if the
549 // target can do an extending load from float to double, we put it into
550 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000551 // double. This shrinks FP constants and canonicalizes them for targets where
552 // an FP extending load is the same cost as a normal load (such as on the x87
553 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000554 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000555 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000556 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000557 if (VT!=MVT::f64 && VT!=MVT::f32)
558 assert(0 && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000559 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000560 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000561 }
562
Duncan Sands83ec4b62008-06-06 12:08:01 +0000563 MVT OrigVT = VT;
564 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000565 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000566 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000567 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
568 // Only do this if the target has a native EXTLOAD instruction from
569 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000570 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000571 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000572 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000573 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
574 VT = SVT;
575 Extend = true;
576 }
Evan Cheng00495212006-12-12 21:32:44 +0000577 }
578
Dan Gohman475871a2008-07-27 21:46:04 +0000579 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +0000580 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000581 if (Extend)
582 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000583 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000584 0, VT, false, Alignment);
Evan Chengef120572008-03-04 08:05:30 +0000585 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000586 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000587}
588
Chris Lattner6831a812006-02-13 09:18:02 +0000589
Evan Cheng912095b2007-01-04 21:56:39 +0000590/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
591/// operations.
592static
Dan Gohman475871a2008-07-27 21:46:04 +0000593SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
Dan Gohman0d137d72009-01-15 16:43:02 +0000594 SelectionDAG &DAG,
595 const TargetLowering &TLI) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000596 MVT VT = Node->getValueType(0);
597 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000598 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
599 "fcopysign expansion only supported for f32 and f64");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000600 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000601
Evan Cheng912095b2007-01-04 21:56:39 +0000602 // First get the sign bit of second operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000603 SDValue Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000604 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
605 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000606 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman475871a2008-07-27 21:46:04 +0000607 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000608 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000609 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000610 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Evan Cheng912095b2007-01-04 21:56:39 +0000611 if (SizeDiff > 0) {
612 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
613 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
614 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnerc563e1d2008-07-10 23:46:13 +0000615 } else if (SizeDiff < 0) {
616 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
617 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
618 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
619 }
Evan Cheng068c5f42007-01-05 21:31:51 +0000620
621 // Clear the sign bit of first operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000622 SDValue Mask2 = (VT == MVT::f64)
Evan Cheng068c5f42007-01-05 21:31:51 +0000623 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
624 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
625 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman475871a2008-07-27 21:46:04 +0000626 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000627 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
628
629 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000630 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
631 return Result;
632}
633
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000634/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
635static
Dan Gohman475871a2008-07-27 21:46:04 +0000636SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000637 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000638 SDValue Chain = ST->getChain();
639 SDValue Ptr = ST->getBasePtr();
640 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000641 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000642 int Alignment = ST->getAlignment();
643 int SVOffset = ST->getSrcValueOffset();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000644 if (ST->getMemoryVT().isFloatingPoint() ||
645 ST->getMemoryVT().isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000646 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
647 if (TLI.isTypeLegal(intVT)) {
648 // Expand to a bitconvert of the value to the integer type of the
649 // same size, then a (misaligned) int store.
650 // FIXME: Does not handle truncating floating point stores!
651 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
652 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
653 SVOffset, ST->isVolatile(), Alignment);
654 } else {
655 // Do a (aligned) store to a stack slot, then copy from the stack slot
656 // to the final destination using (unaligned) integer loads and stores.
657 MVT StoredVT = ST->getMemoryVT();
658 MVT RegVT =
659 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
660 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
661 unsigned RegBytes = RegVT.getSizeInBits() / 8;
662 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000663
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000664 // Make sure the stack slot is also aligned for the register type.
665 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
666
667 // Perform the original store, only redirected to the stack slot.
Duncan Sands05e11fa2008-12-12 21:47:02 +0000668 SDValue Store = DAG.getTruncStore(Chain, Val, StackPtr, NULL, 0,StoredVT);
669 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
670 SmallVector<SDValue, 8> Stores;
671 unsigned Offset = 0;
672
673 // Do all but one copies using the full register width.
674 for (unsigned i = 1; i < NumRegs; i++) {
675 // Load one integer register's worth from the stack slot.
676 SDValue Load = DAG.getLoad(RegVT, Store, StackPtr, NULL, 0);
677 // Store it to the final location. Remember the store.
678 Stores.push_back(DAG.getStore(Load.getValue(1), Load, Ptr,
679 ST->getSrcValue(), SVOffset + Offset,
680 ST->isVolatile(),
681 MinAlign(ST->getAlignment(), Offset)));
682 // Increment the pointers.
683 Offset += RegBytes;
684 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
685 Increment);
686 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
687 }
688
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000689 // The last store may be partial. Do a truncating store. On big-endian
690 // machines this requires an extending load from the stack slot to ensure
691 // that the bits are in the right place.
692 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000693
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000694 // Load from the stack slot.
695 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Store, StackPtr,
696 NULL, 0, MemVT);
697
Duncan Sands05e11fa2008-12-12 21:47:02 +0000698 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, Ptr,
699 ST->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000700 MemVT, ST->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000701 MinAlign(ST->getAlignment(), Offset)));
702 // The order of the stores doesn't matter - say it with a TokenFactor.
703 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
704 Stores.size());
705 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000706 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000707 assert(ST->getMemoryVT().isInteger() &&
708 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000709 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000710 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000711 MVT NewStoredVT =
712 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
713 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000714 int IncrementSize = NumBits / 8;
715
716 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000717 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
718 SDValue Lo = Val;
719 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000720
721 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000722 SDValue Store1, Store2;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000723 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
724 ST->getSrcValue(), SVOffset, NewStoredVT,
725 ST->isVolatile(), Alignment);
726 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
727 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000728 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000729 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
730 ST->getSrcValue(), SVOffset + IncrementSize,
731 NewStoredVT, ST->isVolatile(), Alignment);
732
733 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
734}
735
736/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
737static
Dan Gohman475871a2008-07-27 21:46:04 +0000738SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000739 const TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000740 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000741 SDValue Chain = LD->getChain();
742 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000743 MVT VT = LD->getValueType(0);
744 MVT LoadedVT = LD->getMemoryVT();
745 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000746 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
747 if (TLI.isTypeLegal(intVT)) {
748 // Expand to a (misaligned) integer load of the same size,
749 // then bitconvert to floating point or vector.
750 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
751 SVOffset, LD->isVolatile(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000752 LD->getAlignment());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000753 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
754 if (VT.isFloatingPoint() && LoadedVT != VT)
755 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000756
Duncan Sands05e11fa2008-12-12 21:47:02 +0000757 SDValue Ops[] = { Result, Chain };
758 return DAG.getMergeValues(Ops, 2);
759 } else {
760 // Copy the value to a (aligned) stack slot using (unaligned) integer
761 // loads and stores, then do a (aligned) load from the stack slot.
762 MVT RegVT = TLI.getRegisterType(intVT);
763 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
764 unsigned RegBytes = RegVT.getSizeInBits() / 8;
765 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
766
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000767 // Make sure the stack slot is also aligned for the register type.
768 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
769
Duncan Sands05e11fa2008-12-12 21:47:02 +0000770 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
771 SmallVector<SDValue, 8> Stores;
772 SDValue StackPtr = StackBase;
773 unsigned Offset = 0;
774
775 // Do all but one copies using the full register width.
776 for (unsigned i = 1; i < NumRegs; i++) {
777 // Load one integer register's worth from the original location.
778 SDValue Load = DAG.getLoad(RegVT, Chain, Ptr, LD->getSrcValue(),
779 SVOffset + Offset, LD->isVolatile(),
780 MinAlign(LD->getAlignment(), Offset));
781 // Follow the load with a store to the stack slot. Remember the store.
782 Stores.push_back(DAG.getStore(Load.getValue(1), Load, StackPtr,
783 NULL, 0));
784 // Increment the pointers.
785 Offset += RegBytes;
786 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
787 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
788 Increment);
789 }
790
791 // The last copy may be partial. Do an extending load.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000792 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000793 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Chain, Ptr,
794 LD->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000795 MemVT, LD->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000796 MinAlign(LD->getAlignment(), Offset));
797 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000798 // On big-endian machines this requires a truncating store to ensure
799 // that the bits end up in the right place.
800 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, StackPtr,
801 NULL, 0, MemVT));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000802
803 // The order of the stores doesn't matter - say it with a TokenFactor.
804 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
805 Stores.size());
806
807 // Finally, perform the original load only redirected to the stack slot.
808 Load = DAG.getExtLoad(LD->getExtensionType(), VT, TF, StackBase,
809 NULL, 0, LoadedVT);
810
811 // Callers expect a MERGE_VALUES node.
812 SDValue Ops[] = { Load, TF };
813 return DAG.getMergeValues(Ops, 2);
814 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000815 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000816 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000817 "Unaligned load of unsupported type.");
818
Dale Johannesen8155d642008-02-27 22:36:00 +0000819 // Compute the new VT that is half the size of the old one. This is an
820 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000821 unsigned NumBits = LoadedVT.getSizeInBits();
822 MVT NewLoadedVT;
823 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000824 NumBits >>= 1;
825
826 unsigned Alignment = LD->getAlignment();
827 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000828 ISD::LoadExtType HiExtType = LD->getExtensionType();
829
830 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
831 if (HiExtType == ISD::NON_EXTLOAD)
832 HiExtType = ISD::ZEXTLOAD;
833
834 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000835 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000836 if (TLI.isLittleEndian()) {
837 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
838 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
839 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
840 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
841 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
842 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000843 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000844 } else {
845 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
846 NewLoadedVT,LD->isVolatile(), Alignment);
847 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
848 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
849 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
850 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000851 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000852 }
853
854 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000855 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
856 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000857 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
858
Dan Gohman475871a2008-07-27 21:46:04 +0000859 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000860 Hi.getValue(1));
861
Dan Gohman475871a2008-07-27 21:46:04 +0000862 SDValue Ops[] = { Result, TF };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000863 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000864}
Evan Cheng912095b2007-01-04 21:56:39 +0000865
Dan Gohman82669522007-10-11 23:57:53 +0000866/// UnrollVectorOp - We know that the given vector has a legal type, however
867/// the operation it performs is not legal and is an operation that we have
868/// no way of lowering. "Unroll" the vector, splitting out the scalars and
869/// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000870SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000871 MVT VT = Op.getValueType();
Dan Gohman82669522007-10-11 23:57:53 +0000872 assert(isTypeLegal(VT) &&
873 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000874 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman82669522007-10-11 23:57:53 +0000875 "Can't unroll a vector with multiple results!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000876 unsigned NE = VT.getVectorNumElements();
877 MVT EltVT = VT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000878
Dan Gohman475871a2008-07-27 21:46:04 +0000879 SmallVector<SDValue, 8> Scalars;
880 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman82669522007-10-11 23:57:53 +0000881 for (unsigned i = 0; i != NE; ++i) {
882 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +0000883 SDValue Operand = Op.getOperand(j);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000884 MVT OperandVT = Operand.getValueType();
885 if (OperandVT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +0000886 // A vector operand; extract a single element.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000887 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000888 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
889 OperandEltVT,
890 Operand,
891 DAG.getConstant(i, MVT::i32));
892 } else {
893 // A scalar operand; just use it as is.
894 Operands[j] = Operand;
895 }
896 }
Mon P Wange9f10152008-12-09 05:46:39 +0000897
898 switch (Op.getOpcode()) {
899 default:
900 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
901 &Operands[0], Operands.size()));
902 break;
903 case ISD::SHL:
904 case ISD::SRA:
905 case ISD::SRL:
906 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT, Operands[0],
907 LegalizeShiftAmount(Operands[1])));
908 break;
909 }
Dan Gohman82669522007-10-11 23:57:53 +0000910 }
911
912 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
913}
914
Duncan Sands007f9842008-01-10 10:28:30 +0000915/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000916static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000917 RTLIB::Libcall Call_F32,
918 RTLIB::Libcall Call_F64,
919 RTLIB::Libcall Call_F80,
920 RTLIB::Libcall Call_PPCF128) {
921 return
922 VT == MVT::f32 ? Call_F32 :
923 VT == MVT::f64 ? Call_F64 :
924 VT == MVT::f80 ? Call_F80 :
925 VT == MVT::ppcf128 ? Call_PPCF128 :
926 RTLIB::UNKNOWN_LIBCALL;
927}
928
Nate Begeman68679912008-04-25 18:07:40 +0000929/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
930/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
931/// is necessary to spill the vector being inserted into to memory, perform
932/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000933SDValue SelectionDAGLegalize::
934PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
935 SDValue Tmp1 = Vec;
936 SDValue Tmp2 = Val;
937 SDValue Tmp3 = Idx;
Nate Begeman68679912008-04-25 18:07:40 +0000938
939 // If the target doesn't support this, we have to spill the input vector
940 // to a temporary stack slot, update the element, then reload it. This is
941 // badness. We could also load the value into a vector register (either
942 // with a "move to register" or "extload into register" instruction, then
943 // permute it into place, if the idx is a constant and if the idx is
944 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000945 MVT VT = Tmp1.getValueType();
946 MVT EltVT = VT.getVectorElementType();
947 MVT IdxVT = Tmp3.getValueType();
948 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000949 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000950
Gabor Greifba36cb52008-08-28 21:40:38 +0000951 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000952
953 // Store the vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000954 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000955 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000956
957 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000958 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman68679912008-04-25 18:07:40 +0000959 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
960 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000961 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman68679912008-04-25 18:07:40 +0000962 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman475871a2008-07-27 21:46:04 +0000963 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000964 // Store the scalar value.
965 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000966 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000967 // Load the updated vector.
Dan Gohmana54cf172008-07-11 22:44:52 +0000968 return DAG.getLoad(VT, Ch, StackPtr,
969 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000970}
971
Mon P Wange9f10152008-12-09 05:46:39 +0000972SDValue SelectionDAGLegalize::LegalizeShiftAmount(SDValue ShiftAmt) {
973 if (TLI.getShiftAmountTy().bitsLT(ShiftAmt.getValueType()))
974 return DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), ShiftAmt);
975
976 if (TLI.getShiftAmountTy().bitsGT(ShiftAmt.getValueType()))
Dan Gohman77f7a572009-01-28 02:58:31 +0000977 return DAG.getNode(ISD::ZERO_EXTEND, TLI.getShiftAmountTy(), ShiftAmt);
Mon P Wange9f10152008-12-09 05:46:39 +0000978
979 return ShiftAmt;
980}
981
982
Dan Gohmana3466152007-07-13 20:14:11 +0000983/// LegalizeOp - We know that the specified value has a legal type, and
984/// that its operands are legal. Now ensure that the operation itself
985/// is legal, recursively ensuring that the operands' operations remain
986/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000987SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000988 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
989 return Op;
990
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000991 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000992 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000993 SDNode *Node = Op.getNode();
Chris Lattnere3304a32005-01-08 20:35:13 +0000994
Chris Lattner3e928bb2005-01-07 07:47:09 +0000995 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000996 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000997 if (Node->getNumValues() > 1) {
998 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000999 if (getTypeAction(Node->getValueType(i)) != Legal) {
1000 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001001 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +00001002 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +00001003 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +00001004 }
1005 }
1006
Chris Lattner45982da2005-05-12 16:53:42 +00001007 // Note that LegalizeOp may be reentered even from single-use nodes, which
1008 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00001009 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001010 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001011
Dan Gohman475871a2008-07-27 21:46:04 +00001012 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
1013 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +00001014 bool isCustom = false;
1015
Chris Lattner3e928bb2005-01-07 07:47:09 +00001016 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +00001017 case ISD::FrameIndex:
1018 case ISD::EntryToken:
1019 case ISD::Register:
1020 case ISD::BasicBlock:
1021 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +00001022 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +00001023 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +00001024 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +00001025 case ISD::TargetConstantPool:
1026 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001027 case ISD::TargetGlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001028 case ISD::TargetExternalSymbol:
Chris Lattner948c1b12006-01-28 08:31:04 +00001029 case ISD::VALUETYPE:
1030 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +00001031 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +00001032 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +00001033 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +00001034 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00001035 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +00001036 "This must be legal!");
1037 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001038 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001039 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1040 // If this is a target node, legalize it by legalizing the operands then
1041 // passing it through.
Dan Gohman475871a2008-07-27 21:46:04 +00001042 SmallVector<SDValue, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001043 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001044 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001045
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001046 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001047
1048 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1049 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001050 return Result.getValue(Op.getResNo());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001051 }
1052 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001053#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00001054 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001055#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00001056 assert(0 && "Do not know how to legalize this operator!");
1057 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +00001058 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001059 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001060 case ISD::GlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001061 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +00001062 case ISD::ConstantPool:
1063 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001064 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1065 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +00001066 case TargetLowering::Custom:
1067 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001068 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner948c1b12006-01-28 08:31:04 +00001069 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001070 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001071 break;
1072 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001073 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +00001074 case ISD::FRAMEADDR:
1075 case ISD::RETURNADDR:
1076 // The only option for these nodes is to custom lower them. If the target
1077 // does not custom lower them, then return zero.
1078 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001079 if (Tmp1.getNode())
Nate Begemanbcc5f362007-01-29 22:58:52 +00001080 Result = Tmp1;
1081 else
1082 Result = DAG.getConstant(0, TLI.getPointerTy());
1083 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001084 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001085 MVT VT = Node->getValueType(0);
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001086 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1087 default: assert(0 && "This action is not supported yet!");
1088 case TargetLowering::Custom:
1089 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001090 if (Result.getNode()) break;
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001091 // Fall Thru
1092 case TargetLowering::Legal:
1093 Result = DAG.getConstant(0, VT);
1094 break;
1095 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001096 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001097 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001098 case ISD::EXCEPTIONADDR: {
1099 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001100 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001101 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1102 default: assert(0 && "This action is not supported yet!");
1103 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +00001104 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +00001105 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001106 }
1107 break;
1108 case TargetLowering::Custom:
1109 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001110 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001111 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001112 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001113 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands4bdcb612008-07-02 17:40:58 +00001114 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001115 break;
1116 }
1117 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001118 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001119 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001120
Gabor Greifba36cb52008-08-28 21:40:38 +00001121 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001122 "Cannot return more than two values!");
1123
1124 // Since we produced two values, make sure to remember that we
1125 // legalized both of them.
1126 Tmp1 = LegalizeOp(Result);
1127 Tmp2 = LegalizeOp(Result.getValue(1));
1128 AddLegalizedOperand(Op.getValue(0), Tmp1);
1129 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001130 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +00001131 case ISD::EHSELECTION: {
1132 Tmp1 = LegalizeOp(Node->getOperand(0));
1133 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001134 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +00001135 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1136 default: assert(0 && "This action is not supported yet!");
1137 case TargetLowering::Expand: {
1138 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +00001139 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +00001140 }
1141 break;
1142 case TargetLowering::Custom:
1143 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001144 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +00001145 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001146 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001147 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands4bdcb612008-07-02 17:40:58 +00001148 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +00001149 break;
1150 }
1151 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001152 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001153 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001154
Gabor Greifba36cb52008-08-28 21:40:38 +00001155 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001156 "Cannot return more than two values!");
1157
1158 // Since we produced two values, make sure to remember that we
1159 // legalized both of them.
1160 Tmp1 = LegalizeOp(Result);
1161 Tmp2 = LegalizeOp(Result.getValue(1));
1162 AddLegalizedOperand(Op.getValue(0), Tmp1);
1163 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001164 return Op.getResNo() ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001165 case ISD::EH_RETURN: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001166 MVT VT = Node->getValueType(0);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001167 // The only "good" option for this node is to custom lower it.
1168 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1169 default: assert(0 && "This action is not supported at all!");
1170 case TargetLowering::Custom:
1171 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001172 if (Result.getNode()) break;
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001173 // Fall Thru
1174 case TargetLowering::Legal:
1175 // Target does not know, how to lower this, lower to noop
1176 Result = LegalizeOp(Node->getOperand(0));
1177 break;
1178 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001179 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001180 break;
Chris Lattner08951a32005-09-02 01:15:01 +00001181 case ISD::AssertSext:
1182 case ISD::AssertZext:
1183 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001184 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +00001185 break;
Chris Lattner308575b2005-11-20 22:56:56 +00001186 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001187 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif99a6cb92008-08-26 22:36:50 +00001188 Result = Node->getOperand(Op.getResNo());
Chris Lattner456a93a2006-01-28 07:39:30 +00001189 break;
Chris Lattner69a52152005-01-14 22:38:01 +00001190 case ISD::CopyFromReg:
1191 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001192 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001193 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001194 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +00001195 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001196 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001197 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001198 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001199 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1200 } else {
1201 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1202 }
Chris Lattner7310fb12005-12-18 15:27:43 +00001203 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1204 }
Chris Lattner13c184d2005-01-28 06:27:38 +00001205 // Since CopyFromReg produces two values, make sure to remember that we
1206 // legalized both of them.
1207 AddLegalizedOperand(Op.getValue(0), Result);
1208 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001209 return Result.getValue(Op.getResNo());
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001210 case ISD::UNDEF: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001211 MVT VT = Op.getValueType();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001212 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +00001213 default: assert(0 && "This action is not supported yet!");
1214 case TargetLowering::Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001215 if (VT.isInteger())
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001216 Result = DAG.getConstant(0, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001217 else if (VT.isFloatingPoint())
1218 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf41db212007-09-26 17:26:49 +00001219 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001220 else
1221 assert(0 && "Unknown value type!");
1222 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001223 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001224 break;
1225 }
1226 break;
1227 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001228
Chris Lattner48b61a72006-03-28 00:40:33 +00001229 case ISD::INTRINSIC_W_CHAIN:
1230 case ISD::INTRINSIC_WO_CHAIN:
1231 case ISD::INTRINSIC_VOID: {
Dan Gohman475871a2008-07-27 21:46:04 +00001232 SmallVector<SDValue, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001233 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1234 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001235 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001236
1237 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001238 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001239 TargetLowering::Custom) {
1240 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001241 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001242 }
1243
Gabor Greifba36cb52008-08-28 21:40:38 +00001244 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001245
1246 // Must have return value and chain result.
Gabor Greifba36cb52008-08-28 21:40:38 +00001247 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattner13fc2f12006-03-27 20:28:29 +00001248 "Cannot return more than two values!");
1249
1250 // Since loads produce two values, make sure to remember that we
1251 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001252 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1253 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001254 return Result.getValue(Op.getResNo());
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001255 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001256
Dan Gohman7f460202008-06-30 20:59:49 +00001257 case ISD::DBG_STOPPOINT:
1258 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +00001259 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1260
Dan Gohman7f460202008-06-30 20:59:49 +00001261 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +00001262 case TargetLowering::Promote:
1263 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001264 case TargetLowering::Expand: {
Devang Patel83489bb2009-01-13 00:35:13 +00001265 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf560ffa2009-01-28 17:46:25 +00001266 bool useDEBUG_LOC = TLI.isOperationLegalOrCustom(ISD::DEBUG_LOC,
1267 MVT::Other);
1268 bool useLABEL = TLI.isOperationLegalOrCustom(ISD::DBG_LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001269
Dan Gohman7f460202008-06-30 20:59:49 +00001270 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Devang Patel83489bb2009-01-13 00:35:13 +00001271 GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
1272 if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
1273 DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
1274 unsigned SrcFile = DW->RecordSource(CU.getDirectory(),
1275 CU.getFilename());
1276
Dan Gohman7f460202008-06-30 20:59:49 +00001277 unsigned Line = DSP->getLine();
1278 unsigned Col = DSP->getColumn();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001279
1280 if (useDEBUG_LOC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001281 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Cheng71e86852008-07-08 20:06:39 +00001282 DAG.getConstant(Col, MVT::i32),
1283 DAG.getConstant(SrcFile, MVT::i32) };
1284 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001285 } else {
Devang Patel83489bb2009-01-13 00:35:13 +00001286 unsigned ID = DW->RecordSourceLine(Line, Col, SrcFile);
Dan Gohman44066042008-07-01 00:05:16 +00001287 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001288 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001289 } else {
1290 Result = Tmp1; // chain
1291 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001292 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001293 }
Evan Cheng71e86852008-07-08 20:06:39 +00001294 case TargetLowering::Legal: {
1295 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1296 if (Action == Legal && Tmp1 == Node->getOperand(0))
1297 break;
1298
Dan Gohman475871a2008-07-27 21:46:04 +00001299 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001300 Ops.push_back(Tmp1);
1301 if (Action == Legal) {
1302 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1303 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1304 } else {
1305 // Otherwise promote them.
1306 Ops.push_back(PromoteOp(Node->getOperand(1)));
1307 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner36ce6912005-11-29 06:21:05 +00001308 }
Evan Cheng71e86852008-07-08 20:06:39 +00001309 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1310 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1311 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001312 break;
1313 }
Evan Cheng71e86852008-07-08 20:06:39 +00001314 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001315 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001316
1317 case ISD::DECLARE:
1318 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1319 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1320 default: assert(0 && "This action is not supported yet!");
1321 case TargetLowering::Legal:
1322 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1323 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1324 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1325 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1326 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001327 case TargetLowering::Expand:
1328 Result = LegalizeOp(Node->getOperand(0));
1329 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001330 }
1331 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001332
1333 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001334 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001335 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001336 default: assert(0 && "This action is not supported yet!");
Evan Cheng71e86852008-07-08 20:06:39 +00001337 case TargetLowering::Legal: {
1338 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001339 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Cheng71e86852008-07-08 20:06:39 +00001340 if (Action == Legal && Tmp1 == Node->getOperand(0))
1341 break;
1342 if (Action == Legal) {
1343 Tmp2 = Node->getOperand(1);
1344 Tmp3 = Node->getOperand(2);
1345 Tmp4 = Node->getOperand(3);
1346 } else {
1347 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1348 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1349 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1350 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001351 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001352 break;
1353 }
Evan Cheng71e86852008-07-08 20:06:39 +00001354 }
Jim Laskeyabf6d172006-01-05 01:25:28 +00001355 break;
1356
Dan Gohman44066042008-07-01 00:05:16 +00001357 case ISD::DBG_LABEL:
1358 case ISD::EH_LABEL:
1359 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1360 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001361 default: assert(0 && "This action is not supported yet!");
1362 case TargetLowering::Legal:
1363 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohman44066042008-07-01 00:05:16 +00001364 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001365 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001366 case TargetLowering::Expand:
1367 Result = LegalizeOp(Node->getOperand(0));
1368 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001369 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001370 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001371
Evan Cheng27b7db52008-03-08 00:58:38 +00001372 case ISD::PREFETCH:
1373 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1374 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1375 default: assert(0 && "This action is not supported yet!");
1376 case TargetLowering::Legal:
1377 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1378 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1379 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1380 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1381 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1382 break;
1383 case TargetLowering::Expand:
1384 // It's a noop.
1385 Result = LegalizeOp(Node->getOperand(0));
1386 break;
1387 }
1388 break;
1389
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001390 case ISD::MEMBARRIER: {
1391 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001392 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1393 default: assert(0 && "This action is not supported yet!");
1394 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001395 SDValue Ops[6];
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001396 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001397 for (int x = 1; x < 6; ++x) {
1398 Ops[x] = Node->getOperand(x);
1399 if (!isTypeLegal(Ops[x].getValueType()))
1400 Ops[x] = PromoteOp(Ops[x]);
1401 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001402 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1403 break;
1404 }
1405 case TargetLowering::Expand:
1406 //There is no libgcc call for this op
1407 Result = Node->getOperand(0); // Noop
1408 break;
1409 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001410 break;
1411 }
1412
Dan Gohman0b1d4a72008-12-23 21:37:04 +00001413 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang63307c32008-05-05 19:05:59 +00001414 unsigned int num_operands = 4;
1415 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001416 SDValue Ops[4];
Mon P Wang63307c32008-05-05 19:05:59 +00001417 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001418 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang63307c32008-05-05 19:05:59 +00001419 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1420
1421 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1422 default: assert(0 && "This action is not supported yet!");
1423 case TargetLowering::Custom:
1424 Result = TLI.LowerOperation(Result, DAG);
1425 break;
1426 case TargetLowering::Legal:
1427 break;
1428 }
Dan Gohman475871a2008-07-27 21:46:04 +00001429 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1430 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001431 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001432 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00001433 case ISD::ATOMIC_LOAD_ADD:
1434 case ISD::ATOMIC_LOAD_SUB:
1435 case ISD::ATOMIC_LOAD_AND:
1436 case ISD::ATOMIC_LOAD_OR:
1437 case ISD::ATOMIC_LOAD_XOR:
1438 case ISD::ATOMIC_LOAD_NAND:
1439 case ISD::ATOMIC_LOAD_MIN:
1440 case ISD::ATOMIC_LOAD_MAX:
1441 case ISD::ATOMIC_LOAD_UMIN:
1442 case ISD::ATOMIC_LOAD_UMAX:
1443 case ISD::ATOMIC_SWAP: {
Mon P Wang63307c32008-05-05 19:05:59 +00001444 unsigned int num_operands = 3;
1445 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001446 SDValue Ops[3];
Mon P Wang63307c32008-05-05 19:05:59 +00001447 for (unsigned int x = 0; x < num_operands; ++x)
1448 Ops[x] = LegalizeOp(Node->getOperand(x));
1449 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands126d9072008-07-04 11:47:58 +00001450
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001451 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001452 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001453 case TargetLowering::Custom:
1454 Result = TLI.LowerOperation(Result, DAG);
1455 break;
1456 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001457 break;
1458 }
Dan Gohman475871a2008-07-27 21:46:04 +00001459 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1460 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001461 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001462 }
Scott Michelc1513d22007-08-08 23:23:31 +00001463 case ISD::Constant: {
1464 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1465 unsigned opAction =
1466 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1467
Chris Lattner3e928bb2005-01-07 07:47:09 +00001468 // We know we don't need to expand constants here, constants only have one
1469 // value and we check that it is fine above.
1470
Scott Michelc1513d22007-08-08 23:23:31 +00001471 if (opAction == TargetLowering::Custom) {
1472 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001473 if (Tmp1.getNode())
Scott Michelc1513d22007-08-08 23:23:31 +00001474 Result = Tmp1;
1475 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001476 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001477 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001478 case ISD::ConstantFP: {
1479 // Spill FP immediates to the constant pool if the target cannot directly
1480 // codegen them. Targets often have some immediate values that can be
1481 // efficiently generated into an FP register without a load. We explicitly
1482 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001483 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1484
Chris Lattner3181a772006-01-29 06:26:56 +00001485 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1486 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001487 case TargetLowering::Legal:
1488 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001489 case TargetLowering::Custom:
1490 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001491 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001492 Result = Tmp3;
1493 break;
1494 }
1495 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001496 case TargetLowering::Expand: {
1497 // Check to see if this FP immediate is already legal.
1498 bool isLegal = false;
1499 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1500 E = TLI.legal_fpimm_end(); I != E; ++I) {
1501 if (CFP->isExactlyValue(*I)) {
1502 isLegal = true;
1503 break;
1504 }
1505 }
1506 // If this is a legal constant, turn it into a TargetConstantFP node.
1507 if (isLegal)
1508 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001509 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001510 }
Nate Begemane1795842008-02-14 08:57:00 +00001511 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001512 break;
1513 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001514 case ISD::TokenFactor:
1515 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001516 Tmp1 = LegalizeOp(Node->getOperand(0));
1517 Tmp2 = LegalizeOp(Node->getOperand(1));
1518 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1519 } else if (Node->getNumOperands() == 3) {
1520 Tmp1 = LegalizeOp(Node->getOperand(0));
1521 Tmp2 = LegalizeOp(Node->getOperand(1));
1522 Tmp3 = LegalizeOp(Node->getOperand(2));
1523 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001524 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001525 SmallVector<SDValue, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001526 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001527 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1528 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001529 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001530 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001531 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001532
1533 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001534 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001535 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001536 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001537 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001538 // A call within a calling sequence must be legalized to something
1539 // other than the normal CALLSEQ_END. Violating this gets Legalize
1540 // into an infinite loop.
1541 assert ((!IsLegalizingCall ||
1542 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001543 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001544 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001545
1546 // The number of incoming and outgoing values should match; unless the final
1547 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001548 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1549 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1550 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001551 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001552 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001553
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001554 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001555 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001556 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1557 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001558 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001559 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001560 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001561 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001562 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001563 }
1564 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001565 case ISD::EXTRACT_SUBREG: {
1566 Tmp1 = LegalizeOp(Node->getOperand(0));
1567 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1568 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001569 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001570 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1571 }
1572 break;
1573 case ISD::INSERT_SUBREG: {
1574 Tmp1 = LegalizeOp(Node->getOperand(0));
1575 Tmp2 = LegalizeOp(Node->getOperand(1));
1576 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1577 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001578 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001579 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1580 }
1581 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001582 case ISD::BUILD_VECTOR:
1583 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001584 default: assert(0 && "This action is not supported yet!");
1585 case TargetLowering::Custom:
1586 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001587 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001588 Result = Tmp3;
1589 break;
1590 }
1591 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001592 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001593 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001594 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001595 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001596 break;
1597 case ISD::INSERT_VECTOR_ELT:
1598 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001599 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001600
1601 // The type of the value to insert may not be legal, even though the vector
1602 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1603 // here.
1604 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1605 default: assert(0 && "Cannot expand insert element operand");
1606 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1607 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang0c397192008-10-30 08:01:45 +00001608 case Expand:
1609 // FIXME: An alternative would be to check to see if the target is not
1610 // going to custom lower this operation, we could bitcast to half elt
1611 // width and perform two inserts at that width, if that is legal.
1612 Tmp2 = Node->getOperand(1);
1613 break;
Nate Begeman0325d902008-02-13 06:43:04 +00001614 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001615 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1616
1617 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1618 Node->getValueType(0))) {
1619 default: assert(0 && "This action is not supported yet!");
1620 case TargetLowering::Legal:
1621 break;
1622 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001623 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001624 if (Tmp4.getNode()) {
Nate Begeman2281a992008-01-05 20:47:37 +00001625 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001626 break;
1627 }
1628 // FALLTHROUGH
Mon P Wang0c397192008-10-30 08:01:45 +00001629 case TargetLowering::Promote:
1630 // Fall thru for vector case
Chris Lattner2332b9f2006-03-19 01:17:20 +00001631 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001632 // If the insert index is a constant, codegen this as a scalar_to_vector,
1633 // then a shuffle that inserts it into the right position in the vector.
1634 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001635 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1636 // match the element type of the vector being created.
1637 if (Tmp2.getValueType() ==
Duncan Sands83ec4b62008-06-06 12:08:01 +00001638 Op.getValueType().getVectorElementType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001639 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman0325d902008-02-13 06:43:04 +00001640 Tmp1.getValueType(), Tmp2);
1641
Duncan Sands83ec4b62008-06-06 12:08:01 +00001642 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1643 MVT ShufMaskVT =
1644 MVT::getIntVectorWithNumElements(NumElts);
1645 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman0325d902008-02-13 06:43:04 +00001646
1647 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1648 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1649 // elt 0 of the RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00001650 SmallVector<SDValue, 8> ShufOps;
Nate Begeman0325d902008-02-13 06:43:04 +00001651 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001652 if (i != InsertPos->getZExtValue())
Nate Begeman0325d902008-02-13 06:43:04 +00001653 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1654 else
1655 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1656 }
Dan Gohman475871a2008-07-27 21:46:04 +00001657 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman0325d902008-02-13 06:43:04 +00001658 &ShufOps[0], ShufOps.size());
1659
1660 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1661 Tmp1, ScVec, ShufMask);
1662 Result = LegalizeOp(Result);
1663 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001664 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001665 }
Nate Begeman68679912008-04-25 18:07:40 +00001666 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001667 break;
1668 }
1669 }
1670 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001671 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001672 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1673 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1674 break;
1675 }
1676
Chris Lattnerce872152006-03-19 06:31:19 +00001677 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1678 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1679 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1680 Node->getValueType(0))) {
1681 default: assert(0 && "This action is not supported yet!");
1682 case TargetLowering::Legal:
1683 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001684 case TargetLowering::Custom:
1685 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001686 if (Tmp3.getNode()) {
Chris Lattner4d3abee2006-03-19 06:47:21 +00001687 Result = Tmp3;
1688 break;
1689 }
1690 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001691 case TargetLowering::Expand:
1692 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001693 break;
1694 }
Chris Lattnerce872152006-03-19 06:31:19 +00001695 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001696 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001697 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1698 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1699 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1700
1701 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001702 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1703 default: assert(0 && "Unknown operation action!");
1704 case TargetLowering::Legal:
1705 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1706 "vector shuffle should not be created if not legal!");
1707 break;
1708 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001709 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001710 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001711 Result = Tmp3;
1712 break;
1713 }
1714 // FALLTHROUGH
1715 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001716 MVT VT = Node->getValueType(0);
1717 MVT EltVT = VT.getVectorElementType();
1718 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001719 SDValue Mask = Node->getOperand(2);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001720 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001721 SmallVector<SDValue,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001722 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001723 SDValue Arg = Mask.getOperand(i);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001724 if (Arg.getOpcode() == ISD::UNDEF) {
1725 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1726 } else {
1727 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001728 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng18dd6d02006-04-05 06:07:11 +00001729 if (Idx < NumElems)
1730 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1731 DAG.getConstant(Idx, PtrVT)));
1732 else
1733 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1734 DAG.getConstant(Idx - NumElems, PtrVT)));
1735 }
1736 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001737 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001738 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001739 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001740 case TargetLowering::Promote: {
1741 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001742 MVT OVT = Node->getValueType(0);
1743 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001744
1745 // Cast the two input vectors.
1746 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1747 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1748
1749 // Convert the shuffle mask to the right # elements.
Dan Gohman475871a2008-07-27 21:46:04 +00001750 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001751 assert(Tmp3.getNode() && "Shuffle not legal?");
Chris Lattner4352cc92006-04-04 17:23:26 +00001752 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1753 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1754 break;
1755 }
Chris Lattner87100e02006-03-20 01:52:29 +00001756 }
1757 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001758
1759 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001760 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001761 Tmp2 = LegalizeOp(Node->getOperand(1));
1762 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001763 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001764 break;
1765
Dan Gohman7f321562007-06-25 16:23:39 +00001766 case ISD::EXTRACT_SUBVECTOR:
1767 Tmp1 = Node->getOperand(0);
1768 Tmp2 = LegalizeOp(Node->getOperand(1));
1769 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1770 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001771 break;
1772
Mon P Wang0c397192008-10-30 08:01:45 +00001773 case ISD::CONCAT_VECTORS: {
1774 // Use extract/insert/build vector for now. We might try to be
1775 // more clever later.
1776 MVT PtrVT = TLI.getPointerTy();
1777 SmallVector<SDValue, 8> Ops;
1778 unsigned NumOperands = Node->getNumOperands();
1779 for (unsigned i=0; i < NumOperands; ++i) {
1780 SDValue SubOp = Node->getOperand(i);
1781 MVT VVT = SubOp.getNode()->getValueType(0);
1782 MVT EltVT = VVT.getVectorElementType();
1783 unsigned NumSubElem = VVT.getVectorNumElements();
1784 for (unsigned j=0; j < NumSubElem; ++j) {
1785 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1786 DAG.getConstant(j, PtrVT)));
1787 }
1788 }
1789 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1790 &Ops[0], Ops.size()));
1791 }
1792
Chris Lattner6831a812006-02-13 09:18:02 +00001793 case ISD::CALLSEQ_START: {
1794 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1795
1796 // Recursively Legalize all of the inputs of the call end that do not lead
1797 // to this call start. This ensures that any libcalls that need be inserted
1798 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001799 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001800 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001801 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001802 NodesLeadingTo);
1803 }
Chris Lattner6831a812006-02-13 09:18:02 +00001804
1805 // Now that we legalized all of the inputs (which may have inserted
1806 // libcalls) create the new CALLSEQ_START node.
1807 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1808
1809 // Merge in the last call, to ensure that this call start after the last
1810 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001811 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001812 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1813 Tmp1 = LegalizeOp(Tmp1);
1814 }
Chris Lattner6831a812006-02-13 09:18:02 +00001815
1816 // Do not try to legalize the target-specific arguments (#1+).
1817 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001818 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001819 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001820 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001821 }
1822
1823 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001824 AddLegalizedOperand(Op.getValue(0), Result);
1825 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1826 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1827
Chris Lattner6831a812006-02-13 09:18:02 +00001828 // Now that the callseq_start and all of the non-call nodes above this call
1829 // sequence have been legalized, legalize the call itself. During this
1830 // process, no libcalls can/will be inserted, guaranteeing that no calls
1831 // can overlap.
1832 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001833 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001834 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001835 IsLegalizingCall = true;
1836
1837 // Legalize the call, starting from the CALLSEQ_END.
1838 LegalizeOp(LastCALLSEQ_END);
1839 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1840 return Result;
1841 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001842 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001843 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1844 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001845 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001846 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1847 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001848 assert(I != LegalizedNodes.end() &&
1849 "Legalizing the call start should have legalized this node!");
1850 return I->second;
1851 }
1852
1853 // Otherwise, the call start has been legalized and everything is going
1854 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001855 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001856 // Do not try to legalize the target-specific arguments (#1+), except for
1857 // an optional flag input.
1858 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1859 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001860 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001861 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001862 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001863 }
1864 } else {
1865 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1866 if (Tmp1 != Node->getOperand(0) ||
1867 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001868 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001869 Ops[0] = Tmp1;
1870 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001871 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001872 }
Chris Lattner6a542892006-01-24 05:48:21 +00001873 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001874 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001875 // This finishes up call legalization.
1876 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001877
1878 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001879 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001880 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001881 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001882 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001883 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001884 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001885 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1886 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1887 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001888 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001889
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001890 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001891 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001892 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001893 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001894 case TargetLowering::Expand: {
1895 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1896 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1897 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001898 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001899
1900 // Chain the dynamic stack allocation so that it doesn't modify the stack
1901 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001902 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001903
Dan Gohman475871a2008-07-27 21:46:04 +00001904 SDValue Size = Tmp2.getOperand(1);
1905 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001906 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001907 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001908 unsigned StackAlign =
1909 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1910 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001911 SP = DAG.getNode(ISD::AND, VT, SP,
1912 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001913 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001914 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1915
Chris Lattnere563bbc2008-10-11 22:08:30 +00001916 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1917 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001918
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001919 Tmp1 = LegalizeOp(Tmp1);
1920 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001921 break;
1922 }
1923 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001924 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001925 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001926 Tmp1 = LegalizeOp(Tmp3);
1927 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001928 }
Chris Lattner903d2782006-01-15 08:54:32 +00001929 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001930 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001931 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001932 }
Chris Lattner903d2782006-01-15 08:54:32 +00001933 // Since this op produce two values, make sure to remember that we
1934 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001935 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1936 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001937 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001938 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001939 case ISD::INLINEASM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001940 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001941 bool Changed = false;
1942 // Legalize all of the operands of the inline asm, in case they are nodes
1943 // that need to be expanded or something. Note we skip the asm string and
1944 // all of the TargetConstant flags.
Dan Gohman475871a2008-07-27 21:46:04 +00001945 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001946 Changed = Op != Ops[0];
1947 Ops[0] = Op;
1948
1949 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1950 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001951 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Chris Lattner25a022c2006-07-11 01:40:09 +00001952 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman475871a2008-07-27 21:46:04 +00001953 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001954 if (Op != Ops[i]) {
1955 Changed = true;
1956 Ops[i] = Op;
1957 }
1958 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001959 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001960
1961 if (HasInFlag) {
1962 Op = LegalizeOp(Ops.back());
1963 Changed |= Op != Ops.back();
1964 Ops.back() = Op;
1965 }
1966
1967 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001968 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001969
1970 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman475871a2008-07-27 21:46:04 +00001971 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1972 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001973 return Result.getValue(Op.getResNo());
Chris Lattner25a022c2006-07-11 01:40:09 +00001974 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001975 case ISD::BR:
1976 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001977 // Ensure that libcalls are emitted before a branch.
1978 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1979 Tmp1 = LegalizeOp(Tmp1);
1980 LastCALLSEQ_END = DAG.getEntryNode();
1981
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001982 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001983 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001984 case ISD::BRIND:
1985 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1986 // Ensure that libcalls are emitted before a branch.
1987 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1988 Tmp1 = LegalizeOp(Tmp1);
1989 LastCALLSEQ_END = DAG.getEntryNode();
1990
1991 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1992 default: assert(0 && "Indirect target must be legal type (pointer)!");
1993 case Legal:
1994 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1995 break;
1996 }
1997 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1998 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001999 case ISD::BR_JT:
2000 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2001 // Ensure that libcalls are emitted before a branch.
2002 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2003 Tmp1 = LegalizeOp(Tmp1);
2004 LastCALLSEQ_END = DAG.getEntryNode();
2005
2006 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
2007 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2008
2009 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
2010 default: assert(0 && "This action is not supported yet!");
2011 case TargetLowering::Legal: break;
2012 case TargetLowering::Custom:
2013 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002014 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00002015 break;
2016 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002017 SDValue Chain = Result.getOperand(0);
2018 SDValue Table = Result.getOperand(1);
2019 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002020
Duncan Sands83ec4b62008-06-06 12:08:01 +00002021 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002022 MachineFunction &MF = DAG.getMachineFunction();
2023 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00002024 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman475871a2008-07-27 21:46:04 +00002025 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002026
Duncan Sands712f7b32008-12-12 08:13:38 +00002027 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
2028 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, Chain, Addr,
2029 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00002030 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002031 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00002032 // For PIC, the sequence is:
2033 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00002034 // RelocBase can be JumpTable, GOT or some sort of global base.
Evan Chengcc415862007-11-09 01:32:10 +00002035 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
2036 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00002037 }
Evan Chengcc415862007-11-09 01:32:10 +00002038 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002039 }
2040 }
2041 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002042 case ISD::BRCOND:
2043 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002044 // Ensure that libcalls are emitted before a return.
2045 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2046 Tmp1 = LegalizeOp(Tmp1);
2047 LastCALLSEQ_END = DAG.getEntryNode();
2048
Chris Lattner47e92232005-01-18 19:27:06 +00002049 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2050 case Expand: assert(0 && "It's impossible to expand bools");
2051 case Legal:
2052 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2053 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002054 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00002055 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00002056
2057 // The top bits of the promoted condition are not necessarily zero, ensure
2058 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002059 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002060 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002061 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerf9908172006-11-27 04:39:56 +00002062 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002063 break;
2064 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002065 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002066
2067 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002068 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00002069
2070 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
2071 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002072 case TargetLowering::Legal: break;
2073 case TargetLowering::Custom:
2074 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002075 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002076 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002077 case TargetLowering::Expand:
2078 // Expand brcond's setcc into its constituent parts and create a BR_CC
2079 // Node.
2080 if (Tmp2.getOpcode() == ISD::SETCC) {
2081 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
2082 Tmp2.getOperand(0), Tmp2.getOperand(1),
2083 Node->getOperand(2));
2084 } else {
2085 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
2086 DAG.getCondCode(ISD::SETNE), Tmp2,
2087 DAG.getConstant(0, Tmp2.getValueType()),
2088 Node->getOperand(2));
2089 }
2090 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002091 }
2092 break;
2093 case ISD::BR_CC:
2094 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002095 // Ensure that libcalls are emitted before a branch.
2096 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2097 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002098 Tmp2 = Node->getOperand(2); // LHS
2099 Tmp3 = Node->getOperand(3); // RHS
2100 Tmp4 = Node->getOperand(1); // CC
2101
Duncan Sands5480c042009-01-01 15:52:00 +00002102 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()), Tmp2, Tmp3,Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00002103 LastCALLSEQ_END = DAG.getEntryNode();
2104
Evan Cheng7f042682008-10-15 02:05:31 +00002105 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002106 // the LHS is a legal SETCC itself. In this case, we need to compare
2107 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002108 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002109 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2110 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00002111 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00002112
2113 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2114 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002115
Chris Lattner181b7a32005-12-17 23:46:46 +00002116 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2117 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002118 case TargetLowering::Legal: break;
2119 case TargetLowering::Custom:
2120 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002121 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00002122 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002123 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002124 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002125 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00002126 LoadSDNode *LD = cast<LoadSDNode>(Node);
2127 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2128 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002129
Evan Cheng466685d2006-10-09 20:57:25 +00002130 ISD::LoadExtType ExtType = LD->getExtensionType();
2131 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002132 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00002133 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2134 Tmp3 = Result.getValue(0);
2135 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002136
Evan Cheng466685d2006-10-09 20:57:25 +00002137 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2138 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002139 case TargetLowering::Legal:
2140 // If this is an unaligned load and the target doesn't support it,
2141 // expand it.
2142 if (!TLI.allowsUnalignedMemoryAccesses()) {
2143 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002144 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002145 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002146 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002147 TLI);
2148 Tmp3 = Result.getOperand(0);
2149 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00002150 Tmp3 = LegalizeOp(Tmp3);
2151 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002152 }
2153 }
2154 break;
Evan Cheng466685d2006-10-09 20:57:25 +00002155 case TargetLowering::Custom:
2156 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002157 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002158 Tmp3 = LegalizeOp(Tmp1);
2159 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00002160 }
Evan Cheng466685d2006-10-09 20:57:25 +00002161 break;
2162 case TargetLowering::Promote: {
2163 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002164 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00002165 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002166 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002167
2168 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002169 LD->getSrcValueOffset(),
2170 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00002171 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2172 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002173 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00002174 }
Evan Cheng466685d2006-10-09 20:57:25 +00002175 }
2176 // Since loads produce two values, make sure to remember that we
2177 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002178 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2179 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002180 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00002181 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002182 MVT SrcVT = LD->getMemoryVT();
2183 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002184 int SVOffset = LD->getSrcValueOffset();
2185 unsigned Alignment = LD->getAlignment();
2186 bool isVolatile = LD->isVolatile();
2187
Duncan Sands83ec4b62008-06-06 12:08:01 +00002188 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002189 // Some targets pretend to have an i1 loading operation, and actually
2190 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2191 // bits are guaranteed to be zero; it helps the optimizers understand
2192 // that these bits are zero. It is also useful for EXTLOAD, since it
2193 // tells the optimizers that those bits are undefined. It would be
2194 // nice to have an effective generic way of getting these benefits...
2195 // Until such a way is found, don't insist on promoting i1 here.
2196 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00002197 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002198 // Promote to a byte-sized load if not loading an integral number of
2199 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002200 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2201 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002202 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002203
2204 // The extra bits are guaranteed to be zero, since we stored them that
2205 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2206
2207 ISD::LoadExtType NewExtType =
2208 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2209
2210 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2211 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2212 NVT, isVolatile, Alignment);
2213
2214 Ch = Result.getValue(1); // The chain.
2215
2216 if (ExtType == ISD::SEXTLOAD)
2217 // Having the top bits zero doesn't help when sign extending.
2218 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2219 Result, DAG.getValueType(SrcVT));
2220 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2221 // All the top bits are guaranteed to be zero - inform the optimizers.
2222 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2223 DAG.getValueType(SrcVT));
2224
2225 Tmp1 = LegalizeOp(Result);
2226 Tmp2 = LegalizeOp(Ch);
2227 } else if (SrcWidth & (SrcWidth - 1)) {
2228 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002229 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002230 "Unsupported extload!");
2231 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2232 assert(RoundWidth < SrcWidth);
2233 unsigned ExtraWidth = SrcWidth - RoundWidth;
2234 assert(ExtraWidth < RoundWidth);
2235 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2236 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002237 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2238 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002239 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002240 unsigned IncrementSize;
2241
2242 if (TLI.isLittleEndian()) {
2243 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2244 // Load the bottom RoundWidth bits.
2245 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2246 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2247 Alignment);
2248
2249 // Load the remaining ExtraWidth bits.
2250 IncrementSize = RoundWidth / 8;
2251 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2252 DAG.getIntPtrConstant(IncrementSize));
2253 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2254 LD->getSrcValue(), SVOffset + IncrementSize,
2255 ExtraVT, isVolatile,
2256 MinAlign(Alignment, IncrementSize));
2257
2258 // Build a factor node to remember that this load is independent of the
2259 // other one.
2260 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2261 Hi.getValue(1));
2262
2263 // Move the top bits to the right place.
2264 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2265 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2266
2267 // Join the hi and lo parts.
2268 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002269 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002270 // Big endian - avoid unaligned loads.
2271 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2272 // Load the top RoundWidth bits.
2273 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2274 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2275 Alignment);
2276
2277 // Load the remaining ExtraWidth bits.
2278 IncrementSize = RoundWidth / 8;
2279 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2280 DAG.getIntPtrConstant(IncrementSize));
2281 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2282 LD->getSrcValue(), SVOffset + IncrementSize,
2283 ExtraVT, isVolatile,
2284 MinAlign(Alignment, IncrementSize));
2285
2286 // Build a factor node to remember that this load is independent of the
2287 // other one.
2288 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2289 Hi.getValue(1));
2290
2291 // Move the top bits to the right place.
2292 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2293 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2294
2295 // Join the hi and lo parts.
2296 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2297 }
2298
2299 Tmp1 = LegalizeOp(Result);
2300 Tmp2 = LegalizeOp(Ch);
2301 } else {
Evan Cheng03294662008-10-14 21:26:46 +00002302 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002303 default: assert(0 && "This action is not supported yet!");
2304 case TargetLowering::Custom:
2305 isCustom = true;
2306 // FALLTHROUGH
2307 case TargetLowering::Legal:
2308 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2309 Tmp1 = Result.getValue(0);
2310 Tmp2 = Result.getValue(1);
2311
2312 if (isCustom) {
2313 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002314 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002315 Tmp1 = LegalizeOp(Tmp3);
2316 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2317 }
2318 } else {
2319 // If this is an unaligned load and the target doesn't support it,
2320 // expand it.
2321 if (!TLI.allowsUnalignedMemoryAccesses()) {
2322 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002323 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002324 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002325 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002326 TLI);
2327 Tmp1 = Result.getOperand(0);
2328 Tmp2 = Result.getOperand(1);
2329 Tmp1 = LegalizeOp(Tmp1);
2330 Tmp2 = LegalizeOp(Tmp2);
2331 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002332 }
2333 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002334 break;
2335 case TargetLowering::Expand:
2336 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2337 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman475871a2008-07-27 21:46:04 +00002338 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002339 LD->getSrcValueOffset(),
2340 LD->isVolatile(), LD->getAlignment());
2341 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2342 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2343 Tmp2 = LegalizeOp(Load.getValue(1));
2344 break;
2345 }
2346 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2347 // Turn the unsupported load into an EXTLOAD followed by an explicit
2348 // zero/sign extend inreg.
2349 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2350 Tmp1, Tmp2, LD->getSrcValue(),
2351 LD->getSrcValueOffset(), SrcVT,
2352 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00002353 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002354 if (ExtType == ISD::SEXTLOAD)
2355 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2356 Result, DAG.getValueType(SrcVT));
2357 else
2358 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2359 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2360 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002361 break;
2362 }
Evan Cheng466685d2006-10-09 20:57:25 +00002363 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002364
Evan Cheng466685d2006-10-09 20:57:25 +00002365 // Since loads produce two values, make sure to remember that we legalized
2366 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002367 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2368 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002369 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002370 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002371 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002372 case ISD::EXTRACT_ELEMENT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002373 MVT OpTy = Node->getOperand(0).getValueType();
Nate Begeman5dc897b2005-10-19 00:06:56 +00002374 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002375 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002376 case Legal:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002377 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Nate Begeman5dc897b2005-10-19 00:06:56 +00002378 // 1 -> Hi
2379 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002380 DAG.getConstant(OpTy.getSizeInBits()/2,
Nate Begeman5dc897b2005-10-19 00:06:56 +00002381 TLI.getShiftAmountTy()));
2382 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2383 } else {
2384 // 0 -> Lo
2385 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2386 Node->getOperand(0));
2387 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002388 break;
2389 case Expand:
2390 // Get both the low and high parts.
2391 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002392 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Nate Begeman5dc897b2005-10-19 00:06:56 +00002393 Result = Tmp2; // 1 -> Hi
2394 else
2395 Result = Tmp1; // 0 -> Lo
2396 break;
2397 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002398 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002399 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002400
2401 case ISD::CopyToReg:
2402 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002403
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002404 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002405 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002406 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002407 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002408 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002409 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002410 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002411 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002412 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002413 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002414 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2415 Tmp3);
2416 } else {
2417 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002418 }
2419
2420 // Since this produces two values, make sure to remember that we legalized
2421 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002422 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2423 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002424 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002425 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002426 break;
2427
2428 case ISD::RET:
2429 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002430
2431 // Ensure that libcalls are emitted before a return.
2432 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2433 Tmp1 = LegalizeOp(Tmp1);
2434 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002435
Chris Lattner3e928bb2005-01-07 07:47:09 +00002436 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002437 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002438 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002439 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002440 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002441 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002442 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002443 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002444 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002445 if (!Tmp2.getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002446 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002447 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002448
2449 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002450 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002451 std::swap(Lo, Hi);
2452
Gabor Greifba36cb52008-08-28 21:40:38 +00002453 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00002454 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2455 else
2456 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002457 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002458 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00002459 SDNode *InVal = Tmp2.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002460 int InIx = Tmp2.getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002461 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2462 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002463
Dan Gohman7f321562007-06-25 16:23:39 +00002464 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002465 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002466 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002467 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002468 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002469 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002470 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002471 } else if (NumElems == 1) {
2472 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002473 Tmp2 = ScalarizeVectorOp(Tmp2);
2474 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002475 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002476
2477 // FIXME: Returns of gcc generic vectors smaller than a legal type
2478 // should be returned in integer registers!
2479
Chris Lattnerf87324e2006-04-11 01:31:51 +00002480 // The scalarized value type may not be legal, e.g. it might require
2481 // promotion or expansion. Relegalize the return.
2482 Result = LegalizeOp(Result);
2483 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002484 // FIXME: Returns of gcc generic vectors larger than a legal vector
2485 // type should be returned by reference!
Dan Gohman475871a2008-07-27 21:46:04 +00002486 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002487 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002488 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002489 Result = LegalizeOp(Result);
2490 }
2491 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002492 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002493 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002494 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002495 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002496 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002497 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002498 }
2499 break;
2500 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002501 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002502 break;
2503 default: { // ret <values>
Dan Gohman475871a2008-07-27 21:46:04 +00002504 SmallVector<SDValue, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002505 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002506 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002507 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2508 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002509 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002510 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002511 break;
2512 case Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002513 SDValue Lo, Hi;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002514 assert(!Node->getOperand(i).getValueType().isExtended() &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002515 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002516 ExpandOp(Node->getOperand(i), Lo, Hi);
2517 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002518 NewValues.push_back(Node->getOperand(i+1));
Gabor Greifba36cb52008-08-28 21:40:38 +00002519 if (Hi.getNode()) {
Evan Cheng13acce32006-12-11 19:27:14 +00002520 NewValues.push_back(Hi);
2521 NewValues.push_back(Node->getOperand(i+1));
2522 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002523 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002524 }
2525 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002526 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002527 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002528
2529 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002530 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002531 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002532 Result = DAG.getNode(ISD::RET, MVT::Other,
2533 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002534 break;
2535 }
2536 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002537
Chris Lattner6862dbc2006-01-29 21:02:23 +00002538 if (Result.getOpcode() == ISD::RET) {
2539 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2540 default: assert(0 && "This action is not supported yet!");
2541 case TargetLowering::Legal: break;
2542 case TargetLowering::Custom:
2543 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002544 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner6862dbc2006-01-29 21:02:23 +00002545 break;
2546 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002547 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002548 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002549 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002550 StoreSDNode *ST = cast<StoreSDNode>(Node);
2551 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2552 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002553 int SVOffset = ST->getSrcValueOffset();
2554 unsigned Alignment = ST->getAlignment();
2555 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002556
Evan Cheng8b2794a2006-10-13 21:14:26 +00002557 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002558 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2559 // FIXME: We shouldn't do this for TargetConstantFP's.
2560 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2561 // to phase ordering between legalized code and the dag combiner. This
2562 // probably means that we need to integrate dag combiner and legalizer
2563 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002564 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002565 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002566 if (CFP->getValueType(0) == MVT::f32 &&
2567 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002568 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00002569 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002570 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002571 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2572 SVOffset, isVolatile, Alignment);
2573 break;
2574 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002575 // If this target supports 64-bit registers, do a single 64-bit store.
2576 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00002577 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002578 zextOrTrunc(64), MVT::i64);
Chris Lattner3cb93512007-10-15 05:46:06 +00002579 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2580 SVOffset, isVolatile, Alignment);
2581 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002582 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002583 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2584 // stores. If the target supports neither 32- nor 64-bits, this
2585 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00002586 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00002587 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2588 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002589 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002590
2591 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2592 SVOffset, isVolatile, Alignment);
2593 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002594 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002595 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002596 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002597
2598 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2599 break;
2600 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002601 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002602 }
2603
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002604 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002605 case Legal: {
2606 Tmp3 = LegalizeOp(ST->getValue());
2607 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2608 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002609
Duncan Sands83ec4b62008-06-06 12:08:01 +00002610 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002611 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2612 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002613 case TargetLowering::Legal:
2614 // If this is an unaligned store and the target doesn't support it,
2615 // expand it.
2616 if (!TLI.allowsUnalignedMemoryAccesses()) {
2617 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002618 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002619 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002620 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002621 TLI);
2622 }
2623 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002624 case TargetLowering::Custom:
2625 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002626 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002627 break;
2628 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002629 assert(VT.isVector() && "Unknown legal promote case!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002630 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2631 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2632 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002633 ST->getSrcValue(), SVOffset, isVolatile,
2634 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002635 break;
2636 }
2637 break;
2638 }
2639 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002640 if (!ST->getMemoryVT().isVector()) {
2641 // Truncate the value and store the result.
2642 Tmp3 = PromoteOp(ST->getValue());
2643 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2644 SVOffset, ST->getMemoryVT(),
2645 isVolatile, Alignment);
2646 break;
2647 }
2648 // Fall thru to expand for vector
2649 case Expand: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002650 unsigned IncrementSize = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002651 SDValue Lo, Hi;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002652
2653 // If this is a vector type, then we have to calculate the increment as
2654 // the product of the element size in bytes, and the number of elements
2655 // in the high half of the vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002656 if (ST->getValue().getValueType().isVector()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002657 SDNode *InVal = ST->getValue().getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002658 int InIx = ST->getValue().getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002659 MVT InVT = InVal->getValueType(InIx);
2660 unsigned NumElems = InVT.getVectorNumElements();
2661 MVT EVT = InVT.getVectorElementType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002662
Dan Gohman7f321562007-06-25 16:23:39 +00002663 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002664 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002665 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002666 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002667 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002668 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002669 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002670 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002671 Result = LegalizeOp(Result);
2672 break;
2673 } else if (NumElems == 1) {
2674 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002675 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002676 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002677 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002678 // The scalarized value type may not be legal, e.g. it might require
2679 // promotion or expansion. Relegalize the scalar store.
2680 Result = LegalizeOp(Result);
2681 break;
2682 } else {
Mon P Wang0c397192008-10-30 08:01:45 +00002683 // Check if we have widen this node with another value
2684 std::map<SDValue, SDValue>::iterator I =
2685 WidenNodes.find(ST->getValue());
2686 if (I != WidenNodes.end()) {
2687 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2688 break;
2689 }
2690 else {
2691 SplitVectorOp(ST->getValue(), Lo, Hi);
2692 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2693 EVT.getSizeInBits()/8;
2694 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002695 }
2696 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002697 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greifba36cb52008-08-28 21:40:38 +00002698 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002699
Richard Pennington4b052dc2008-09-25 16:15:10 +00002700 if (Hi.getNode() && TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002701 std::swap(Lo, Hi);
2702 }
2703
2704 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002705 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002706
Gabor Greifba36cb52008-08-28 21:40:38 +00002707 if (Hi.getNode() == NULL) {
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002708 // Must be int <-> float one-to-one expansion.
2709 Result = Lo;
2710 break;
2711 }
2712
Evan Cheng8b2794a2006-10-13 21:14:26 +00002713 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002714 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002715 assert(isTypeLegal(Tmp2.getValueType()) &&
2716 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002717 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002718 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002719 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002720 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002721 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2722 break;
Mon P Wang0c397192008-10-30 08:01:45 +00002723 } // case Expand
Evan Cheng8b2794a2006-10-13 21:14:26 +00002724 }
2725 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002726 switch (getTypeAction(ST->getValue().getValueType())) {
2727 case Legal:
2728 Tmp3 = LegalizeOp(ST->getValue());
2729 break;
2730 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002731 if (!ST->getValue().getValueType().isVector()) {
2732 // We can promote the value, the truncstore will still take care of it.
2733 Tmp3 = PromoteOp(ST->getValue());
2734 break;
2735 }
2736 // Vector case falls through to expand
Chris Lattnerddf89562008-01-17 19:59:44 +00002737 case Expand:
2738 // Just store the low part. This may become a non-trunc store, so make
2739 // sure to use getTruncStore, not UpdateNodeOperands below.
2740 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2741 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2742 SVOffset, MVT::i8, isVolatile, Alignment);
2743 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002744
Duncan Sands83ec4b62008-06-06 12:08:01 +00002745 MVT StVT = ST->getMemoryVT();
2746 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00002747
Duncan Sands83ec4b62008-06-06 12:08:01 +00002748 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00002749 // Promote to a byte-sized store with upper bits zero if not
2750 // storing an integral number of bytes. For example, promote
2751 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002752 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands7e857202008-01-22 07:17:34 +00002753 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2754 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2755 SVOffset, NVT, isVolatile, Alignment);
2756 } else if (StWidth & (StWidth - 1)) {
2757 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002758 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00002759 "Unsupported truncstore!");
2760 unsigned RoundWidth = 1 << Log2_32(StWidth);
2761 assert(RoundWidth < StWidth);
2762 unsigned ExtraWidth = StWidth - RoundWidth;
2763 assert(ExtraWidth < RoundWidth);
2764 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2765 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002766 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2767 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002768 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00002769 unsigned IncrementSize;
2770
2771 if (TLI.isLittleEndian()) {
2772 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2773 // Store the bottom RoundWidth bits.
2774 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2775 SVOffset, RoundVT,
2776 isVolatile, Alignment);
2777
2778 // Store the remaining ExtraWidth bits.
2779 IncrementSize = RoundWidth / 8;
2780 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2781 DAG.getIntPtrConstant(IncrementSize));
2782 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2783 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2784 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2785 SVOffset + IncrementSize, ExtraVT, isVolatile,
2786 MinAlign(Alignment, IncrementSize));
2787 } else {
2788 // Big endian - avoid unaligned stores.
2789 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2790 // Store the top RoundWidth bits.
2791 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2792 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2793 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2794 RoundVT, isVolatile, Alignment);
2795
2796 // Store the remaining ExtraWidth bits.
2797 IncrementSize = RoundWidth / 8;
2798 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2799 DAG.getIntPtrConstant(IncrementSize));
2800 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2801 SVOffset + IncrementSize, ExtraVT, isVolatile,
2802 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002803 }
Duncan Sands7e857202008-01-22 07:17:34 +00002804
2805 // The order of the stores doesn't matter.
2806 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2807 } else {
2808 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2809 Tmp2 != ST->getBasePtr())
2810 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2811 ST->getOffset());
2812
2813 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2814 default: assert(0 && "This action is not supported yet!");
2815 case TargetLowering::Legal:
2816 // If this is an unaligned store and the target doesn't support it,
2817 // expand it.
2818 if (!TLI.allowsUnalignedMemoryAccesses()) {
2819 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002820 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00002821 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002822 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00002823 TLI);
2824 }
2825 break;
2826 case TargetLowering::Custom:
2827 Result = TLI.LowerOperation(Result, DAG);
2828 break;
2829 case Expand:
2830 // TRUNCSTORE:i16 i32 -> STORE i16
2831 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2832 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2833 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2834 isVolatile, Alignment);
2835 break;
2836 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002837 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002838 }
2839 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002840 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002841 case ISD::PCMARKER:
2842 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002843 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002844 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002845 case ISD::STACKSAVE:
2846 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002847 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2848 Tmp1 = Result.getValue(0);
2849 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002850
Chris Lattner140d53c2006-01-13 02:50:02 +00002851 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2852 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002853 case TargetLowering::Legal: break;
2854 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002855 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002856 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002857 Tmp1 = LegalizeOp(Tmp3);
2858 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002859 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002860 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002861 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002862 // Expand to CopyFromReg if the target set
2863 // StackPointerRegisterToSaveRestore.
2864 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002865 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002866 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002867 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002868 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002869 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2870 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002871 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002872 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002873 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002874
2875 // Since stacksave produce two values, make sure to remember that we
2876 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002877 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2878 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002879 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002880
Chris Lattner140d53c2006-01-13 02:50:02 +00002881 case ISD::STACKRESTORE:
2882 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2883 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002884 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002885
2886 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2887 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002888 case TargetLowering::Legal: break;
2889 case TargetLowering::Custom:
2890 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002891 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002892 break;
2893 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002894 // Expand to CopyToReg if the target set
2895 // StackPointerRegisterToSaveRestore.
2896 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2897 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2898 } else {
2899 Result = Tmp1;
2900 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002901 break;
2902 }
2903 break;
2904
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002905 case ISD::READCYCLECOUNTER:
2906 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002907 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002908 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2909 Node->getValueType(0))) {
2910 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002911 case TargetLowering::Legal:
2912 Tmp1 = Result.getValue(0);
2913 Tmp2 = Result.getValue(1);
2914 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002915 case TargetLowering::Custom:
2916 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002917 Tmp1 = LegalizeOp(Result.getValue(0));
2918 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002919 break;
2920 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002921
2922 // Since rdcc produce two values, make sure to remember that we legalized
2923 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002924 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2925 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002926 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002927
Chris Lattner2ee743f2005-01-14 22:08:15 +00002928 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002929 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2930 case Expand: assert(0 && "It's impossible to expand bools");
2931 case Legal:
2932 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2933 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002934 case Promote: {
Mon P Wang0c397192008-10-30 08:01:45 +00002935 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Chris Lattner47e92232005-01-18 19:27:06 +00002936 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002937 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002938 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002939 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002940 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerb6c80602006-11-28 01:03:30 +00002941 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002942 break;
2943 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002944 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002945 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002946 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002947
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002948 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002949
Nate Begemanb942a3d2005-08-23 04:29:48 +00002950 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002951 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002952 case TargetLowering::Legal: break;
2953 case TargetLowering::Custom: {
2954 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002955 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002956 break;
2957 }
Nate Begeman9373a812005-08-10 20:51:12 +00002958 case TargetLowering::Expand:
2959 if (Tmp1.getOpcode() == ISD::SETCC) {
2960 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2961 Tmp2, Tmp3,
2962 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2963 } else {
2964 Result = DAG.getSelectCC(Tmp1,
2965 DAG.getConstant(0, Tmp1.getValueType()),
2966 Tmp2, Tmp3, ISD::SETNE);
2967 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002968 break;
2969 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002970 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002971 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2972 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002973 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002974 ExtOp = ISD::BIT_CONVERT;
2975 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002976 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002977 ExtOp = ISD::ANY_EXTEND;
2978 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002979 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002980 ExtOp = ISD::FP_EXTEND;
2981 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002982 }
2983 // Promote each of the values to the new type.
2984 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2985 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2986 // Perform the larger operation, then round down.
2987 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002988 if (TruncOp != ISD::FP_ROUND)
2989 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2990 else
2991 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2992 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002993 break;
2994 }
2995 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002996 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002997 case ISD::SELECT_CC: {
2998 Tmp1 = Node->getOperand(0); // LHS
2999 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00003000 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
3001 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00003002 SDValue CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00003003
Duncan Sands5480c042009-01-01 15:52:00 +00003004 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003005
Evan Cheng7f042682008-10-15 02:05:31 +00003006 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00003007 // the LHS is a legal SETCC itself. In this case, we need to compare
3008 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00003009 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003010 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3011 CC = DAG.getCondCode(ISD::SETNE);
3012 }
3013 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
3014
3015 // Everything is legal, see if we should expand this op or something.
3016 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
3017 default: assert(0 && "This action is not supported yet!");
3018 case TargetLowering::Legal: break;
3019 case TargetLowering::Custom:
3020 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003021 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00003022 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003023 }
3024 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00003025 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003026 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00003027 Tmp1 = Node->getOperand(0);
3028 Tmp2 = Node->getOperand(1);
3029 Tmp3 = Node->getOperand(2);
Evan Cheng7f042682008-10-15 02:05:31 +00003030 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003031
3032 // If we had to Expand the SetCC operands into a SELECT node, then it may
3033 // not always be possible to return a true LHS & RHS. In this case, just
3034 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003035 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003036 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003037 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003038 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003039
Chris Lattner73e142f2006-01-30 22:43:50 +00003040 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003041 default: assert(0 && "Cannot handle this action for SETCC yet!");
3042 case TargetLowering::Custom:
3043 isCustom = true;
3044 // FALLTHROUGH.
3045 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00003046 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00003047 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00003048 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003049 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00003050 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003051 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00003052 case TargetLowering::Promote: {
3053 // First step, figure out the appropriate operation to use.
3054 // Allow SETCC to not be supported for all legal data types
3055 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00003056 MVT NewInTy = Node->getOperand(0).getValueType();
3057 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00003058
3059 // Scan for the appropriate larger type to use.
3060 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003061 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00003062
Duncan Sands83ec4b62008-06-06 12:08:01 +00003063 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003064 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003065 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003066 "Fell off of the edge of the floating point world");
3067
3068 // If the target supports SETCC of this type, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003069 if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00003070 break;
3071 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003072 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00003073 assert(0 && "Cannot promote Legal Integer SETCC yet");
3074 else {
3075 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
3076 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
3077 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003078 Tmp1 = LegalizeOp(Tmp1);
3079 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00003080 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00003081 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00003082 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00003083 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003084 case TargetLowering::Expand:
3085 // Expand a setcc node into a select_cc of the same condition, lhs, and
3086 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003087 MVT VT = Node->getValueType(0);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003088 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3089 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00003090 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003091 break;
3092 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003093 break;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003094 case ISD::VSETCC: {
3095 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3096 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman475871a2008-07-27 21:46:04 +00003097 SDValue CC = Node->getOperand(2);
Nate Begemanb43e9c12008-05-12 19:40:03 +00003098
3099 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3100
3101 // Everything is legal, see if we should expand this op or something.
3102 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3103 default: assert(0 && "This action is not supported yet!");
3104 case TargetLowering::Legal: break;
3105 case TargetLowering::Custom:
3106 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003107 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003108 break;
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003109 case TargetLowering::Expand: {
3110 // Unroll into a nasty set of scalar code for now.
3111 MVT VT = Node->getValueType(0);
3112 unsigned NumElems = VT.getVectorNumElements();
3113 MVT EltVT = VT.getVectorElementType();
3114 MVT TmpEltVT = Tmp1.getValueType().getVectorElementType();
3115 SmallVector<SDValue, 8> Ops(NumElems);
3116 for (unsigned i = 0; i < NumElems; ++i) {
3117 SDValue In1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3118 Tmp1, DAG.getIntPtrConstant(i));
Duncan Sands5480c042009-01-01 15:52:00 +00003119 Ops[i] = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(TmpEltVT), In1,
Mon P Wang84aff842008-12-17 08:49:47 +00003120 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3121 Tmp2, DAG.getIntPtrConstant(i)),
3122 CC);
3123 Ops[i] = DAG.getNode(ISD::SELECT, EltVT, Ops[i],
3124 DAG.getConstant(EltVT.getIntegerVTBitMask(),EltVT),
3125 DAG.getConstant(0, EltVT));
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003126 }
3127 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], NumElems);
3128 break;
3129 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00003130 }
3131 break;
3132 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00003133
Chris Lattner5b359c62005-04-02 04:00:59 +00003134 case ISD::SHL_PARTS:
3135 case ISD::SRA_PARTS:
3136 case ISD::SRL_PARTS: {
Dan Gohman475871a2008-07-27 21:46:04 +00003137 SmallVector<SDValue, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00003138 bool Changed = false;
3139 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3140 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3141 Changed |= Ops.back() != Node->getOperand(i);
3142 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003143 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003144 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00003145
Evan Cheng05a2d562006-01-09 18:31:59 +00003146 switch (TLI.getOperationAction(Node->getOpcode(),
3147 Node->getValueType(0))) {
3148 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003149 case TargetLowering::Legal: break;
3150 case TargetLowering::Custom:
3151 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003152 if (Tmp1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003153 SDValue Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00003154 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003155 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman475871a2008-07-27 21:46:04 +00003156 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003157 if (i == Op.getResNo())
Evan Cheng12f22742006-01-19 04:54:52 +00003158 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00003159 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003160 assert(RetVal.getNode() && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00003161 return RetVal;
3162 }
Evan Cheng05a2d562006-01-09 18:31:59 +00003163 break;
3164 }
3165
Chris Lattner2c8086f2005-04-02 05:00:07 +00003166 // Since these produce multiple values, make sure to remember that we
3167 // legalized all of them.
3168 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +00003169 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00003170 return Result.getValue(Op.getResNo());
Chris Lattner84f67882005-01-20 18:52:28 +00003171 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003172
3173 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00003174 case ISD::ADD:
3175 case ISD::SUB:
3176 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00003177 case ISD::MULHS:
3178 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003179 case ISD::UDIV:
3180 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003181 case ISD::AND:
3182 case ISD::OR:
3183 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00003184 case ISD::SHL:
3185 case ISD::SRL:
3186 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00003187 case ISD::FADD:
3188 case ISD::FSUB:
3189 case ISD::FMUL:
3190 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00003191 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003192 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00003193 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3194 case Expand: assert(0 && "Not possible");
3195 case Legal:
3196 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3197 break;
3198 case Promote:
3199 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3200 break;
3201 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003202
3203 if ((Node->getOpcode() == ISD::SHL ||
3204 Node->getOpcode() == ISD::SRL ||
3205 Node->getOpcode() == ISD::SRA) &&
3206 !Node->getValueType(0).isVector()) {
Mon P Wange9f10152008-12-09 05:46:39 +00003207 Tmp2 = LegalizeShiftAmount(Tmp2);
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003208 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003209
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003210 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003211
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003212 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003213 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00003214 case TargetLowering::Legal: break;
3215 case TargetLowering::Custom:
3216 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003217 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003218 Result = Tmp1;
3219 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00003220 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003221 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003222 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003223 MVT VT = Op.getValueType();
Mon P Wang0c397192008-10-30 08:01:45 +00003224
Dan Gohman525178c2007-10-08 18:33:35 +00003225 // See if multiply or divide can be lowered using two-result operations.
3226 SDVTList VTs = DAG.getVTList(VT, VT);
3227 if (Node->getOpcode() == ISD::MUL) {
3228 // We just need the low half of the multiply; try both the signed
3229 // and unsigned forms. If the target supports both SMUL_LOHI and
3230 // UMUL_LOHI, form a preference by checking which forms of plain
3231 // MULH it supports.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003232 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3233 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3234 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3235 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
Dan Gohman525178c2007-10-08 18:33:35 +00003236 unsigned OpToUse = 0;
3237 if (HasSMUL_LOHI && !HasMULHS) {
3238 OpToUse = ISD::SMUL_LOHI;
3239 } else if (HasUMUL_LOHI && !HasMULHU) {
3240 OpToUse = ISD::UMUL_LOHI;
3241 } else if (HasSMUL_LOHI) {
3242 OpToUse = ISD::SMUL_LOHI;
3243 } else if (HasUMUL_LOHI) {
3244 OpToUse = ISD::UMUL_LOHI;
3245 }
3246 if (OpToUse) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003247 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003248 break;
3249 }
3250 }
3251 if (Node->getOpcode() == ISD::MULHS &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003252 TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003253 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3254 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003255 break;
3256 }
3257 if (Node->getOpcode() == ISD::MULHU &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003258 TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003259 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3260 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003261 break;
3262 }
3263 if (Node->getOpcode() == ISD::SDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003264 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003265 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3266 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003267 break;
3268 }
3269 if (Node->getOpcode() == ISD::UDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003270 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003271 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3272 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003273 break;
3274 }
Mon P Wang87c8a8f2008-12-18 20:03:17 +00003275
Dan Gohman82669522007-10-11 23:57:53 +00003276 // Check to see if we have a libcall for this operator.
3277 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3278 bool isSigned = false;
3279 switch (Node->getOpcode()) {
3280 case ISD::UDIV:
3281 case ISD::SDIV:
3282 if (VT == MVT::i32) {
3283 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang0c397192008-10-30 08:01:45 +00003284 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00003285 isSigned = Node->getOpcode() == ISD::SDIV;
3286 }
3287 break;
Chris Lattner31d71612008-10-04 21:27:46 +00003288 case ISD::MUL:
3289 if (VT == MVT::i32)
3290 LC = RTLIB::MUL_I32;
Nate Begeman9b994852009-01-24 22:12:48 +00003291 else if (VT == MVT::i64)
Scott Michel845145f2008-12-29 03:21:37 +00003292 LC = RTLIB::MUL_I64;
Chris Lattner31d71612008-10-04 21:27:46 +00003293 break;
Dan Gohman82669522007-10-11 23:57:53 +00003294 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003295 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3296 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003297 break;
Scott Micheld1e8d9c2009-01-21 04:58:48 +00003298 case ISD::FDIV:
3299 LC = GetFPLibCall(VT, RTLIB::DIV_F32, RTLIB::DIV_F64, RTLIB::DIV_F80,
3300 RTLIB::DIV_PPCF128);
3301 break;
Dan Gohman82669522007-10-11 23:57:53 +00003302 default: break;
3303 }
3304 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00003305 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003306 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003307 break;
3308 }
Mon P Wang0c397192008-10-30 08:01:45 +00003309
Duncan Sands83ec4b62008-06-06 12:08:01 +00003310 assert(Node->getValueType(0).isVector() &&
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003311 "Cannot expand this binary operator!");
3312 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003313 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003314 break;
3315 }
Evan Chengcc987612006-04-12 21:20:24 +00003316 case TargetLowering::Promote: {
3317 switch (Node->getOpcode()) {
3318 default: assert(0 && "Do not know how to promote this BinOp!");
3319 case ISD::AND:
3320 case ISD::OR:
3321 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003322 MVT OVT = Node->getValueType(0);
3323 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3324 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00003325 // Bit convert each of the values to the new type.
3326 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3327 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3328 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3329 // Bit convert the result back the original type.
3330 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3331 break;
3332 }
3333 }
3334 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003335 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003336 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003337
Dan Gohmane14ea862007-10-05 14:17:22 +00003338 case ISD::SMUL_LOHI:
3339 case ISD::UMUL_LOHI:
3340 case ISD::SDIVREM:
3341 case ISD::UDIVREM:
3342 // These nodes will only be produced by target-specific lowering, so
3343 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003344 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003345 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003346
3347 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3348 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3349 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003350 break;
3351
Chris Lattnera09f8482006-03-05 05:09:38 +00003352 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3353 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3354 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3355 case Expand: assert(0 && "Not possible");
3356 case Legal:
3357 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3358 break;
3359 case Promote:
3360 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3361 break;
3362 }
3363
3364 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3365
3366 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3367 default: assert(0 && "Operation not supported");
3368 case TargetLowering::Custom:
3369 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003370 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003371 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003372 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003373 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003374 // If this target supports fabs/fneg natively and select is cheap,
3375 // do this efficiently.
3376 if (!TLI.isSelectExpensive() &&
3377 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3378 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003379 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003380 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003381 // Get the sign bit of the RHS.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003382 MVT IVT =
Chris Lattner8f4191d2006-03-13 06:08:38 +00003383 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman475871a2008-07-27 21:46:04 +00003384 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Duncan Sands5480c042009-01-01 15:52:00 +00003385 SignBit = DAG.getSetCC(TLI.getSetCCResultType(IVT),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003386 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3387 // Get the absolute value of the result.
Dan Gohman475871a2008-07-27 21:46:04 +00003388 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Chris Lattner8f4191d2006-03-13 06:08:38 +00003389 // Select between the nabs and abs value based on the sign bit of
3390 // the input.
3391 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3392 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3393 AbsVal),
3394 AbsVal);
3395 Result = LegalizeOp(Result);
3396 break;
3397 }
3398
3399 // Otherwise, do bitwise ops!
Duncan Sands83ec4b62008-06-06 12:08:01 +00003400 MVT NVT =
Evan Cheng912095b2007-01-04 21:56:39 +00003401 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3402 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3403 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3404 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003405 break;
3406 }
Evan Cheng912095b2007-01-04 21:56:39 +00003407 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003408 break;
3409
Nate Begeman551bf3f2006-02-17 05:43:56 +00003410 case ISD::ADDC:
3411 case ISD::SUBC:
3412 Tmp1 = LegalizeOp(Node->getOperand(0));
3413 Tmp2 = LegalizeOp(Node->getOperand(1));
3414 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003415 Tmp3 = Result.getValue(0);
3416 Tmp4 = Result.getValue(1);
3417
3418 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3419 default: assert(0 && "This action is not supported yet!");
3420 case TargetLowering::Legal:
3421 break;
3422 case TargetLowering::Custom:
3423 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3424 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003425 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003426 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3427 }
3428 break;
3429 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003430 // Since this produces two values, make sure to remember that we legalized
3431 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003432 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3433 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3434 return Op.getResNo() ? Tmp4 : Tmp3;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003435
Nate Begeman551bf3f2006-02-17 05:43:56 +00003436 case ISD::ADDE:
3437 case ISD::SUBE:
3438 Tmp1 = LegalizeOp(Node->getOperand(0));
3439 Tmp2 = LegalizeOp(Node->getOperand(1));
3440 Tmp3 = LegalizeOp(Node->getOperand(2));
3441 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003442 Tmp3 = Result.getValue(0);
3443 Tmp4 = Result.getValue(1);
3444
3445 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3446 default: assert(0 && "This action is not supported yet!");
3447 case TargetLowering::Legal:
3448 break;
3449 case TargetLowering::Custom:
3450 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3451 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003452 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003453 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3454 }
3455 break;
3456 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003457 // Since this produces two values, make sure to remember that we legalized
3458 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003459 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3460 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3461 return Op.getResNo() ? Tmp4 : Tmp3;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003462
Nate Begeman419f8b62005-10-18 00:27:41 +00003463 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003464 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00003465 // TODO: handle the case where the Lo and Hi operands are not of legal type
3466 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3467 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3468 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003469 case TargetLowering::Promote:
3470 case TargetLowering::Custom:
3471 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003472 case TargetLowering::Legal:
3473 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3474 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3475 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003476 case TargetLowering::Expand:
3477 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3478 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3479 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003480 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00003481 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003482 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003483 break;
3484 }
3485 break;
3486 }
3487
Nate Begemanc105e192005-04-06 00:23:54 +00003488 case ISD::UREM:
3489 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003490 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003491 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3492 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003493
Nate Begemanc105e192005-04-06 00:23:54 +00003494 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003495 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3496 case TargetLowering::Custom:
3497 isCustom = true;
3498 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003499 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003500 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003501 if (isCustom) {
3502 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003503 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003504 }
Nate Begemanc105e192005-04-06 00:23:54 +00003505 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003506 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003507 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003508 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003509 MVT VT = Node->getValueType(0);
Dan Gohman525178c2007-10-08 18:33:35 +00003510
3511 // See if remainder can be lowered using two-result operations.
3512 SDVTList VTs = DAG.getVTList(VT, VT);
3513 if (Node->getOpcode() == ISD::SREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003514 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003515 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003516 break;
3517 }
3518 if (Node->getOpcode() == ISD::UREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003519 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003520 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003521 break;
3522 }
3523
Duncan Sands83ec4b62008-06-06 12:08:01 +00003524 if (VT.isInteger()) {
Dan Gohman525178c2007-10-08 18:33:35 +00003525 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003526 TargetLowering::Legal) {
3527 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003528 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003529 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3530 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003531 } else if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003532 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003533 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003534 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003535 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003536 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3537 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman475871a2008-07-27 21:46:04 +00003538 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003539 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003540 }
Dan Gohman0d974262007-11-06 22:11:54 +00003541 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003542 assert(VT.isFloatingPoint() &&
Dan Gohman0d974262007-11-06 22:11:54 +00003543 "remainder op must have integer or floating-point type");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003544 if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003545 Result = LegalizeOp(UnrollVectorOp(Op));
3546 } else {
3547 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003548 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3549 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003550 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003551 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman80176312007-11-05 23:35:22 +00003552 }
Nate Begemanc105e192005-04-06 00:23:54 +00003553 }
3554 break;
3555 }
Dan Gohman525178c2007-10-08 18:33:35 +00003556 }
Nate Begemanc105e192005-04-06 00:23:54 +00003557 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003558 case ISD::VAARG: {
3559 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3560 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3561
Duncan Sands83ec4b62008-06-06 12:08:01 +00003562 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003563 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3564 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003565 case TargetLowering::Custom:
3566 isCustom = true;
3567 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003568 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003569 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3570 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003571 Tmp1 = Result.getValue(1);
3572
3573 if (isCustom) {
3574 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003575 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003576 Result = LegalizeOp(Tmp2);
3577 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3578 }
3579 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003580 break;
3581 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003582 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003583 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003584 // Increment the pointer, VAList, to the next vaarg
Duncan Sands5c58a312008-11-03 11:51:11 +00003585 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00003586 DAG.getConstant(TLI.getTargetData()->
3587 getTypePaddedSize(VT.getTypeForMVT()),
3588 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00003589 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00003590 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003591 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003592 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003593 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003594 Result = LegalizeOp(Result);
3595 break;
3596 }
3597 }
3598 // Since VAARG produces two values, make sure to remember that we
3599 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003600 AddLegalizedOperand(SDValue(Node, 0), Result);
3601 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003602 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003603 }
3604
3605 case ISD::VACOPY:
3606 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3607 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3608 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3609
3610 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3611 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003612 case TargetLowering::Custom:
3613 isCustom = true;
3614 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003615 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003616 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3617 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003618 if (isCustom) {
3619 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003620 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003621 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003622 break;
3623 case TargetLowering::Expand:
3624 // This defaults to loading a pointer from the input and storing it to the
3625 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003626 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3627 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman499c1bd2008-04-17 02:09:26 +00003628 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3629 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003630 break;
3631 }
3632 break;
3633
3634 case ISD::VAEND:
3635 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3636 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3637
3638 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3639 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003640 case TargetLowering::Custom:
3641 isCustom = true;
3642 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003643 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003644 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003645 if (isCustom) {
3646 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003647 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003648 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003649 break;
3650 case TargetLowering::Expand:
3651 Result = Tmp1; // Default to a no-op, return the chain
3652 break;
3653 }
3654 break;
3655
3656 case ISD::VASTART:
3657 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3658 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3659
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003660 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3661
Nate Begemanacc398c2006-01-25 18:21:52 +00003662 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3663 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003664 case TargetLowering::Legal: break;
3665 case TargetLowering::Custom:
3666 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003667 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003668 break;
3669 }
3670 break;
3671
Nate Begeman35ef9132006-01-11 21:21:00 +00003672 case ISD::ROTL:
3673 case ISD::ROTR:
3674 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3675 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003676 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003677 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3678 default:
3679 assert(0 && "ROTL/ROTR legalize operation not supported");
3680 break;
3681 case TargetLowering::Legal:
3682 break;
3683 case TargetLowering::Custom:
3684 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003685 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelc9dc1142007-04-02 21:36:32 +00003686 break;
3687 case TargetLowering::Promote:
3688 assert(0 && "Do not know how to promote ROTL/ROTR");
3689 break;
3690 case TargetLowering::Expand:
3691 assert(0 && "Do not know how to expand ROTL/ROTR");
3692 break;
3693 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003694 break;
3695
Nate Begemand88fc032006-01-14 03:14:10 +00003696 case ISD::BSWAP:
3697 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3698 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003699 case TargetLowering::Custom:
3700 assert(0 && "Cannot custom legalize this yet!");
3701 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003702 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003703 break;
3704 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003705 MVT OVT = Tmp1.getValueType();
3706 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3707 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begemand88fc032006-01-14 03:14:10 +00003708
Chris Lattner456a93a2006-01-28 07:39:30 +00003709 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3710 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3711 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3712 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3713 break;
3714 }
3715 case TargetLowering::Expand:
3716 Result = ExpandBSWAP(Tmp1);
3717 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003718 }
3719 break;
3720
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003721 case ISD::CTPOP:
3722 case ISD::CTTZ:
3723 case ISD::CTLZ:
3724 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3725 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003726 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003727 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003728 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003729 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003730 TargetLowering::Custom) {
3731 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003732 if (Tmp1.getNode()) {
Scott Michel335f4f72007-08-02 02:22:46 +00003733 Result = Tmp1;
3734 }
Scott Michel910b66d2007-07-30 21:00:31 +00003735 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003736 break;
3737 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003738 MVT OVT = Tmp1.getValueType();
3739 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003740
3741 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003742 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3743 // Perform the larger operation, then subtract if needed.
3744 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003745 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003746 case ISD::CTPOP:
3747 Result = Tmp1;
3748 break;
3749 case ISD::CTTZ:
3750 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Duncan Sands5480c042009-01-01 15:52:00 +00003751 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003752 DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003753 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003754 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003755 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003756 break;
3757 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003758 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003759 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003760 DAG.getConstant(NVT.getSizeInBits() -
3761 OVT.getSizeInBits(), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003762 break;
3763 }
3764 break;
3765 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003766 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003767 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003768 break;
3769 }
3770 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003771
Chris Lattner2c8086f2005-04-02 05:00:07 +00003772 // Unary operators
3773 case ISD::FABS:
3774 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003775 case ISD::FSQRT:
3776 case ISD::FSIN:
3777 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003778 case ISD::FLOG:
3779 case ISD::FLOG2:
3780 case ISD::FLOG10:
3781 case ISD::FEXP:
3782 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00003783 case ISD::FTRUNC:
3784 case ISD::FFLOOR:
3785 case ISD::FCEIL:
3786 case ISD::FRINT:
3787 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003788 Tmp1 = LegalizeOp(Node->getOperand(0));
3789 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003790 case TargetLowering::Promote:
3791 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003792 isCustom = true;
3793 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003794 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003795 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003796 if (isCustom) {
3797 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003798 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00003799 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003800 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003801 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003802 switch (Node->getOpcode()) {
3803 default: assert(0 && "Unreachable!");
3804 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003805 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3806 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003807 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003808 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003809 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003810 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003811 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003812 Tmp2 = DAG.getConstantFP(0.0, VT);
Duncan Sands5480c042009-01-01 15:52:00 +00003813 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
3814 Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003815 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3816 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003817 break;
3818 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00003819 case ISD::FSQRT:
3820 case ISD::FSIN:
3821 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003822 case ISD::FLOG:
3823 case ISD::FLOG2:
3824 case ISD::FLOG10:
3825 case ISD::FEXP:
3826 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003827 case ISD::FTRUNC:
3828 case ISD::FFLOOR:
3829 case ISD::FCEIL:
3830 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00003831 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003832 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003833
3834 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003835 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003836 Result = LegalizeOp(UnrollVectorOp(Op));
3837 break;
3838 }
3839
Evan Cheng56966222007-01-12 02:11:51 +00003840 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003841 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003842 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003843 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3844 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003845 break;
3846 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003847 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3848 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003849 break;
3850 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003851 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3852 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003853 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003854 case ISD::FLOG:
3855 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3856 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3857 break;
3858 case ISD::FLOG2:
3859 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3860 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3861 break;
3862 case ISD::FLOG10:
3863 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3864 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3865 break;
3866 case ISD::FEXP:
3867 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3868 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3869 break;
3870 case ISD::FEXP2:
3871 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3872 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3873 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003874 case ISD::FTRUNC:
3875 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3876 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3877 break;
3878 case ISD::FFLOOR:
3879 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3880 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3881 break;
3882 case ISD::FCEIL:
3883 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3884 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3885 break;
3886 case ISD::FRINT:
3887 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3888 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3889 break;
3890 case ISD::FNEARBYINT:
3891 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3892 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3893 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00003894 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003895 default: assert(0 && "Unreachable!");
3896 }
Dan Gohman475871a2008-07-27 21:46:04 +00003897 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003898 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003899 break;
3900 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003901 }
3902 break;
3903 }
3904 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003905 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003906 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003907
3908 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003909 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003910 Result = LegalizeOp(UnrollVectorOp(Op));
3911 break;
3912 }
3913
3914 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003915 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3916 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003917 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003918 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003919 break;
3920 }
Chris Lattner35481892005-12-23 00:16:34 +00003921 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003922 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003923 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3924 Node->getValueType(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003925 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003926 // The input has to be a vector type, we have to either scalarize it, pack
3927 // it, or convert it based on whether the input vector type is legal.
Gabor Greifba36cb52008-08-28 21:40:38 +00003928 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00003929 int InIx = Node->getOperand(0).getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003930 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3931 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohman7f321562007-06-25 16:23:39 +00003932
3933 // Figure out if there is a simple type corresponding to this Vector
3934 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003935 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00003936 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003937 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003938 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3939 LegalizeOp(Node->getOperand(0)));
3940 break;
3941 } else if (NumElems == 1) {
3942 // Turn this into a bit convert of the scalar input.
3943 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3944 ScalarizeVectorOp(Node->getOperand(0)));
3945 break;
3946 } else {
3947 // FIXME: UNIMP! Store then reload
3948 assert(0 && "Cast from unsupported vector type not implemented yet!");
3949 }
Chris Lattner67993f72006-01-23 07:30:46 +00003950 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003951 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3952 Node->getOperand(0).getValueType())) {
3953 default: assert(0 && "Unknown operation action!");
3954 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003955 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3956 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003957 break;
3958 case TargetLowering::Legal:
3959 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003960 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003961 break;
3962 }
3963 }
3964 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003965 case ISD::CONVERT_RNDSAT: {
3966 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3967 switch (CvtCode) {
3968 default: assert(0 && "Unknown cvt code!");
3969 case ISD::CVT_SF:
3970 case ISD::CVT_UF:
Mon P Wang77cdf302008-11-10 20:54:11 +00003971 case ISD::CVT_FF:
Mon P Wang1cd46bb2008-12-09 07:27:39 +00003972 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003973 case ISD::CVT_FS:
3974 case ISD::CVT_FU:
3975 case ISD::CVT_SS:
3976 case ISD::CVT_SU:
3977 case ISD::CVT_US:
3978 case ISD::CVT_UU: {
3979 SDValue DTyOp = Node->getOperand(1);
3980 SDValue STyOp = Node->getOperand(2);
3981 SDValue RndOp = Node->getOperand(3);
3982 SDValue SatOp = Node->getOperand(4);
3983 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3984 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3985 case Legal:
3986 Tmp1 = LegalizeOp(Node->getOperand(0));
3987 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3988 RndOp, SatOp);
3989 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3990 TargetLowering::Custom) {
3991 Tmp1 = TLI.LowerOperation(Result, DAG);
3992 if (Tmp1.getNode()) Result = Tmp1;
3993 }
3994 break;
3995 case Promote:
3996 Result = PromoteOp(Node->getOperand(0));
3997 // For FP, make Op1 a i32
3998
Mon P Wang1cd46bb2008-12-09 07:27:39 +00003999 Result = DAG.getConvertRndSat(Op.getValueType(), Result,
Mon P Wang77cdf302008-11-10 20:54:11 +00004000 DTyOp, STyOp, RndOp, SatOp, CvtCode);
4001 break;
4002 }
4003 break;
4004 }
4005 } // end switch CvtCode
4006 break;
4007 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00004008 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00004009 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004010 case ISD::UINT_TO_FP: {
4011 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman7f8613e2008-08-14 20:04:46 +00004012 Result = LegalizeINT_TO_FP(Result, isSigned,
4013 Node->getValueType(0), Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004014 break;
4015 }
4016 case ISD::TRUNCATE:
4017 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4018 case Legal:
4019 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel546d7b52008-12-02 19:55:08 +00004020 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
4021 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
4022 case TargetLowering::Custom:
Mon P Wangf67303d2008-12-11 00:44:22 +00004023 isCustom = true;
4024 // FALLTHROUGH
Scott Michel546d7b52008-12-02 19:55:08 +00004025 case TargetLowering::Legal:
Mon P Wangf67303d2008-12-11 00:44:22 +00004026 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4027 if (isCustom) {
4028 Tmp1 = TLI.LowerOperation(Result, DAG);
4029 if (Tmp1.getNode()) Result = Tmp1;
4030 }
4031 break;
Mon P Wang9e5ecb82008-12-12 01:25:51 +00004032 case TargetLowering::Expand:
4033 assert(Result.getValueType().isVector() && "must be vector type");
4034 // Unroll the truncate. We should do better.
4035 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Schellerb0a5cdd2008-12-02 12:12:25 +00004036 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004037 break;
4038 case Expand:
4039 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4040
4041 // Since the result is legal, we should just be able to truncate the low
4042 // part of the source.
4043 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
4044 break;
4045 case Promote:
4046 Result = PromoteOp(Node->getOperand(0));
4047 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
4048 break;
4049 }
4050 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004051
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004052 case ISD::FP_TO_SINT:
4053 case ISD::FP_TO_UINT:
4054 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4055 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00004056 Tmp1 = LegalizeOp(Node->getOperand(0));
4057
Chris Lattner1618beb2005-07-29 00:11:56 +00004058 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
4059 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004060 case TargetLowering::Custom:
4061 isCustom = true;
4062 // FALLTHROUGH
4063 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004064 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004065 if (isCustom) {
4066 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004067 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00004068 }
4069 break;
4070 case TargetLowering::Promote:
4071 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
4072 Node->getOpcode() == ISD::FP_TO_SINT);
4073 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00004074 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00004075 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004076 SDValue True, False;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004077 MVT VT = Node->getOperand(0).getValueType();
4078 MVT NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00004079 const uint64_t zero[] = {0, 0};
Duncan Sands83ec4b62008-06-06 12:08:01 +00004080 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
4081 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohmanc7773bf2008-02-29 01:44:25 +00004082 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00004083 Tmp2 = DAG.getConstantFP(apf, VT);
Duncan Sands5480c042009-01-01 15:52:00 +00004084 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(VT), Node->getOperand(0),
4085 Tmp2, ISD::SETLT);
Nate Begemand2558e32005-08-14 01:20:53 +00004086 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
4087 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00004088 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00004089 Tmp2));
4090 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00004091 DAG.getConstant(x, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004092 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
4093 break;
Nate Begemand2558e32005-08-14 01:20:53 +00004094 } else {
4095 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
4096 }
4097 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00004098 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004099 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00004100 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004101 MVT VT = Op.getValueType();
4102 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004103 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004104 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004105 if (Node->getOpcode() == ISD::FP_TO_SINT) {
4106 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
4107 Node->getOperand(0), DAG.getValueType(MVT::f64));
4108 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
4109 DAG.getIntPtrConstant(1));
4110 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
4111 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004112 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4113 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4114 Tmp2 = DAG.getConstantFP(apf, OVT);
4115 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4116 // FIXME: generated code sucks.
4117 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
4118 DAG.getNode(ISD::ADD, MVT::i32,
4119 DAG.getNode(ISD::FP_TO_SINT, VT,
4120 DAG.getNode(ISD::FSUB, OVT,
4121 Node->getOperand(0), Tmp2)),
4122 DAG.getConstant(0x80000000, MVT::i32)),
4123 DAG.getNode(ISD::FP_TO_SINT, VT,
4124 Node->getOperand(0)),
4125 DAG.getCondCode(ISD::SETGE));
4126 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004127 break;
4128 }
Dan Gohmana2e94852008-03-10 23:03:31 +00004129 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsb2ff8852008-07-17 02:36:29 +00004130 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4131 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4132 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman475871a2008-07-27 21:46:04 +00004133 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00004134 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00004135 break;
4136 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004137 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004138 Tmp1 = PromoteOp(Node->getOperand(0));
4139 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4140 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004141 break;
4142 }
4143 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004144
Chris Lattnerf2670a82008-01-16 06:57:07 +00004145 case ISD::FP_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004146 MVT DstVT = Op.getValueType();
4147 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004148 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4149 // The only other way we can lower this is to turn it into a STORE,
4150 // LOAD pair, targetting a temporary location (a stack slot).
4151 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
4152 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00004153 }
4154 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4155 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4156 case Legal:
4157 Tmp1 = LegalizeOp(Node->getOperand(0));
4158 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4159 break;
4160 case Promote:
4161 Tmp1 = PromoteOp(Node->getOperand(0));
4162 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4163 break;
4164 }
4165 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004166 }
Dale Johannesen5411a392007-08-09 01:04:01 +00004167 case ISD::FP_ROUND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004168 MVT DstVT = Op.getValueType();
4169 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004170 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4171 if (SrcVT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00004172 SDValue Lo;
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004173 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00004174 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004175 if (DstVT!=MVT::f64)
4176 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00004177 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00004178 }
Chris Lattner0bd48932008-01-17 07:00:52 +00004179 // The only other way we can lower this is to turn it into a STORE,
4180 // LOAD pair, targetting a temporary location (a stack slot).
4181 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4182 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00004183 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00004184 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4185 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4186 case Legal:
4187 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004188 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004189 break;
4190 case Promote:
4191 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004192 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4193 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004194 break;
4195 }
4196 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004197 }
Chris Lattner13c78e22005-09-02 00:18:10 +00004198 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004199 case ISD::ZERO_EXTEND:
4200 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004201 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00004202 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004203 case Legal:
4204 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel82747a52008-04-30 00:26:38 +00004205 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel0123b7d2008-02-15 23:05:48 +00004206 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4207 TargetLowering::Custom) {
Scott Michel82747a52008-04-30 00:26:38 +00004208 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004209 if (Tmp1.getNode()) Result = Tmp1;
Scott Michel0123b7d2008-02-15 23:05:48 +00004210 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004211 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004212 case Promote:
4213 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00004214 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004215 Tmp1 = PromoteOp(Node->getOperand(0));
4216 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00004217 break;
Chris Lattner1713e732005-01-16 00:38:00 +00004218 case ISD::ZERO_EXTEND:
4219 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004220 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00004221 Result = DAG.getZeroExtendInReg(Result,
4222 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00004223 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004224 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00004225 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004226 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00004227 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004228 Result,
4229 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00004230 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004231 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004232 }
4233 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00004234 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00004235 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00004236 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004237 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00004238
4239 // If this operation is not supported, convert it to a shl/shr or load/store
4240 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004241 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4242 default: assert(0 && "This action not supported for this op yet!");
4243 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004244 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004245 break;
4246 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00004247 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00004248 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00004249 // NOTE: we could fall back on load/store here too for targets without
4250 // SAR. However, it is doubtful that any exist.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004251 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4252 ExtraVT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00004253 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00004254 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4255 Node->getOperand(0), ShiftCst);
4256 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4257 Result, ShiftCst);
4258 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00004259 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00004260 // EXTLOAD pair, targetting a temporary location (a stack slot).
4261
4262 // NOTE: there is a choice here between constantly creating new stack
4263 // slots and always reusing the same one. We currently always create
4264 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00004265 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4266 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00004267 } else {
4268 assert(0 && "Unknown op");
4269 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004270 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00004271 }
Chris Lattner0f69b292005-01-15 06:18:18 +00004272 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004273 }
Duncan Sands36397f52007-07-27 12:58:54 +00004274 case ISD::TRAMPOLINE: {
Dan Gohman475871a2008-07-27 21:46:04 +00004275 SDValue Ops[6];
Duncan Sands36397f52007-07-27 12:58:54 +00004276 for (unsigned i = 0; i != 6; ++i)
4277 Ops[i] = LegalizeOp(Node->getOperand(i));
4278 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4279 // The only option for this node is to custom lower it.
4280 Result = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004281 assert(Result.getNode() && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00004282
4283 // Since trampoline produces two values, make sure to remember that we
4284 // legalized both of them.
4285 Tmp1 = LegalizeOp(Result.getValue(1));
4286 Result = LegalizeOp(Result);
Dan Gohman475871a2008-07-27 21:46:04 +00004287 AddLegalizedOperand(SDValue(Node, 0), Result);
4288 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00004289 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00004290 }
Dan Gohman9c78a392008-05-14 00:43:10 +00004291 case ISD::FLT_ROUNDS_: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004292 MVT VT = Node->getValueType(0);
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004293 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4294 default: assert(0 && "This action not supported for this op yet!");
4295 case TargetLowering::Custom:
4296 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004297 if (Result.getNode()) break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004298 // Fall Thru
4299 case TargetLowering::Legal:
4300 // If this operation is not supported, lower it to constant 1
4301 Result = DAG.getConstant(1, VT);
4302 break;
4303 }
Dan Gohman9ab9ee82008-05-12 16:07:15 +00004304 break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004305 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004306 case ISD::TRAP: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004307 MVT VT = Node->getValueType(0);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004308 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4309 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00004310 case TargetLowering::Legal:
4311 Tmp1 = LegalizeOp(Node->getOperand(0));
4312 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4313 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004314 case TargetLowering::Custom:
4315 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004316 if (Result.getNode()) break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004317 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00004318 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004319 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00004320 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004321 TargetLowering::ArgListTy Args;
Dan Gohman475871a2008-07-27 21:46:04 +00004322 std::pair<SDValue,SDValue> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00004323 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00004324 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00004325 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner034f12e2008-01-15 22:09:33 +00004326 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004327 Result = CallResult.second;
4328 break;
4329 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004330 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004331 }
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004332
Bill Wendling74c37652008-12-09 22:08:41 +00004333 case ISD::SADDO:
4334 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004335 MVT VT = Node->getValueType(0);
4336 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4337 default: assert(0 && "This action not supported for this op yet!");
4338 case TargetLowering::Custom:
4339 Result = TLI.LowerOperation(Op, DAG);
4340 if (Result.getNode()) break;
4341 // FALLTHROUGH
4342 case TargetLowering::Legal: {
4343 SDValue LHS = LegalizeOp(Node->getOperand(0));
4344 SDValue RHS = LegalizeOp(Node->getOperand(1));
4345
Bill Wendling74c37652008-12-09 22:08:41 +00004346 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
4347 ISD::ADD : ISD::SUB, LHS.getValueType(),
4348 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004349 MVT OType = Node->getValueType(1);
4350
Bill Wendlinga6af91a2008-11-25 08:19:22 +00004351 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004352
Bill Wendling740464e2008-11-25 19:40:17 +00004353 // LHSSign -> LHS >= 0
4354 // RHSSign -> RHS >= 0
4355 // SumSign -> Sum >= 0
4356 //
Bill Wendling74c37652008-12-09 22:08:41 +00004357 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00004358 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00004359 // Sub:
4360 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00004361 //
4362 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4363 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
Bill Wendling74c37652008-12-09 22:08:41 +00004364 SDValue SignsMatch = DAG.getSetCC(OType, LHSSign, RHSSign,
4365 Node->getOpcode() == ISD::SADDO ?
4366 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004367
Bill Wendling740464e2008-11-25 19:40:17 +00004368 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4369 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004370
Bill Wendling74c37652008-12-09 22:08:41 +00004371 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004372
4373 MVT ValueVTs[] = { LHS.getValueType(), OType };
4374 SDValue Ops[] = { Sum, Cmp };
4375
Duncan Sandsaaffa052008-12-01 11:41:29 +00004376 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4377 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004378 SDNode *RNode = Result.getNode();
4379 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4380 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4381 break;
4382 }
4383 }
4384
4385 break;
4386 }
Bill Wendling74c37652008-12-09 22:08:41 +00004387 case ISD::UADDO:
4388 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00004389 MVT VT = Node->getValueType(0);
4390 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4391 default: assert(0 && "This action not supported for this op yet!");
4392 case TargetLowering::Custom:
4393 Result = TLI.LowerOperation(Op, DAG);
4394 if (Result.getNode()) break;
4395 // FALLTHROUGH
4396 case TargetLowering::Legal: {
4397 SDValue LHS = LegalizeOp(Node->getOperand(0));
4398 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004399
Bill Wendling74c37652008-12-09 22:08:41 +00004400 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
4401 ISD::ADD : ISD::SUB, LHS.getValueType(),
4402 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004403 MVT OType = Node->getValueType(1);
Bill Wendling74c37652008-12-09 22:08:41 +00004404 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS,
4405 Node->getOpcode () == ISD::UADDO ?
4406 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004407
Bill Wendling41ea7e72008-11-24 19:21:46 +00004408 MVT ValueVTs[] = { LHS.getValueType(), OType };
4409 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004410
Duncan Sandsaaffa052008-12-01 11:41:29 +00004411 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4412 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004413 SDNode *RNode = Result.getNode();
4414 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4415 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4416 break;
4417 }
4418 }
4419
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004420 break;
4421 }
Bill Wendling74c37652008-12-09 22:08:41 +00004422 case ISD::SMULO:
4423 case ISD::UMULO: {
4424 MVT VT = Node->getValueType(0);
4425 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4426 default: assert(0 && "This action is not supported at all!");
4427 case TargetLowering::Custom:
4428 Result = TLI.LowerOperation(Op, DAG);
4429 if (Result.getNode()) break;
4430 // Fall Thru
4431 case TargetLowering::Legal:
4432 // FIXME: According to Hacker's Delight, this can be implemented in
4433 // target independent lowering, but it would be inefficient, since it
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00004434 // requires a division + a branch.
Bill Wendling74c37652008-12-09 22:08:41 +00004435 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4436 break;
4437 }
4438 break;
4439 }
4440
Chris Lattner45b8caf2005-01-15 07:15:18 +00004441 }
Chris Lattner456a93a2006-01-28 07:39:30 +00004442
Chris Lattner4ddd2832006-04-08 04:13:17 +00004443 assert(Result.getValueType() == Op.getValueType() &&
4444 "Bad legalization!");
4445
Chris Lattner456a93a2006-01-28 07:39:30 +00004446 // Make sure that the generated code is itself legal.
4447 if (Result != Op)
4448 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004449
Chris Lattner45982da2005-05-12 16:53:42 +00004450 // Note that LegalizeOp may be reentered even from single-use nodes, which
4451 // means that we always must cache transformed nodes.
4452 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004453 return Result;
4454}
4455
Chris Lattner8b6fa222005-01-15 22:16:26 +00004456/// PromoteOp - Given an operation that produces a value in an invalid type,
4457/// promote it to compute the value into a larger type. The produced value will
4458/// have the correct bits for the low portion of the register, but no guarantee
4459/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman475871a2008-07-27 21:46:04 +00004460SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004461 MVT VT = Op.getValueType();
4462 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004463 assert(getTypeAction(VT) == Promote &&
4464 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004465 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner03c85462005-01-15 05:21:40 +00004466 "Cannot promote to smaller type!");
4467
Dan Gohman475871a2008-07-27 21:46:04 +00004468 SDValue Tmp1, Tmp2, Tmp3;
4469 SDValue Result;
Gabor Greifba36cb52008-08-28 21:40:38 +00004470 SDNode *Node = Op.getNode();
Chris Lattner03c85462005-01-15 05:21:40 +00004471
Dan Gohman475871a2008-07-27 21:46:04 +00004472 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004473 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004474
Chris Lattner03c85462005-01-15 05:21:40 +00004475 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004476 case ISD::CopyFromReg:
4477 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004478 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004479#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004480 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004481#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004482 assert(0 && "Do not know how to promote this operator!");
4483 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004484 case ISD::UNDEF:
4485 Result = DAG.getNode(ISD::UNDEF, NVT);
4486 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004487 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004488 if (VT != MVT::i1)
4489 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4490 else
4491 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004492 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4493 break;
4494 case ISD::ConstantFP:
4495 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4496 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4497 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004498
Duncan Sands5480c042009-01-01 15:52:00 +00004499 case ISD::SETCC: {
4500 MVT VT0 = Node->getOperand(0).getValueType();
4501 assert(isTypeLegal(TLI.getSetCCResultType(VT0))
Nate Begeman5922f562008-03-14 00:53:31 +00004502 && "SetCC type is not legal??");
Duncan Sands5480c042009-01-01 15:52:00 +00004503 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(VT0),
Nate Begeman5922f562008-03-14 00:53:31 +00004504 Node->getOperand(0), Node->getOperand(1),
4505 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004506 break;
Duncan Sands5480c042009-01-01 15:52:00 +00004507 }
Chris Lattner03c85462005-01-15 05:21:40 +00004508 case ISD::TRUNCATE:
4509 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4510 case Legal:
4511 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004512 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner03c85462005-01-15 05:21:40 +00004513 "This truncation doesn't make sense!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004514 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Chris Lattner03c85462005-01-15 05:21:40 +00004515 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4516 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004517 case Promote:
4518 // The truncation is not required, because we don't guarantee anything
4519 // about high bits anyway.
4520 Result = PromoteOp(Node->getOperand(0));
4521 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004522 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004523 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4524 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004525 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004526 }
4527 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004528 case ISD::SIGN_EXTEND:
4529 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004530 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004531 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4532 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4533 case Legal:
4534 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004535 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004536 break;
4537 case Promote:
4538 // Promote the reg if it's smaller.
4539 Result = PromoteOp(Node->getOperand(0));
4540 // The high bits are not guaranteed to be anything. Insert an extend.
4541 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004542 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004543 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004544 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004545 Result = DAG.getZeroExtendInReg(Result,
4546 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004547 break;
4548 }
4549 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00004550 case ISD::CONVERT_RNDSAT: {
4551 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4552 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4553 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4554 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4555 "can only promote integers");
4556 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4557 Node->getOperand(1), Node->getOperand(2),
4558 Node->getOperand(3), Node->getOperand(4),
4559 CvtCode);
4560 break;
4561
4562 }
Chris Lattner35481892005-12-23 00:16:34 +00004563 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004564 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4565 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004566 Result = PromoteOp(Result);
4567 break;
4568
Chris Lattner8b6fa222005-01-15 22:16:26 +00004569 case ISD::FP_EXTEND:
4570 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4571 case ISD::FP_ROUND:
4572 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4573 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4574 case Promote: assert(0 && "Unreachable with 2 FP types!");
4575 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004576 if (Node->getConstantOperandVal(1) == 0) {
4577 // Input is legal? Do an FP_ROUND_INREG.
4578 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4579 DAG.getValueType(VT));
4580 } else {
4581 // Just remove the truncate, it isn't affecting the value.
4582 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4583 Node->getOperand(1));
4584 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004585 break;
4586 }
4587 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004588 case ISD::SINT_TO_FP:
4589 case ISD::UINT_TO_FP:
4590 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4591 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004592 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004593 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004594 break;
4595
4596 case Promote:
4597 Result = PromoteOp(Node->getOperand(0));
4598 if (Node->getOpcode() == ISD::SINT_TO_FP)
4599 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004600 Result,
4601 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004602 else
Chris Lattner23993562005-04-13 02:38:47 +00004603 Result = DAG.getZeroExtendInReg(Result,
4604 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004605 // No extra round required here.
4606 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004607 break;
4608 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004609 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4610 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00004611 // Round if we cannot tolerate excess precision.
4612 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004613 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4614 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004615 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004616 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004617 break;
4618
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004619 case ISD::SIGN_EXTEND_INREG:
4620 Result = PromoteOp(Node->getOperand(0));
4621 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4622 Node->getOperand(1));
4623 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004624 case ISD::FP_TO_SINT:
4625 case ISD::FP_TO_UINT:
4626 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4627 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004628 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004629 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004630 break;
4631 case Promote:
4632 // The input result is prerounded, so we don't have to do anything
4633 // special.
4634 Tmp1 = PromoteOp(Node->getOperand(0));
4635 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004636 }
Nate Begemand2558e32005-08-14 01:20:53 +00004637 // If we're promoting a UINT to a larger size, check to see if the new node
4638 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4639 // we can use that instead. This allows us to generate better code for
4640 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4641 // legal, such as PowerPC.
4642 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004643 !TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NVT) &&
4644 (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT) ||
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004645 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004646 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4647 } else {
4648 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4649 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004650 break;
4651
Chris Lattner2c8086f2005-04-02 05:00:07 +00004652 case ISD::FABS:
4653 case ISD::FNEG:
4654 Tmp1 = PromoteOp(Node->getOperand(0));
4655 assert(Tmp1.getValueType() == NVT);
4656 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4657 // NOTE: we do not have to do any extra rounding here for
4658 // NoExcessFPPrecision, because we know the input will have the appropriate
4659 // precision, and these operations don't modify precision at all.
4660 break;
4661
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004662 case ISD::FLOG:
4663 case ISD::FLOG2:
4664 case ISD::FLOG10:
4665 case ISD::FEXP:
4666 case ISD::FEXP2:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004667 case ISD::FSQRT:
4668 case ISD::FSIN:
4669 case ISD::FCOS:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00004670 case ISD::FTRUNC:
4671 case ISD::FFLOOR:
4672 case ISD::FCEIL:
4673 case ISD::FRINT:
4674 case ISD::FNEARBYINT:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004675 Tmp1 = PromoteOp(Node->getOperand(0));
4676 assert(Tmp1.getValueType() == NVT);
4677 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004678 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004679 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4680 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004681 break;
4682
Evan Cheng9d24ac52008-09-09 23:35:53 +00004683 case ISD::FPOW:
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004684 case ISD::FPOWI: {
Evan Cheng9d24ac52008-09-09 23:35:53 +00004685 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004686 // directly as well, which may be better.
4687 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng9d24ac52008-09-09 23:35:53 +00004688 Tmp2 = Node->getOperand(1);
4689 if (Node->getOpcode() == ISD::FPOW)
4690 Tmp2 = PromoteOp(Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004691 assert(Tmp1.getValueType() == NVT);
Evan Cheng9d24ac52008-09-09 23:35:53 +00004692 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004693 if (NoExcessFPPrecision)
4694 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4695 DAG.getValueType(VT));
4696 break;
4697 }
4698
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004699 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang28873102008-06-25 08:15:39 +00004700 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004701 Tmp2 = PromoteOp(Node->getOperand(2));
4702 Tmp3 = PromoteOp(Node->getOperand(3));
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004703 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getMemoryVT(),
4704 AtomNode->getChain(),
Mon P Wang28873102008-06-25 08:15:39 +00004705 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004706 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004707 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004708 // Remember that we legalized the chain.
4709 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4710 break;
4711 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004712 case ISD::ATOMIC_LOAD_ADD:
4713 case ISD::ATOMIC_LOAD_SUB:
4714 case ISD::ATOMIC_LOAD_AND:
4715 case ISD::ATOMIC_LOAD_OR:
4716 case ISD::ATOMIC_LOAD_XOR:
4717 case ISD::ATOMIC_LOAD_NAND:
4718 case ISD::ATOMIC_LOAD_MIN:
4719 case ISD::ATOMIC_LOAD_MAX:
4720 case ISD::ATOMIC_LOAD_UMIN:
4721 case ISD::ATOMIC_LOAD_UMAX:
4722 case ISD::ATOMIC_SWAP: {
Mon P Wang28873102008-06-25 08:15:39 +00004723 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004724 Tmp2 = PromoteOp(Node->getOperand(2));
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004725 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getMemoryVT(),
4726 AtomNode->getChain(),
Mon P Wang28873102008-06-25 08:15:39 +00004727 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004728 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004729 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004730 // Remember that we legalized the chain.
4731 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4732 break;
4733 }
4734
Chris Lattner03c85462005-01-15 05:21:40 +00004735 case ISD::AND:
4736 case ISD::OR:
4737 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004738 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004739 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004740 case ISD::MUL:
4741 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004742 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004743 // that too is okay if they are integer operations.
4744 Tmp1 = PromoteOp(Node->getOperand(0));
4745 Tmp2 = PromoteOp(Node->getOperand(1));
4746 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4747 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004748 break;
4749 case ISD::FADD:
4750 case ISD::FSUB:
4751 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004752 Tmp1 = PromoteOp(Node->getOperand(0));
4753 Tmp2 = PromoteOp(Node->getOperand(1));
4754 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4755 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4756
4757 // Floating point operations will give excess precision that we may not be
4758 // able to tolerate. If we DO allow excess precision, just leave it,
4759 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004760 // FIXME: Why would we need to round FP ops more than integer ones?
4761 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004762 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004763 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4764 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004765 break;
4766
Chris Lattner8b6fa222005-01-15 22:16:26 +00004767 case ISD::SDIV:
4768 case ISD::SREM:
4769 // These operators require that their input be sign extended.
4770 Tmp1 = PromoteOp(Node->getOperand(0));
4771 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004772 if (NVT.isInteger()) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004773 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4774 DAG.getValueType(VT));
4775 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4776 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004777 }
4778 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4779
4780 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004781 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004782 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4783 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004784 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004785 case ISD::FDIV:
4786 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004787 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004788 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004789 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004790 case Expand: assert(0 && "not implemented");
4791 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4792 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004793 }
4794 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004795 case Expand: assert(0 && "not implemented");
4796 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4797 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004798 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004799 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4800
4801 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004802 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004803 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4804 DAG.getValueType(VT));
4805 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004806
4807 case ISD::UDIV:
4808 case ISD::UREM:
4809 // These operators require that their input be zero extended.
4810 Tmp1 = PromoteOp(Node->getOperand(0));
4811 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004812 assert(NVT.isInteger() && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004813 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4814 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004815 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4816 break;
4817
4818 case ISD::SHL:
4819 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004820 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004821 break;
4822 case ISD::SRA:
4823 // The input value must be properly sign extended.
4824 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004825 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4826 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004827 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004828 break;
4829 case ISD::SRL:
4830 // The input value must be properly zero extended.
4831 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004832 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004833 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004834 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004835
4836 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004837 Tmp1 = Node->getOperand(0); // Get the chain.
4838 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004839 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4840 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands126d9072008-07-04 11:47:58 +00004841 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman0aed7842006-01-28 03:14:31 +00004842 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004843 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004844 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004845 // Increment the pointer, VAList, to the next vaarg
4846 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004847 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman0aed7842006-01-28 03:14:31 +00004848 TLI.getPointerTy()));
4849 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00004850 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004851 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004852 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004853 }
4854 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004855 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004856 break;
4857
Evan Cheng466685d2006-10-09 20:57:25 +00004858 case ISD::LOAD: {
4859 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004860 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4861 ? ISD::EXTLOAD : LD->getExtensionType();
4862 Result = DAG.getExtLoad(ExtType, NVT,
4863 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004864 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004865 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004866 LD->isVolatile(),
4867 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004868 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004869 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004870 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004871 }
Scott Michel8bf61e82008-06-02 22:18:03 +00004872 case ISD::SELECT: {
Chris Lattner03c85462005-01-15 05:21:40 +00004873 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4874 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel8bf61e82008-06-02 22:18:03 +00004875
Duncan Sands83ec4b62008-06-06 12:08:01 +00004876 MVT VT2 = Tmp2.getValueType();
Scott Michel8bf61e82008-06-02 22:18:03 +00004877 assert(VT2 == Tmp3.getValueType()
Scott Michelba12f572008-06-03 19:13:20 +00004878 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4879 // Ensure that the resulting node is at least the same size as the operands'
4880 // value types, because we cannot assume that TLI.getSetCCValueType() is
4881 // constant.
4882 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004883 break;
Scott Michel8bf61e82008-06-02 22:18:03 +00004884 }
Nate Begeman9373a812005-08-10 20:51:12 +00004885 case ISD::SELECT_CC:
4886 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4887 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4888 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004889 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004890 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004891 case ISD::BSWAP:
4892 Tmp1 = Node->getOperand(0);
4893 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4894 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4895 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004896 DAG.getConstant(NVT.getSizeInBits() -
4897 VT.getSizeInBits(),
Nate Begemand88fc032006-01-14 03:14:10 +00004898 TLI.getShiftAmountTy()));
4899 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004900 case ISD::CTPOP:
4901 case ISD::CTTZ:
4902 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004903 // Zero extend the argument
4904 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004905 // Perform the larger operation, then subtract if needed.
4906 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004907 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004908 case ISD::CTPOP:
4909 Result = Tmp1;
4910 break;
4911 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004912 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Duncan Sands5480c042009-01-01 15:52:00 +00004913 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004914 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanb55757e2007-05-18 17:52:13 +00004915 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004916 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004917 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004918 break;
4919 case ISD::CTLZ:
4920 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004921 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004922 DAG.getConstant(NVT.getSizeInBits() -
4923 VT.getSizeInBits(), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004924 break;
4925 }
4926 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004927 case ISD::EXTRACT_SUBVECTOR:
4928 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004929 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004930 case ISD::EXTRACT_VECTOR_ELT:
4931 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4932 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004933 }
4934
Gabor Greifba36cb52008-08-28 21:40:38 +00004935 assert(Result.getNode() && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004936
4937 // Make sure the result is itself legal.
4938 Result = LegalizeOp(Result);
4939
4940 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004941 AddPromotedOperand(Op, Result);
4942 return Result;
4943}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004944
Dan Gohman7f321562007-06-25 16:23:39 +00004945/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4946/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4947/// based on the vector type. The return type of this matches the element type
4948/// of the vector, which may not be legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00004949SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004950 // We know that operand #0 is the Vec vector. If the index is a constant
4951 // or if the invec is a supported hardware type, we can use it. Otherwise,
4952 // lower to a store then an indexed load.
Dan Gohman475871a2008-07-27 21:46:04 +00004953 SDValue Vec = Op.getOperand(0);
4954 SDValue Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004955
Duncan Sands83ec4b62008-06-06 12:08:01 +00004956 MVT TVT = Vec.getValueType();
4957 unsigned NumElems = TVT.getVectorNumElements();
Chris Lattner15972212006-03-31 17:55:51 +00004958
Dan Gohman7f321562007-06-25 16:23:39 +00004959 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4960 default: assert(0 && "This action is not supported yet!");
4961 case TargetLowering::Custom: {
4962 Vec = LegalizeOp(Vec);
4963 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004964 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004965 if (Tmp3.getNode())
Dan Gohman7f321562007-06-25 16:23:39 +00004966 return Tmp3;
4967 break;
4968 }
4969 case TargetLowering::Legal:
4970 if (isTypeLegal(TVT)) {
4971 Vec = LegalizeOp(Vec);
4972 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004973 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004974 }
4975 break;
Mon P Wang0c397192008-10-30 08:01:45 +00004976 case TargetLowering::Promote:
4977 assert(TVT.isVector() && "not vector type");
4978 // fall thru to expand since vectors are by default are promote
Dan Gohman7f321562007-06-25 16:23:39 +00004979 case TargetLowering::Expand:
4980 break;
4981 }
4982
4983 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004984 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004985 Op = ScalarizeVectorOp(Vec);
4986 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00004987 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00004988 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004989 SDValue Lo, Hi;
Chris Lattner15972212006-03-31 17:55:51 +00004990 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004991 if (CIdx->getZExtValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00004992 Vec = Lo;
4993 } else {
4994 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004995 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00004996 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004997 }
Dan Gohman7f321562007-06-25 16:23:39 +00004998
Chris Lattner15972212006-03-31 17:55:51 +00004999 // It's now an extract from the appropriate high or low part. Recurse.
5000 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00005001 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00005002 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00005003 // Store the value to a temporary stack slot, then LOAD the scalar
5004 // element back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005005 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
5006 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohman7f321562007-06-25 16:23:39 +00005007
5008 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005009 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohman7f321562007-06-25 16:23:39 +00005010 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
5011 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005012
Duncan Sands8e4eb092008-06-08 20:54:56 +00005013 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00005014 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005015 else
Chris Lattnerf185e672007-10-19 16:47:35 +00005016 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005017
Dan Gohman7f321562007-06-25 16:23:39 +00005018 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
5019
5020 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00005021 }
Dan Gohman7f321562007-06-25 16:23:39 +00005022 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00005023}
5024
Dan Gohman7f321562007-06-25 16:23:39 +00005025/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00005026/// we assume the operation can be split if it is not already legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005027SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman65956352007-06-13 15:12:02 +00005028 // We know that operand #0 is the Vec vector. For now we assume the index
5029 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman475871a2008-07-27 21:46:04 +00005030 SDValue Vec = Op.getOperand(0);
5031 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00005032
Duncan Sands83ec4b62008-06-06 12:08:01 +00005033 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohman65956352007-06-13 15:12:02 +00005034
Duncan Sands83ec4b62008-06-06 12:08:01 +00005035 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman65956352007-06-13 15:12:02 +00005036 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00005037 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00005038 }
5039
5040 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00005041 SDValue Lo, Hi;
Dan Gohman65956352007-06-13 15:12:02 +00005042 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005043 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohman65956352007-06-13 15:12:02 +00005044 Vec = Lo;
5045 } else {
5046 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005047 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
5048 Idx.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00005049 }
5050
5051 // It's now an extract from the appropriate high or low part. Recurse.
5052 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00005053 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00005054}
5055
Nate Begeman750ac1b2006-02-01 07:19:44 +00005056/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
5057/// with condition CC on the current target. This usually involves legalizing
5058/// or promoting the arguments. In the case where LHS and RHS must be expanded,
5059/// there may be no choice but to create a new SetCC node to represent the
5060/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00005061/// LHS, and the SDValue returned in RHS has a nil SDNode value.
5062void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
5063 SDValue &RHS,
5064 SDValue &CC) {
5065 SDValue Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005066
5067 switch (getTypeAction(LHS.getValueType())) {
5068 case Legal:
5069 Tmp1 = LegalizeOp(LHS); // LHS
5070 Tmp2 = LegalizeOp(RHS); // RHS
5071 break;
5072 case Promote:
5073 Tmp1 = PromoteOp(LHS); // LHS
5074 Tmp2 = PromoteOp(RHS); // RHS
5075
5076 // If this is an FP compare, the operands have already been extended.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005077 if (LHS.getValueType().isInteger()) {
5078 MVT VT = LHS.getValueType();
5079 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00005080
5081 // Otherwise, we have to insert explicit sign or zero extends. Note
5082 // that we could insert sign extends for ALL conditions, but zero extend
5083 // is cheaper on many machines (an AND instead of two shifts), so prefer
5084 // it.
5085 switch (cast<CondCodeSDNode>(CC)->get()) {
5086 default: assert(0 && "Unknown integer comparison!");
5087 case ISD::SETEQ:
5088 case ISD::SETNE:
5089 case ISD::SETUGE:
5090 case ISD::SETUGT:
5091 case ISD::SETULE:
5092 case ISD::SETULT:
5093 // ALL of these operations will work if we either sign or zero extend
5094 // the operands (including the unsigned comparisons!). Zero extend is
5095 // usually a simpler/cheaper operation, so prefer it.
5096 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
5097 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
5098 break;
5099 case ISD::SETGE:
5100 case ISD::SETGT:
5101 case ISD::SETLT:
5102 case ISD::SETLE:
5103 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
5104 DAG.getValueType(VT));
5105 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
5106 DAG.getValueType(VT));
Evan Chengefa53392008-10-13 18:46:18 +00005107 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5108 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Nate Begeman750ac1b2006-02-01 07:19:44 +00005109 break;
5110 }
5111 }
5112 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005113 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005114 MVT VT = LHS.getValueType();
Evan Cheng2b49c502006-12-15 02:59:56 +00005115 if (VT == MVT::f32 || VT == MVT::f64) {
5116 // Expand into one or more soft-fp libcall(s).
Evan Cheng6518c6e2008-07-01 21:35:46 +00005117 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00005118 switch (cast<CondCodeSDNode>(CC)->get()) {
5119 case ISD::SETEQ:
5120 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00005121 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005122 break;
5123 case ISD::SETNE:
5124 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00005125 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005126 break;
5127 case ISD::SETGE:
5128 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00005129 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005130 break;
5131 case ISD::SETLT:
5132 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00005133 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005134 break;
5135 case ISD::SETLE:
5136 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00005137 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005138 break;
5139 case ISD::SETGT:
5140 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00005141 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005142 break;
5143 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00005144 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5145 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005146 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00005147 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005148 break;
5149 default:
Evan Cheng56966222007-01-12 02:11:51 +00005150 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005151 switch (cast<CondCodeSDNode>(CC)->get()) {
5152 case ISD::SETONE:
5153 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00005154 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005155 // Fallthrough
5156 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00005157 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005158 break;
5159 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00005160 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005161 break;
5162 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00005163 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005164 break;
5165 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00005166 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005167 break;
Evan Cheng56966222007-01-12 02:11:51 +00005168 case ISD::SETUEQ:
5169 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00005170 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005171 default: assert(0 && "Unsupported FP setcc!");
5172 }
5173 }
Duncan Sandsf9516202008-06-30 10:19:09 +00005174
Dan Gohman475871a2008-07-27 21:46:04 +00005175 SDValue Dummy;
5176 SDValue Ops[2] = { LHS, RHS };
Gabor Greifba36cb52008-08-28 21:40:38 +00005177 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005178 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00005179 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00005180 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00005181 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Duncan Sands5480c042009-01-01 15:52:00 +00005182 Tmp1 = DAG.getNode(ISD::SETCC,
5183 TLI.getSetCCResultType(Tmp1.getValueType()),
5184 Tmp1, Tmp2, CC);
Gabor Greifba36cb52008-08-28 21:40:38 +00005185 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005186 false /*sign irrelevant*/, Dummy);
Duncan Sands5480c042009-01-01 15:52:00 +00005187 Tmp2 = DAG.getNode(ISD::SETCC,
5188 TLI.getSetCCResultType(LHS.getValueType()), LHS,
5189 Tmp2, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00005190 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman475871a2008-07-27 21:46:04 +00005191 Tmp2 = SDValue();
Evan Cheng2b49c502006-12-15 02:59:56 +00005192 }
Evan Cheng21cacc42008-07-07 07:18:09 +00005193 LHS = LegalizeOp(Tmp1);
Evan Cheng2b49c502006-12-15 02:59:56 +00005194 RHS = Tmp2;
5195 return;
5196 }
5197
Dan Gohman475871a2008-07-27 21:46:04 +00005198 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005199 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005200 ExpandOp(RHS, RHSLo, RHSHi);
5201 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5202
5203 if (VT==MVT::ppcf128) {
5204 // FIXME: This generated code sucks. We want to generate
Dale Johannesene2f20832008-09-12 00:30:56 +00005205 // FCMPU crN, hi1, hi2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005206 // BNE crN, L:
Dale Johannesene2f20832008-09-12 00:30:56 +00005207 // FCMPU crN, lo1, lo2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005208 // The following can be improved, but not that much.
Duncan Sands5480c042009-01-01 15:52:00 +00005209 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5210 LHSHi, RHSHi, ISD::SETOEQ);
5211 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5212 LHSLo, RHSLo, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005213 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Duncan Sands5480c042009-01-01 15:52:00 +00005214 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5215 LHSHi, RHSHi, ISD::SETUNE);
5216 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5217 LHSHi, RHSHi, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005218 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5219 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00005220 Tmp2 = SDValue();
Dale Johannesen638ccd52007-10-06 01:24:11 +00005221 break;
5222 }
5223
5224 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005225 case ISD::SETEQ:
5226 case ISD::SETNE:
5227 if (RHSLo == RHSHi)
5228 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5229 if (RHSCST->isAllOnesValue()) {
5230 // Comparison to -1.
5231 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5232 Tmp2 = RHSLo;
5233 break;
5234 }
5235
5236 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5237 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5238 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5239 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5240 break;
5241 default:
5242 // If this is a comparison of the sign bit, just look at the top part.
5243 // X > -1, x < 0
5244 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5245 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005246 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00005247 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5248 CST->isAllOnesValue())) { // X > -1
5249 Tmp1 = LHSHi;
5250 Tmp2 = RHSHi;
5251 break;
5252 }
5253
5254 // FIXME: This generated code sucks.
5255 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00005256 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005257 default: assert(0 && "Unknown integer setcc!");
5258 case ISD::SETLT:
5259 case ISD::SETULT: LowCC = ISD::SETULT; break;
5260 case ISD::SETGT:
5261 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5262 case ISD::SETLE:
5263 case ISD::SETULE: LowCC = ISD::SETULE; break;
5264 case ISD::SETGE:
5265 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5266 }
5267
5268 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5269 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5270 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5271
5272 // NOTE: on targets without efficient SELECT of bools, we can always use
5273 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00005274 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Duncan Sands5480c042009-01-01 15:52:00 +00005275 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5276 LHSLo, RHSLo, LowCC, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005277 if (!Tmp1.getNode())
Duncan Sands5480c042009-01-01 15:52:00 +00005278 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5279 LHSLo, RHSLo, LowCC);
5280 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5281 LHSHi, RHSHi, CCCode, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005282 if (!Tmp2.getNode())
Duncan Sands5480c042009-01-01 15:52:00 +00005283 Tmp2 = DAG.getNode(ISD::SETCC,
5284 TLI.getSetCCResultType(LHSHi.getValueType()),
5285 LHSHi, RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00005286
Gabor Greifba36cb52008-08-28 21:40:38 +00005287 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5288 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman002e5d02008-03-13 22:13:53 +00005289 if ((Tmp1C && Tmp1C->isNullValue()) ||
5290 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00005291 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5292 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00005293 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00005294 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5295 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5296 // low part is known false, returns high part.
5297 // For LE / GE, if high part is known false, ignore the low part.
5298 // For LT / GT, if high part is known true, ignore the low part.
5299 Tmp1 = Tmp2;
Dan Gohman475871a2008-07-27 21:46:04 +00005300 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005301 } else {
Duncan Sands5480c042009-01-01 15:52:00 +00005302 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5303 LHSHi, RHSHi, ISD::SETEQ, false,
5304 DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005305 if (!Result.getNode())
Duncan Sands5480c042009-01-01 15:52:00 +00005306 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5307 LHSHi, RHSHi, ISD::SETEQ);
Evan Cheng2e677812007-02-08 22:16:19 +00005308 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5309 Result, Tmp1, Tmp2));
5310 Tmp1 = Result;
Dan Gohman475871a2008-07-27 21:46:04 +00005311 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005312 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005313 }
5314 }
Evan Cheng2b49c502006-12-15 02:59:56 +00005315 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005316 LHS = Tmp1;
5317 RHS = Tmp2;
5318}
5319
Evan Cheng7f042682008-10-15 02:05:31 +00005320/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5321/// condition code CC on the current target. This routine assumes LHS and rHS
5322/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5323/// illegal condition code into AND / OR of multiple SETCC values.
5324void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5325 SDValue &LHS, SDValue &RHS,
5326 SDValue &CC) {
5327 MVT OpVT = LHS.getValueType();
5328 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5329 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5330 default: assert(0 && "Unknown condition code action!");
5331 case TargetLowering::Legal:
5332 // Nothing to do.
5333 break;
5334 case TargetLowering::Expand: {
5335 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5336 unsigned Opc = 0;
5337 switch (CCCode) {
5338 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00005339 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5340 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5341 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5342 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5343 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5344 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5345 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5346 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5347 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5348 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5349 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5350 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00005351 // FIXME: Implement more expansions.
5352 }
5353
5354 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5355 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5356 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5357 RHS = SDValue();
5358 CC = SDValue();
5359 break;
5360 }
5361 }
5362}
5363
Chris Lattner1401d152008-01-16 07:45:30 +00005364/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5365/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5366/// a load from the stack slot to DestVT, extending it if needed.
5367/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005368SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5369 MVT SlotVT,
5370 MVT DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00005371 // Create the stack frame object.
Mon P Wang364d73d2008-07-05 20:40:31 +00005372 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5373 SrcOp.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00005374 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang364d73d2008-07-05 20:40:31 +00005375
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005376 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005377 int SPFI = StackPtrFI->getIndex();
Mon P Wang364d73d2008-07-05 20:40:31 +00005378
Duncan Sands83ec4b62008-06-06 12:08:01 +00005379 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5380 unsigned SlotSize = SlotVT.getSizeInBits();
5381 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang364d73d2008-07-05 20:40:31 +00005382 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5383 DestVT.getTypeForMVT());
Chris Lattner35481892005-12-23 00:16:34 +00005384
Chris Lattner1401d152008-01-16 07:45:30 +00005385 // Emit a store to the stack slot. Use a truncstore if the input value is
5386 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00005387 SDValue Store;
Mon P Wang364d73d2008-07-05 20:40:31 +00005388
Chris Lattner1401d152008-01-16 07:45:30 +00005389 if (SrcSize > SlotSize)
Dan Gohman69de1932008-02-06 22:27:42 +00005390 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005391 PseudoSourceValue::getFixedStack(SPFI), 0,
5392 SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005393 else {
5394 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman69de1932008-02-06 22:27:42 +00005395 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005396 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang364d73d2008-07-05 20:40:31 +00005397 false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005398 }
5399
Chris Lattner35481892005-12-23 00:16:34 +00005400 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00005401 if (SlotSize == DestSize)
Mon P Wang364d73d2008-07-05 20:40:31 +00005402 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005403
5404 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang364d73d2008-07-05 20:40:31 +00005405 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5406 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00005407}
5408
Dan Gohman475871a2008-07-27 21:46:04 +00005409SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Chris Lattner4352cc92006-04-04 17:23:26 +00005410 // Create a vector sized/aligned stack slot, store the value to element #0,
5411 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005412 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00005413
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005414 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005415 int SPFI = StackPtrFI->getIndex();
5416
Dan Gohman475871a2008-07-27 21:46:04 +00005417 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005418 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00005419 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005420 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00005421}
5422
5423
Chris Lattnerce872152006-03-19 06:31:19 +00005424/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00005425/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00005426SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Chris Lattnerce872152006-03-19 06:31:19 +00005427
5428 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00005429 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00005430 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00005431 bool isOnlyLowElement = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005432 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005433
Dan Gohman475871a2008-07-27 21:46:04 +00005434 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005435 // and use a bitmask instead of a list of elements.
Dan Gohman475871a2008-07-27 21:46:04 +00005436 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00005437 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005438 bool isConstant = true;
5439 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5440 SplatValue.getOpcode() != ISD::UNDEF)
5441 isConstant = false;
5442
Evan Cheng033e6812006-03-24 01:17:21 +00005443 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005444 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00005445 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00005446 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00005447 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00005448 if (SplatValue != V)
Dan Gohman475871a2008-07-27 21:46:04 +00005449 SplatValue = SDValue(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005450
5451 // If this isn't a constant element or an undef, we can't use a constant
5452 // pool load.
5453 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5454 V.getOpcode() != ISD::UNDEF)
5455 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00005456 }
5457
5458 if (isOnlyLowElement) {
5459 // If the low element is an undef too, then this whole things is an undef.
5460 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5461 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5462 // Otherwise, turn this into a scalar_to_vector node.
5463 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5464 Node->getOperand(0));
5465 }
5466
Chris Lattner2eb86532006-03-24 07:29:17 +00005467 // If all elements are constants, create a load from the constant pool.
5468 if (isConstant) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005469 MVT VT = Node->getValueType(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005470 std::vector<Constant*> CV;
5471 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5472 if (ConstantFPSDNode *V =
5473 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005474 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005475 } else if (ConstantSDNode *V =
Chris Lattner02a260a2008-04-20 00:41:09 +00005476 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005477 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005478 } else {
5479 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner02a260a2008-04-20 00:41:09 +00005480 const Type *OpNTy =
Duncan Sands83ec4b62008-06-06 12:08:01 +00005481 Node->getOperand(0).getValueType().getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00005482 CV.push_back(UndefValue::get(OpNTy));
5483 }
5484 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00005485 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005486 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005487 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman69de1932008-02-06 22:27:42 +00005488 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005489 PseudoSourceValue::getConstantPool(), 0,
5490 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00005491 }
5492
Gabor Greifba36cb52008-08-28 21:40:38 +00005493 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00005494 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005495 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman475871a2008-07-27 21:46:04 +00005496 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5497 std::vector<SDValue> ZeroVec(NumElems, Zero);
5498 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005499 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00005500
5501 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005502 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00005503 // Get the splatted value into the low element of a vector register.
Dan Gohman475871a2008-07-27 21:46:04 +00005504 SDValue LowValVec =
Chris Lattner87100e02006-03-20 01:52:29 +00005505 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5506
5507 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5508 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5509 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5510 SplatMask);
5511 }
5512 }
5513
Evan Cheng033e6812006-03-24 01:17:21 +00005514 // If there are only two unique elements, we may be able to turn this into a
5515 // vector shuffle.
5516 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005517 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00005518 SDValue Val1 = Node->getOperand(1);
5519 SDValue Val2;
5520 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005521 if (MI->first != Val1)
5522 Val2 = MI->first;
5523 else
5524 Val2 = (++MI)->first;
5525
5526 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5527 // vector shuffle has the undef vector on the RHS.
5528 if (Val1.getOpcode() == ISD::UNDEF)
5529 std::swap(Val1, Val2);
5530
Evan Cheng033e6812006-03-24 01:17:21 +00005531 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005532 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5533 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005534 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005535
5536 // Set elements of the shuffle mask for Val1.
5537 std::vector<unsigned> &Val1Elts = Values[Val1];
5538 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5539 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5540
5541 // Set elements of the shuffle mask for Val2.
5542 std::vector<unsigned> &Val2Elts = Values[Val2];
5543 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5544 if (Val2.getOpcode() != ISD::UNDEF)
5545 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5546 else
5547 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5548
Dan Gohman475871a2008-07-27 21:46:04 +00005549 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005550 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005551
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005552 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005553 if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR,
5554 Node->getValueType(0)) &&
Chris Lattner4352cc92006-04-04 17:23:26 +00005555 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005556 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5557 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman475871a2008-07-27 21:46:04 +00005558 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng033e6812006-03-24 01:17:21 +00005559
5560 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005561 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng033e6812006-03-24 01:17:21 +00005562 }
5563 }
Chris Lattnerce872152006-03-19 06:31:19 +00005564
5565 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5566 // aligned object on the stack, store each element into it, then load
5567 // the result as a vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005568 MVT VT = Node->getValueType(0);
Chris Lattnerce872152006-03-19 06:31:19 +00005569 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00005570 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00005571
5572 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00005573 SmallVector<SDValue, 8> Stores;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005574 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005575 // Store (in the right endianness) the elements to memory.
5576 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5577 // Ignore undef elements.
5578 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5579
Chris Lattner841c8822006-03-22 01:46:54 +00005580 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005581
Dan Gohman475871a2008-07-27 21:46:04 +00005582 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Chris Lattnerce872152006-03-19 06:31:19 +00005583 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5584
Evan Cheng786225a2006-10-05 23:01:46 +00005585 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00005586 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00005587 }
5588
Dan Gohman475871a2008-07-27 21:46:04 +00005589 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00005590 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005591 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5592 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005593 else
5594 StoreChain = DAG.getEntryNode();
5595
5596 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00005597 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005598}
5599
Chris Lattner5b359c62005-04-02 04:00:59 +00005600void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman475871a2008-07-27 21:46:04 +00005601 SDValue Op, SDValue Amt,
5602 SDValue &Lo, SDValue &Hi) {
Chris Lattner5b359c62005-04-02 04:00:59 +00005603 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00005604 SDValue LHSL, LHSH;
Chris Lattner5b359c62005-04-02 04:00:59 +00005605 ExpandOp(Op, LHSL, LHSH);
5606
Dan Gohman475871a2008-07-27 21:46:04 +00005607 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005608 MVT VT = LHSL.getValueType();
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00005609 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005610 Hi = Lo.getValue(1);
5611}
5612
5613
Chris Lattnere34b3962005-01-19 04:19:40 +00005614/// ExpandShift - Try to find a clever way to expand this shift operation out to
5615/// smaller elements. If we can't find a way that is more efficient than a
5616/// libcall on this target, return false. Otherwise, return true with the
5617/// low-parts expanded into Lo and Hi.
Dan Gohman475871a2008-07-27 21:46:04 +00005618bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5619 SDValue &Lo, SDValue &Hi) {
Chris Lattnere34b3962005-01-19 04:19:40 +00005620 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5621 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005622
Duncan Sands83ec4b62008-06-06 12:08:01 +00005623 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman475871a2008-07-27 21:46:04 +00005624 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005625 MVT ShTy = ShAmt.getValueType();
5626 unsigned ShBits = ShTy.getSizeInBits();
5627 unsigned VTBits = Op.getValueType().getSizeInBits();
5628 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005629
Chris Lattner7a3c8552007-10-14 20:35:12 +00005630 // Handle the case when Amt is an immediate.
Gabor Greifba36cb52008-08-28 21:40:38 +00005631 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005632 unsigned Cst = CN->getZExtValue();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005633 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005634 SDValue InL, InH;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005635 ExpandOp(Op, InL, InH);
5636 switch(Opc) {
5637 case ISD::SHL:
5638 if (Cst > VTBits) {
5639 Lo = DAG.getConstant(0, NVT);
5640 Hi = DAG.getConstant(0, NVT);
5641 } else if (Cst > NVTBits) {
5642 Lo = DAG.getConstant(0, NVT);
5643 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005644 } else if (Cst == NVTBits) {
5645 Lo = DAG.getConstant(0, NVT);
5646 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005647 } else {
5648 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5649 Hi = DAG.getNode(ISD::OR, NVT,
5650 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5651 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5652 }
5653 return true;
5654 case ISD::SRL:
5655 if (Cst > VTBits) {
5656 Lo = DAG.getConstant(0, NVT);
5657 Hi = DAG.getConstant(0, NVT);
5658 } else if (Cst > NVTBits) {
5659 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5660 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005661 } else if (Cst == NVTBits) {
5662 Lo = InH;
5663 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005664 } else {
5665 Lo = DAG.getNode(ISD::OR, NVT,
5666 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5667 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5668 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5669 }
5670 return true;
5671 case ISD::SRA:
5672 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005673 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005674 DAG.getConstant(NVTBits-1, ShTy));
5675 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005676 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005677 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005678 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005679 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005680 } else if (Cst == NVTBits) {
5681 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005682 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005683 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005684 } else {
5685 Lo = DAG.getNode(ISD::OR, NVT,
5686 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5687 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5688 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5689 }
5690 return true;
5691 }
5692 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005693
5694 // Okay, the shift amount isn't constant. However, if we can tell that it is
5695 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005696 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5697 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005698 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005699
Dan Gohman9e255b72008-02-22 01:12:31 +00005700 // If we know that if any of the high bits of the shift amount are one, then
5701 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005702 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005703 // Mask out the high bit, which we know is set.
5704 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005705 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005706
5707 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005708 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005709 ExpandOp(Op, InL, InH);
5710 switch(Opc) {
5711 case ISD::SHL:
5712 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5713 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5714 return true;
5715 case ISD::SRL:
5716 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5717 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5718 return true;
5719 case ISD::SRA:
5720 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5721 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5722 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5723 return true;
5724 }
5725 }
5726
Dan Gohman9e255b72008-02-22 01:12:31 +00005727 // If we know that the high bits of the shift amount are all zero, then we can
5728 // do this as a couple of simple shifts.
5729 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005730 // Compute 32-amt.
Dan Gohman475871a2008-07-27 21:46:04 +00005731 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005732 DAG.getConstant(NVTBits, Amt.getValueType()),
5733 Amt);
5734
5735 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005736 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005737 ExpandOp(Op, InL, InH);
5738 switch(Opc) {
5739 case ISD::SHL:
5740 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5741 Hi = DAG.getNode(ISD::OR, NVT,
5742 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5743 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5744 return true;
5745 case ISD::SRL:
5746 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5747 Lo = DAG.getNode(ISD::OR, NVT,
5748 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5749 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5750 return true;
5751 case ISD::SRA:
5752 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5753 Lo = DAG.getNode(ISD::OR, NVT,
5754 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5755 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5756 return true;
5757 }
5758 }
5759
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005760 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005761}
Chris Lattner77e77a62005-01-21 06:05:23 +00005762
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005763
Chris Lattner77e77a62005-01-21 06:05:23 +00005764// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5765// does not fit into a register, return the lo part and set the hi part to the
5766// by-reg argument. If it does fit into a single register, return the result
5767// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00005768SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5769 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005770 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5771 // The input chain to this libcall is the entry node of the function.
5772 // Legalizing the call will automatically add the previous call to the
5773 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00005774 SDValue InChain = DAG.getEntryNode();
Chris Lattner6831a812006-02-13 09:18:02 +00005775
Chris Lattner77e77a62005-01-21 06:05:23 +00005776 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005777 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005778 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005779 MVT ArgVT = Node->getOperand(i).getValueType();
5780 const Type *ArgTy = ArgVT.getTypeForMVT();
Reid Spencer47857812006-12-31 05:55:36 +00005781 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005782 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005783 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005784 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005785 }
Bill Wendling056292f2008-09-16 21:48:12 +00005786 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00005787 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005788
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005789 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005790 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman475871a2008-07-27 21:46:04 +00005791 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005792 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5793 CallingConv::C, false, Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005794
Chris Lattner6831a812006-02-13 09:18:02 +00005795 // Legalize the call sequence, starting with the chain. This will advance
5796 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5797 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5798 LegalizeOp(CallInfo.second);
Dan Gohman475871a2008-07-27 21:46:04 +00005799 SDValue Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005800 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005801 default: assert(0 && "Unknown thing");
5802 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005803 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005804 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005805 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005806 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005807 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005808 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005809 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005810}
5811
Dan Gohman7f8613e2008-08-14 20:04:46 +00005812/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5813///
5814SDValue SelectionDAGLegalize::
5815LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5816 bool isCustom = false;
5817 SDValue Tmp1;
5818 switch (getTypeAction(Op.getValueType())) {
5819 case Legal:
5820 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5821 Op.getValueType())) {
5822 default: assert(0 && "Unknown operation action!");
5823 case TargetLowering::Custom:
5824 isCustom = true;
5825 // FALLTHROUGH
5826 case TargetLowering::Legal:
5827 Tmp1 = LegalizeOp(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00005828 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005829 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5830 else
5831 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5832 DestTy, Tmp1);
5833 if (isCustom) {
5834 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005835 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005836 }
5837 break;
5838 case TargetLowering::Expand:
5839 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5840 break;
5841 case TargetLowering::Promote:
5842 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5843 break;
5844 }
5845 break;
5846 case Expand:
5847 Result = ExpandIntToFP(isSigned, DestTy, Op);
5848 break;
5849 case Promote:
5850 Tmp1 = PromoteOp(Op);
5851 if (isSigned) {
5852 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5853 Tmp1, DAG.getValueType(Op.getValueType()));
5854 } else {
5855 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5856 Op.getValueType());
5857 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005858 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005859 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5860 else
5861 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5862 DestTy, Tmp1);
5863 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5864 break;
5865 }
5866 return Result;
5867}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005868
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005869/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5870///
Dan Gohman475871a2008-07-27 21:46:04 +00005871SDValue SelectionDAGLegalize::
5872ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005873 MVT SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005874 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005875
Dan Gohman7f8613e2008-08-14 20:04:46 +00005876 // Expand unsupported int-to-fp vector casts by unrolling them.
5877 if (DestTy.isVector()) {
5878 if (!ExpandSource)
5879 return LegalizeOp(UnrollVectorOp(Source));
5880 MVT DestEltTy = DestTy.getVectorElementType();
5881 if (DestTy.getVectorNumElements() == 1) {
5882 SDValue Scalar = ScalarizeVectorOp(Source);
5883 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5884 DestEltTy, Scalar);
5885 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5886 }
5887 SDValue Lo, Hi;
5888 SplitVectorOp(Source, Lo, Hi);
5889 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5890 DestTy.getVectorNumElements() / 2);
5891 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5892 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengefa53392008-10-13 18:46:18 +00005893 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5894 HiResult));
Dan Gohman7f8613e2008-08-14 20:04:46 +00005895 }
5896
Evan Cheng9845eb52008-04-01 02:18:22 +00005897 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5898 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005899 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005900 // incoming integer is set. To handle this, we dynamically test to see if
5901 // it is set, and, if so, add a fudge factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005902 SDValue Hi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005903 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005904 SDValue Lo;
Dan Gohman034f60e2008-03-11 01:59:03 +00005905 ExpandOp(Source, Lo, Hi);
5906 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5907 } else {
5908 // The comparison for the sign bit will use the entire operand.
5909 Hi = Source;
5910 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005911
Dale Johannesen53997b02008-11-04 20:52:49 +00005912 // Check to see if the target has a custom way to lower this. If so, use
5913 // it. (Note we've already expanded the operand in this case.)
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005914 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5915 default: assert(0 && "This action not implemented for this operation!");
5916 case TargetLowering::Legal:
5917 case TargetLowering::Expand:
5918 break; // This case is handled below.
5919 case TargetLowering::Custom: {
5920 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5921 Source), DAG);
5922 if (NV.getNode())
5923 return LegalizeOp(NV);
5924 break; // The target decided this was legal after all
5925 }
5926 }
5927
Chris Lattner66de05b2005-05-13 04:45:13 +00005928 // If this is unsigned, and not supported, first perform the conversion to
5929 // signed, then adjust the result if the sign bit is set.
Dan Gohman475871a2008-07-27 21:46:04 +00005930 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner66de05b2005-05-13 04:45:13 +00005931
Duncan Sands5480c042009-01-01 15:52:00 +00005932 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi.getValueType()),
5933 Hi, DAG.getConstant(0, Hi.getValueType()),
5934 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005935 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5936 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattnere9c35e72005-04-13 05:09:42 +00005937 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005938 uint64_t FF = 0x5f800000ULL;
5939 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohman34bc1782008-03-05 02:07:31 +00005940 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005941
Dan Gohman475871a2008-07-27 21:46:04 +00005942 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005943 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattnere9c35e72005-04-13 05:09:42 +00005944 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005945 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005946 SDValue FudgeInReg;
Chris Lattnere9c35e72005-04-13 05:09:42 +00005947 if (DestTy == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005948 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005949 PseudoSourceValue::getConstantPool(), 0,
5950 false, Alignment);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005951 else if (DestTy.bitsGT(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005952 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005953 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005954 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005955 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005956 MVT::f32, false, Alignment);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005957 else
5958 assert(0 && "Unexpected conversion");
5959
Duncan Sands83ec4b62008-06-06 12:08:01 +00005960 MVT SCVT = SignedConv.getValueType();
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005961 if (SCVT != DestTy) {
5962 // Destination type needs to be expanded as well. The FADD now we are
5963 // constructing will be expanded into a libcall.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005964 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5965 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmand91446d2008-03-05 01:08:17 +00005966 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005967 SignedConv, SignedConv.getValue(1));
5968 }
5969 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5970 }
Chris Lattner473a9902005-09-29 06:44:39 +00005971 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005972 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005973
Chris Lattnera88a2602005-05-14 05:33:54 +00005974 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00005975 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00005976 default: assert(0 && "This action not implemented for this operation!");
5977 case TargetLowering::Legal:
5978 case TargetLowering::Expand:
5979 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005980 case TargetLowering::Custom: {
Dan Gohman475871a2008-07-27 21:46:04 +00005981 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Chris Lattner07dffd62005-08-26 00:14:16 +00005982 Source), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005983 if (NV.getNode())
Chris Lattner07dffd62005-08-26 00:14:16 +00005984 return LegalizeOp(NV);
5985 break; // The target decided this was legal after all
5986 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005987 }
5988
Chris Lattner13689e22005-05-12 07:00:44 +00005989 // Expand the source, then glue it back together for the call. We must expand
5990 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00005991 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005992 SDValue SrcLo, SrcHi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005993 ExpandOp(Source, SrcLo, SrcHi);
5994 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5995 }
Chris Lattner13689e22005-05-12 07:00:44 +00005996
Duncan Sandsb2ff8852008-07-17 02:36:29 +00005997 RTLIB::Libcall LC = isSigned ?
5998 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5999 RTLIB::getUINTTOFP(SourceVT, DestTy);
6000 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
6001
Chris Lattner6831a812006-02-13 09:18:02 +00006002 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman475871a2008-07-27 21:46:04 +00006003 SDValue HiPart;
Gabor Greifba36cb52008-08-28 21:40:38 +00006004 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
6005 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmana2e94852008-03-10 23:03:31 +00006006 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
6007 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00006008}
Misha Brukmanedf128a2005-04-21 22:36:52 +00006009
Chris Lattner22cde6a2006-01-28 08:25:58 +00006010/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
6011/// INT_TO_FP operation of the specified operand when the target requests that
6012/// we expand it. At this point, we know that the result and operand types are
6013/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00006014SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
6015 SDValue Op0,
6016 MVT DestVT) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006017 if (Op0.getValueType() == MVT::i32) {
6018 // simple 32-bit [signed|unsigned] integer to float/double expansion
6019
Chris Lattner23594d42008-01-16 07:03:22 +00006020 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00006021 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner23594d42008-01-16 07:03:22 +00006022
Chris Lattner22cde6a2006-01-28 08:25:58 +00006023 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00006024 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00006025 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00006026 SDValue Hi = StackSlot;
6027 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00006028 if (TLI.isLittleEndian())
6029 std::swap(Hi, Lo);
6030
Chris Lattner22cde6a2006-01-28 08:25:58 +00006031 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00006032 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006033 if (isSigned) {
6034 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00006035 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006036 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
6037 } else {
6038 Op0Mapped = Op0;
6039 }
6040 // store the lo of the constructed double - based on integer input
Dan Gohman475871a2008-07-27 21:46:04 +00006041 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00006042 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006043 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00006044 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006045 // store the hi of the constructed double - biased exponent
Dan Gohman475871a2008-07-27 21:46:04 +00006046 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006047 // load the constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00006048 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006049 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00006050 SDValue Bias = DAG.getConstantFP(isSigned ?
Chris Lattner22cde6a2006-01-28 08:25:58 +00006051 BitsToDouble(0x4330000080000000ULL)
6052 : BitsToDouble(0x4330000000000000ULL),
6053 MVT::f64);
6054 // subtract the bias
Dan Gohman475871a2008-07-27 21:46:04 +00006055 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006056 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00006057 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006058 // handle final rounding
6059 if (DestVT == MVT::f64) {
6060 // do nothing
6061 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006062 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00006063 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
6064 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00006065 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesen118cd9d2007-09-16 16:51:49 +00006066 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006067 }
6068 return Result;
6069 }
6070 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman475871a2008-07-27 21:46:04 +00006071 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006072
Duncan Sands5480c042009-01-01 15:52:00 +00006073 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0.getValueType()),
6074 Op0, DAG.getConstant(0, Op0.getValueType()),
6075 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00006076 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
6077 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006078 SignSet, Four, Zero);
6079
6080 // If the sign bit of the integer is set, the large number will be treated
6081 // as a negative number. To counteract this, the dynamic code adds an
6082 // offset depending on the data type.
6083 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006084 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006085 default: assert(0 && "Unsupported integer type!");
6086 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6087 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6088 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6089 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6090 }
6091 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00006092 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006093
Dan Gohman475871a2008-07-27 21:46:04 +00006094 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00006095 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006096 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00006097 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00006098 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006099 if (DestVT == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00006100 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00006101 PseudoSourceValue::getConstantPool(), 0,
6102 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006103 else {
Dan Gohman69de1932008-02-06 22:27:42 +00006104 FudgeInReg =
6105 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
6106 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00006107 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00006108 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006109 }
6110
6111 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
6112}
6113
6114/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6115/// *INT_TO_FP operation of the specified operand when the target requests that
6116/// we promote it. At this point, we know that the result and operand types are
6117/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6118/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00006119SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6120 MVT DestVT,
6121 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006122 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006123 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006124
6125 unsigned OpToUse = 0;
6126
6127 // Scan for the appropriate larger type to use.
6128 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006129 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6130 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006131
6132 // If the target supports SINT_TO_FP of this type, use it.
6133 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6134 default: break;
6135 case TargetLowering::Legal:
6136 if (!TLI.isTypeLegal(NewInTy))
6137 break; // Can't use this datatype.
6138 // FALL THROUGH.
6139 case TargetLowering::Custom:
6140 OpToUse = ISD::SINT_TO_FP;
6141 break;
6142 }
6143 if (OpToUse) break;
6144 if (isSigned) continue;
6145
6146 // If the target supports UINT_TO_FP of this type, use it.
6147 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6148 default: break;
6149 case TargetLowering::Legal:
6150 if (!TLI.isTypeLegal(NewInTy))
6151 break; // Can't use this datatype.
6152 // FALL THROUGH.
6153 case TargetLowering::Custom:
6154 OpToUse = ISD::UINT_TO_FP;
6155 break;
6156 }
6157 if (OpToUse) break;
6158
6159 // Otherwise, try a larger type.
6160 }
6161
6162 // Okay, we found the operation and type to use. Zero extend our input to the
6163 // desired type then run the operation on it.
6164 return DAG.getNode(OpToUse, DestVT,
6165 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
6166 NewInTy, LegalOp));
6167}
6168
6169/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6170/// FP_TO_*INT operation of the specified operand when the target requests that
6171/// we promote it. At this point, we know that the result and operand types are
6172/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6173/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00006174SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6175 MVT DestVT,
6176 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006177 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006178 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006179
6180 unsigned OpToUse = 0;
6181
6182 // Scan for the appropriate larger type to use.
6183 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006184 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6185 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006186
6187 // If the target supports FP_TO_SINT returning this type, use it.
6188 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6189 default: break;
6190 case TargetLowering::Legal:
6191 if (!TLI.isTypeLegal(NewOutTy))
6192 break; // Can't use this datatype.
6193 // FALL THROUGH.
6194 case TargetLowering::Custom:
6195 OpToUse = ISD::FP_TO_SINT;
6196 break;
6197 }
6198 if (OpToUse) break;
6199
6200 // If the target supports FP_TO_UINT of this type, use it.
6201 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6202 default: break;
6203 case TargetLowering::Legal:
6204 if (!TLI.isTypeLegal(NewOutTy))
6205 break; // Can't use this datatype.
6206 // FALL THROUGH.
6207 case TargetLowering::Custom:
6208 OpToUse = ISD::FP_TO_UINT;
6209 break;
6210 }
6211 if (OpToUse) break;
6212
6213 // Otherwise, try a larger type.
6214 }
6215
Chris Lattner27a6c732007-11-24 07:07:01 +00006216
6217 // Okay, we found the operation and type to use.
Dan Gohman475871a2008-07-27 21:46:04 +00006218 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00006219
Chris Lattner27a6c732007-11-24 07:07:01 +00006220 // If the operation produces an invalid type, it must be custom lowered. Use
6221 // the target lowering hooks to expand it. Just keep the low part of the
6222 // expanded operation, we know that we're truncating anyway.
6223 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00006224 SmallVector<SDValue, 2> Results;
6225 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6226 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6227 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00006228 }
Duncan Sands126d9072008-07-04 11:47:58 +00006229
Chris Lattner27a6c732007-11-24 07:07:01 +00006230 // Truncate the result of the extended FP_TO_*INT operation to the desired
6231 // size.
6232 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006233}
6234
6235/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6236///
Dan Gohman475871a2008-07-27 21:46:04 +00006237SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006238 MVT VT = Op.getValueType();
6239 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00006240 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006241 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006242 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6243 case MVT::i16:
6244 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6245 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6246 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6247 case MVT::i32:
6248 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6249 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6250 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6251 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6252 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6253 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6254 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6255 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6256 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6257 case MVT::i64:
6258 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6259 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6260 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6261 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6262 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6263 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6264 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6265 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6266 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6267 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6268 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6269 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6270 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6271 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6272 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6273 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6274 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6275 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6276 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6277 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6278 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6279 }
6280}
6281
6282/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6283///
Dan Gohman475871a2008-07-27 21:46:04 +00006284SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006285 switch (Opc) {
6286 default: assert(0 && "Cannot expand this yet!");
6287 case ISD::CTPOP: {
6288 static const uint64_t mask[6] = {
6289 0x5555555555555555ULL, 0x3333333333333333ULL,
6290 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6291 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6292 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00006293 MVT VT = Op.getValueType();
6294 MVT ShVT = TLI.getShiftAmountTy();
6295 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006296 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6297 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohmance9bc122009-01-27 20:39:34 +00006298 SDValue Tmp2 = DAG.getConstant(VT.getIntegerVTBitMask() & mask[i], VT);
Dan Gohman475871a2008-07-27 21:46:04 +00006299 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006300 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6301 DAG.getNode(ISD::AND, VT,
6302 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6303 }
6304 return Op;
6305 }
6306 case ISD::CTLZ: {
6307 // for now, we do this:
6308 // x = x | (x >> 1);
6309 // x = x | (x >> 2);
6310 // ...
6311 // x = x | (x >>16);
6312 // x = x | (x >>32); // for 64-bit input
6313 // return popcount(~x);
6314 //
6315 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006316 MVT VT = Op.getValueType();
6317 MVT ShVT = TLI.getShiftAmountTy();
6318 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006319 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006320 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006321 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6322 }
Bob Wilson4c245462009-01-22 17:39:32 +00006323 Op = DAG.getNOT(Op, VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006324 return DAG.getNode(ISD::CTPOP, VT, Op);
6325 }
6326 case ISD::CTTZ: {
6327 // for now, we use: { return popcount(~x & (x - 1)); }
6328 // unless the target has ctlz but not ctpop, in which case we use:
6329 // { return 32 - nlz(~x & (x-1)); }
6330 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006331 MVT VT = Op.getValueType();
Bob Wilson4c245462009-01-22 17:39:32 +00006332 SDValue Tmp3 = DAG.getNode(ISD::AND, VT, DAG.getNOT(Op, VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006333 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6334 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006335 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
6336 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Chris Lattner22cde6a2006-01-28 08:25:58 +00006337 return DAG.getNode(ISD::SUB, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006338 DAG.getConstant(VT.getSizeInBits(), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006339 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6340 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6341 }
6342 }
6343}
Chris Lattnere34b3962005-01-19 04:19:40 +00006344
Dan Gohman475871a2008-07-27 21:46:04 +00006345/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner3e928bb2005-01-07 07:47:09 +00006346/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman929d3eb2008-10-01 15:07:49 +00006347/// LegalizedNodes map is filled in for any results that are not expanded, the
Chris Lattner3e928bb2005-01-07 07:47:09 +00006348/// ExpandedNodes map is filled in for any results that are expanded, and the
6349/// Lo/Hi values are returned.
Dan Gohman475871a2008-07-27 21:46:04 +00006350void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands83ec4b62008-06-06 12:08:01 +00006351 MVT VT = Op.getValueType();
6352 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greifba36cb52008-08-28 21:40:38 +00006353 SDNode *Node = Op.getNode();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006354 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00006355 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands83ec4b62008-06-06 12:08:01 +00006356 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006357
Chris Lattner6fdcb252005-09-02 20:32:45 +00006358 // See if we already expanded it.
Dan Gohman475871a2008-07-27 21:46:04 +00006359 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00006360 = ExpandedNodes.find(Op);
6361 if (I != ExpandedNodes.end()) {
6362 Lo = I->second.first;
6363 Hi = I->second.second;
6364 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006365 }
6366
Chris Lattner3e928bb2005-01-07 07:47:09 +00006367 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006368 case ISD::CopyFromReg:
6369 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006370 case ISD::FP_ROUND_INREG:
6371 if (VT == MVT::ppcf128 &&
6372 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6373 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006374 SDValue SrcLo, SrcHi, Src;
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006375 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6376 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman475871a2008-07-27 21:46:04 +00006377 SDValue Result = TLI.LowerOperation(
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006378 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006379 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6380 Lo = Result.getNode()->getOperand(0);
6381 Hi = Result.getNode()->getOperand(1);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006382 break;
6383 }
6384 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00006385 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006386#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006387 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006388#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00006389 assert(0 && "Do not know how to expand this operator!");
6390 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00006391 case ISD::EXTRACT_ELEMENT:
6392 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006393 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman1953ecb2008-02-27 01:52:30 +00006394 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00006395 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00006396 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen25f1d082007-10-31 00:32:36 +00006397 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6398 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6399 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006400 case ISD::UNDEF:
6401 Lo = DAG.getNode(ISD::UNDEF, NVT);
6402 Hi = DAG.getNode(ISD::UNDEF, NVT);
6403 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006404 case ISD::Constant: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006405 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman050f5502008-03-03 22:20:46 +00006406 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6407 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6408 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006409 break;
6410 }
Evan Cheng00495212006-12-12 21:32:44 +00006411 case ISD::ConstantFP: {
6412 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00006413 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen7111b022008-10-09 18:53:47 +00006414 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesena471c2e2007-10-11 18:07:22 +00006415 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6416 MVT::f64);
6417 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6418 MVT::f64);
6419 break;
6420 }
Evan Cheng279101e2006-12-12 22:19:28 +00006421 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00006422 if (getTypeAction(Lo.getValueType()) == Expand)
6423 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006424 break;
6425 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006426 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006427 // Return the operands.
6428 Lo = Node->getOperand(0);
6429 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006430 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006431
6432 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00006433 if (Node->getNumValues() == 1) {
6434 ExpandOp(Op.getOperand(0), Lo, Hi);
6435 break;
6436 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006437 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif99a6cb92008-08-26 22:36:50 +00006438 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattner27a6c732007-11-24 07:07:01 +00006439 Op.getValue(1).getValueType() == MVT::Other &&
6440 "unhandled MERGE_VALUES");
6441 ExpandOp(Op.getOperand(0), Lo, Hi);
6442 // Remember that we legalized the chain.
6443 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6444 break;
Chris Lattner58f79632005-12-12 22:27:43 +00006445
6446 case ISD::SIGN_EXTEND_INREG:
6447 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00006448 // sext_inreg the low part if needed.
6449 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6450
6451 // The high part gets the sign extension from the lo-part. This handles
6452 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00006453 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006454 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattner58f79632005-12-12 22:27:43 +00006455 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00006456 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006457
Nate Begemand88fc032006-01-14 03:14:10 +00006458 case ISD::BSWAP: {
6459 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006460 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Nate Begemand88fc032006-01-14 03:14:10 +00006461 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6462 Lo = TempLo;
6463 break;
6464 }
6465
Chris Lattneredb1add2005-05-11 04:51:16 +00006466 case ISD::CTPOP:
6467 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00006468 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6469 DAG.getNode(ISD::CTPOP, NVT, Lo),
6470 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00006471 Hi = DAG.getConstant(0, NVT);
6472 break;
6473
Chris Lattner39a8f332005-05-12 19:05:01 +00006474 case ISD::CTLZ: {
6475 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006476 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006477 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6478 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Duncan Sands5480c042009-01-01 15:52:00 +00006479 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), HLZ, BitsC,
6480 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006481 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Chris Lattner39a8f332005-05-12 19:05:01 +00006482 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6483
6484 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6485 Hi = DAG.getConstant(0, NVT);
6486 break;
6487 }
6488
6489 case ISD::CTTZ: {
6490 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006491 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006492 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6493 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Duncan Sands5480c042009-01-01 15:52:00 +00006494 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), LTZ, BitsC,
6495 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006496 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00006497 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6498
6499 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6500 Hi = DAG.getConstant(0, NVT);
6501 break;
6502 }
Chris Lattneredb1add2005-05-11 04:51:16 +00006503
Nate Begemanacc398c2006-01-25 18:21:52 +00006504 case ISD::VAARG: {
Dan Gohman475871a2008-07-27 21:46:04 +00006505 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6506 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00006507 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6508 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6509
6510 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00006511 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00006512 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00006513 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00006514 std::swap(Lo, Hi);
6515 break;
6516 }
6517
Chris Lattner3e928bb2005-01-07 07:47:09 +00006518 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00006519 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00006520 SDValue Ch = LD->getChain(); // Legalize the chain.
6521 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Cheng466685d2006-10-09 20:57:25 +00006522 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f8613e2008-08-14 20:04:46 +00006523 const Value *SV = LD->getSrcValue();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006524 int SVOffset = LD->getSrcValueOffset();
6525 unsigned Alignment = LD->getAlignment();
6526 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006527
Evan Cheng466685d2006-10-09 20:57:25 +00006528 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00006529 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006530 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00006531 if (VT == MVT::f32 || VT == MVT::f64) {
6532 // f32->i32 or f64->i64 one to one expansion.
6533 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006534 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00006535 // Recursively expand the new load.
6536 if (getTypeAction(NVT) == Expand)
6537 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006538 break;
6539 }
Chris Lattnerec39a452005-01-19 18:02:17 +00006540
Evan Cheng466685d2006-10-09 20:57:25 +00006541 // Increment the pointer to the other half.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006542 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Evan Cheng466685d2006-10-09 20:57:25 +00006543 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006544 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006545 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006546 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00006547 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006548 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00006549
Evan Cheng466685d2006-10-09 20:57:25 +00006550 // Build a factor node to remember that this load is independent of the
6551 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00006552 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Evan Cheng466685d2006-10-09 20:57:25 +00006553 Hi.getValue(1));
6554
6555 // Remember that we legalized the chain.
6556 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00006557 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00006558 std::swap(Lo, Hi);
6559 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006560 MVT EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00006561
Dale Johannesenb6210fc2007-10-19 20:29:00 +00006562 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6563 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00006564 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman7f8613e2008-08-14 20:04:46 +00006565 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006566 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00006567 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006568 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Evan Cheng548f6112006-12-13 03:19:57 +00006569 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6570 break;
6571 }
Evan Cheng466685d2006-10-09 20:57:25 +00006572
6573 if (EVT == NVT)
Dan Gohman7f8613e2008-08-14 20:04:46 +00006574 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006575 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006576 else
Dan Gohman7f8613e2008-08-14 20:04:46 +00006577 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006578 SVOffset, EVT, isVolatile,
6579 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006580
6581 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006582 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng466685d2006-10-09 20:57:25 +00006583
6584 if (ExtType == ISD::SEXTLOAD) {
6585 // The high part is obtained by SRA'ing all but one of the bits of the
6586 // lo part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006587 unsigned LoSize = Lo.getValueType().getSizeInBits();
Evan Cheng466685d2006-10-09 20:57:25 +00006588 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6589 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6590 } else if (ExtType == ISD::ZEXTLOAD) {
6591 // The high part is just a zero.
6592 Hi = DAG.getConstant(0, NVT);
6593 } else /* if (ExtType == ISD::EXTLOAD) */ {
6594 // The high part is undefined.
6595 Hi = DAG.getNode(ISD::UNDEF, NVT);
6596 }
6597 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006598 break;
6599 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006600 case ISD::AND:
6601 case ISD::OR:
6602 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman475871a2008-07-27 21:46:04 +00006603 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006604 ExpandOp(Node->getOperand(0), LL, LH);
6605 ExpandOp(Node->getOperand(1), RL, RH);
6606 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6607 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6608 break;
6609 }
6610 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006611 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006612 ExpandOp(Node->getOperand(1), LL, LH);
6613 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00006614 if (getTypeAction(NVT) == Expand)
6615 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00006616 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00006617 if (VT != MVT::f32)
6618 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006619 break;
6620 }
Nate Begeman9373a812005-08-10 20:51:12 +00006621 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00006622 SDValue TL, TH, FL, FH;
Nate Begeman9373a812005-08-10 20:51:12 +00006623 ExpandOp(Node->getOperand(2), TL, TH);
6624 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00006625 if (getTypeAction(NVT) == Expand)
6626 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00006627 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6628 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00006629 if (VT != MVT::f32)
6630 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6631 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006632 break;
6633 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006634 case ISD::ANY_EXTEND:
6635 // The low part is any extension of the input (which degenerates to a copy).
6636 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6637 // The high part is undefined.
6638 Hi = DAG.getNode(ISD::UNDEF, NVT);
6639 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006640 case ISD::SIGN_EXTEND: {
6641 // The low part is just a sign extension of the input (which degenerates to
6642 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006643 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006644
Chris Lattner3e928bb2005-01-07 07:47:09 +00006645 // The high part is obtained by SRA'ing all but one of the bits of the lo
6646 // part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006647 unsigned LoSize = Lo.getValueType().getSizeInBits();
Chris Lattner8137c9e2006-01-28 05:07:51 +00006648 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6649 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006650 break;
6651 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006652 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006653 // The low part is just a zero extension of the input (which degenerates to
6654 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006655 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006656
Chris Lattner3e928bb2005-01-07 07:47:09 +00006657 // The high part is just a zero.
6658 Hi = DAG.getConstant(0, NVT);
6659 break;
Chris Lattner35481892005-12-23 00:16:34 +00006660
Chris Lattner4c948eb2007-02-13 23:55:16 +00006661 case ISD::TRUNCATE: {
6662 // The input value must be larger than this value. Expand *it*.
Dan Gohman475871a2008-07-27 21:46:04 +00006663 SDValue NewLo;
Chris Lattner4c948eb2007-02-13 23:55:16 +00006664 ExpandOp(Node->getOperand(0), NewLo, Hi);
6665
6666 // The low part is now either the right size, or it is closer. If not the
6667 // right size, make an illegal truncate so we recursively expand it.
6668 if (NewLo.getValueType() != Node->getValueType(0))
6669 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6670 ExpandOp(NewLo, Lo, Hi);
6671 break;
6672 }
6673
Chris Lattner35481892005-12-23 00:16:34 +00006674 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006675 SDValue Tmp;
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006676 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6677 // If the target wants to, allow it to lower this itself.
6678 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6679 case Expand: assert(0 && "cannot expand FP!");
6680 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6681 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6682 }
6683 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6684 }
6685
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006686 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006687 if (VT == MVT::f32 || VT == MVT::f64) {
6688 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006689 if (getTypeAction(NVT) == Expand)
6690 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006691 break;
6692 }
6693
6694 // If source operand will be expanded to the same type as VT, i.e.
6695 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006696 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006697 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6698 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006699 break;
6700 }
6701
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006702 // Turn this into a load/store pair by default.
Gabor Greifba36cb52008-08-28 21:40:38 +00006703 if (Tmp.getNode() == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00006704 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006705
Chris Lattner35481892005-12-23 00:16:34 +00006706 ExpandOp(Tmp, Lo, Hi);
6707 break;
6708 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006709
Chris Lattner27a6c732007-11-24 07:07:01 +00006710 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006711 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6712 TargetLowering::Custom &&
6713 "Must custom expand ReadCycleCounter");
Dan Gohman475871a2008-07-27 21:46:04 +00006714 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006715 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattner27a6c732007-11-24 07:07:01 +00006716 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006717 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006718 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006719 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006720 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006721
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006722 case ISD::ATOMIC_CMP_SWAP: {
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006723 // This operation does not need a loop.
6724 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6725 assert(Tmp.getNode() && "Node must be custom expanded!");
6726 ExpandOp(Tmp.getValue(0), Lo, Hi);
6727 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6728 LegalizeOp(Tmp.getValue(1)));
6729 break;
6730 }
6731
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006732 case ISD::ATOMIC_LOAD_ADD:
6733 case ISD::ATOMIC_LOAD_SUB:
6734 case ISD::ATOMIC_LOAD_AND:
6735 case ISD::ATOMIC_LOAD_OR:
6736 case ISD::ATOMIC_LOAD_XOR:
6737 case ISD::ATOMIC_LOAD_NAND:
6738 case ISD::ATOMIC_SWAP: {
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006739 // These operations require a loop to be generated. We can't do that yet,
6740 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006741 SDValue In2Lo, In2Hi, In2;
6742 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6743 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006744 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6745 SDValue Replace =
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006746 DAG.getAtomic(Op.getOpcode(), Anode->getMemoryVT(),
6747 Op.getOperand(0), Op.getOperand(1), In2,
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006748 Anode->getSrcValue(), Anode->getAlignment());
6749 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006750 ExpandOp(Result.getValue(0), Lo, Hi);
6751 // Remember that we legalized the chain.
6752 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006753 break;
6754 }
6755
Chris Lattner4e6c7462005-01-08 19:27:05 +00006756 // These operators cannot be expanded directly, emit them as calls to
6757 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006758 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006759 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006760 SDValue Op;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006761 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6762 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006763 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6764 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006765 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006766
Chris Lattnerf20d1832005-07-30 01:40:57 +00006767 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6768
Chris Lattner80a3e942005-07-29 00:33:32 +00006769 // Now that the custom expander is done, expand the result, which is still
6770 // VT.
Gabor Greifba36cb52008-08-28 21:40:38 +00006771 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006772 ExpandOp(Op, Lo, Hi);
6773 break;
6774 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006775 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006776
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006777 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6778 VT);
6779 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6780 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006781 break;
Evan Cheng56966222007-01-12 02:11:51 +00006782 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006783
Evan Cheng56966222007-01-12 02:11:51 +00006784 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006785 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006786 SDValue Op;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006787 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6788 case Expand: assert(0 && "cannot expand FP!");
6789 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6790 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6791 }
6792
6793 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6794
6795 // Now that the custom expander is done, expand the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00006796 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006797 ExpandOp(Op, Lo, Hi);
6798 break;
6799 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006800 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006801
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006802 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6803 VT);
6804 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6805 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006806 break;
Evan Cheng56966222007-01-12 02:11:51 +00006807 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006808
Evan Cheng05a2d562006-01-09 18:31:59 +00006809 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006810 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006811 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006812 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006813 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006814 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006815 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006816 // Now that the custom expander is done, expand the result, which is
6817 // still VT.
6818 ExpandOp(Op, Lo, Hi);
6819 break;
6820 }
6821 }
6822
Chris Lattner79980b02006-09-13 03:50:39 +00006823 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6824 // this X << 1 as X+X.
6825 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006826 if (ShAmt->getAPIntValue() == 1 &&
6827 TLI.isOperationLegalOrCustom(ISD::ADDC, NVT) &&
6828 TLI.isOperationLegalOrCustom(ISD::ADDE, NVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006829 SDValue LoOps[2], HiOps[3];
Chris Lattner79980b02006-09-13 03:50:39 +00006830 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6831 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6832 LoOps[1] = LoOps[0];
6833 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6834
6835 HiOps[1] = HiOps[0];
6836 HiOps[2] = Lo.getValue(1);
6837 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6838 break;
6839 }
6840 }
6841
Chris Lattnere34b3962005-01-19 04:19:40 +00006842 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006843 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006844 break;
Chris Lattner47599822005-04-02 03:38:53 +00006845
6846 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006847 TargetLowering::LegalizeAction Action =
6848 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6849 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6850 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006851 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006852 break;
6853 }
6854
Chris Lattnere34b3962005-01-19 04:19:40 +00006855 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006856 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006857 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006858 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006859
Evan Cheng05a2d562006-01-09 18:31:59 +00006860 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006861 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006862 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006863 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006864 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006865 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006866 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006867 // Now that the custom expander is done, expand the result, which is
6868 // still VT.
6869 ExpandOp(Op, Lo, Hi);
6870 break;
6871 }
6872 }
6873
Chris Lattnere34b3962005-01-19 04:19:40 +00006874 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006875 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006876 break;
Chris Lattner47599822005-04-02 03:38:53 +00006877
6878 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006879 TargetLowering::LegalizeAction Action =
6880 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6881 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6882 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006883 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006884 break;
6885 }
6886
Chris Lattnere34b3962005-01-19 04:19:40 +00006887 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006888 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006889 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006890 }
6891
6892 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006893 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006894 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006895 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006896 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006897 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006898 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006899 // Now that the custom expander is done, expand the result, which is
6900 // still VT.
6901 ExpandOp(Op, Lo, Hi);
6902 break;
6903 }
6904 }
6905
Chris Lattnere34b3962005-01-19 04:19:40 +00006906 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006907 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006908 break;
Chris Lattner47599822005-04-02 03:38:53 +00006909
6910 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006911 TargetLowering::LegalizeAction Action =
6912 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6913 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6914 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006915 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006916 break;
6917 }
6918
Chris Lattnere34b3962005-01-19 04:19:40 +00006919 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006920 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006921 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006922 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006923
Misha Brukmanedf128a2005-04-21 22:36:52 +00006924 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006925 case ISD::SUB: {
6926 // If the target wants to custom expand this, let them.
6927 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6928 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006929 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006930 if (Result.getNode()) {
Duncan Sands69bfb152008-06-22 09:42:16 +00006931 ExpandOp(Result, Lo, Hi);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006932 break;
6933 }
6934 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006935 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006936 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006937 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6938 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman475871a2008-07-27 21:46:04 +00006939 SDValue LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006940 LoOps[0] = LHSL;
6941 LoOps[1] = RHSL;
6942 HiOps[0] = LHSH;
6943 HiOps[1] = RHSH;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006944
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006945 //cascaded check to see if any smaller size has a a carry flag.
6946 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6947 bool hasCarry = false;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006948 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6949 MVT AVT = MVT::getIntegerVT(BitSize);
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006950 if (TLI.isOperationLegalOrCustom(OpV, AVT)) {
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006951 hasCarry = true;
6952 break;
6953 }
6954 }
6955
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006956 if(hasCarry) {
Evan Cheng637ed032008-12-12 18:49:09 +00006957 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006958 if (Node->getOpcode() == ISD::ADD) {
6959 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6960 HiOps[2] = Lo.getValue(1);
6961 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6962 } else {
6963 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6964 HiOps[2] = Lo.getValue(1);
6965 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6966 }
6967 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006968 } else {
Andrew Lenharth40d51392008-10-07 14:15:42 +00006969 if (Node->getOpcode() == ISD::ADD) {
Evan Cheng637ed032008-12-12 18:49:09 +00006970 Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
6971 Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
Duncan Sands5480c042009-01-01 15:52:00 +00006972 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(NVT),
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006973 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006974 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6975 DAG.getConstant(1, NVT),
6976 DAG.getConstant(0, NVT));
Duncan Sands5480c042009-01-01 15:52:00 +00006977 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(NVT),
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006978 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006979 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6980 DAG.getConstant(1, NVT),
6981 Carry1);
6982 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6983 } else {
Evan Cheng637ed032008-12-12 18:49:09 +00006984 Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
6985 Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006986 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6987 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6988 DAG.getConstant(1, NVT),
6989 DAG.getConstant(0, NVT));
6990 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6991 }
6992 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006993 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006994 }
Chris Lattnerb429f732007-05-17 18:15:41 +00006995
6996 case ISD::ADDC:
6997 case ISD::SUBC: {
6998 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006999 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00007000 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7001 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7002 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00007003 SDValue LoOps[2] = { LHSL, RHSL };
7004 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00007005
7006 if (Node->getOpcode() == ISD::ADDC) {
7007 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
7008 HiOps[2] = Lo.getValue(1);
7009 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
7010 } else {
7011 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
7012 HiOps[2] = Lo.getValue(1);
7013 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
7014 }
7015 // Remember that we legalized the flag.
7016 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7017 break;
7018 }
7019 case ISD::ADDE:
7020 case ISD::SUBE: {
7021 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00007022 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00007023 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7024 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7025 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00007026 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
7027 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00007028
7029 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
7030 HiOps[2] = Lo.getValue(1);
7031 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
7032
7033 // Remember that we legalized the flag.
7034 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7035 break;
7036 }
Nate Begemanc7c16572005-04-11 03:01:51 +00007037 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00007038 // If the target wants to custom expand this, let them.
7039 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00007040 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007041 if (New.getNode()) {
Chris Lattner8829dc82006-09-16 05:08:34 +00007042 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00007043 break;
7044 }
7045 }
7046
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007047 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, NVT);
7048 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, NVT);
7049 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, NVT);
7050 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00007051 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman475871a2008-07-27 21:46:04 +00007052 SDValue LL, LH, RL, RH;
Nate Begemanc7c16572005-04-11 03:01:51 +00007053 ExpandOp(Node->getOperand(0), LL, LH);
7054 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007055 unsigned OuterBitSize = Op.getValueSizeInBits();
7056 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00007057 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
7058 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00007059 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
7060 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
7061 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00007062 // The inputs are both zero-extended.
7063 if (HasUMUL_LOHI) {
7064 // We can emit a umul_lohi.
7065 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00007066 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00007067 break;
7068 }
7069 if (HasMULHU) {
7070 // We can emit a mulhu+mul.
7071 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7072 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7073 break;
7074 }
Dan Gohman525178c2007-10-08 18:33:35 +00007075 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007076 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00007077 // The input values are both sign-extended.
7078 if (HasSMUL_LOHI) {
7079 // We can emit a smul_lohi.
7080 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00007081 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00007082 break;
7083 }
7084 if (HasMULHS) {
7085 // We can emit a mulhs+mul.
7086 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7087 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
7088 break;
7089 }
7090 }
7091 if (HasUMUL_LOHI) {
7092 // Lo,Hi = umul LHS, RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00007093 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman525178c2007-10-08 18:33:35 +00007094 DAG.getVTList(NVT, NVT), LL, RL);
7095 Lo = UMulLOHI;
7096 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00007097 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7098 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7099 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7100 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00007101 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00007102 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00007103 if (HasMULHU) {
7104 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7105 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7106 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7107 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7108 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7109 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7110 break;
7111 }
Nate Begemanc7c16572005-04-11 03:01:51 +00007112 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00007113
Dan Gohman525178c2007-10-08 18:33:35 +00007114 // If nothing else, we can make a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00007115 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00007116 break;
7117 }
Evan Cheng56966222007-01-12 02:11:51 +00007118 case ISD::SDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007119 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007120 break;
7121 case ISD::UDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007122 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007123 break;
7124 case ISD::SREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007125 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007126 break;
7127 case ISD::UREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007128 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007129 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00007130
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007131 case ISD::FADD:
Duncan Sands460a14e2008-04-12 17:14:18 +00007132 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7133 RTLIB::ADD_F64,
7134 RTLIB::ADD_F80,
7135 RTLIB::ADD_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007136 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007137 break;
7138 case ISD::FSUB:
Duncan Sands460a14e2008-04-12 17:14:18 +00007139 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7140 RTLIB::SUB_F64,
7141 RTLIB::SUB_F80,
7142 RTLIB::SUB_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007143 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007144 break;
7145 case ISD::FMUL:
Duncan Sands460a14e2008-04-12 17:14:18 +00007146 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7147 RTLIB::MUL_F64,
7148 RTLIB::MUL_F80,
7149 RTLIB::MUL_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007150 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007151 break;
7152 case ISD::FDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007153 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7154 RTLIB::DIV_F64,
7155 RTLIB::DIV_F80,
7156 RTLIB::DIV_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007157 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007158 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007159 case ISD::FP_EXTEND: {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007160 if (VT == MVT::ppcf128) {
7161 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7162 Node->getOperand(0).getValueType()==MVT::f64);
7163 const uint64_t zero = 0;
7164 if (Node->getOperand(0).getValueType()==MVT::f32)
7165 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
7166 else
7167 Hi = Node->getOperand(0);
7168 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7169 break;
7170 }
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007171 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7172 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7173 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007174 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007175 }
7176 case ISD::FP_ROUND: {
7177 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7178 VT);
7179 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7180 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007181 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007182 }
Evan Cheng4b887022008-09-09 23:02:14 +00007183 case ISD::FSQRT:
7184 case ISD::FSIN:
7185 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007186 case ISD::FLOG:
7187 case ISD::FLOG2:
7188 case ISD::FLOG10:
7189 case ISD::FEXP:
7190 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007191 case ISD::FTRUNC:
7192 case ISD::FFLOOR:
7193 case ISD::FCEIL:
7194 case ISD::FRINT:
7195 case ISD::FNEARBYINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00007196 case ISD::FPOW:
7197 case ISD::FPOWI: {
Evan Cheng56966222007-01-12 02:11:51 +00007198 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007199 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00007200 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00007201 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7202 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007203 break;
7204 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00007205 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7206 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007207 break;
7208 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00007209 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7210 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007211 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007212 case ISD::FLOG:
7213 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7214 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7215 break;
7216 case ISD::FLOG2:
7217 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7218 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7219 break;
7220 case ISD::FLOG10:
7221 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7222 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7223 break;
7224 case ISD::FEXP:
7225 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7226 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7227 break;
7228 case ISD::FEXP2:
7229 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7230 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7231 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007232 case ISD::FTRUNC:
7233 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7234 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7235 break;
7236 case ISD::FFLOOR:
7237 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7238 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7239 break;
7240 case ISD::FCEIL:
7241 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7242 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7243 break;
7244 case ISD::FRINT:
7245 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7246 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7247 break;
7248 case ISD::FNEARBYINT:
7249 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7250 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7251 break;
Evan Cheng4b887022008-09-09 23:02:14 +00007252 case ISD::FPOW:
7253 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7254 RTLIB::POW_PPCF128);
7255 break;
7256 case ISD::FPOWI:
7257 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7258 RTLIB::POWI_PPCF128);
7259 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007260 default: assert(0 && "Unreachable!");
7261 }
Duncan Sands460a14e2008-04-12 17:14:18 +00007262 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00007263 break;
7264 }
Evan Cheng966bf242006-12-16 00:52:40 +00007265 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007266 if (VT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00007267 SDValue Tmp;
Dale Johannesenf6467742007-10-12 19:02:17 +00007268 ExpandOp(Node->getOperand(0), Lo, Tmp);
7269 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7270 // lo = hi==fabs(hi) ? lo : -lo;
7271 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7272 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7273 DAG.getCondCode(ISD::SETEQ));
7274 break;
7275 }
Dan Gohman475871a2008-07-27 21:46:04 +00007276 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007277 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7278 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7279 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7280 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7281 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7282 if (getTypeAction(NVT) == Expand)
7283 ExpandOp(Lo, Lo, Hi);
7284 break;
7285 }
7286 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007287 if (VT == MVT::ppcf128) {
7288 ExpandOp(Node->getOperand(0), Lo, Hi);
7289 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7290 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7291 break;
7292 }
Dan Gohman475871a2008-07-27 21:46:04 +00007293 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007294 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7295 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7296 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7297 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7298 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7299 if (getTypeAction(NVT) == Expand)
7300 ExpandOp(Lo, Lo, Hi);
7301 break;
7302 }
Evan Cheng912095b2007-01-04 21:56:39 +00007303 case ISD::FCOPYSIGN: {
7304 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7305 if (getTypeAction(NVT) == Expand)
7306 ExpandOp(Lo, Lo, Hi);
7307 break;
7308 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007309 case ISD::SINT_TO_FP:
7310 case ISD::UINT_TO_FP: {
7311 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007312 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00007313
7314 // Promote the operand if needed. Do this before checking for
7315 // ppcf128 so conversions of i16 and i8 work.
7316 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman475871a2008-07-27 21:46:04 +00007317 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesencf498192008-03-18 17:28:38 +00007318 Tmp = isSigned
7319 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7320 DAG.getValueType(SrcVT))
7321 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greifba36cb52008-08-28 21:40:38 +00007322 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesencf498192008-03-18 17:28:38 +00007323 SrcVT = Node->getOperand(0).getValueType();
7324 }
7325
Dan Gohmana2e94852008-03-10 23:03:31 +00007326 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007327 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007328 if (isSigned) {
7329 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7330 Node->getOperand(0)));
7331 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7332 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007333 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007334 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7335 Node->getOperand(0)));
7336 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7337 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00007338 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007339 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7340 DAG.getConstant(0, MVT::i32),
7341 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7342 DAG.getConstantFP(
7343 APFloat(APInt(128, 2, TwoE32)),
7344 MVT::ppcf128)),
7345 Hi,
7346 DAG.getCondCode(ISD::SETLT)),
7347 Lo, Hi);
7348 }
7349 break;
7350 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00007351 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7352 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007353 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen6e63e092007-10-12 17:52:03 +00007354 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7355 Lo, Hi);
7356 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7357 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7358 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7359 DAG.getConstant(0, MVT::i64),
7360 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7361 DAG.getConstantFP(
7362 APFloat(APInt(128, 2, TwoE64)),
7363 MVT::ppcf128)),
7364 Hi,
7365 DAG.getCondCode(ISD::SETLT)),
7366 Lo, Hi);
7367 break;
7368 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007369
Dan Gohmana2e94852008-03-10 23:03:31 +00007370 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7371 Node->getOperand(0));
Evan Cheng110cf482008-04-01 01:50:16 +00007372 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng6ad2f932008-04-01 01:51:26 +00007373 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng110cf482008-04-01 01:50:16 +00007374 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00007375 break;
7376 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00007377 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00007378
Chris Lattner83397362005-12-21 18:02:52 +00007379 // Make sure the resultant values have been legalized themselves, unless this
7380 // is a type that requires multi-step expansion.
7381 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7382 Lo = LegalizeOp(Lo);
Gabor Greifba36cb52008-08-28 21:40:38 +00007383 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00007384 // Don't legalize the high part if it is expanded to a single node.
7385 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00007386 }
Evan Cheng05a2d562006-01-09 18:31:59 +00007387
7388 // Remember in a map if the values will be reused later.
Dan Gohman6b345ee2008-07-07 17:46:23 +00007389 bool isNew =
7390 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng05a2d562006-01-09 18:31:59 +00007391 assert(isNew && "Value already expanded?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007392 isNew = isNew;
Chris Lattner3e928bb2005-01-07 07:47:09 +00007393}
7394
Dan Gohman7f321562007-06-25 16:23:39 +00007395/// SplitVectorOp - Given an operand of vector type, break it down into
7396/// two smaller values, still of vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00007397void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7398 SDValue &Hi) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007399 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007400 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007401 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattnerc7029802006-03-18 01:44:44 +00007402 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00007403
Duncan Sands83ec4b62008-06-06 12:08:01 +00007404 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman5db1afb2007-11-15 21:15:26 +00007405
7406 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7407 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7408
Duncan Sands83ec4b62008-06-06 12:08:01 +00007409 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7410 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007411
Chris Lattnerc7029802006-03-18 01:44:44 +00007412 // See if we already split it.
Dan Gohman475871a2008-07-27 21:46:04 +00007413 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattnerc7029802006-03-18 01:44:44 +00007414 = SplitNodes.find(Op);
7415 if (I != SplitNodes.end()) {
7416 Lo = I->second.first;
7417 Hi = I->second.second;
7418 return;
7419 }
7420
7421 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007422 default:
7423#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007424 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007425#endif
7426 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007427 case ISD::UNDEF:
7428 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7429 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7430 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007431 case ISD::BUILD_PAIR:
7432 Lo = Node->getOperand(0);
7433 Hi = Node->getOperand(1);
7434 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00007435 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman68679912008-04-25 18:07:40 +00007436 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7437 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007438 unsigned Index = Idx->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007439 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman68679912008-04-25 18:07:40 +00007440 if (Index < NewNumElts_Lo)
7441 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7442 DAG.getIntPtrConstant(Index));
7443 else
7444 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7445 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7446 break;
7447 }
Dan Gohman475871a2008-07-27 21:46:04 +00007448 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman68679912008-04-25 18:07:40 +00007449 Node->getOperand(1),
7450 Node->getOperand(2));
7451 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohman9fe46622007-09-28 23:53:40 +00007452 break;
7453 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00007454 case ISD::VECTOR_SHUFFLE: {
7455 // Build the low part.
Dan Gohman475871a2008-07-27 21:46:04 +00007456 SDValue Mask = Node->getOperand(2);
7457 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007458 MVT PtrVT = TLI.getPointerTy();
Chris Lattner6c9c6802007-11-19 21:16:54 +00007459
7460 // Insert all of the elements from the input that are needed. We use
7461 // buildvector of extractelement here because the input vectors will have
7462 // to be legalized, so this makes the code simpler.
7463 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007464 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007465 if (IdxNode.getOpcode() == ISD::UNDEF) {
7466 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7467 continue;
7468 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007469 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007470 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007471 if (Idx >= NumElements) {
7472 InVec = Node->getOperand(1);
7473 Idx -= NumElements;
7474 }
7475 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7476 DAG.getConstant(Idx, PtrVT)));
7477 }
7478 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7479 Ops.clear();
7480
7481 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007482 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007483 if (IdxNode.getOpcode() == ISD::UNDEF) {
7484 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7485 continue;
7486 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007487 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007488 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007489 if (Idx >= NumElements) {
7490 InVec = Node->getOperand(1);
7491 Idx -= NumElements;
7492 }
7493 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7494 DAG.getConstant(Idx, PtrVT)));
7495 }
Mon P Wang92879f32008-07-25 01:30:26 +00007496 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007497 break;
7498 }
Dan Gohman7f321562007-06-25 16:23:39 +00007499 case ISD::BUILD_VECTOR: {
Dan Gohman475871a2008-07-27 21:46:04 +00007500 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00007501 Node->op_begin()+NewNumElts_Lo);
7502 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007503
Dan Gohman475871a2008-07-27 21:46:04 +00007504 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00007505 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007506 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007507 break;
7508 }
Dan Gohman7f321562007-06-25 16:23:39 +00007509 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00007510 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00007511 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7512 if (NewNumSubvectors == 1) {
7513 Lo = Node->getOperand(0);
7514 Hi = Node->getOperand(1);
7515 } else {
Mon P Wangaeb06d22008-11-10 04:46:22 +00007516 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7517 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007518 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00007519
Mon P Wangaeb06d22008-11-10 04:46:22 +00007520 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohman7f321562007-06-25 16:23:39 +00007521 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007522 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00007523 }
Dan Gohman65956352007-06-13 15:12:02 +00007524 break;
7525 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00007526 case ISD::EXTRACT_SUBVECTOR: {
7527 SDValue Vec = Op.getOperand(0);
7528 SDValue Idx = Op.getOperand(1);
7529 MVT IdxVT = Idx.getValueType();
7530
7531 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7532 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7533 if (CIdx) {
7534 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7535 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7536 IdxVT));
7537 } else {
7538 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7539 DAG.getConstant(NewNumElts_Lo, IdxVT));
7540 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7541 }
7542 break;
7543 }
Dan Gohmanc6230962007-10-17 14:48:28 +00007544 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007545 SDValue Cond = Node->getOperand(0);
Dan Gohmanc6230962007-10-17 14:48:28 +00007546
Dan Gohman475871a2008-07-27 21:46:04 +00007547 SDValue LL, LH, RL, RH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007548 SplitVectorOp(Node->getOperand(1), LL, LH);
7549 SplitVectorOp(Node->getOperand(2), RL, RH);
7550
Duncan Sands83ec4b62008-06-06 12:08:01 +00007551 if (Cond.getValueType().isVector()) {
Dan Gohmanc6230962007-10-17 14:48:28 +00007552 // Handle a vector merge.
Dan Gohman475871a2008-07-27 21:46:04 +00007553 SDValue CL, CH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007554 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007555 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7556 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007557 } else {
7558 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00007559 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7560 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007561 }
7562 break;
7563 }
Chris Lattner80c1a562008-06-30 02:43:01 +00007564 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007565 SDValue CondLHS = Node->getOperand(0);
7566 SDValue CondRHS = Node->getOperand(1);
7567 SDValue CondCode = Node->getOperand(4);
Chris Lattner80c1a562008-06-30 02:43:01 +00007568
Dan Gohman475871a2008-07-27 21:46:04 +00007569 SDValue LL, LH, RL, RH;
Chris Lattner80c1a562008-06-30 02:43:01 +00007570 SplitVectorOp(Node->getOperand(2), LL, LH);
7571 SplitVectorOp(Node->getOperand(3), RL, RH);
7572
7573 // Handle a simple select with vector operands.
7574 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7575 LL, RL, CondCode);
7576 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7577 LH, RH, CondCode);
7578 break;
7579 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00007580 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007581 SDValue LL, LH, RL, RH;
Nate Begemanb43e9c12008-05-12 19:40:03 +00007582 SplitVectorOp(Node->getOperand(0), LL, LH);
7583 SplitVectorOp(Node->getOperand(1), RL, RH);
7584 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7585 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7586 break;
7587 }
Dan Gohman7f321562007-06-25 16:23:39 +00007588 case ISD::ADD:
7589 case ISD::SUB:
7590 case ISD::MUL:
7591 case ISD::FADD:
7592 case ISD::FSUB:
7593 case ISD::FMUL:
7594 case ISD::SDIV:
7595 case ISD::UDIV:
7596 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00007597 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007598 case ISD::AND:
7599 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00007600 case ISD::XOR:
7601 case ISD::UREM:
7602 case ISD::SREM:
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007603 case ISD::FREM:
7604 case ISD::SHL:
7605 case ISD::SRA:
7606 case ISD::SRL: {
Dan Gohman475871a2008-07-27 21:46:04 +00007607 SDValue LL, LH, RL, RH;
Chris Lattnerc7029802006-03-18 01:44:44 +00007608 SplitVectorOp(Node->getOperand(0), LL, LH);
7609 SplitVectorOp(Node->getOperand(1), RL, RH);
7610
Nate Begeman5db1afb2007-11-15 21:15:26 +00007611 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7612 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00007613 break;
7614 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00007615 case ISD::FP_ROUND:
Dan Gohman82669522007-10-11 23:57:53 +00007616 case ISD::FPOWI: {
Dan Gohman475871a2008-07-27 21:46:04 +00007617 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007618 SplitVectorOp(Node->getOperand(0), L, H);
7619
Nate Begeman5db1afb2007-11-15 21:15:26 +00007620 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7621 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00007622 break;
7623 }
7624 case ISD::CTTZ:
7625 case ISD::CTLZ:
7626 case ISD::CTPOP:
7627 case ISD::FNEG:
7628 case ISD::FABS:
7629 case ISD::FSQRT:
7630 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00007631 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007632 case ISD::FLOG:
7633 case ISD::FLOG2:
7634 case ISD::FLOG10:
7635 case ISD::FEXP:
7636 case ISD::FEXP2:
Nate Begemanb348d182007-11-17 03:58:34 +00007637 case ISD::FP_TO_SINT:
7638 case ISD::FP_TO_UINT:
7639 case ISD::SINT_TO_FP:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007640 case ISD::UINT_TO_FP:
7641 case ISD::TRUNCATE:
7642 case ISD::ANY_EXTEND:
7643 case ISD::SIGN_EXTEND:
7644 case ISD::ZERO_EXTEND:
7645 case ISD::FP_EXTEND: {
Dan Gohman475871a2008-07-27 21:46:04 +00007646 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007647 SplitVectorOp(Node->getOperand(0), L, H);
7648
Nate Begeman5db1afb2007-11-15 21:15:26 +00007649 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7650 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00007651 break;
7652 }
Mon P Wang77cdf302008-11-10 20:54:11 +00007653 case ISD::CONVERT_RNDSAT: {
7654 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7655 SDValue L, H;
7656 SplitVectorOp(Node->getOperand(0), L, H);
7657 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7658 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7659 SDValue STyOpL = DAG.getValueType(L.getValueType());
7660 SDValue STyOpH = DAG.getValueType(H.getValueType());
7661
7662 SDValue RndOp = Node->getOperand(3);
7663 SDValue SatOp = Node->getOperand(4);
7664
7665 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7666 RndOp, SatOp, CvtCode);
7667 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7668 RndOp, SatOp, CvtCode);
7669 break;
7670 }
Dan Gohman7f321562007-06-25 16:23:39 +00007671 case ISD::LOAD: {
7672 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007673 SDValue Ch = LD->getChain();
7674 SDValue Ptr = LD->getBasePtr();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007675 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007676 const Value *SV = LD->getSrcValue();
7677 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007678 MVT MemoryVT = LD->getMemoryVT();
Dan Gohman7f321562007-06-25 16:23:39 +00007679 unsigned Alignment = LD->getAlignment();
7680 bool isVolatile = LD->isVolatile();
7681
Dan Gohman7f8613e2008-08-14 20:04:46 +00007682 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7683 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7684
7685 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7686 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7687 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7688
7689 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7690 NewVT_Lo, Ch, Ptr, Offset,
7691 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7692 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00007693 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00007694 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00007695 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00007696 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00007697 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7698 NewVT_Hi, Ch, Ptr, Offset,
7699 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00007700
7701 // Build a factor node to remember that this load is independent of the
7702 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00007703 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Chris Lattnerc7029802006-03-18 01:44:44 +00007704 Hi.getValue(1));
7705
7706 // Remember that we legalized the chain.
7707 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00007708 break;
7709 }
Dan Gohman7f321562007-06-25 16:23:39 +00007710 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00007711 // We know the result is a vector. The input may be either a vector or a
7712 // scalar value.
Dan Gohman475871a2008-07-27 21:46:04 +00007713 SDValue InOp = Node->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007714 if (!InOp.getValueType().isVector() ||
7715 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman10a7aa62007-06-29 00:09:08 +00007716 // The input is a scalar or single-element vector.
7717 // Lower to a store/load so that it can be split.
7718 // FIXME: this could be improved probably.
Mon P Wang2920d2b2008-07-15 05:28:34 +00007719 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7720 Op.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00007721 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greifba36cb52008-08-28 21:40:38 +00007722 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Chris Lattner7692eb42006-03-23 21:16:34 +00007723
Dan Gohman475871a2008-07-27 21:46:04 +00007724 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00007725 InOp, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007726 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00007727 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007728 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00007729 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00007730 // Split the vector and convert each of the pieces now.
7731 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007732 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7733 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00007734 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00007735 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007736 }
7737
7738 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00007739 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00007740 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00007741 assert(isNew && "Value already split?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007742 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007743}
7744
7745
Dan Gohman89b20c02007-06-27 14:06:22 +00007746/// ScalarizeVectorOp - Given an operand of single-element vector type
7747/// (e.g. v1f32), convert it into the equivalent operation that returns a
7748/// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +00007749SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007750 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007751 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007752 MVT NewVT = Op.getValueType().getVectorElementType();
7753 assert(Op.getValueType().getVectorNumElements() == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00007754
Dan Gohman7f321562007-06-25 16:23:39 +00007755 // See if we already scalarized it.
Dan Gohman475871a2008-07-27 21:46:04 +00007756 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohman7f321562007-06-25 16:23:39 +00007757 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00007758
Dan Gohman475871a2008-07-27 21:46:04 +00007759 SDValue Result;
Chris Lattnerc7029802006-03-18 01:44:44 +00007760 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00007761 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007762#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007763 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007764#endif
Dan Gohman7f321562007-06-25 16:23:39 +00007765 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7766 case ISD::ADD:
7767 case ISD::FADD:
7768 case ISD::SUB:
7769 case ISD::FSUB:
7770 case ISD::MUL:
7771 case ISD::FMUL:
7772 case ISD::SDIV:
7773 case ISD::UDIV:
7774 case ISD::FDIV:
7775 case ISD::SREM:
7776 case ISD::UREM:
7777 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00007778 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007779 case ISD::AND:
7780 case ISD::OR:
7781 case ISD::XOR:
7782 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00007783 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007784 ScalarizeVectorOp(Node->getOperand(0)),
7785 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00007786 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007787 case ISD::FNEG:
7788 case ISD::FABS:
7789 case ISD::FSQRT:
7790 case ISD::FSIN:
7791 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007792 case ISD::FLOG:
7793 case ISD::FLOG2:
7794 case ISD::FLOG10:
7795 case ISD::FEXP:
7796 case ISD::FEXP2:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007797 case ISD::FP_TO_SINT:
7798 case ISD::FP_TO_UINT:
7799 case ISD::SINT_TO_FP:
7800 case ISD::UINT_TO_FP:
7801 case ISD::SIGN_EXTEND:
7802 case ISD::ZERO_EXTEND:
7803 case ISD::ANY_EXTEND:
7804 case ISD::TRUNCATE:
7805 case ISD::FP_EXTEND:
Dan Gohman7f321562007-06-25 16:23:39 +00007806 Result = DAG.getNode(Node->getOpcode(),
7807 NewVT,
7808 ScalarizeVectorOp(Node->getOperand(0)));
7809 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00007810 case ISD::CONVERT_RNDSAT: {
7811 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7812 Result = DAG.getConvertRndSat(NewVT, Op0,
7813 DAG.getValueType(NewVT),
7814 DAG.getValueType(Op0.getValueType()),
7815 Node->getOperand(3),
7816 Node->getOperand(4),
7817 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7818 break;
7819 }
Dan Gohman9e04c822007-10-12 14:13:46 +00007820 case ISD::FPOWI:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007821 case ISD::FP_ROUND:
Dan Gohman9e04c822007-10-12 14:13:46 +00007822 Result = DAG.getNode(Node->getOpcode(),
7823 NewVT,
7824 ScalarizeVectorOp(Node->getOperand(0)),
7825 Node->getOperand(1));
7826 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007827 case ISD::LOAD: {
7828 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007829 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7830 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman7f8613e2008-08-14 20:04:46 +00007831 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007832 const Value *SV = LD->getSrcValue();
7833 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007834 MVT MemoryVT = LD->getMemoryVT();
7835 unsigned Alignment = LD->getAlignment();
7836 bool isVolatile = LD->isVolatile();
7837
7838 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7839 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7840
7841 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7842 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7843 MemoryVT.getVectorElementType(),
7844 isVolatile, Alignment);
Dan Gohman7f321562007-06-25 16:23:39 +00007845
Chris Lattnerc7029802006-03-18 01:44:44 +00007846 // Remember that we legalized the chain.
7847 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7848 break;
7849 }
Dan Gohman7f321562007-06-25 16:23:39 +00007850 case ISD::BUILD_VECTOR:
7851 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007852 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007853 case ISD::INSERT_VECTOR_ELT:
7854 // Returning the inserted scalar element.
7855 Result = Node->getOperand(1);
7856 break;
7857 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007858 assert(Node->getOperand(0).getValueType() == NewVT &&
7859 "Concat of non-legal vectors not yet supported!");
7860 Result = Node->getOperand(0);
7861 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007862 case ISD::VECTOR_SHUFFLE: {
7863 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman475871a2008-07-27 21:46:04 +00007864 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007865 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohman7f321562007-06-25 16:23:39 +00007866 Result = ScalarizeVectorOp(Node->getOperand(1));
7867 else
7868 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007869 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007870 }
7871 case ISD::EXTRACT_SUBVECTOR:
Mon P Wange0b436a2008-11-06 22:52:21 +00007872 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wangaeb06d22008-11-10 04:46:22 +00007873 Node->getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00007874 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007875 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007876 SDValue Op0 = Op.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007877 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng446efdd2008-05-16 17:19:05 +00007878 Op0 = ScalarizeVectorOp(Op0);
7879 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Chris Lattner5b2316e2006-03-28 20:24:43 +00007880 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007881 }
Dan Gohman7f321562007-06-25 16:23:39 +00007882 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007883 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007884 ScalarizeVectorOp(Op.getOperand(1)),
7885 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007886 break;
Chris Lattner80c1a562008-06-30 02:43:01 +00007887 case ISD::SELECT_CC:
7888 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7889 Node->getOperand(1),
7890 ScalarizeVectorOp(Op.getOperand(2)),
7891 ScalarizeVectorOp(Op.getOperand(3)),
7892 Node->getOperand(4));
7893 break;
Nate Begeman0d1704b2008-05-12 23:09:43 +00007894 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007895 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7896 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Duncan Sands5480c042009-01-01 15:52:00 +00007897 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0.getValueType()),
7898 Op0, Op1, Op.getOperand(2));
Nate Begeman0d1704b2008-05-12 23:09:43 +00007899 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7900 DAG.getConstant(-1ULL, NewVT),
7901 DAG.getConstant(0ULL, NewVT));
7902 break;
7903 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007904 }
7905
7906 if (TLI.isTypeLegal(NewVT))
7907 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007908 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7909 assert(isNew && "Value already scalarized?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007910 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007911 return Result;
7912}
7913
Chris Lattner3e928bb2005-01-07 07:47:09 +00007914
Mon P Wang0c397192008-10-30 08:01:45 +00007915SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7916 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7917 if (I != WidenNodes.end()) return I->second;
7918
7919 MVT VT = Op.getValueType();
7920 assert(VT.isVector() && "Cannot widen non-vector type!");
7921
7922 SDValue Result;
7923 SDNode *Node = Op.getNode();
7924 MVT EVT = VT.getVectorElementType();
7925
7926 unsigned NumElts = VT.getVectorNumElements();
7927 unsigned NewNumElts = WidenVT.getVectorNumElements();
7928 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7929 assert(NewNumElts < 17);
7930
7931 // When widen is called, it is assumed that it is more efficient to use a
7932 // wide type. The default action is to widen to operation to a wider legal
7933 // vector type and then do the operation if it is legal by calling LegalizeOp
7934 // again. If there is no vector equivalent, we will unroll the operation, do
7935 // it, and rebuild the vector. If most of the operations are vectorizible to
7936 // the legal type, the resulting code will be more efficient. If this is not
7937 // the case, the resulting code will preform badly as we end up generating
7938 // code to pack/unpack the results. It is the function that calls widen
Mon P Wangf007a8b2008-11-06 05:31:54 +00007939 // that is responsible for seeing this doesn't happen.
Mon P Wang0c397192008-10-30 08:01:45 +00007940 switch (Node->getOpcode()) {
7941 default:
7942#ifndef NDEBUG
7943 Node->dump(&DAG);
7944#endif
7945 assert(0 && "Unexpected operation in WidenVectorOp!");
7946 break;
7947 case ISD::CopyFromReg:
Mon P Wang49292f12008-11-15 06:05:52 +00007948 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang0c397192008-10-30 08:01:45 +00007949 case ISD::Constant:
7950 case ISD::ConstantFP:
7951 // To build a vector of these elements, clients should call BuildVector
7952 // and with each element instead of creating a node with a vector type
7953 assert(0 && "Unexpected operation in WidenVectorOp!");
7954 case ISD::VAARG:
7955 // Variable Arguments with vector types doesn't make any sense to me
7956 assert(0 && "Unexpected operation in WidenVectorOp!");
7957 break;
Mon P Wang49292f12008-11-15 06:05:52 +00007958 case ISD::UNDEF:
7959 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7960 break;
Mon P Wang0c397192008-10-30 08:01:45 +00007961 case ISD::BUILD_VECTOR: {
7962 // Build a vector with undefined for the new nodes
7963 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7964 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7965 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7966 }
7967 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7968 break;
7969 }
7970 case ISD::INSERT_VECTOR_ELT: {
7971 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7972 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7973 Node->getOperand(1), Node->getOperand(2));
7974 break;
7975 }
7976 case ISD::VECTOR_SHUFFLE: {
7977 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7978 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7979 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7980 // used as permutation array. We build the vector here instead of widening
7981 // because we don't want to legalize and have it turned to something else.
7982 SDValue PermOp = Node->getOperand(2);
7983 SDValueVector NewOps;
7984 MVT PVT = PermOp.getValueType().getVectorElementType();
7985 for (unsigned i = 0; i < NumElts; ++i) {
7986 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7987 NewOps.push_back(PermOp.getOperand(i));
7988 } else {
7989 unsigned Idx =
Mon P Wange1a0b2e2008-12-13 08:15:14 +00007990 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
Mon P Wang0c397192008-10-30 08:01:45 +00007991 if (Idx < NumElts) {
7992 NewOps.push_back(PermOp.getOperand(i));
7993 }
7994 else {
7995 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7996 PermOp.getOperand(i).getValueType()));
7997 }
7998 }
7999 }
8000 for (unsigned i = NumElts; i < NewNumElts; ++i) {
8001 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
8002 }
8003
8004 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
8005 MVT::getVectorVT(PVT, NewOps.size()),
8006 &NewOps[0], NewOps.size());
8007
8008 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
8009 break;
8010 }
8011 case ISD::LOAD: {
8012 // If the load widen returns true, we can use a single load for the
8013 // vector. Otherwise, it is returning a token factor for multiple
8014 // loads.
8015 SDValue TFOp;
8016 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
8017 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
8018 else
8019 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
8020 break;
8021 }
8022
8023 case ISD::BIT_CONVERT: {
8024 SDValue Tmp1 = Node->getOperand(0);
8025 // Converts between two different types so we need to determine
8026 // the correct widen type for the input operand.
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008027 MVT InVT = Tmp1.getValueType();
8028 unsigned WidenSize = WidenVT.getSizeInBits();
8029 if (InVT.isVector()) {
8030 MVT InEltVT = InVT.getVectorElementType();
8031 unsigned InEltSize = InEltVT.getSizeInBits();
8032 assert(WidenSize % InEltSize == 0 &&
8033 "can not widen bit convert that are not multiple of element type");
8034 MVT NewInWidenVT = MVT::getVectorVT(InEltVT, WidenSize / InEltSize);
8035 Tmp1 = WidenVectorOp(Tmp1, NewInWidenVT);
8036 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
8037 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Tmp1);
8038 } else {
8039 // If the result size is a multiple of the input size, widen the input
8040 // and then convert.
8041 unsigned InSize = InVT.getSizeInBits();
8042 assert(WidenSize % InSize == 0 &&
8043 "can not widen bit convert that are not multiple of element type");
8044 unsigned NewNumElts = WidenSize / InSize;
8045 SmallVector<SDValue, 16> Ops(NewNumElts);
8046 SDValue UndefVal = DAG.getNode(ISD::UNDEF, InVT);
8047 Ops[0] = Tmp1;
8048 for (unsigned i = 1; i < NewNumElts; ++i)
8049 Ops[i] = UndefVal;
Mon P Wang0c397192008-10-30 08:01:45 +00008050
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008051 MVT NewInVT = MVT::getVectorVT(InVT, NewNumElts);
8052 Result = DAG.getNode(ISD::BUILD_VECTOR, NewInVT, &Ops[0], NewNumElts);
8053 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00008054 }
8055 break;
8056 }
8057
8058 case ISD::SINT_TO_FP:
8059 case ISD::UINT_TO_FP:
8060 case ISD::FP_TO_SINT:
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008061 case ISD::FP_TO_UINT:
8062 case ISD::FP_ROUND: {
Mon P Wang0c397192008-10-30 08:01:45 +00008063 SDValue Tmp1 = Node->getOperand(0);
8064 // Converts between two different types so we need to determine
8065 // the correct widen type for the input operand.
8066 MVT TVT = Tmp1.getValueType();
8067 assert(TVT.isVector() && "can not widen non vector type");
8068 MVT TEVT = TVT.getVectorElementType();
8069 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
8070 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
8071 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
8072 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00008073 break;
8074 }
8075
8076 case ISD::FP_EXTEND:
8077 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
8078 case ISD::TRUNCATE:
8079 case ISD::SIGN_EXTEND:
8080 case ISD::ZERO_EXTEND:
8081 case ISD::ANY_EXTEND:
Mon P Wang0c397192008-10-30 08:01:45 +00008082 case ISD::SIGN_EXTEND_INREG:
8083 case ISD::FABS:
8084 case ISD::FNEG:
8085 case ISD::FSQRT:
8086 case ISD::FSIN:
Mon P Wang49292f12008-11-15 06:05:52 +00008087 case ISD::FCOS:
8088 case ISD::CTPOP:
8089 case ISD::CTTZ:
8090 case ISD::CTLZ: {
Mon P Wang0c397192008-10-30 08:01:45 +00008091 // Unary op widening
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008092 SDValue Tmp1;
Mon P Wang0c397192008-10-30 08:01:45 +00008093 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8094 assert(Tmp1.getValueType() == WidenVT);
8095 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00008096 break;
8097 }
Mon P Wang77cdf302008-11-10 20:54:11 +00008098 case ISD::CONVERT_RNDSAT: {
8099 SDValue RndOp = Node->getOperand(3);
8100 SDValue SatOp = Node->getOperand(4);
Mon P Wang77cdf302008-11-10 20:54:11 +00008101 SDValue SrcOp = Node->getOperand(0);
8102
8103 // Converts between two different types so we need to determine
8104 // the correct widen type for the input operand.
8105 MVT SVT = SrcOp.getValueType();
8106 assert(SVT.isVector() && "can not widen non vector type");
8107 MVT SEVT = SVT.getVectorElementType();
8108 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8109
8110 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8111 assert(SrcOp.getValueType() == WidenVT);
8112 SDValue DTyOp = DAG.getValueType(WidenVT);
8113 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8114 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8115
8116 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
8117 RndOp, SatOp, CvtCode);
Mon P Wang77cdf302008-11-10 20:54:11 +00008118 break;
8119 }
Mon P Wang0c397192008-10-30 08:01:45 +00008120 case ISD::FPOW:
8121 case ISD::FPOWI:
8122 case ISD::ADD:
8123 case ISD::SUB:
8124 case ISD::MUL:
8125 case ISD::MULHS:
8126 case ISD::MULHU:
8127 case ISD::AND:
8128 case ISD::OR:
8129 case ISD::XOR:
8130 case ISD::FADD:
8131 case ISD::FSUB:
8132 case ISD::FMUL:
8133 case ISD::SDIV:
8134 case ISD::SREM:
8135 case ISD::FDIV:
8136 case ISD::FREM:
8137 case ISD::FCOPYSIGN:
8138 case ISD::UDIV:
8139 case ISD::UREM:
8140 case ISD::BSWAP: {
8141 // Binary op widening
Mon P Wang0c397192008-10-30 08:01:45 +00008142 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8143 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8144 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8145 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008146 break;
8147 }
8148
8149 case ISD::SHL:
8150 case ISD::SRA:
8151 case ISD::SRL: {
Mon P Wang0c397192008-10-30 08:01:45 +00008152 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8153 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangfb13f002008-12-02 07:35:08 +00008154 SDValue ShOp = Node->getOperand(1);
8155 MVT ShVT = ShOp.getValueType();
8156 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8157 WidenVT.getVectorNumElements());
8158 ShOp = WidenVectorOp(ShOp, NewShVT);
8159 assert(ShOp.getValueType() == NewShVT);
8160 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, ShOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008161 break;
8162 }
Mon P Wangfb13f002008-12-02 07:35:08 +00008163
Mon P Wang0c397192008-10-30 08:01:45 +00008164 case ISD::EXTRACT_VECTOR_ELT: {
8165 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8166 assert(Tmp1.getValueType() == WidenVT);
8167 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8168 break;
8169 }
8170 case ISD::CONCAT_VECTORS: {
8171 // We concurrently support only widen on a multiple of the incoming vector.
8172 // We could widen on a multiple of the incoming operand if necessary.
8173 unsigned NumConcat = NewNumElts / NumElts;
8174 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Mon P Wangfb13f002008-12-02 07:35:08 +00008175 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
Mon P Wang0c397192008-10-30 08:01:45 +00008176 SmallVector<SDValue, 8> MOps;
8177 MOps.push_back(Op);
8178 for (unsigned i = 1; i != NumConcat; ++i) {
8179 MOps.push_back(UndefVal);
8180 }
8181 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8182 &MOps[0], MOps.size()));
8183 break;
8184 }
8185 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang49292f12008-11-15 06:05:52 +00008186 SDValue Tmp1 = Node->getOperand(0);
8187 SDValue Idx = Node->getOperand(1);
8188 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8189 if (CIdx && CIdx->getZExtValue() == 0) {
8190 // Since we are access the start of the vector, the incoming
8191 // vector type might be the proper.
8192 MVT Tmp1VT = Tmp1.getValueType();
8193 if (Tmp1VT == WidenVT)
8194 return Tmp1;
8195 else {
8196 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8197 if (Tmp1VTNumElts < NewNumElts)
8198 Result = WidenVectorOp(Tmp1, WidenVT);
8199 else
8200 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8201 }
8202 } else if (NewNumElts % NumElts == 0) {
8203 // Widen the extracted subvector.
8204 unsigned NumConcat = NewNumElts / NumElts;
8205 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8206 SmallVector<SDValue, 8> MOps;
8207 MOps.push_back(Op);
8208 for (unsigned i = 1; i != NumConcat; ++i) {
8209 MOps.push_back(UndefVal);
8210 }
8211 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8212 &MOps[0], MOps.size()));
8213 } else {
8214 assert(0 && "can not widen extract subvector");
8215 // This could be implemented using insert and build vector but I would
8216 // like to see when this happens.
8217 }
Mon P Wang0c397192008-10-30 08:01:45 +00008218 break;
8219 }
8220
8221 case ISD::SELECT: {
Mon P Wang0c397192008-10-30 08:01:45 +00008222 // Determine new condition widen type and widen
8223 SDValue Cond1 = Node->getOperand(0);
8224 MVT CondVT = Cond1.getValueType();
8225 assert(CondVT.isVector() && "can not widen non vector type");
8226 MVT CondEVT = CondVT.getVectorElementType();
8227 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8228 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8229 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8230
8231 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8232 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8233 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8234 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008235 break;
8236 }
8237
8238 case ISD::SELECT_CC: {
Mon P Wang0c397192008-10-30 08:01:45 +00008239 // Determine new condition widen type and widen
8240 SDValue Cond1 = Node->getOperand(0);
8241 SDValue Cond2 = Node->getOperand(1);
8242 MVT CondVT = Cond1.getValueType();
8243 assert(CondVT.isVector() && "can not widen non vector type");
8244 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8245 MVT CondEVT = CondVT.getVectorElementType();
8246 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8247 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8248 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8249 assert(Cond1.getValueType() == CondWidenVT &&
8250 Cond2.getValueType() == CondWidenVT && "condition not widen");
8251
8252 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8253 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8254 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8255 "operands not widen");
8256 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8257 Tmp2, Node->getOperand(4));
Mon P Wang0c397192008-10-30 08:01:45 +00008258 break;
Mon P Wang2eb13c32008-10-30 18:21:52 +00008259 }
8260 case ISD::VSETCC: {
8261 // Determine widen for the operand
8262 SDValue Tmp1 = Node->getOperand(0);
8263 MVT TmpVT = Tmp1.getValueType();
8264 assert(TmpVT.isVector() && "can not widen non vector type");
8265 MVT TmpEVT = TmpVT.getVectorElementType();
8266 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8267 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8268 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008269 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
Mon P Wang2eb13c32008-10-30 18:21:52 +00008270 Node->getOperand(2));
Mon P Wang0c397192008-10-30 08:01:45 +00008271 break;
8272 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008273 case ISD::ATOMIC_CMP_SWAP:
8274 case ISD::ATOMIC_LOAD_ADD:
8275 case ISD::ATOMIC_LOAD_SUB:
8276 case ISD::ATOMIC_LOAD_AND:
8277 case ISD::ATOMIC_LOAD_OR:
8278 case ISD::ATOMIC_LOAD_XOR:
8279 case ISD::ATOMIC_LOAD_NAND:
8280 case ISD::ATOMIC_LOAD_MIN:
8281 case ISD::ATOMIC_LOAD_MAX:
8282 case ISD::ATOMIC_LOAD_UMIN:
8283 case ISD::ATOMIC_LOAD_UMAX:
8284 case ISD::ATOMIC_SWAP: {
Mon P Wang0c397192008-10-30 08:01:45 +00008285 // For now, we assume that using vectors for these operations don't make
8286 // much sense so we just split it. We return an empty result
8287 SDValue X, Y;
8288 SplitVectorOp(Op, X, Y);
8289 return Result;
8290 break;
8291 }
8292
8293 } // end switch (Node->getOpcode())
8294
8295 assert(Result.getNode() && "Didn't set a result!");
8296 if (Result != Op)
8297 Result = LegalizeOp(Result);
8298
Mon P Wangf007a8b2008-11-06 05:31:54 +00008299 AddWidenedOperand(Op, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00008300 return Result;
8301}
8302
8303// Utility function to find a legal vector type and its associated element
8304// type from a preferred width and whose vector type must be the same size
8305// as the VVT.
8306// TLI: Target lowering used to determine legal types
8307// Width: Preferred width of element type
8308// VVT: Vector value type whose size we must match.
8309// Returns VecEVT and EVT - the vector type and its associated element type
Dan Gohman0d137d72009-01-15 16:43:02 +00008310static void FindWidenVecType(const TargetLowering &TLI, unsigned Width, MVT VVT,
Mon P Wang0c397192008-10-30 08:01:45 +00008311 MVT& EVT, MVT& VecEVT) {
8312 // We start with the preferred width, make it a power of 2 and see if
8313 // we can find a vector type of that width. If not, we reduce it by
8314 // another power of 2. If we have widen the type, a vector of bytes should
8315 // always be legal.
8316 assert(TLI.isTypeLegal(VVT));
8317 unsigned EWidth = Width + 1;
8318 do {
8319 assert(EWidth > 0);
8320 EWidth = (1 << Log2_32(EWidth-1));
8321 EVT = MVT::getIntegerVT(EWidth);
8322 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8323 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8324 } while (!TLI.isTypeLegal(VecEVT) ||
8325 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8326}
8327
8328SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8329 SDValue Chain,
8330 SDValue BasePtr,
8331 const Value *SV,
8332 int SVOffset,
8333 unsigned Alignment,
8334 bool isVolatile,
8335 unsigned LdWidth,
8336 MVT ResType) {
8337 // We assume that we have good rules to handle loading power of two loads so
8338 // we break down the operations to power of 2 loads. The strategy is to
8339 // load the largest power of 2 that we can easily transform to a legal vector
8340 // and then insert into that vector, and the cast the result into the legal
8341 // vector that we want. This avoids unnecessary stack converts.
8342 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8343 // the load is nonvolatile, we an use a wider load for the value.
8344 // Find a vector length we can load a large chunk
8345 MVT EVT, VecEVT;
8346 unsigned EVTWidth;
8347 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8348 EVTWidth = EVT.getSizeInBits();
8349
8350 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8351 isVolatile, Alignment);
8352 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8353 LdChain.push_back(LdOp.getValue(1));
8354
8355 // Check if we can load the element with one instruction
8356 if (LdWidth == EVTWidth) {
8357 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8358 }
8359
8360 // The vector element order is endianness dependent.
8361 unsigned Idx = 1;
8362 LdWidth -= EVTWidth;
8363 unsigned Offset = 0;
8364
8365 while (LdWidth > 0) {
8366 unsigned Increment = EVTWidth / 8;
8367 Offset += Increment;
8368 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8369 DAG.getIntPtrConstant(Increment));
8370
8371 if (LdWidth < EVTWidth) {
8372 // Our current type we are using is too large, use a smaller size by
8373 // using a smaller power of 2
8374 unsigned oEVTWidth = EVTWidth;
8375 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8376 EVTWidth = EVT.getSizeInBits();
8377 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008378 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang0c397192008-10-30 08:01:45 +00008379 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8380 }
8381
8382 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8383 SVOffset+Offset, isVolatile,
8384 MinAlign(Alignment, Offset));
8385 LdChain.push_back(LdOp.getValue(1));
8386 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8387 DAG.getIntPtrConstant(Idx++));
8388
8389 LdWidth -= EVTWidth;
8390 }
8391
8392 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8393}
8394
8395bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8396 SDValue& TFOp,
8397 SDValue Op,
8398 MVT NVT) {
8399 // TODO: Add support for ConcatVec and the ability to load many vector
8400 // types (e.g., v4i8). This will not work when a vector register
8401 // to memory mapping is strange (e.g., vector elements are not
8402 // stored in some sequential order).
8403
8404 // It must be true that the widen vector type is bigger than where
8405 // we need to load from.
8406 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8407 MVT LdVT = LD->getMemoryVT();
8408 assert(LdVT.isVector() && NVT.isVector());
8409 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8410
8411 // Load information
8412 SDValue Chain = LD->getChain();
8413 SDValue BasePtr = LD->getBasePtr();
8414 int SVOffset = LD->getSrcValueOffset();
8415 unsigned Alignment = LD->getAlignment();
8416 bool isVolatile = LD->isVolatile();
8417 const Value *SV = LD->getSrcValue();
8418 unsigned int LdWidth = LdVT.getSizeInBits();
8419
8420 // Load value as a large register
8421 SDValueVector LdChain;
8422 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8423 Alignment, isVolatile, LdWidth, NVT);
8424
8425 if (LdChain.size() == 1) {
8426 TFOp = LdChain[0];
8427 return true;
8428 }
8429 else {
8430 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8431 return false;
8432 }
8433}
8434
8435
8436void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8437 SDValue Chain,
8438 SDValue BasePtr,
8439 const Value *SV,
8440 int SVOffset,
8441 unsigned Alignment,
8442 bool isVolatile,
Mon P Wang49292f12008-11-15 06:05:52 +00008443 SDValue ValOp,
Mon P Wang0c397192008-10-30 08:01:45 +00008444 unsigned StWidth) {
8445 // Breaks the stores into a series of power of 2 width stores. For any
8446 // width, we convert the vector to the vector of element size that we
8447 // want to store. This avoids requiring a stack convert.
8448
8449 // Find a width of the element type we can store with
8450 MVT VVT = ValOp.getValueType();
8451 MVT EVT, VecEVT;
8452 unsigned EVTWidth;
8453 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8454 EVTWidth = EVT.getSizeInBits();
8455
8456 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8457 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wange0b436a2008-11-06 22:52:21 +00008458 DAG.getIntPtrConstant(0));
Mon P Wang0c397192008-10-30 08:01:45 +00008459 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8460 isVolatile, Alignment);
8461 StChain.push_back(StOp);
8462
8463 // Check if we are done
8464 if (StWidth == EVTWidth) {
8465 return;
8466 }
8467
8468 unsigned Idx = 1;
8469 StWidth -= EVTWidth;
8470 unsigned Offset = 0;
8471
8472 while (StWidth > 0) {
8473 unsigned Increment = EVTWidth / 8;
8474 Offset += Increment;
8475 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8476 DAG.getIntPtrConstant(Increment));
8477
8478 if (StWidth < EVTWidth) {
8479 // Our current type we are using is too large, use a smaller size by
8480 // using a smaller power of 2
8481 unsigned oEVTWidth = EVTWidth;
8482 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8483 EVTWidth = EVT.getSizeInBits();
8484 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008485 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang0c397192008-10-30 08:01:45 +00008486 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8487 }
8488
8489 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang49292f12008-11-15 06:05:52 +00008490 DAG.getIntPtrConstant(Idx++));
Mon P Wang0c397192008-10-30 08:01:45 +00008491 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8492 SVOffset + Offset, isVolatile,
8493 MinAlign(Alignment, Offset)));
8494 StWidth -= EVTWidth;
8495 }
8496}
8497
8498
8499SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8500 SDValue Chain,
8501 SDValue BasePtr) {
8502 // TODO: It might be cleaner if we can use SplitVector and have more legal
8503 // vector types that can be stored into memory (e.g., v4xi8 can
8504 // be stored as a word). This will not work when a vector register
8505 // to memory mapping is strange (e.g., vector elements are not
8506 // stored in some sequential order).
8507
8508 MVT StVT = ST->getMemoryVT();
8509 SDValue ValOp = ST->getValue();
8510
8511 // Check if we have widen this node with another value
8512 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8513 if (I != WidenNodes.end())
8514 ValOp = I->second;
8515
8516 MVT VVT = ValOp.getValueType();
8517
8518 // It must be true that we the widen vector type is bigger than where
8519 // we need to store.
8520 assert(StVT.isVector() && VVT.isVector());
Dan Gohmanf83c81a2009-01-28 03:10:52 +00008521 assert(StVT.bitsLT(VVT));
Mon P Wang0c397192008-10-30 08:01:45 +00008522 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8523
8524 // Store value
8525 SDValueVector StChain;
8526 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8527 ST->getSrcValueOffset(), ST->getAlignment(),
8528 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8529 if (StChain.size() == 1)
8530 return StChain[0];
8531 else
8532 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8533}
8534
8535
Chris Lattner3e928bb2005-01-07 07:47:09 +00008536// SelectionDAG::Legalize - This is the entry point for the file.
8537//
Duncan Sandsb6862bb2008-12-14 09:43:15 +00008538void SelectionDAG::Legalize(bool TypesNeedLegalizing) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00008539 /// run - This is the main entry point to this class.
8540 ///
Duncan Sandsb6862bb2008-12-14 09:43:15 +00008541 SelectionDAGLegalize(*this, TypesNeedLegalizing).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00008542}
8543