blob: d02e68d30c5e861efc408b98e31ccc1f885f2ccd [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000019#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng61bbbab2007-08-16 23:50:06 +000020#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000022#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000024#include "llvm/Target/TargetOptions.h"
Dan Gohman707e0182008-04-12 04:36:06 +000025#include "llvm/Target/TargetSubtarget.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000026#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000027#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000028#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000029#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000030#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000031#include "llvm/Support/MathExtras.h"
Chris Lattner79715142007-02-03 01:12:36 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000033#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000034#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000035#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000036using namespace llvm;
37
38//===----------------------------------------------------------------------===//
39/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
40/// hacks on it until the target machine can handle it. This involves
41/// eliminating value sizes the machine cannot handle (promoting small sizes to
42/// large sizes or splitting up large values into small values) as well as
43/// eliminating operations the machine cannot handle.
44///
45/// This code also does a small amount of optimization and recognition of idioms
46/// as part of its processing. For example, if a target does not support a
47/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
48/// will attempt merge setcc and brc instructions into brcc's.
49///
50namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000051class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000052 TargetLowering &TLI;
53 SelectionDAG &DAG;
54
Chris Lattner6831a812006-02-13 09:18:02 +000055 // Libcall insertion helpers.
56
57 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
58 /// legalized. We use this to ensure that calls are properly serialized
59 /// against each other, including inserted libcalls.
Dan Gohman475871a2008-07-27 21:46:04 +000060 SDValue LastCALLSEQ_END;
Chris Lattner6831a812006-02-13 09:18:02 +000061
62 /// IsLegalizingCall - This member is used *only* for purposes of providing
63 /// helpful assertions that a libcall isn't created while another call is
64 /// being legalized (which could lead to non-serialized call sequences).
65 bool IsLegalizingCall;
66
Chris Lattner3e928bb2005-01-07 07:47:09 +000067 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000068 Legal, // The target natively supports this operation.
69 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000070 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000071 };
Chris Lattner6831a812006-02-13 09:18:02 +000072
Chris Lattner3e928bb2005-01-07 07:47:09 +000073 /// ValueTypeActions - This is a bitvector that contains two bits for each
74 /// value type, where the two bits correspond to the LegalizeAction enum.
75 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000076 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000077
Chris Lattner3e928bb2005-01-07 07:47:09 +000078 /// LegalizedNodes - For nodes that are of legal width, and that have more
79 /// than one use, this map indicates what regularized operand to use. This
80 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000081 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000082
Chris Lattner03c85462005-01-15 05:21:40 +000083 /// PromotedNodes - For nodes that are below legal width, and that have more
84 /// than one use, this map indicates what promoted value to use. This allows
85 /// us to avoid promoting the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000086 DenseMap<SDValue, SDValue> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000087
Chris Lattnerc7029802006-03-18 01:44:44 +000088 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +000089 /// which operands are the expanded version of the input. This allows
Chris Lattnerc7029802006-03-18 01:44:44 +000090 /// us to avoid expanding the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000091 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000092
Chris Lattnerc7029802006-03-18 01:44:44 +000093 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +000094 /// which operands are the split version of the input. This allows us
Chris Lattnerc7029802006-03-18 01:44:44 +000095 /// to avoid splitting the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000096 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +000097
Dan Gohman7f321562007-06-25 16:23:39 +000098 /// ScalarizedNodes - For nodes that need to be converted from vector types to
99 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000100 /// processed to the result.
Dan Gohman475871a2008-07-27 21:46:04 +0000101 std::map<SDValue, SDValue> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000102
Mon P Wangf007a8b2008-11-06 05:31:54 +0000103 /// WidenNodes - For nodes that need to be widened from one vector type to
104 /// another, this contains the mapping of those that we have already widen.
105 /// This allows us to avoid widening more than once.
Mon P Wang0c397192008-10-30 08:01:45 +0000106 std::map<SDValue, SDValue> WidenNodes;
107
Dan Gohman475871a2008-07-27 21:46:04 +0000108 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000109 LegalizedNodes.insert(std::make_pair(From, To));
110 // If someone requests legalization of the new node, return itself.
111 if (From != To)
112 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000113 }
Dan Gohman475871a2008-07-27 21:46:04 +0000114 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman6b345ee2008-07-07 17:46:23 +0000115 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Chris Lattner03c85462005-01-15 05:21:40 +0000116 assert(isNew && "Got into the map somehow?");
Evan Cheng24ac4082008-11-24 07:09:49 +0000117 isNew = isNew;
Chris Lattner69a889e2005-12-20 00:53:54 +0000118 // If someone requests legalization of the new node, return itself.
119 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000120 }
Mon P Wangf007a8b2008-11-06 05:31:54 +0000121 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang0c397192008-10-30 08:01:45 +0000122 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
123 assert(isNew && "Got into the map somehow?");
Evan Cheng24ac4082008-11-24 07:09:49 +0000124 isNew = isNew;
Mon P Wang0c397192008-10-30 08:01:45 +0000125 // If someone requests legalization of the new node, return itself.
126 LegalizedNodes.insert(std::make_pair(To, To));
127 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000128
Chris Lattner3e928bb2005-01-07 07:47:09 +0000129public:
Dan Gohman1002c022008-07-07 18:00:37 +0000130 explicit SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000131
Chris Lattner3e928bb2005-01-07 07:47:09 +0000132 /// getTypeAction - Return how we should legalize values of this type, either
133 /// it is already legal or we need to expand it into multiple registers of
134 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000135 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000136 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000137 }
138
139 /// isTypeLegal - Return true if this type is legal on this target.
140 ///
Duncan Sands83ec4b62008-06-06 12:08:01 +0000141 bool isTypeLegal(MVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000142 return getTypeAction(VT) == Legal;
143 }
144
Chris Lattner3e928bb2005-01-07 07:47:09 +0000145 void LegalizeDAG();
146
Chris Lattner456a93a2006-01-28 07:39:30 +0000147private:
Dan Gohman7f321562007-06-25 16:23:39 +0000148 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000149 /// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000150 void HandleOp(SDValue Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000151
152 /// LegalizeOp - We know that the specified value has a legal type.
153 /// Recursively ensure that the operands have legal types, then return the
154 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000155 SDValue LegalizeOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000156
Dan Gohman82669522007-10-11 23:57:53 +0000157 /// UnrollVectorOp - We know that the given vector has a legal type, however
158 /// the operation it performs is not legal and is an operation that we have
159 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
160 /// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000161 SDValue UnrollVectorOp(SDValue O);
Nate Begeman68679912008-04-25 18:07:40 +0000162
163 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
164 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
165 /// is necessary to spill the vector being inserted into to memory, perform
166 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000167 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
168 SDValue Idx);
Dan Gohman82669522007-10-11 23:57:53 +0000169
Chris Lattnerc7029802006-03-18 01:44:44 +0000170 /// PromoteOp - Given an operation that produces a value in an invalid type,
171 /// promote it to compute the value into a larger type. The produced value
172 /// will have the correct bits for the low portion of the register, but no
173 /// guarantee is made about the top bits: it may be zero, sign-extended, or
174 /// garbage.
Dan Gohman475871a2008-07-27 21:46:04 +0000175 SDValue PromoteOp(SDValue O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000176
Dan Gohman475871a2008-07-27 21:46:04 +0000177 /// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattnerc7029802006-03-18 01:44:44 +0000178 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman929d3eb2008-10-01 15:07:49 +0000179 /// the LegalizedNodes map is filled in for any results that are not expanded,
Chris Lattnerc7029802006-03-18 01:44:44 +0000180 /// the ExpandedNodes map is filled in for any results that are expanded, and
181 /// the Lo/Hi values are returned. This applies to integer types and Vector
182 /// types.
Dan Gohman475871a2008-07-27 21:46:04 +0000183 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000184
Mon P Wangf007a8b2008-11-06 05:31:54 +0000185 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
186 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
187 /// for the existing elements but no guarantee is made about the new elements
188 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
189 /// when we have an instruction operating on an illegal vector type and we
190 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang0c397192008-10-30 08:01:45 +0000191 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
192
Dan Gohman7f321562007-06-25 16:23:39 +0000193 /// SplitVectorOp - Given an operand of vector type, break it down into
194 /// two smaller values.
Dan Gohman475871a2008-07-27 21:46:04 +0000195 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000196
Dan Gohman89b20c02007-06-27 14:06:22 +0000197 /// ScalarizeVectorOp - Given an operand of single-element vector type
198 /// (e.g. v1f32), convert it into the equivalent operation that returns a
199 /// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +0000200 SDValue ScalarizeVectorOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000201
Mon P Wangf007a8b2008-11-06 05:31:54 +0000202 /// Useful 16 element vector type that is used to pass operands for widening.
Mon P Wang0c397192008-10-30 08:01:45 +0000203 typedef SmallVector<SDValue, 16> SDValueVector;
204
205 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
206 /// the LdChain contains a single load and false if it contains a token
207 /// factor for multiple loads. It takes
208 /// Result: location to return the result
209 /// LdChain: location to return the load chain
210 /// Op: load operation to widen
211 /// NVT: widen vector result type we want for the load
212 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
213 SDValue Op, MVT NVT);
214
215 /// Helper genWidenVectorLoads - Helper function to generate a set of
216 /// loads to load a vector with a resulting wider type. It takes
217 /// LdChain: list of chains for the load we have generated
218 /// Chain: incoming chain for the ld vector
219 /// BasePtr: base pointer to load from
220 /// SV: memory disambiguation source value
221 /// SVOffset: memory disambiugation offset
222 /// Alignment: alignment of the memory
223 /// isVolatile: volatile load
224 /// LdWidth: width of memory that we want to load
225 /// ResType: the wider result result type for the resulting loaded vector
226 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
227 SDValue BasePtr, const Value *SV,
228 int SVOffset, unsigned Alignment,
229 bool isVolatile, unsigned LdWidth,
230 MVT ResType);
231
232 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
233 /// location. It takes
234 /// ST: store node that we want to replace
235 /// Chain: incoming store chain
236 /// BasePtr: base address of where we want to store into
237 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
238 SDValue BasePtr);
239
240 /// Helper genWidenVectorStores - Helper function to generate a set of
241 /// stores to store a widen vector into non widen memory
242 // It takes
243 // StChain: list of chains for the stores we have generated
244 // Chain: incoming chain for the ld vector
245 // BasePtr: base pointer to load from
246 // SV: memory disambiguation source value
247 // SVOffset: memory disambiugation offset
248 // Alignment: alignment of the memory
249 // isVolatile: volatile lod
250 // ValOp: value to store
251 // StWidth: width of memory that we want to store
252 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
253 SDValue BasePtr, const Value *SV,
254 int SVOffset, unsigned Alignment,
255 bool isVolatile, SDValue ValOp,
256 unsigned StWidth);
257
Duncan Sandsd038e042008-07-21 10:20:31 +0000258 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Chris Lattner4352cc92006-04-04 17:23:26 +0000259 /// specified mask and type. Targets can specify exactly which masks they
260 /// support and the code generator is tasked with not creating illegal masks.
261 ///
262 /// Note that this will also return true for shuffles that are promoted to a
263 /// different type.
264 ///
265 /// If this is a legal shuffle, this method returns the (possibly promoted)
266 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman475871a2008-07-27 21:46:04 +0000267 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Chris Lattner4352cc92006-04-04 17:23:26 +0000268
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000269 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000270 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000271
Dan Gohman475871a2008-07-27 21:46:04 +0000272 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Evan Cheng7f042682008-10-15 02:05:31 +0000273 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC);
274 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC) {
275 LegalizeSetCCOperands(LHS, RHS, CC);
276 LegalizeSetCCCondCode(VT, LHS, RHS, CC);
277 }
Nate Begeman750ac1b2006-02-01 07:19:44 +0000278
Dan Gohman475871a2008-07-27 21:46:04 +0000279 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
280 SDValue &Hi);
281 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000282
Dan Gohman475871a2008-07-27 21:46:04 +0000283 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
284 SDValue ExpandBUILD_VECTOR(SDNode *Node);
285 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dan Gohman7f8613e2008-08-14 20:04:46 +0000286 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000287 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
288 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
289 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000290
Dan Gohman475871a2008-07-27 21:46:04 +0000291 SDValue ExpandBSWAP(SDValue Op);
292 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
293 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
294 SDValue &Lo, SDValue &Hi);
295 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
296 SDValue &Lo, SDValue &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000297
Dan Gohman475871a2008-07-27 21:46:04 +0000298 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
299 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Mon P Wange9f10152008-12-09 05:46:39 +0000300
301 // Returns the legalized (truncated or extended) shift amount.
302 SDValue LegalizeShiftAmount(SDValue ShiftAmt);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000303};
304}
305
Chris Lattner4352cc92006-04-04 17:23:26 +0000306/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
307/// specified mask and type. Targets can specify exactly which masks they
308/// support and the code generator is tasked with not creating illegal masks.
309///
310/// Note that this will also return true for shuffles that are promoted to a
311/// different type.
Dan Gohman475871a2008-07-27 21:46:04 +0000312SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Chris Lattner4352cc92006-04-04 17:23:26 +0000313 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
314 default: return 0;
315 case TargetLowering::Legal:
316 case TargetLowering::Custom:
317 break;
318 case TargetLowering::Promote: {
319 // If this is promoted to a different type, convert the shuffle mask and
320 // ask if it is legal in the promoted type!
Duncan Sands83ec4b62008-06-06 12:08:01 +0000321 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd038e042008-07-21 10:20:31 +0000322 MVT EltVT = NVT.getVectorElementType();
Chris Lattner4352cc92006-04-04 17:23:26 +0000323
324 // If we changed # elements, change the shuffle mask.
325 unsigned NumEltsGrowth =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000326 NVT.getVectorNumElements() / VT.getVectorNumElements();
Chris Lattner4352cc92006-04-04 17:23:26 +0000327 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
328 if (NumEltsGrowth > 1) {
329 // Renumber the elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000330 SmallVector<SDValue, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000331 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000332 SDValue InOp = Mask.getOperand(i);
Chris Lattner4352cc92006-04-04 17:23:26 +0000333 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
334 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd038e042008-07-21 10:20:31 +0000335 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000336 else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000337 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd038e042008-07-21 10:20:31 +0000338 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000339 }
340 }
341 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000342 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000343 }
344 VT = NVT;
345 break;
346 }
347 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000348 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Chris Lattner4352cc92006-04-04 17:23:26 +0000349}
350
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000351SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
352 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
353 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000354 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000355 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000356}
357
Chris Lattner3e928bb2005-01-07 07:47:09 +0000358void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000359 LastCALLSEQ_END = DAG.getEntryNode();
360 IsLegalizingCall = false;
361
Chris Lattnerab510a72005-10-02 17:49:46 +0000362 // The legalize process is inherently a bottom-up recursive process (users
363 // legalize their uses before themselves). Given infinite stack space, we
364 // could just start legalizing on the root and traverse the whole graph. In
365 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000366 // blocks. To avoid this problem, compute an ordering of the nodes where each
367 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000368 DAG.AssignTopologicalOrder();
369 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
370 E = prior(DAG.allnodes_end()); I != next(E); ++I)
371 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000372
373 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000374 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000375 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
376 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000377
378 ExpandedNodes.clear();
379 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000380 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000381 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000382 ScalarizedNodes.clear();
Mon P Wang0c397192008-10-30 08:01:45 +0000383 WidenNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000384
385 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000386 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000387}
388
Chris Lattner6831a812006-02-13 09:18:02 +0000389
390/// FindCallEndFromCallStart - Given a chained node that is part of a call
391/// sequence, find the CALLSEQ_END node that terminates the call sequence.
392static SDNode *FindCallEndFromCallStart(SDNode *Node) {
393 if (Node->getOpcode() == ISD::CALLSEQ_END)
394 return Node;
395 if (Node->use_empty())
396 return 0; // No CallSeqEnd
397
398 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000399 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000400 if (TheChain.getValueType() != MVT::Other) {
401 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000402 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000403 if (TheChain.getValueType() != MVT::Other) {
404 // Otherwise, hunt for it.
405 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
406 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000407 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000408 break;
409 }
410
411 // Otherwise, we walked into a node without a chain.
412 if (TheChain.getValueType() != MVT::Other)
413 return 0;
414 }
415 }
416
417 for (SDNode::use_iterator UI = Node->use_begin(),
418 E = Node->use_end(); UI != E; ++UI) {
419
420 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000421 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000422 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
423 if (User->getOperand(i) == TheChain)
424 if (SDNode *Result = FindCallEndFromCallStart(User))
425 return Result;
426 }
427 return 0;
428}
429
430/// FindCallStartFromCallEnd - Given a chained node that is part of a call
431/// sequence, find the CALLSEQ_START node that initiates the call sequence.
432static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
433 assert(Node && "Didn't find callseq_start for a call??");
434 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
435
436 assert(Node->getOperand(0).getValueType() == MVT::Other &&
437 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000438 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000439}
440
441/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
442/// see if any uses can reach Dest. If no dest operands can get to dest,
443/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000444///
445/// Keep track of the nodes we fine that actually do lead to Dest in
446/// NodesLeadingTo. This avoids retraversing them exponential number of times.
447///
448bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000449 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000450 if (N == Dest) return true; // N certainly leads to Dest :)
451
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000452 // If we've already processed this node and it does lead to Dest, there is no
453 // need to reprocess it.
454 if (NodesLeadingTo.count(N)) return true;
455
Chris Lattner6831a812006-02-13 09:18:02 +0000456 // If the first result of this node has been already legalized, then it cannot
457 // reach N.
458 switch (getTypeAction(N->getValueType(0))) {
459 case Legal:
Dan Gohman475871a2008-07-27 21:46:04 +0000460 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000461 break;
462 case Promote:
Dan Gohman475871a2008-07-27 21:46:04 +0000463 if (PromotedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000464 break;
465 case Expand:
Dan Gohman475871a2008-07-27 21:46:04 +0000466 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000467 break;
468 }
469
470 // Okay, this node has not already been legalized. Check and legalize all
471 // operands. If none lead to Dest, then we can legalize this node.
472 bool OperandsLeadToDest = false;
473 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
474 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000475 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000476
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000477 if (OperandsLeadToDest) {
478 NodesLeadingTo.insert(N);
479 return true;
480 }
Chris Lattner6831a812006-02-13 09:18:02 +0000481
482 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000483 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000484 return false;
485}
486
Mon P Wang0c397192008-10-30 08:01:45 +0000487/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000488/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000489void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000490 MVT VT = Op.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000491 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000492 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000493 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang0c397192008-10-30 08:01:45 +0000494 case Promote:
495 if (!VT.isVector()) {
496 (void)PromoteOp(Op);
497 break;
498 }
499 else {
500 // See if we can widen otherwise use Expand to either scalarize or split
501 MVT WidenVT = TLI.getWidenVectorType(VT);
502 if (WidenVT != MVT::Other) {
503 (void) WidenVectorOp(Op, WidenVT);
504 break;
505 }
506 // else fall thru to expand since we can't widen the vector
507 }
Chris Lattnerc7029802006-03-18 01:44:44 +0000508 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000509 if (!VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000510 // If this is an illegal scalar, expand it into its two component
511 // pieces.
Dan Gohman475871a2008-07-27 21:46:04 +0000512 SDValue X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000513 if (Op.getOpcode() == ISD::TargetConstant)
514 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000515 ExpandOp(Op, X, Y);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000516 } else if (VT.getVectorNumElements() == 1) {
Dan Gohman7f321562007-06-25 16:23:39 +0000517 // If this is an illegal single element vector, convert it to a
518 // scalar operation.
519 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000520 } else {
Mon P Wang0c397192008-10-30 08:01:45 +0000521 // This is an illegal multiple element vector.
Dan Gohman7f321562007-06-25 16:23:39 +0000522 // Split it in half and legalize both parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000523 SDValue X, Y;
Dan Gohman7f321562007-06-25 16:23:39 +0000524 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000525 }
526 break;
527 }
528}
Chris Lattner6831a812006-02-13 09:18:02 +0000529
Evan Cheng9f877882006-12-13 20:57:08 +0000530/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
531/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000532static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000533 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000534 bool Extend = false;
535
536 // If a FP immediate is precise when represented as a float and if the
537 // target can do an extending load from float to double, we put it into
538 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000539 // double. This shrinks FP constants and canonicalizes them for targets where
540 // an FP extending load is the same cost as a normal load (such as on the x87
541 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000542 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000543 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000544 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000545 if (VT!=MVT::f64 && VT!=MVT::f32)
546 assert(0 && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000547 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000548 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000549 }
550
Duncan Sands83ec4b62008-06-06 12:08:01 +0000551 MVT OrigVT = VT;
552 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000553 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000554 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000555 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
556 // Only do this if the target has a native EXTLOAD instruction from
557 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000558 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000559 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000560 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000561 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
562 VT = SVT;
563 Extend = true;
564 }
Evan Cheng00495212006-12-12 21:32:44 +0000565 }
566
Dan Gohman475871a2008-07-27 21:46:04 +0000567 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +0000568 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000569 if (Extend)
570 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000571 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000572 0, VT, false, Alignment);
Evan Chengef120572008-03-04 08:05:30 +0000573 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000574 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000575}
576
Chris Lattner6831a812006-02-13 09:18:02 +0000577
Evan Cheng912095b2007-01-04 21:56:39 +0000578/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
579/// operations.
580static
Dan Gohman475871a2008-07-27 21:46:04 +0000581SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
582 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000583 MVT VT = Node->getValueType(0);
584 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000585 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
586 "fcopysign expansion only supported for f32 and f64");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000587 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000588
Evan Cheng912095b2007-01-04 21:56:39 +0000589 // First get the sign bit of second operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000590 SDValue Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000591 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
592 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000593 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman475871a2008-07-27 21:46:04 +0000594 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000595 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000596 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000597 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Evan Cheng912095b2007-01-04 21:56:39 +0000598 if (SizeDiff > 0) {
599 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
600 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
601 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnerc563e1d2008-07-10 23:46:13 +0000602 } else if (SizeDiff < 0) {
603 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
604 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
605 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
606 }
Evan Cheng068c5f42007-01-05 21:31:51 +0000607
608 // Clear the sign bit of first operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000609 SDValue Mask2 = (VT == MVT::f64)
Evan Cheng068c5f42007-01-05 21:31:51 +0000610 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
611 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
612 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman475871a2008-07-27 21:46:04 +0000613 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000614 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
615
616 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000617 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
618 return Result;
619}
620
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000621/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
622static
Dan Gohman475871a2008-07-27 21:46:04 +0000623SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
624 TargetLowering &TLI) {
625 SDValue Chain = ST->getChain();
626 SDValue Ptr = ST->getBasePtr();
627 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000628 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000629 int Alignment = ST->getAlignment();
630 int SVOffset = ST->getSrcValueOffset();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000631 if (ST->getMemoryVT().isFloatingPoint() ||
632 ST->getMemoryVT().isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000633 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
634 if (TLI.isTypeLegal(intVT)) {
635 // Expand to a bitconvert of the value to the integer type of the
636 // same size, then a (misaligned) int store.
637 // FIXME: Does not handle truncating floating point stores!
638 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
639 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
640 SVOffset, ST->isVolatile(), Alignment);
641 } else {
642 // Do a (aligned) store to a stack slot, then copy from the stack slot
643 // to the final destination using (unaligned) integer loads and stores.
644 MVT StoredVT = ST->getMemoryVT();
645 MVT RegVT =
646 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
647 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
648 unsigned RegBytes = RegVT.getSizeInBits() / 8;
649 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000650
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000651 // Make sure the stack slot is also aligned for the register type.
652 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
653
654 // Perform the original store, only redirected to the stack slot.
Duncan Sands05e11fa2008-12-12 21:47:02 +0000655 SDValue Store = DAG.getTruncStore(Chain, Val, StackPtr, NULL, 0,StoredVT);
656 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
657 SmallVector<SDValue, 8> Stores;
658 unsigned Offset = 0;
659
660 // Do all but one copies using the full register width.
661 for (unsigned i = 1; i < NumRegs; i++) {
662 // Load one integer register's worth from the stack slot.
663 SDValue Load = DAG.getLoad(RegVT, Store, StackPtr, NULL, 0);
664 // Store it to the final location. Remember the store.
665 Stores.push_back(DAG.getStore(Load.getValue(1), Load, Ptr,
666 ST->getSrcValue(), SVOffset + Offset,
667 ST->isVolatile(),
668 MinAlign(ST->getAlignment(), Offset)));
669 // Increment the pointers.
670 Offset += RegBytes;
671 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
672 Increment);
673 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
674 }
675
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000676 // The last store may be partial. Do a truncating store. On big-endian
677 // machines this requires an extending load from the stack slot to ensure
678 // that the bits are in the right place.
679 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000680
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000681 // Load from the stack slot.
682 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Store, StackPtr,
683 NULL, 0, MemVT);
684
Duncan Sands05e11fa2008-12-12 21:47:02 +0000685 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, Ptr,
686 ST->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000687 MemVT, ST->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000688 MinAlign(ST->getAlignment(), Offset)));
689 // The order of the stores doesn't matter - say it with a TokenFactor.
690 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
691 Stores.size());
692 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000693 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000694 assert(ST->getMemoryVT().isInteger() &&
695 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000696 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000697 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000698 MVT NewStoredVT =
699 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
700 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000701 int IncrementSize = NumBits / 8;
702
703 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000704 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
705 SDValue Lo = Val;
706 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000707
708 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000709 SDValue Store1, Store2;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000710 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
711 ST->getSrcValue(), SVOffset, NewStoredVT,
712 ST->isVolatile(), Alignment);
713 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
714 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000715 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000716 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
717 ST->getSrcValue(), SVOffset + IncrementSize,
718 NewStoredVT, ST->isVolatile(), Alignment);
719
720 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
721}
722
723/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
724static
Dan Gohman475871a2008-07-27 21:46:04 +0000725SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
726 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000727 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000728 SDValue Chain = LD->getChain();
729 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000730 MVT VT = LD->getValueType(0);
731 MVT LoadedVT = LD->getMemoryVT();
732 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000733 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
734 if (TLI.isTypeLegal(intVT)) {
735 // Expand to a (misaligned) integer load of the same size,
736 // then bitconvert to floating point or vector.
737 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
738 SVOffset, LD->isVolatile(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000739 LD->getAlignment());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000740 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
741 if (VT.isFloatingPoint() && LoadedVT != VT)
742 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000743
Duncan Sands05e11fa2008-12-12 21:47:02 +0000744 SDValue Ops[] = { Result, Chain };
745 return DAG.getMergeValues(Ops, 2);
746 } else {
747 // Copy the value to a (aligned) stack slot using (unaligned) integer
748 // loads and stores, then do a (aligned) load from the stack slot.
749 MVT RegVT = TLI.getRegisterType(intVT);
750 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
751 unsigned RegBytes = RegVT.getSizeInBits() / 8;
752 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
753
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000754 // Make sure the stack slot is also aligned for the register type.
755 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
756
Duncan Sands05e11fa2008-12-12 21:47:02 +0000757 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
758 SmallVector<SDValue, 8> Stores;
759 SDValue StackPtr = StackBase;
760 unsigned Offset = 0;
761
762 // Do all but one copies using the full register width.
763 for (unsigned i = 1; i < NumRegs; i++) {
764 // Load one integer register's worth from the original location.
765 SDValue Load = DAG.getLoad(RegVT, Chain, Ptr, LD->getSrcValue(),
766 SVOffset + Offset, LD->isVolatile(),
767 MinAlign(LD->getAlignment(), Offset));
768 // Follow the load with a store to the stack slot. Remember the store.
769 Stores.push_back(DAG.getStore(Load.getValue(1), Load, StackPtr,
770 NULL, 0));
771 // Increment the pointers.
772 Offset += RegBytes;
773 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
774 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
775 Increment);
776 }
777
778 // The last copy may be partial. Do an extending load.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000779 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000780 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Chain, Ptr,
781 LD->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000782 MemVT, LD->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000783 MinAlign(LD->getAlignment(), Offset));
784 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000785 // On big-endian machines this requires a truncating store to ensure
786 // that the bits end up in the right place.
787 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, StackPtr,
788 NULL, 0, MemVT));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000789
790 // The order of the stores doesn't matter - say it with a TokenFactor.
791 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
792 Stores.size());
793
794 // Finally, perform the original load only redirected to the stack slot.
795 Load = DAG.getExtLoad(LD->getExtensionType(), VT, TF, StackBase,
796 NULL, 0, LoadedVT);
797
798 // Callers expect a MERGE_VALUES node.
799 SDValue Ops[] = { Load, TF };
800 return DAG.getMergeValues(Ops, 2);
801 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000802 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000803 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000804 "Unaligned load of unsupported type.");
805
Dale Johannesen8155d642008-02-27 22:36:00 +0000806 // Compute the new VT that is half the size of the old one. This is an
807 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000808 unsigned NumBits = LoadedVT.getSizeInBits();
809 MVT NewLoadedVT;
810 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000811 NumBits >>= 1;
812
813 unsigned Alignment = LD->getAlignment();
814 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000815 ISD::LoadExtType HiExtType = LD->getExtensionType();
816
817 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
818 if (HiExtType == ISD::NON_EXTLOAD)
819 HiExtType = ISD::ZEXTLOAD;
820
821 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000822 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000823 if (TLI.isLittleEndian()) {
824 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
825 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
826 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
827 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
828 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
829 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000830 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000831 } else {
832 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
833 NewLoadedVT,LD->isVolatile(), Alignment);
834 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
835 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
836 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
837 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000838 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000839 }
840
841 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000842 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
843 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000844 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
845
Dan Gohman475871a2008-07-27 21:46:04 +0000846 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000847 Hi.getValue(1));
848
Dan Gohman475871a2008-07-27 21:46:04 +0000849 SDValue Ops[] = { Result, TF };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000850 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000851}
Evan Cheng912095b2007-01-04 21:56:39 +0000852
Dan Gohman82669522007-10-11 23:57:53 +0000853/// UnrollVectorOp - We know that the given vector has a legal type, however
854/// the operation it performs is not legal and is an operation that we have
855/// no way of lowering. "Unroll" the vector, splitting out the scalars and
856/// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000857SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000858 MVT VT = Op.getValueType();
Dan Gohman82669522007-10-11 23:57:53 +0000859 assert(isTypeLegal(VT) &&
860 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000861 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman82669522007-10-11 23:57:53 +0000862 "Can't unroll a vector with multiple results!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000863 unsigned NE = VT.getVectorNumElements();
864 MVT EltVT = VT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000865
Dan Gohman475871a2008-07-27 21:46:04 +0000866 SmallVector<SDValue, 8> Scalars;
867 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman82669522007-10-11 23:57:53 +0000868 for (unsigned i = 0; i != NE; ++i) {
869 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +0000870 SDValue Operand = Op.getOperand(j);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000871 MVT OperandVT = Operand.getValueType();
872 if (OperandVT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +0000873 // A vector operand; extract a single element.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000874 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000875 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
876 OperandEltVT,
877 Operand,
878 DAG.getConstant(i, MVT::i32));
879 } else {
880 // A scalar operand; just use it as is.
881 Operands[j] = Operand;
882 }
883 }
Mon P Wange9f10152008-12-09 05:46:39 +0000884
885 switch (Op.getOpcode()) {
886 default:
887 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
888 &Operands[0], Operands.size()));
889 break;
890 case ISD::SHL:
891 case ISD::SRA:
892 case ISD::SRL:
893 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT, Operands[0],
894 LegalizeShiftAmount(Operands[1])));
895 break;
896 }
Dan Gohman82669522007-10-11 23:57:53 +0000897 }
898
899 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
900}
901
Duncan Sands007f9842008-01-10 10:28:30 +0000902/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000903static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000904 RTLIB::Libcall Call_F32,
905 RTLIB::Libcall Call_F64,
906 RTLIB::Libcall Call_F80,
907 RTLIB::Libcall Call_PPCF128) {
908 return
909 VT == MVT::f32 ? Call_F32 :
910 VT == MVT::f64 ? Call_F64 :
911 VT == MVT::f80 ? Call_F80 :
912 VT == MVT::ppcf128 ? Call_PPCF128 :
913 RTLIB::UNKNOWN_LIBCALL;
914}
915
Nate Begeman68679912008-04-25 18:07:40 +0000916/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
917/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
918/// is necessary to spill the vector being inserted into to memory, perform
919/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000920SDValue SelectionDAGLegalize::
921PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
922 SDValue Tmp1 = Vec;
923 SDValue Tmp2 = Val;
924 SDValue Tmp3 = Idx;
Nate Begeman68679912008-04-25 18:07:40 +0000925
926 // If the target doesn't support this, we have to spill the input vector
927 // to a temporary stack slot, update the element, then reload it. This is
928 // badness. We could also load the value into a vector register (either
929 // with a "move to register" or "extload into register" instruction, then
930 // permute it into place, if the idx is a constant and if the idx is
931 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000932 MVT VT = Tmp1.getValueType();
933 MVT EltVT = VT.getVectorElementType();
934 MVT IdxVT = Tmp3.getValueType();
935 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000936 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000937
Gabor Greifba36cb52008-08-28 21:40:38 +0000938 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000939
940 // Store the vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000941 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000942 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000943
944 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000945 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman68679912008-04-25 18:07:40 +0000946 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
947 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000948 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman68679912008-04-25 18:07:40 +0000949 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman475871a2008-07-27 21:46:04 +0000950 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000951 // Store the scalar value.
952 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000953 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000954 // Load the updated vector.
Dan Gohmana54cf172008-07-11 22:44:52 +0000955 return DAG.getLoad(VT, Ch, StackPtr,
956 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000957}
958
Mon P Wange9f10152008-12-09 05:46:39 +0000959SDValue SelectionDAGLegalize::LegalizeShiftAmount(SDValue ShiftAmt) {
960 if (TLI.getShiftAmountTy().bitsLT(ShiftAmt.getValueType()))
961 return DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), ShiftAmt);
962
963 if (TLI.getShiftAmountTy().bitsGT(ShiftAmt.getValueType()))
964 return DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), ShiftAmt);
965
966 return ShiftAmt;
967}
968
969
Dan Gohmana3466152007-07-13 20:14:11 +0000970/// LegalizeOp - We know that the specified value has a legal type, and
971/// that its operands are legal. Now ensure that the operation itself
972/// is legal, recursively ensuring that the operands' operations remain
973/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000974SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000975 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
976 return Op;
977
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000978 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000979 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000980 SDNode *Node = Op.getNode();
Chris Lattnere3304a32005-01-08 20:35:13 +0000981
Chris Lattner3e928bb2005-01-07 07:47:09 +0000982 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000983 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000984 if (Node->getNumValues() > 1) {
985 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000986 if (getTypeAction(Node->getValueType(i)) != Legal) {
987 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000988 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000989 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000990 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000991 }
992 }
993
Chris Lattner45982da2005-05-12 16:53:42 +0000994 // Note that LegalizeOp may be reentered even from single-use nodes, which
995 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000996 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000997 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000998
Dan Gohman475871a2008-07-27 21:46:04 +0000999 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
1000 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +00001001 bool isCustom = false;
1002
Chris Lattner3e928bb2005-01-07 07:47:09 +00001003 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +00001004 case ISD::FrameIndex:
1005 case ISD::EntryToken:
1006 case ISD::Register:
1007 case ISD::BasicBlock:
1008 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +00001009 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +00001010 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +00001011 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +00001012 case ISD::TargetConstantPool:
1013 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001014 case ISD::TargetGlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001015 case ISD::TargetExternalSymbol:
Chris Lattner948c1b12006-01-28 08:31:04 +00001016 case ISD::VALUETYPE:
1017 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +00001018 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +00001019 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +00001020 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +00001021 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00001022 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +00001023 "This must be legal!");
1024 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001025 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001026 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1027 // If this is a target node, legalize it by legalizing the operands then
1028 // passing it through.
Dan Gohman475871a2008-07-27 21:46:04 +00001029 SmallVector<SDValue, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001030 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001031 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001032
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001033 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001034
1035 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1036 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001037 return Result.getValue(Op.getResNo());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001038 }
1039 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001040#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00001041 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001042#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00001043 assert(0 && "Do not know how to legalize this operator!");
1044 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +00001045 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001046 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001047 case ISD::GlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001048 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +00001049 case ISD::ConstantPool:
1050 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001051 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1052 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +00001053 case TargetLowering::Custom:
1054 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001055 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner948c1b12006-01-28 08:31:04 +00001056 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001057 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001058 break;
1059 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001060 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +00001061 case ISD::FRAMEADDR:
1062 case ISD::RETURNADDR:
1063 // The only option for these nodes is to custom lower them. If the target
1064 // does not custom lower them, then return zero.
1065 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001066 if (Tmp1.getNode())
Nate Begemanbcc5f362007-01-29 22:58:52 +00001067 Result = Tmp1;
1068 else
1069 Result = DAG.getConstant(0, TLI.getPointerTy());
1070 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001071 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001072 MVT VT = Node->getValueType(0);
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001073 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1074 default: assert(0 && "This action is not supported yet!");
1075 case TargetLowering::Custom:
1076 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001077 if (Result.getNode()) break;
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001078 // Fall Thru
1079 case TargetLowering::Legal:
1080 Result = DAG.getConstant(0, VT);
1081 break;
1082 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001083 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001084 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001085 case ISD::EXCEPTIONADDR: {
1086 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001087 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001088 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1089 default: assert(0 && "This action is not supported yet!");
1090 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +00001091 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +00001092 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001093 }
1094 break;
1095 case TargetLowering::Custom:
1096 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001097 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001098 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001099 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001100 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands4bdcb612008-07-02 17:40:58 +00001101 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001102 break;
1103 }
1104 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001105 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001106 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001107
Gabor Greifba36cb52008-08-28 21:40:38 +00001108 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001109 "Cannot return more than two values!");
1110
1111 // Since we produced two values, make sure to remember that we
1112 // legalized both of them.
1113 Tmp1 = LegalizeOp(Result);
1114 Tmp2 = LegalizeOp(Result.getValue(1));
1115 AddLegalizedOperand(Op.getValue(0), Tmp1);
1116 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001117 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +00001118 case ISD::EHSELECTION: {
1119 Tmp1 = LegalizeOp(Node->getOperand(0));
1120 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001121 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +00001122 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1123 default: assert(0 && "This action is not supported yet!");
1124 case TargetLowering::Expand: {
1125 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +00001126 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +00001127 }
1128 break;
1129 case TargetLowering::Custom:
1130 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001131 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +00001132 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001133 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001134 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands4bdcb612008-07-02 17:40:58 +00001135 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +00001136 break;
1137 }
1138 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001139 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001140 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001141
Gabor Greifba36cb52008-08-28 21:40:38 +00001142 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001143 "Cannot return more than two values!");
1144
1145 // Since we produced two values, make sure to remember that we
1146 // legalized both of them.
1147 Tmp1 = LegalizeOp(Result);
1148 Tmp2 = LegalizeOp(Result.getValue(1));
1149 AddLegalizedOperand(Op.getValue(0), Tmp1);
1150 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001151 return Op.getResNo() ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001152 case ISD::EH_RETURN: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001153 MVT VT = Node->getValueType(0);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001154 // The only "good" option for this node is to custom lower it.
1155 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1156 default: assert(0 && "This action is not supported at all!");
1157 case TargetLowering::Custom:
1158 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001159 if (Result.getNode()) break;
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001160 // Fall Thru
1161 case TargetLowering::Legal:
1162 // Target does not know, how to lower this, lower to noop
1163 Result = LegalizeOp(Node->getOperand(0));
1164 break;
1165 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001166 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001167 break;
Chris Lattner08951a32005-09-02 01:15:01 +00001168 case ISD::AssertSext:
1169 case ISD::AssertZext:
1170 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001171 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +00001172 break;
Chris Lattner308575b2005-11-20 22:56:56 +00001173 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001174 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif99a6cb92008-08-26 22:36:50 +00001175 Result = Node->getOperand(Op.getResNo());
Chris Lattner456a93a2006-01-28 07:39:30 +00001176 break;
Chris Lattner69a52152005-01-14 22:38:01 +00001177 case ISD::CopyFromReg:
1178 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001179 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001180 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001181 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +00001182 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001183 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001184 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001185 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001186 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1187 } else {
1188 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1189 }
Chris Lattner7310fb12005-12-18 15:27:43 +00001190 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1191 }
Chris Lattner13c184d2005-01-28 06:27:38 +00001192 // Since CopyFromReg produces two values, make sure to remember that we
1193 // legalized both of them.
1194 AddLegalizedOperand(Op.getValue(0), Result);
1195 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001196 return Result.getValue(Op.getResNo());
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001197 case ISD::UNDEF: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001198 MVT VT = Op.getValueType();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001199 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +00001200 default: assert(0 && "This action is not supported yet!");
1201 case TargetLowering::Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001202 if (VT.isInteger())
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001203 Result = DAG.getConstant(0, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001204 else if (VT.isFloatingPoint())
1205 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf41db212007-09-26 17:26:49 +00001206 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001207 else
1208 assert(0 && "Unknown value type!");
1209 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001210 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001211 break;
1212 }
1213 break;
1214 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001215
Chris Lattner48b61a72006-03-28 00:40:33 +00001216 case ISD::INTRINSIC_W_CHAIN:
1217 case ISD::INTRINSIC_WO_CHAIN:
1218 case ISD::INTRINSIC_VOID: {
Dan Gohman475871a2008-07-27 21:46:04 +00001219 SmallVector<SDValue, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001220 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1221 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001222 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001223
1224 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001225 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001226 TargetLowering::Custom) {
1227 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001228 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001229 }
1230
Gabor Greifba36cb52008-08-28 21:40:38 +00001231 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001232
1233 // Must have return value and chain result.
Gabor Greifba36cb52008-08-28 21:40:38 +00001234 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattner13fc2f12006-03-27 20:28:29 +00001235 "Cannot return more than two values!");
1236
1237 // Since loads produce two values, make sure to remember that we
1238 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001239 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1240 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001241 return Result.getValue(Op.getResNo());
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001242 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001243
Dan Gohman7f460202008-06-30 20:59:49 +00001244 case ISD::DBG_STOPPOINT:
1245 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +00001246 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1247
Dan Gohman7f460202008-06-30 20:59:49 +00001248 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +00001249 case TargetLowering::Promote:
1250 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001251 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001252 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001253 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohman44066042008-07-01 00:05:16 +00001254 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001255
Dan Gohman7f460202008-06-30 20:59:49 +00001256 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001257 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman7f460202008-06-30 20:59:49 +00001258 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1259 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001260
Dan Gohman7f460202008-06-30 20:59:49 +00001261 unsigned Line = DSP->getLine();
1262 unsigned Col = DSP->getColumn();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001263
1264 if (useDEBUG_LOC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001265 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Cheng71e86852008-07-08 20:06:39 +00001266 DAG.getConstant(Col, MVT::i32),
1267 DAG.getConstant(SrcFile, MVT::i32) };
1268 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001269 } else {
Evan Chenga647c922008-02-01 02:05:57 +00001270 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohman44066042008-07-01 00:05:16 +00001271 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001272 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001273 } else {
1274 Result = Tmp1; // chain
1275 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001276 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001277 }
Evan Cheng71e86852008-07-08 20:06:39 +00001278 case TargetLowering::Legal: {
1279 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1280 if (Action == Legal && Tmp1 == Node->getOperand(0))
1281 break;
1282
Dan Gohman475871a2008-07-27 21:46:04 +00001283 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001284 Ops.push_back(Tmp1);
1285 if (Action == Legal) {
1286 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1287 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1288 } else {
1289 // Otherwise promote them.
1290 Ops.push_back(PromoteOp(Node->getOperand(1)));
1291 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner36ce6912005-11-29 06:21:05 +00001292 }
Evan Cheng71e86852008-07-08 20:06:39 +00001293 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1294 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1295 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001296 break;
1297 }
Evan Cheng71e86852008-07-08 20:06:39 +00001298 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001299 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001300
1301 case ISD::DECLARE:
1302 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1303 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1304 default: assert(0 && "This action is not supported yet!");
1305 case TargetLowering::Legal:
1306 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1307 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1308 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1309 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1310 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001311 case TargetLowering::Expand:
1312 Result = LegalizeOp(Node->getOperand(0));
1313 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001314 }
1315 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001316
1317 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001318 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001319 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001320 default: assert(0 && "This action is not supported yet!");
Evan Cheng71e86852008-07-08 20:06:39 +00001321 case TargetLowering::Legal: {
1322 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001323 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Cheng71e86852008-07-08 20:06:39 +00001324 if (Action == Legal && Tmp1 == Node->getOperand(0))
1325 break;
1326 if (Action == Legal) {
1327 Tmp2 = Node->getOperand(1);
1328 Tmp3 = Node->getOperand(2);
1329 Tmp4 = Node->getOperand(3);
1330 } else {
1331 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1332 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1333 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1334 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001335 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001336 break;
1337 }
Evan Cheng71e86852008-07-08 20:06:39 +00001338 }
Jim Laskeyabf6d172006-01-05 01:25:28 +00001339 break;
1340
Dan Gohman44066042008-07-01 00:05:16 +00001341 case ISD::DBG_LABEL:
1342 case ISD::EH_LABEL:
1343 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1344 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001345 default: assert(0 && "This action is not supported yet!");
1346 case TargetLowering::Legal:
1347 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohman44066042008-07-01 00:05:16 +00001348 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001349 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001350 case TargetLowering::Expand:
1351 Result = LegalizeOp(Node->getOperand(0));
1352 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001353 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001354 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001355
Evan Cheng27b7db52008-03-08 00:58:38 +00001356 case ISD::PREFETCH:
1357 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1358 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1359 default: assert(0 && "This action is not supported yet!");
1360 case TargetLowering::Legal:
1361 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1362 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1363 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1364 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1365 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1366 break;
1367 case TargetLowering::Expand:
1368 // It's a noop.
1369 Result = LegalizeOp(Node->getOperand(0));
1370 break;
1371 }
1372 break;
1373
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001374 case ISD::MEMBARRIER: {
1375 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001376 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1377 default: assert(0 && "This action is not supported yet!");
1378 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001379 SDValue Ops[6];
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001380 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001381 for (int x = 1; x < 6; ++x) {
1382 Ops[x] = Node->getOperand(x);
1383 if (!isTypeLegal(Ops[x].getValueType()))
1384 Ops[x] = PromoteOp(Ops[x]);
1385 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001386 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1387 break;
1388 }
1389 case TargetLowering::Expand:
1390 //There is no libgcc call for this op
1391 Result = Node->getOperand(0); // Noop
1392 break;
1393 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001394 break;
1395 }
1396
Dale Johannesene00a8a22008-08-28 02:44:49 +00001397 case ISD::ATOMIC_CMP_SWAP_8:
1398 case ISD::ATOMIC_CMP_SWAP_16:
1399 case ISD::ATOMIC_CMP_SWAP_32:
1400 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang63307c32008-05-05 19:05:59 +00001401 unsigned int num_operands = 4;
1402 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001403 SDValue Ops[4];
Mon P Wang63307c32008-05-05 19:05:59 +00001404 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001405 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang63307c32008-05-05 19:05:59 +00001406 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1407
1408 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1409 default: assert(0 && "This action is not supported yet!");
1410 case TargetLowering::Custom:
1411 Result = TLI.LowerOperation(Result, DAG);
1412 break;
1413 case TargetLowering::Legal:
1414 break;
1415 }
Dan Gohman475871a2008-07-27 21:46:04 +00001416 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1417 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001418 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001419 }
Dale Johannesene00a8a22008-08-28 02:44:49 +00001420 case ISD::ATOMIC_LOAD_ADD_8:
1421 case ISD::ATOMIC_LOAD_SUB_8:
1422 case ISD::ATOMIC_LOAD_AND_8:
1423 case ISD::ATOMIC_LOAD_OR_8:
1424 case ISD::ATOMIC_LOAD_XOR_8:
1425 case ISD::ATOMIC_LOAD_NAND_8:
1426 case ISD::ATOMIC_LOAD_MIN_8:
1427 case ISD::ATOMIC_LOAD_MAX_8:
1428 case ISD::ATOMIC_LOAD_UMIN_8:
1429 case ISD::ATOMIC_LOAD_UMAX_8:
1430 case ISD::ATOMIC_SWAP_8:
1431 case ISD::ATOMIC_LOAD_ADD_16:
1432 case ISD::ATOMIC_LOAD_SUB_16:
1433 case ISD::ATOMIC_LOAD_AND_16:
1434 case ISD::ATOMIC_LOAD_OR_16:
1435 case ISD::ATOMIC_LOAD_XOR_16:
1436 case ISD::ATOMIC_LOAD_NAND_16:
1437 case ISD::ATOMIC_LOAD_MIN_16:
1438 case ISD::ATOMIC_LOAD_MAX_16:
1439 case ISD::ATOMIC_LOAD_UMIN_16:
1440 case ISD::ATOMIC_LOAD_UMAX_16:
1441 case ISD::ATOMIC_SWAP_16:
1442 case ISD::ATOMIC_LOAD_ADD_32:
1443 case ISD::ATOMIC_LOAD_SUB_32:
1444 case ISD::ATOMIC_LOAD_AND_32:
1445 case ISD::ATOMIC_LOAD_OR_32:
1446 case ISD::ATOMIC_LOAD_XOR_32:
1447 case ISD::ATOMIC_LOAD_NAND_32:
1448 case ISD::ATOMIC_LOAD_MIN_32:
1449 case ISD::ATOMIC_LOAD_MAX_32:
1450 case ISD::ATOMIC_LOAD_UMIN_32:
1451 case ISD::ATOMIC_LOAD_UMAX_32:
1452 case ISD::ATOMIC_SWAP_32:
1453 case ISD::ATOMIC_LOAD_ADD_64:
1454 case ISD::ATOMIC_LOAD_SUB_64:
1455 case ISD::ATOMIC_LOAD_AND_64:
1456 case ISD::ATOMIC_LOAD_OR_64:
1457 case ISD::ATOMIC_LOAD_XOR_64:
1458 case ISD::ATOMIC_LOAD_NAND_64:
1459 case ISD::ATOMIC_LOAD_MIN_64:
1460 case ISD::ATOMIC_LOAD_MAX_64:
1461 case ISD::ATOMIC_LOAD_UMIN_64:
1462 case ISD::ATOMIC_LOAD_UMAX_64:
1463 case ISD::ATOMIC_SWAP_64: {
Mon P Wang63307c32008-05-05 19:05:59 +00001464 unsigned int num_operands = 3;
1465 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001466 SDValue Ops[3];
Mon P Wang63307c32008-05-05 19:05:59 +00001467 for (unsigned int x = 0; x < num_operands; ++x)
1468 Ops[x] = LegalizeOp(Node->getOperand(x));
1469 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands126d9072008-07-04 11:47:58 +00001470
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001471 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001472 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001473 case TargetLowering::Custom:
1474 Result = TLI.LowerOperation(Result, DAG);
1475 break;
1476 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001477 break;
1478 }
Dan Gohman475871a2008-07-27 21:46:04 +00001479 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1480 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001481 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001482 }
Scott Michelc1513d22007-08-08 23:23:31 +00001483 case ISD::Constant: {
1484 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1485 unsigned opAction =
1486 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1487
Chris Lattner3e928bb2005-01-07 07:47:09 +00001488 // We know we don't need to expand constants here, constants only have one
1489 // value and we check that it is fine above.
1490
Scott Michelc1513d22007-08-08 23:23:31 +00001491 if (opAction == TargetLowering::Custom) {
1492 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001493 if (Tmp1.getNode())
Scott Michelc1513d22007-08-08 23:23:31 +00001494 Result = Tmp1;
1495 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001496 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001497 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001498 case ISD::ConstantFP: {
1499 // Spill FP immediates to the constant pool if the target cannot directly
1500 // codegen them. Targets often have some immediate values that can be
1501 // efficiently generated into an FP register without a load. We explicitly
1502 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001503 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1504
Chris Lattner3181a772006-01-29 06:26:56 +00001505 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1506 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001507 case TargetLowering::Legal:
1508 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001509 case TargetLowering::Custom:
1510 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001511 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001512 Result = Tmp3;
1513 break;
1514 }
1515 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001516 case TargetLowering::Expand: {
1517 // Check to see if this FP immediate is already legal.
1518 bool isLegal = false;
1519 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1520 E = TLI.legal_fpimm_end(); I != E; ++I) {
1521 if (CFP->isExactlyValue(*I)) {
1522 isLegal = true;
1523 break;
1524 }
1525 }
1526 // If this is a legal constant, turn it into a TargetConstantFP node.
1527 if (isLegal)
1528 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001529 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001530 }
Nate Begemane1795842008-02-14 08:57:00 +00001531 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001532 break;
1533 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001534 case ISD::TokenFactor:
1535 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001536 Tmp1 = LegalizeOp(Node->getOperand(0));
1537 Tmp2 = LegalizeOp(Node->getOperand(1));
1538 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1539 } else if (Node->getNumOperands() == 3) {
1540 Tmp1 = LegalizeOp(Node->getOperand(0));
1541 Tmp2 = LegalizeOp(Node->getOperand(1));
1542 Tmp3 = LegalizeOp(Node->getOperand(2));
1543 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001544 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001545 SmallVector<SDValue, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001546 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001547 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1548 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001549 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001550 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001551 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001552
1553 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001554 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001555 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001556 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001557 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001558 // A call within a calling sequence must be legalized to something
1559 // other than the normal CALLSEQ_END. Violating this gets Legalize
1560 // into an infinite loop.
1561 assert ((!IsLegalizingCall ||
1562 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001563 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001564 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001565
1566 // The number of incoming and outgoing values should match; unless the final
1567 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001568 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1569 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1570 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001571 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001572 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001573
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001574 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001575 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001576 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1577 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001578 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001579 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001580 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001581 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001582 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001583 }
1584 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001585 case ISD::EXTRACT_SUBREG: {
1586 Tmp1 = LegalizeOp(Node->getOperand(0));
1587 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1588 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001589 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001590 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1591 }
1592 break;
1593 case ISD::INSERT_SUBREG: {
1594 Tmp1 = LegalizeOp(Node->getOperand(0));
1595 Tmp2 = LegalizeOp(Node->getOperand(1));
1596 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1597 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001598 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001599 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1600 }
1601 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001602 case ISD::BUILD_VECTOR:
1603 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001604 default: assert(0 && "This action is not supported yet!");
1605 case TargetLowering::Custom:
1606 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001607 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001608 Result = Tmp3;
1609 break;
1610 }
1611 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001612 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001613 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001614 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001615 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001616 break;
1617 case ISD::INSERT_VECTOR_ELT:
1618 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001619 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001620
1621 // The type of the value to insert may not be legal, even though the vector
1622 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1623 // here.
1624 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1625 default: assert(0 && "Cannot expand insert element operand");
1626 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1627 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang0c397192008-10-30 08:01:45 +00001628 case Expand:
1629 // FIXME: An alternative would be to check to see if the target is not
1630 // going to custom lower this operation, we could bitcast to half elt
1631 // width and perform two inserts at that width, if that is legal.
1632 Tmp2 = Node->getOperand(1);
1633 break;
Nate Begeman0325d902008-02-13 06:43:04 +00001634 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001635 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1636
1637 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1638 Node->getValueType(0))) {
1639 default: assert(0 && "This action is not supported yet!");
1640 case TargetLowering::Legal:
1641 break;
1642 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001643 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001644 if (Tmp4.getNode()) {
Nate Begeman2281a992008-01-05 20:47:37 +00001645 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001646 break;
1647 }
1648 // FALLTHROUGH
Mon P Wang0c397192008-10-30 08:01:45 +00001649 case TargetLowering::Promote:
1650 // Fall thru for vector case
Chris Lattner2332b9f2006-03-19 01:17:20 +00001651 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001652 // If the insert index is a constant, codegen this as a scalar_to_vector,
1653 // then a shuffle that inserts it into the right position in the vector.
1654 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001655 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1656 // match the element type of the vector being created.
1657 if (Tmp2.getValueType() ==
Duncan Sands83ec4b62008-06-06 12:08:01 +00001658 Op.getValueType().getVectorElementType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001659 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman0325d902008-02-13 06:43:04 +00001660 Tmp1.getValueType(), Tmp2);
1661
Duncan Sands83ec4b62008-06-06 12:08:01 +00001662 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1663 MVT ShufMaskVT =
1664 MVT::getIntVectorWithNumElements(NumElts);
1665 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman0325d902008-02-13 06:43:04 +00001666
1667 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1668 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1669 // elt 0 of the RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00001670 SmallVector<SDValue, 8> ShufOps;
Nate Begeman0325d902008-02-13 06:43:04 +00001671 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001672 if (i != InsertPos->getZExtValue())
Nate Begeman0325d902008-02-13 06:43:04 +00001673 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1674 else
1675 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1676 }
Dan Gohman475871a2008-07-27 21:46:04 +00001677 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman0325d902008-02-13 06:43:04 +00001678 &ShufOps[0], ShufOps.size());
1679
1680 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1681 Tmp1, ScVec, ShufMask);
1682 Result = LegalizeOp(Result);
1683 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001684 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001685 }
Nate Begeman68679912008-04-25 18:07:40 +00001686 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001687 break;
1688 }
1689 }
1690 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001691 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001692 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1693 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1694 break;
1695 }
1696
Chris Lattnerce872152006-03-19 06:31:19 +00001697 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1698 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1699 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1700 Node->getValueType(0))) {
1701 default: assert(0 && "This action is not supported yet!");
1702 case TargetLowering::Legal:
1703 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001704 case TargetLowering::Custom:
1705 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001706 if (Tmp3.getNode()) {
Chris Lattner4d3abee2006-03-19 06:47:21 +00001707 Result = Tmp3;
1708 break;
1709 }
1710 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001711 case TargetLowering::Expand:
1712 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001713 break;
1714 }
Chris Lattnerce872152006-03-19 06:31:19 +00001715 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001716 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001717 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1718 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1719 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1720
1721 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001722 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1723 default: assert(0 && "Unknown operation action!");
1724 case TargetLowering::Legal:
1725 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1726 "vector shuffle should not be created if not legal!");
1727 break;
1728 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001729 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001730 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001731 Result = Tmp3;
1732 break;
1733 }
1734 // FALLTHROUGH
1735 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001736 MVT VT = Node->getValueType(0);
1737 MVT EltVT = VT.getVectorElementType();
1738 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001739 SDValue Mask = Node->getOperand(2);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001740 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001741 SmallVector<SDValue,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001742 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001743 SDValue Arg = Mask.getOperand(i);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001744 if (Arg.getOpcode() == ISD::UNDEF) {
1745 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1746 } else {
1747 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001748 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng18dd6d02006-04-05 06:07:11 +00001749 if (Idx < NumElems)
1750 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1751 DAG.getConstant(Idx, PtrVT)));
1752 else
1753 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1754 DAG.getConstant(Idx - NumElems, PtrVT)));
1755 }
1756 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001757 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001758 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001759 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001760 case TargetLowering::Promote: {
1761 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001762 MVT OVT = Node->getValueType(0);
1763 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001764
1765 // Cast the two input vectors.
1766 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1767 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1768
1769 // Convert the shuffle mask to the right # elements.
Dan Gohman475871a2008-07-27 21:46:04 +00001770 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001771 assert(Tmp3.getNode() && "Shuffle not legal?");
Chris Lattner4352cc92006-04-04 17:23:26 +00001772 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1773 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1774 break;
1775 }
Chris Lattner87100e02006-03-20 01:52:29 +00001776 }
1777 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001778
1779 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001780 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001781 Tmp2 = LegalizeOp(Node->getOperand(1));
1782 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001783 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001784 break;
1785
Dan Gohman7f321562007-06-25 16:23:39 +00001786 case ISD::EXTRACT_SUBVECTOR:
1787 Tmp1 = Node->getOperand(0);
1788 Tmp2 = LegalizeOp(Node->getOperand(1));
1789 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1790 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001791 break;
1792
Mon P Wang0c397192008-10-30 08:01:45 +00001793 case ISD::CONCAT_VECTORS: {
1794 // Use extract/insert/build vector for now. We might try to be
1795 // more clever later.
1796 MVT PtrVT = TLI.getPointerTy();
1797 SmallVector<SDValue, 8> Ops;
1798 unsigned NumOperands = Node->getNumOperands();
1799 for (unsigned i=0; i < NumOperands; ++i) {
1800 SDValue SubOp = Node->getOperand(i);
1801 MVT VVT = SubOp.getNode()->getValueType(0);
1802 MVT EltVT = VVT.getVectorElementType();
1803 unsigned NumSubElem = VVT.getVectorNumElements();
1804 for (unsigned j=0; j < NumSubElem; ++j) {
1805 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1806 DAG.getConstant(j, PtrVT)));
1807 }
1808 }
1809 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1810 &Ops[0], Ops.size()));
1811 }
1812
Chris Lattner6831a812006-02-13 09:18:02 +00001813 case ISD::CALLSEQ_START: {
1814 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1815
1816 // Recursively Legalize all of the inputs of the call end that do not lead
1817 // to this call start. This ensures that any libcalls that need be inserted
1818 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001819 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001820 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001821 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001822 NodesLeadingTo);
1823 }
Chris Lattner6831a812006-02-13 09:18:02 +00001824
1825 // Now that we legalized all of the inputs (which may have inserted
1826 // libcalls) create the new CALLSEQ_START node.
1827 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1828
1829 // Merge in the last call, to ensure that this call start after the last
1830 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001831 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001832 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1833 Tmp1 = LegalizeOp(Tmp1);
1834 }
Chris Lattner6831a812006-02-13 09:18:02 +00001835
1836 // Do not try to legalize the target-specific arguments (#1+).
1837 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001838 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001839 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001840 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001841 }
1842
1843 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001844 AddLegalizedOperand(Op.getValue(0), Result);
1845 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1846 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1847
Chris Lattner6831a812006-02-13 09:18:02 +00001848 // Now that the callseq_start and all of the non-call nodes above this call
1849 // sequence have been legalized, legalize the call itself. During this
1850 // process, no libcalls can/will be inserted, guaranteeing that no calls
1851 // can overlap.
1852 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001853 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001854 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001855 IsLegalizingCall = true;
1856
1857 // Legalize the call, starting from the CALLSEQ_END.
1858 LegalizeOp(LastCALLSEQ_END);
1859 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1860 return Result;
1861 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001862 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001863 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1864 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001865 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001866 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1867 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001868 assert(I != LegalizedNodes.end() &&
1869 "Legalizing the call start should have legalized this node!");
1870 return I->second;
1871 }
1872
1873 // Otherwise, the call start has been legalized and everything is going
1874 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001875 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001876 // Do not try to legalize the target-specific arguments (#1+), except for
1877 // an optional flag input.
1878 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1879 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001880 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001881 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001882 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001883 }
1884 } else {
1885 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1886 if (Tmp1 != Node->getOperand(0) ||
1887 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001888 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001889 Ops[0] = Tmp1;
1890 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001891 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001892 }
Chris Lattner6a542892006-01-24 05:48:21 +00001893 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001894 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001895 // This finishes up call legalization.
1896 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001897
1898 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001899 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001900 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001901 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001902 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001903 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001904 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001905 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1906 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1907 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001908 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001909
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001910 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001911 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001912 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001913 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001914 case TargetLowering::Expand: {
1915 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1916 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1917 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001918 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001919
1920 // Chain the dynamic stack allocation so that it doesn't modify the stack
1921 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001922 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001923
Dan Gohman475871a2008-07-27 21:46:04 +00001924 SDValue Size = Tmp2.getOperand(1);
1925 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001926 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001927 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001928 unsigned StackAlign =
1929 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1930 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001931 SP = DAG.getNode(ISD::AND, VT, SP,
1932 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001933 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001934 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1935
Chris Lattnere563bbc2008-10-11 22:08:30 +00001936 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1937 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001938
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001939 Tmp1 = LegalizeOp(Tmp1);
1940 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001941 break;
1942 }
1943 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001944 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001945 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001946 Tmp1 = LegalizeOp(Tmp3);
1947 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001948 }
Chris Lattner903d2782006-01-15 08:54:32 +00001949 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001950 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001951 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001952 }
Chris Lattner903d2782006-01-15 08:54:32 +00001953 // Since this op produce two values, make sure to remember that we
1954 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001955 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1956 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001957 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001958 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001959 case ISD::INLINEASM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001960 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001961 bool Changed = false;
1962 // Legalize all of the operands of the inline asm, in case they are nodes
1963 // that need to be expanded or something. Note we skip the asm string and
1964 // all of the TargetConstant flags.
Dan Gohman475871a2008-07-27 21:46:04 +00001965 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001966 Changed = Op != Ops[0];
1967 Ops[0] = Op;
1968
1969 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1970 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001971 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Chris Lattner25a022c2006-07-11 01:40:09 +00001972 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman475871a2008-07-27 21:46:04 +00001973 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001974 if (Op != Ops[i]) {
1975 Changed = true;
1976 Ops[i] = Op;
1977 }
1978 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001979 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001980
1981 if (HasInFlag) {
1982 Op = LegalizeOp(Ops.back());
1983 Changed |= Op != Ops.back();
1984 Ops.back() = Op;
1985 }
1986
1987 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001988 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001989
1990 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman475871a2008-07-27 21:46:04 +00001991 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1992 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001993 return Result.getValue(Op.getResNo());
Chris Lattner25a022c2006-07-11 01:40:09 +00001994 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001995 case ISD::BR:
1996 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001997 // Ensure that libcalls are emitted before a branch.
1998 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1999 Tmp1 = LegalizeOp(Tmp1);
2000 LastCALLSEQ_END = DAG.getEntryNode();
2001
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002002 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00002003 break;
Nate Begeman37efe672006-04-22 18:53:45 +00002004 case ISD::BRIND:
2005 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2006 // Ensure that libcalls are emitted before a branch.
2007 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2008 Tmp1 = LegalizeOp(Tmp1);
2009 LastCALLSEQ_END = DAG.getEntryNode();
2010
2011 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2012 default: assert(0 && "Indirect target must be legal type (pointer)!");
2013 case Legal:
2014 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2015 break;
2016 }
2017 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2018 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00002019 case ISD::BR_JT:
2020 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2021 // Ensure that libcalls are emitted before a branch.
2022 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2023 Tmp1 = LegalizeOp(Tmp1);
2024 LastCALLSEQ_END = DAG.getEntryNode();
2025
2026 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
2027 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2028
2029 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
2030 default: assert(0 && "This action is not supported yet!");
2031 case TargetLowering::Legal: break;
2032 case TargetLowering::Custom:
2033 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002034 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00002035 break;
2036 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SDValue Chain = Result.getOperand(0);
2038 SDValue Table = Result.getOperand(1);
2039 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002040
Duncan Sands83ec4b62008-06-06 12:08:01 +00002041 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002042 MachineFunction &MF = DAG.getMachineFunction();
2043 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00002044 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman475871a2008-07-27 21:46:04 +00002045 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002046
Duncan Sands712f7b32008-12-12 08:13:38 +00002047 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
2048 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, Chain, Addr,
2049 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00002050 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002051 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00002052 // For PIC, the sequence is:
2053 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00002054 // RelocBase can be JumpTable, GOT or some sort of global base.
Evan Chengcc415862007-11-09 01:32:10 +00002055 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
2056 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00002057 }
Evan Chengcc415862007-11-09 01:32:10 +00002058 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002059 }
2060 }
2061 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002062 case ISD::BRCOND:
2063 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002064 // Ensure that libcalls are emitted before a return.
2065 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2066 Tmp1 = LegalizeOp(Tmp1);
2067 LastCALLSEQ_END = DAG.getEntryNode();
2068
Chris Lattner47e92232005-01-18 19:27:06 +00002069 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2070 case Expand: assert(0 && "It's impossible to expand bools");
2071 case Legal:
2072 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2073 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002074 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00002075 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00002076
2077 // The top bits of the promoted condition are not necessarily zero, ensure
2078 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002079 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002080 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002081 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerf9908172006-11-27 04:39:56 +00002082 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002083 break;
2084 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002085 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002086
2087 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002088 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00002089
2090 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
2091 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002092 case TargetLowering::Legal: break;
2093 case TargetLowering::Custom:
2094 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002095 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002096 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002097 case TargetLowering::Expand:
2098 // Expand brcond's setcc into its constituent parts and create a BR_CC
2099 // Node.
2100 if (Tmp2.getOpcode() == ISD::SETCC) {
2101 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
2102 Tmp2.getOperand(0), Tmp2.getOperand(1),
2103 Node->getOperand(2));
2104 } else {
2105 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
2106 DAG.getCondCode(ISD::SETNE), Tmp2,
2107 DAG.getConstant(0, Tmp2.getValueType()),
2108 Node->getOperand(2));
2109 }
2110 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002111 }
2112 break;
2113 case ISD::BR_CC:
2114 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002115 // Ensure that libcalls are emitted before a branch.
2116 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2117 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002118 Tmp2 = Node->getOperand(2); // LHS
2119 Tmp3 = Node->getOperand(3); // RHS
2120 Tmp4 = Node->getOperand(1); // CC
2121
Dale Johannesen53e4e442008-11-07 22:54:33 +00002122 LegalizeSetCC(TLI.getSetCCResultType(Tmp2), Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00002123 LastCALLSEQ_END = DAG.getEntryNode();
2124
Evan Cheng7f042682008-10-15 02:05:31 +00002125 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002126 // the LHS is a legal SETCC itself. In this case, we need to compare
2127 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002128 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002129 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2130 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00002131 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00002132
2133 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2134 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002135
Chris Lattner181b7a32005-12-17 23:46:46 +00002136 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2137 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002138 case TargetLowering::Legal: break;
2139 case TargetLowering::Custom:
2140 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002141 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00002142 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002143 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002144 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002145 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00002146 LoadSDNode *LD = cast<LoadSDNode>(Node);
2147 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2148 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002149
Evan Cheng466685d2006-10-09 20:57:25 +00002150 ISD::LoadExtType ExtType = LD->getExtensionType();
2151 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002152 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00002153 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2154 Tmp3 = Result.getValue(0);
2155 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002156
Evan Cheng466685d2006-10-09 20:57:25 +00002157 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2158 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002159 case TargetLowering::Legal:
2160 // If this is an unaligned load and the target doesn't support it,
2161 // expand it.
2162 if (!TLI.allowsUnalignedMemoryAccesses()) {
2163 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002164 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002165 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002166 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002167 TLI);
2168 Tmp3 = Result.getOperand(0);
2169 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00002170 Tmp3 = LegalizeOp(Tmp3);
2171 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002172 }
2173 }
2174 break;
Evan Cheng466685d2006-10-09 20:57:25 +00002175 case TargetLowering::Custom:
2176 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002177 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002178 Tmp3 = LegalizeOp(Tmp1);
2179 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00002180 }
Evan Cheng466685d2006-10-09 20:57:25 +00002181 break;
2182 case TargetLowering::Promote: {
2183 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002184 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00002185 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002186 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002187
2188 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002189 LD->getSrcValueOffset(),
2190 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00002191 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2192 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002193 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00002194 }
Evan Cheng466685d2006-10-09 20:57:25 +00002195 }
2196 // Since loads produce two values, make sure to remember that we
2197 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002198 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2199 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002200 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00002201 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002202 MVT SrcVT = LD->getMemoryVT();
2203 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002204 int SVOffset = LD->getSrcValueOffset();
2205 unsigned Alignment = LD->getAlignment();
2206 bool isVolatile = LD->isVolatile();
2207
Duncan Sands83ec4b62008-06-06 12:08:01 +00002208 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002209 // Some targets pretend to have an i1 loading operation, and actually
2210 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2211 // bits are guaranteed to be zero; it helps the optimizers understand
2212 // that these bits are zero. It is also useful for EXTLOAD, since it
2213 // tells the optimizers that those bits are undefined. It would be
2214 // nice to have an effective generic way of getting these benefits...
2215 // Until such a way is found, don't insist on promoting i1 here.
2216 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00002217 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002218 // Promote to a byte-sized load if not loading an integral number of
2219 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002220 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2221 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002222 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002223
2224 // The extra bits are guaranteed to be zero, since we stored them that
2225 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2226
2227 ISD::LoadExtType NewExtType =
2228 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2229
2230 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2231 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2232 NVT, isVolatile, Alignment);
2233
2234 Ch = Result.getValue(1); // The chain.
2235
2236 if (ExtType == ISD::SEXTLOAD)
2237 // Having the top bits zero doesn't help when sign extending.
2238 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2239 Result, DAG.getValueType(SrcVT));
2240 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2241 // All the top bits are guaranteed to be zero - inform the optimizers.
2242 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2243 DAG.getValueType(SrcVT));
2244
2245 Tmp1 = LegalizeOp(Result);
2246 Tmp2 = LegalizeOp(Ch);
2247 } else if (SrcWidth & (SrcWidth - 1)) {
2248 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002249 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002250 "Unsupported extload!");
2251 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2252 assert(RoundWidth < SrcWidth);
2253 unsigned ExtraWidth = SrcWidth - RoundWidth;
2254 assert(ExtraWidth < RoundWidth);
2255 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2256 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002257 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2258 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002259 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002260 unsigned IncrementSize;
2261
2262 if (TLI.isLittleEndian()) {
2263 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2264 // Load the bottom RoundWidth bits.
2265 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2266 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2267 Alignment);
2268
2269 // Load the remaining ExtraWidth bits.
2270 IncrementSize = RoundWidth / 8;
2271 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2272 DAG.getIntPtrConstant(IncrementSize));
2273 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2274 LD->getSrcValue(), SVOffset + IncrementSize,
2275 ExtraVT, isVolatile,
2276 MinAlign(Alignment, IncrementSize));
2277
2278 // Build a factor node to remember that this load is independent of the
2279 // other one.
2280 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2281 Hi.getValue(1));
2282
2283 // Move the top bits to the right place.
2284 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2285 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2286
2287 // Join the hi and lo parts.
2288 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002289 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002290 // Big endian - avoid unaligned loads.
2291 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2292 // Load the top RoundWidth bits.
2293 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2294 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2295 Alignment);
2296
2297 // Load the remaining ExtraWidth bits.
2298 IncrementSize = RoundWidth / 8;
2299 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2300 DAG.getIntPtrConstant(IncrementSize));
2301 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2302 LD->getSrcValue(), SVOffset + IncrementSize,
2303 ExtraVT, isVolatile,
2304 MinAlign(Alignment, IncrementSize));
2305
2306 // Build a factor node to remember that this load is independent of the
2307 // other one.
2308 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2309 Hi.getValue(1));
2310
2311 // Move the top bits to the right place.
2312 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2313 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2314
2315 // Join the hi and lo parts.
2316 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2317 }
2318
2319 Tmp1 = LegalizeOp(Result);
2320 Tmp2 = LegalizeOp(Ch);
2321 } else {
Evan Cheng03294662008-10-14 21:26:46 +00002322 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002323 default: assert(0 && "This action is not supported yet!");
2324 case TargetLowering::Custom:
2325 isCustom = true;
2326 // FALLTHROUGH
2327 case TargetLowering::Legal:
2328 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2329 Tmp1 = Result.getValue(0);
2330 Tmp2 = Result.getValue(1);
2331
2332 if (isCustom) {
2333 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002334 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002335 Tmp1 = LegalizeOp(Tmp3);
2336 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2337 }
2338 } else {
2339 // If this is an unaligned load and the target doesn't support it,
2340 // expand it.
2341 if (!TLI.allowsUnalignedMemoryAccesses()) {
2342 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002343 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002344 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002345 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002346 TLI);
2347 Tmp1 = Result.getOperand(0);
2348 Tmp2 = Result.getOperand(1);
2349 Tmp1 = LegalizeOp(Tmp1);
2350 Tmp2 = LegalizeOp(Tmp2);
2351 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002352 }
2353 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002354 break;
2355 case TargetLowering::Expand:
2356 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2357 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman475871a2008-07-27 21:46:04 +00002358 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002359 LD->getSrcValueOffset(),
2360 LD->isVolatile(), LD->getAlignment());
2361 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2362 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2363 Tmp2 = LegalizeOp(Load.getValue(1));
2364 break;
2365 }
2366 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2367 // Turn the unsupported load into an EXTLOAD followed by an explicit
2368 // zero/sign extend inreg.
2369 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2370 Tmp1, Tmp2, LD->getSrcValue(),
2371 LD->getSrcValueOffset(), SrcVT,
2372 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00002373 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002374 if (ExtType == ISD::SEXTLOAD)
2375 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2376 Result, DAG.getValueType(SrcVT));
2377 else
2378 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2379 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2380 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002381 break;
2382 }
Evan Cheng466685d2006-10-09 20:57:25 +00002383 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002384
Evan Cheng466685d2006-10-09 20:57:25 +00002385 // Since loads produce two values, make sure to remember that we legalized
2386 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002387 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2388 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002389 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002390 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002391 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002392 case ISD::EXTRACT_ELEMENT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002393 MVT OpTy = Node->getOperand(0).getValueType();
Nate Begeman5dc897b2005-10-19 00:06:56 +00002394 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002395 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002396 case Legal:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002397 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Nate Begeman5dc897b2005-10-19 00:06:56 +00002398 // 1 -> Hi
2399 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002400 DAG.getConstant(OpTy.getSizeInBits()/2,
Nate Begeman5dc897b2005-10-19 00:06:56 +00002401 TLI.getShiftAmountTy()));
2402 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2403 } else {
2404 // 0 -> Lo
2405 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2406 Node->getOperand(0));
2407 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002408 break;
2409 case Expand:
2410 // Get both the low and high parts.
2411 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002412 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Nate Begeman5dc897b2005-10-19 00:06:56 +00002413 Result = Tmp2; // 1 -> Hi
2414 else
2415 Result = Tmp1; // 0 -> Lo
2416 break;
2417 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002418 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002419 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002420
2421 case ISD::CopyToReg:
2422 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002423
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002424 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002425 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002426 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002427 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002428 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002429 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002430 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002431 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002432 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002433 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002434 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2435 Tmp3);
2436 } else {
2437 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002438 }
2439
2440 // Since this produces two values, make sure to remember that we legalized
2441 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002442 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2443 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002444 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002445 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002446 break;
2447
2448 case ISD::RET:
2449 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002450
2451 // Ensure that libcalls are emitted before a return.
2452 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2453 Tmp1 = LegalizeOp(Tmp1);
2454 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002455
Chris Lattner3e928bb2005-01-07 07:47:09 +00002456 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002457 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002458 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002459 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002460 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002461 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002462 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002463 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002464 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002465 if (!Tmp2.getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002466 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002467 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002468
2469 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002470 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002471 std::swap(Lo, Hi);
2472
Gabor Greifba36cb52008-08-28 21:40:38 +00002473 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00002474 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2475 else
2476 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002477 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002478 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00002479 SDNode *InVal = Tmp2.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002480 int InIx = Tmp2.getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002481 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2482 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002483
Dan Gohman7f321562007-06-25 16:23:39 +00002484 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002485 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002486 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002487 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002488 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002489 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002490 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002491 } else if (NumElems == 1) {
2492 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002493 Tmp2 = ScalarizeVectorOp(Tmp2);
2494 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002495 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002496
2497 // FIXME: Returns of gcc generic vectors smaller than a legal type
2498 // should be returned in integer registers!
2499
Chris Lattnerf87324e2006-04-11 01:31:51 +00002500 // The scalarized value type may not be legal, e.g. it might require
2501 // promotion or expansion. Relegalize the return.
2502 Result = LegalizeOp(Result);
2503 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002504 // FIXME: Returns of gcc generic vectors larger than a legal vector
2505 // type should be returned by reference!
Dan Gohman475871a2008-07-27 21:46:04 +00002506 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002507 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002508 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002509 Result = LegalizeOp(Result);
2510 }
2511 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002512 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002513 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002514 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002515 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002516 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002517 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002518 }
2519 break;
2520 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002521 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002522 break;
2523 default: { // ret <values>
Dan Gohman475871a2008-07-27 21:46:04 +00002524 SmallVector<SDValue, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002525 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002526 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002527 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2528 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002529 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002530 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002531 break;
2532 case Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002533 SDValue Lo, Hi;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002534 assert(!Node->getOperand(i).getValueType().isExtended() &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002535 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002536 ExpandOp(Node->getOperand(i), Lo, Hi);
2537 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002538 NewValues.push_back(Node->getOperand(i+1));
Gabor Greifba36cb52008-08-28 21:40:38 +00002539 if (Hi.getNode()) {
Evan Cheng13acce32006-12-11 19:27:14 +00002540 NewValues.push_back(Hi);
2541 NewValues.push_back(Node->getOperand(i+1));
2542 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002543 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002544 }
2545 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002546 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002547 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002548
2549 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002550 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002551 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002552 Result = DAG.getNode(ISD::RET, MVT::Other,
2553 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002554 break;
2555 }
2556 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002557
Chris Lattner6862dbc2006-01-29 21:02:23 +00002558 if (Result.getOpcode() == ISD::RET) {
2559 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2560 default: assert(0 && "This action is not supported yet!");
2561 case TargetLowering::Legal: break;
2562 case TargetLowering::Custom:
2563 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002564 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner6862dbc2006-01-29 21:02:23 +00002565 break;
2566 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002567 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002568 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002569 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002570 StoreSDNode *ST = cast<StoreSDNode>(Node);
2571 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2572 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002573 int SVOffset = ST->getSrcValueOffset();
2574 unsigned Alignment = ST->getAlignment();
2575 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002576
Evan Cheng8b2794a2006-10-13 21:14:26 +00002577 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002578 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2579 // FIXME: We shouldn't do this for TargetConstantFP's.
2580 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2581 // to phase ordering between legalized code and the dag combiner. This
2582 // probably means that we need to integrate dag combiner and legalizer
2583 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002584 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002585 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002586 if (CFP->getValueType(0) == MVT::f32 &&
2587 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002588 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00002589 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002590 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002591 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2592 SVOffset, isVolatile, Alignment);
2593 break;
2594 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002595 // If this target supports 64-bit registers, do a single 64-bit store.
2596 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00002597 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002598 zextOrTrunc(64), MVT::i64);
Chris Lattner3cb93512007-10-15 05:46:06 +00002599 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2600 SVOffset, isVolatile, Alignment);
2601 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002602 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002603 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2604 // stores. If the target supports neither 32- nor 64-bits, this
2605 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00002606 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00002607 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2608 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002609 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002610
2611 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2612 SVOffset, isVolatile, Alignment);
2613 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002614 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002615 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002616 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002617
2618 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2619 break;
2620 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002621 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002622 }
2623
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002624 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002625 case Legal: {
2626 Tmp3 = LegalizeOp(ST->getValue());
2627 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2628 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002629
Duncan Sands83ec4b62008-06-06 12:08:01 +00002630 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002631 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2632 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002633 case TargetLowering::Legal:
2634 // If this is an unaligned store and the target doesn't support it,
2635 // expand it.
2636 if (!TLI.allowsUnalignedMemoryAccesses()) {
2637 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002638 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002639 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002640 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002641 TLI);
2642 }
2643 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002644 case TargetLowering::Custom:
2645 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002646 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002647 break;
2648 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002649 assert(VT.isVector() && "Unknown legal promote case!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002650 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2651 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2652 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002653 ST->getSrcValue(), SVOffset, isVolatile,
2654 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002655 break;
2656 }
2657 break;
2658 }
2659 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002660 if (!ST->getMemoryVT().isVector()) {
2661 // Truncate the value and store the result.
2662 Tmp3 = PromoteOp(ST->getValue());
2663 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2664 SVOffset, ST->getMemoryVT(),
2665 isVolatile, Alignment);
2666 break;
2667 }
2668 // Fall thru to expand for vector
2669 case Expand: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002670 unsigned IncrementSize = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002671 SDValue Lo, Hi;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002672
2673 // If this is a vector type, then we have to calculate the increment as
2674 // the product of the element size in bytes, and the number of elements
2675 // in the high half of the vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002676 if (ST->getValue().getValueType().isVector()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002677 SDNode *InVal = ST->getValue().getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002678 int InIx = ST->getValue().getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002679 MVT InVT = InVal->getValueType(InIx);
2680 unsigned NumElems = InVT.getVectorNumElements();
2681 MVT EVT = InVT.getVectorElementType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002682
Dan Gohman7f321562007-06-25 16:23:39 +00002683 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002684 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002685 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002686 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002687 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002688 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002689 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002690 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002691 Result = LegalizeOp(Result);
2692 break;
2693 } else if (NumElems == 1) {
2694 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002695 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002696 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002697 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002698 // The scalarized value type may not be legal, e.g. it might require
2699 // promotion or expansion. Relegalize the scalar store.
2700 Result = LegalizeOp(Result);
2701 break;
2702 } else {
Mon P Wang0c397192008-10-30 08:01:45 +00002703 // Check if we have widen this node with another value
2704 std::map<SDValue, SDValue>::iterator I =
2705 WidenNodes.find(ST->getValue());
2706 if (I != WidenNodes.end()) {
2707 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2708 break;
2709 }
2710 else {
2711 SplitVectorOp(ST->getValue(), Lo, Hi);
2712 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2713 EVT.getSizeInBits()/8;
2714 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002715 }
2716 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002717 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greifba36cb52008-08-28 21:40:38 +00002718 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002719
Richard Pennington4b052dc2008-09-25 16:15:10 +00002720 if (Hi.getNode() && TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002721 std::swap(Lo, Hi);
2722 }
2723
2724 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002725 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002726
Gabor Greifba36cb52008-08-28 21:40:38 +00002727 if (Hi.getNode() == NULL) {
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002728 // Must be int <-> float one-to-one expansion.
2729 Result = Lo;
2730 break;
2731 }
2732
Evan Cheng8b2794a2006-10-13 21:14:26 +00002733 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002734 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002735 assert(isTypeLegal(Tmp2.getValueType()) &&
2736 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002737 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002738 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002739 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002740 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002741 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2742 break;
Mon P Wang0c397192008-10-30 08:01:45 +00002743 } // case Expand
Evan Cheng8b2794a2006-10-13 21:14:26 +00002744 }
2745 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002746 switch (getTypeAction(ST->getValue().getValueType())) {
2747 case Legal:
2748 Tmp3 = LegalizeOp(ST->getValue());
2749 break;
2750 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002751 if (!ST->getValue().getValueType().isVector()) {
2752 // We can promote the value, the truncstore will still take care of it.
2753 Tmp3 = PromoteOp(ST->getValue());
2754 break;
2755 }
2756 // Vector case falls through to expand
Chris Lattnerddf89562008-01-17 19:59:44 +00002757 case Expand:
2758 // Just store the low part. This may become a non-trunc store, so make
2759 // sure to use getTruncStore, not UpdateNodeOperands below.
2760 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2761 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2762 SVOffset, MVT::i8, isVolatile, Alignment);
2763 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002764
Duncan Sands83ec4b62008-06-06 12:08:01 +00002765 MVT StVT = ST->getMemoryVT();
2766 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00002767
Duncan Sands83ec4b62008-06-06 12:08:01 +00002768 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00002769 // Promote to a byte-sized store with upper bits zero if not
2770 // storing an integral number of bytes. For example, promote
2771 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002772 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands7e857202008-01-22 07:17:34 +00002773 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2774 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2775 SVOffset, NVT, isVolatile, Alignment);
2776 } else if (StWidth & (StWidth - 1)) {
2777 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002778 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00002779 "Unsupported truncstore!");
2780 unsigned RoundWidth = 1 << Log2_32(StWidth);
2781 assert(RoundWidth < StWidth);
2782 unsigned ExtraWidth = StWidth - RoundWidth;
2783 assert(ExtraWidth < RoundWidth);
2784 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2785 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002786 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2787 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002788 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00002789 unsigned IncrementSize;
2790
2791 if (TLI.isLittleEndian()) {
2792 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2793 // Store the bottom RoundWidth bits.
2794 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2795 SVOffset, RoundVT,
2796 isVolatile, Alignment);
2797
2798 // Store the remaining ExtraWidth bits.
2799 IncrementSize = RoundWidth / 8;
2800 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2801 DAG.getIntPtrConstant(IncrementSize));
2802 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2803 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2804 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2805 SVOffset + IncrementSize, ExtraVT, isVolatile,
2806 MinAlign(Alignment, IncrementSize));
2807 } else {
2808 // Big endian - avoid unaligned stores.
2809 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2810 // Store the top RoundWidth bits.
2811 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2812 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2813 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2814 RoundVT, isVolatile, Alignment);
2815
2816 // Store the remaining ExtraWidth bits.
2817 IncrementSize = RoundWidth / 8;
2818 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2819 DAG.getIntPtrConstant(IncrementSize));
2820 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2821 SVOffset + IncrementSize, ExtraVT, isVolatile,
2822 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002823 }
Duncan Sands7e857202008-01-22 07:17:34 +00002824
2825 // The order of the stores doesn't matter.
2826 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2827 } else {
2828 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2829 Tmp2 != ST->getBasePtr())
2830 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2831 ST->getOffset());
2832
2833 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2834 default: assert(0 && "This action is not supported yet!");
2835 case TargetLowering::Legal:
2836 // If this is an unaligned store and the target doesn't support it,
2837 // expand it.
2838 if (!TLI.allowsUnalignedMemoryAccesses()) {
2839 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002840 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00002841 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002842 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00002843 TLI);
2844 }
2845 break;
2846 case TargetLowering::Custom:
2847 Result = TLI.LowerOperation(Result, DAG);
2848 break;
2849 case Expand:
2850 // TRUNCSTORE:i16 i32 -> STORE i16
2851 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2852 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2853 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2854 isVolatile, Alignment);
2855 break;
2856 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002857 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002858 }
2859 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002860 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002861 case ISD::PCMARKER:
2862 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002863 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002864 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002865 case ISD::STACKSAVE:
2866 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002867 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2868 Tmp1 = Result.getValue(0);
2869 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002870
Chris Lattner140d53c2006-01-13 02:50:02 +00002871 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2872 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002873 case TargetLowering::Legal: break;
2874 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002875 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002876 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002877 Tmp1 = LegalizeOp(Tmp3);
2878 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002879 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002880 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002881 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002882 // Expand to CopyFromReg if the target set
2883 // StackPointerRegisterToSaveRestore.
2884 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002885 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002886 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002887 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002888 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002889 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2890 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002891 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002892 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002893 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002894
2895 // Since stacksave produce two values, make sure to remember that we
2896 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002897 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2898 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002899 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002900
Chris Lattner140d53c2006-01-13 02:50:02 +00002901 case ISD::STACKRESTORE:
2902 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2903 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002904 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002905
2906 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2907 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002908 case TargetLowering::Legal: break;
2909 case TargetLowering::Custom:
2910 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002911 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002912 break;
2913 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002914 // Expand to CopyToReg if the target set
2915 // StackPointerRegisterToSaveRestore.
2916 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2917 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2918 } else {
2919 Result = Tmp1;
2920 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002921 break;
2922 }
2923 break;
2924
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002925 case ISD::READCYCLECOUNTER:
2926 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002927 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002928 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2929 Node->getValueType(0))) {
2930 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002931 case TargetLowering::Legal:
2932 Tmp1 = Result.getValue(0);
2933 Tmp2 = Result.getValue(1);
2934 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002935 case TargetLowering::Custom:
2936 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002937 Tmp1 = LegalizeOp(Result.getValue(0));
2938 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002939 break;
2940 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002941
2942 // Since rdcc produce two values, make sure to remember that we legalized
2943 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002944 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2945 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002946 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002947
Chris Lattner2ee743f2005-01-14 22:08:15 +00002948 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002949 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2950 case Expand: assert(0 && "It's impossible to expand bools");
2951 case Legal:
2952 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2953 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002954 case Promote: {
Mon P Wang0c397192008-10-30 08:01:45 +00002955 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Chris Lattner47e92232005-01-18 19:27:06 +00002956 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002957 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002958 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002959 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002960 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerb6c80602006-11-28 01:03:30 +00002961 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002962 break;
2963 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002964 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002965 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002966 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002967
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002968 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002969
Nate Begemanb942a3d2005-08-23 04:29:48 +00002970 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002971 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002972 case TargetLowering::Legal: break;
2973 case TargetLowering::Custom: {
2974 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002975 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002976 break;
2977 }
Nate Begeman9373a812005-08-10 20:51:12 +00002978 case TargetLowering::Expand:
2979 if (Tmp1.getOpcode() == ISD::SETCC) {
2980 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2981 Tmp2, Tmp3,
2982 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2983 } else {
2984 Result = DAG.getSelectCC(Tmp1,
2985 DAG.getConstant(0, Tmp1.getValueType()),
2986 Tmp2, Tmp3, ISD::SETNE);
2987 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002988 break;
2989 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002990 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002991 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2992 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002993 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002994 ExtOp = ISD::BIT_CONVERT;
2995 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002996 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002997 ExtOp = ISD::ANY_EXTEND;
2998 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002999 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00003000 ExtOp = ISD::FP_EXTEND;
3001 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003002 }
3003 // Promote each of the values to the new type.
3004 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
3005 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
3006 // Perform the larger operation, then round down.
3007 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00003008 if (TruncOp != ISD::FP_ROUND)
3009 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
3010 else
3011 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
3012 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003013 break;
3014 }
3015 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003016 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00003017 case ISD::SELECT_CC: {
3018 Tmp1 = Node->getOperand(0); // LHS
3019 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00003020 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
3021 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00003022 SDValue CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00003023
Dale Johannesen53e4e442008-11-07 22:54:33 +00003024 LegalizeSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2, CC);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003025
Evan Cheng7f042682008-10-15 02:05:31 +00003026 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00003027 // the LHS is a legal SETCC itself. In this case, we need to compare
3028 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00003029 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003030 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3031 CC = DAG.getCondCode(ISD::SETNE);
3032 }
3033 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
3034
3035 // Everything is legal, see if we should expand this op or something.
3036 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
3037 default: assert(0 && "This action is not supported yet!");
3038 case TargetLowering::Legal: break;
3039 case TargetLowering::Custom:
3040 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003041 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00003042 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003043 }
3044 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00003045 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003046 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00003047 Tmp1 = Node->getOperand(0);
3048 Tmp2 = Node->getOperand(1);
3049 Tmp3 = Node->getOperand(2);
Evan Cheng7f042682008-10-15 02:05:31 +00003050 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003051
3052 // If we had to Expand the SetCC operands into a SELECT node, then it may
3053 // not always be possible to return a true LHS & RHS. In this case, just
3054 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003055 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003056 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003057 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003058 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003059
Chris Lattner73e142f2006-01-30 22:43:50 +00003060 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003061 default: assert(0 && "Cannot handle this action for SETCC yet!");
3062 case TargetLowering::Custom:
3063 isCustom = true;
3064 // FALLTHROUGH.
3065 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00003066 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00003067 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00003068 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003069 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00003070 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003071 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00003072 case TargetLowering::Promote: {
3073 // First step, figure out the appropriate operation to use.
3074 // Allow SETCC to not be supported for all legal data types
3075 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00003076 MVT NewInTy = Node->getOperand(0).getValueType();
3077 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00003078
3079 // Scan for the appropriate larger type to use.
3080 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003081 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00003082
Duncan Sands83ec4b62008-06-06 12:08:01 +00003083 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003084 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003085 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003086 "Fell off of the edge of the floating point world");
3087
3088 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00003089 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00003090 break;
3091 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003092 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00003093 assert(0 && "Cannot promote Legal Integer SETCC yet");
3094 else {
3095 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
3096 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
3097 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003098 Tmp1 = LegalizeOp(Tmp1);
3099 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00003100 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00003101 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00003102 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00003103 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003104 case TargetLowering::Expand:
3105 // Expand a setcc node into a select_cc of the same condition, lhs, and
3106 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003107 MVT VT = Node->getValueType(0);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003108 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3109 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00003110 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003111 break;
3112 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003113 break;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003114 case ISD::VSETCC: {
3115 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3116 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman475871a2008-07-27 21:46:04 +00003117 SDValue CC = Node->getOperand(2);
Nate Begemanb43e9c12008-05-12 19:40:03 +00003118
3119 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3120
3121 // Everything is legal, see if we should expand this op or something.
3122 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3123 default: assert(0 && "This action is not supported yet!");
3124 case TargetLowering::Legal: break;
3125 case TargetLowering::Custom:
3126 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003127 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003128 break;
3129 }
3130 break;
3131 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00003132
Chris Lattner5b359c62005-04-02 04:00:59 +00003133 case ISD::SHL_PARTS:
3134 case ISD::SRA_PARTS:
3135 case ISD::SRL_PARTS: {
Dan Gohman475871a2008-07-27 21:46:04 +00003136 SmallVector<SDValue, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00003137 bool Changed = false;
3138 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3139 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3140 Changed |= Ops.back() != Node->getOperand(i);
3141 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003142 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003143 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00003144
Evan Cheng05a2d562006-01-09 18:31:59 +00003145 switch (TLI.getOperationAction(Node->getOpcode(),
3146 Node->getValueType(0))) {
3147 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003148 case TargetLowering::Legal: break;
3149 case TargetLowering::Custom:
3150 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003151 if (Tmp1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003152 SDValue Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00003153 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003154 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman475871a2008-07-27 21:46:04 +00003155 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003156 if (i == Op.getResNo())
Evan Cheng12f22742006-01-19 04:54:52 +00003157 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00003158 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003159 assert(RetVal.getNode() && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00003160 return RetVal;
3161 }
Evan Cheng05a2d562006-01-09 18:31:59 +00003162 break;
3163 }
3164
Chris Lattner2c8086f2005-04-02 05:00:07 +00003165 // Since these produce multiple values, make sure to remember that we
3166 // legalized all of them.
3167 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +00003168 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00003169 return Result.getValue(Op.getResNo());
Chris Lattner84f67882005-01-20 18:52:28 +00003170 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003171
3172 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00003173 case ISD::ADD:
3174 case ISD::SUB:
3175 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00003176 case ISD::MULHS:
3177 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003178 case ISD::UDIV:
3179 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003180 case ISD::AND:
3181 case ISD::OR:
3182 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00003183 case ISD::SHL:
3184 case ISD::SRL:
3185 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00003186 case ISD::FADD:
3187 case ISD::FSUB:
3188 case ISD::FMUL:
3189 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00003190 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003191 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00003192 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3193 case Expand: assert(0 && "Not possible");
3194 case Legal:
3195 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3196 break;
3197 case Promote:
3198 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3199 break;
3200 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003201
3202 if ((Node->getOpcode() == ISD::SHL ||
3203 Node->getOpcode() == ISD::SRL ||
3204 Node->getOpcode() == ISD::SRA) &&
3205 !Node->getValueType(0).isVector()) {
Mon P Wange9f10152008-12-09 05:46:39 +00003206 Tmp2 = LegalizeShiftAmount(Tmp2);
3207 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003208
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003209 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003210
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003211 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003212 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00003213 case TargetLowering::Legal: break;
3214 case TargetLowering::Custom:
3215 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003216 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003217 Result = Tmp1;
3218 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00003219 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003220 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003221 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003222 MVT VT = Op.getValueType();
Mon P Wang0c397192008-10-30 08:01:45 +00003223
Dan Gohman525178c2007-10-08 18:33:35 +00003224 // See if multiply or divide can be lowered using two-result operations.
3225 SDVTList VTs = DAG.getVTList(VT, VT);
3226 if (Node->getOpcode() == ISD::MUL) {
3227 // We just need the low half of the multiply; try both the signed
3228 // and unsigned forms. If the target supports both SMUL_LOHI and
3229 // UMUL_LOHI, form a preference by checking which forms of plain
3230 // MULH it supports.
3231 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3232 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3233 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3234 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3235 unsigned OpToUse = 0;
3236 if (HasSMUL_LOHI && !HasMULHS) {
3237 OpToUse = ISD::SMUL_LOHI;
3238 } else if (HasUMUL_LOHI && !HasMULHU) {
3239 OpToUse = ISD::UMUL_LOHI;
3240 } else if (HasSMUL_LOHI) {
3241 OpToUse = ISD::SMUL_LOHI;
3242 } else if (HasUMUL_LOHI) {
3243 OpToUse = ISD::UMUL_LOHI;
3244 }
3245 if (OpToUse) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003246 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003247 break;
3248 }
3249 }
3250 if (Node->getOpcode() == ISD::MULHS &&
3251 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003252 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3253 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003254 break;
3255 }
3256 if (Node->getOpcode() == ISD::MULHU &&
3257 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003258 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3259 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003260 break;
3261 }
3262 if (Node->getOpcode() == ISD::SDIV &&
3263 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003264 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3265 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003266 break;
3267 }
3268 if (Node->getOpcode() == ISD::UDIV &&
3269 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003270 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3271 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003272 break;
3273 }
Mon P Wang0c397192008-10-30 08:01:45 +00003274
Dan Gohman82669522007-10-11 23:57:53 +00003275 // Check to see if we have a libcall for this operator.
3276 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3277 bool isSigned = false;
3278 switch (Node->getOpcode()) {
3279 case ISD::UDIV:
3280 case ISD::SDIV:
3281 if (VT == MVT::i32) {
3282 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang0c397192008-10-30 08:01:45 +00003283 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00003284 isSigned = Node->getOpcode() == ISD::SDIV;
3285 }
3286 break;
Chris Lattner31d71612008-10-04 21:27:46 +00003287 case ISD::MUL:
3288 if (VT == MVT::i32)
3289 LC = RTLIB::MUL_I32;
3290 break;
Dan Gohman82669522007-10-11 23:57:53 +00003291 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003292 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3293 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003294 break;
3295 default: break;
3296 }
3297 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00003298 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003299 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003300 break;
3301 }
Mon P Wang0c397192008-10-30 08:01:45 +00003302
Duncan Sands83ec4b62008-06-06 12:08:01 +00003303 assert(Node->getValueType(0).isVector() &&
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003304 "Cannot expand this binary operator!");
3305 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003306 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003307 break;
3308 }
Evan Chengcc987612006-04-12 21:20:24 +00003309 case TargetLowering::Promote: {
3310 switch (Node->getOpcode()) {
3311 default: assert(0 && "Do not know how to promote this BinOp!");
3312 case ISD::AND:
3313 case ISD::OR:
3314 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003315 MVT OVT = Node->getValueType(0);
3316 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3317 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00003318 // Bit convert each of the values to the new type.
3319 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3320 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3321 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3322 // Bit convert the result back the original type.
3323 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3324 break;
3325 }
3326 }
3327 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003328 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003329 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003330
Dan Gohmane14ea862007-10-05 14:17:22 +00003331 case ISD::SMUL_LOHI:
3332 case ISD::UMUL_LOHI:
3333 case ISD::SDIVREM:
3334 case ISD::UDIVREM:
3335 // These nodes will only be produced by target-specific lowering, so
3336 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003337 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003338 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003339
3340 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3341 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3342 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003343 break;
3344
Chris Lattnera09f8482006-03-05 05:09:38 +00003345 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3346 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3347 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3348 case Expand: assert(0 && "Not possible");
3349 case Legal:
3350 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3351 break;
3352 case Promote:
3353 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3354 break;
3355 }
3356
3357 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3358
3359 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3360 default: assert(0 && "Operation not supported");
3361 case TargetLowering::Custom:
3362 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003363 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003364 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003365 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003366 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003367 // If this target supports fabs/fneg natively and select is cheap,
3368 // do this efficiently.
3369 if (!TLI.isSelectExpensive() &&
3370 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3371 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003372 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003373 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003374 // Get the sign bit of the RHS.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003375 MVT IVT =
Chris Lattner8f4191d2006-03-13 06:08:38 +00003376 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman475871a2008-07-27 21:46:04 +00003377 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003378 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003379 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3380 // Get the absolute value of the result.
Dan Gohman475871a2008-07-27 21:46:04 +00003381 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Chris Lattner8f4191d2006-03-13 06:08:38 +00003382 // Select between the nabs and abs value based on the sign bit of
3383 // the input.
3384 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3385 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3386 AbsVal),
3387 AbsVal);
3388 Result = LegalizeOp(Result);
3389 break;
3390 }
3391
3392 // Otherwise, do bitwise ops!
Duncan Sands83ec4b62008-06-06 12:08:01 +00003393 MVT NVT =
Evan Cheng912095b2007-01-04 21:56:39 +00003394 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3395 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3396 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3397 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003398 break;
3399 }
Evan Cheng912095b2007-01-04 21:56:39 +00003400 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003401 break;
3402
Nate Begeman551bf3f2006-02-17 05:43:56 +00003403 case ISD::ADDC:
3404 case ISD::SUBC:
3405 Tmp1 = LegalizeOp(Node->getOperand(0));
3406 Tmp2 = LegalizeOp(Node->getOperand(1));
3407 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003408 Tmp3 = Result.getValue(0);
3409 Tmp4 = Result.getValue(1);
3410
3411 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3412 default: assert(0 && "This action is not supported yet!");
3413 case TargetLowering::Legal:
3414 break;
3415 case TargetLowering::Custom:
3416 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3417 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003418 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003419 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3420 }
3421 break;
3422 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003423 // Since this produces two values, make sure to remember that we legalized
3424 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003425 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3426 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3427 return Op.getResNo() ? Tmp4 : Tmp3;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003428
Nate Begeman551bf3f2006-02-17 05:43:56 +00003429 case ISD::ADDE:
3430 case ISD::SUBE:
3431 Tmp1 = LegalizeOp(Node->getOperand(0));
3432 Tmp2 = LegalizeOp(Node->getOperand(1));
3433 Tmp3 = LegalizeOp(Node->getOperand(2));
3434 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003435 Tmp3 = Result.getValue(0);
3436 Tmp4 = Result.getValue(1);
3437
3438 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3439 default: assert(0 && "This action is not supported yet!");
3440 case TargetLowering::Legal:
3441 break;
3442 case TargetLowering::Custom:
3443 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3444 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003445 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003446 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3447 }
3448 break;
3449 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003450 // Since this produces two values, make sure to remember that we legalized
3451 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003452 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3453 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3454 return Op.getResNo() ? Tmp4 : Tmp3;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003455
Nate Begeman419f8b62005-10-18 00:27:41 +00003456 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003457 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00003458 // TODO: handle the case where the Lo and Hi operands are not of legal type
3459 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3460 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3461 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003462 case TargetLowering::Promote:
3463 case TargetLowering::Custom:
3464 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003465 case TargetLowering::Legal:
3466 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3467 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3468 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003469 case TargetLowering::Expand:
3470 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3471 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3472 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003473 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00003474 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003475 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003476 break;
3477 }
3478 break;
3479 }
3480
Nate Begemanc105e192005-04-06 00:23:54 +00003481 case ISD::UREM:
3482 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003483 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003484 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3485 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003486
Nate Begemanc105e192005-04-06 00:23:54 +00003487 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003488 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3489 case TargetLowering::Custom:
3490 isCustom = true;
3491 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003492 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003493 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003494 if (isCustom) {
3495 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003496 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003497 }
Nate Begemanc105e192005-04-06 00:23:54 +00003498 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003499 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003500 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003501 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003502 MVT VT = Node->getValueType(0);
Dan Gohman525178c2007-10-08 18:33:35 +00003503
3504 // See if remainder can be lowered using two-result operations.
3505 SDVTList VTs = DAG.getVTList(VT, VT);
3506 if (Node->getOpcode() == ISD::SREM &&
3507 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003508 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003509 break;
3510 }
3511 if (Node->getOpcode() == ISD::UREM &&
3512 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003513 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003514 break;
3515 }
3516
Duncan Sands83ec4b62008-06-06 12:08:01 +00003517 if (VT.isInteger()) {
Dan Gohman525178c2007-10-08 18:33:35 +00003518 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003519 TargetLowering::Legal) {
3520 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003521 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003522 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3523 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003524 } else if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003525 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003526 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003527 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003528 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003529 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3530 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman475871a2008-07-27 21:46:04 +00003531 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003532 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003533 }
Dan Gohman0d974262007-11-06 22:11:54 +00003534 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003535 assert(VT.isFloatingPoint() &&
Dan Gohman0d974262007-11-06 22:11:54 +00003536 "remainder op must have integer or floating-point type");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003537 if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003538 Result = LegalizeOp(UnrollVectorOp(Op));
3539 } else {
3540 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003541 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3542 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003543 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003544 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman80176312007-11-05 23:35:22 +00003545 }
Nate Begemanc105e192005-04-06 00:23:54 +00003546 }
3547 break;
3548 }
Dan Gohman525178c2007-10-08 18:33:35 +00003549 }
Nate Begemanc105e192005-04-06 00:23:54 +00003550 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003551 case ISD::VAARG: {
3552 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3553 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3554
Duncan Sands83ec4b62008-06-06 12:08:01 +00003555 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003556 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3557 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003558 case TargetLowering::Custom:
3559 isCustom = true;
3560 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003561 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003562 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3563 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003564 Tmp1 = Result.getValue(1);
3565
3566 if (isCustom) {
3567 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003568 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003569 Result = LegalizeOp(Tmp2);
3570 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3571 }
3572 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003573 break;
3574 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003575 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003576 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003577 // Increment the pointer, VAList, to the next vaarg
Duncan Sands5c58a312008-11-03 11:51:11 +00003578 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3579 DAG.getConstant(TLI.getTargetData()->getABITypeSize(VT.getTypeForMVT()),
3580 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00003581 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00003582 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003583 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003584 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003585 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003586 Result = LegalizeOp(Result);
3587 break;
3588 }
3589 }
3590 // Since VAARG produces two values, make sure to remember that we
3591 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003592 AddLegalizedOperand(SDValue(Node, 0), Result);
3593 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003594 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003595 }
3596
3597 case ISD::VACOPY:
3598 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3599 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3600 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3601
3602 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3603 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003604 case TargetLowering::Custom:
3605 isCustom = true;
3606 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003607 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003608 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3609 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003610 if (isCustom) {
3611 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003612 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003613 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003614 break;
3615 case TargetLowering::Expand:
3616 // This defaults to loading a pointer from the input and storing it to the
3617 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003618 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3619 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman499c1bd2008-04-17 02:09:26 +00003620 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3621 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003622 break;
3623 }
3624 break;
3625
3626 case ISD::VAEND:
3627 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3628 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3629
3630 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3631 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003632 case TargetLowering::Custom:
3633 isCustom = true;
3634 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003635 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003636 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003637 if (isCustom) {
3638 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003639 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003640 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003641 break;
3642 case TargetLowering::Expand:
3643 Result = Tmp1; // Default to a no-op, return the chain
3644 break;
3645 }
3646 break;
3647
3648 case ISD::VASTART:
3649 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3650 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3651
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003652 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3653
Nate Begemanacc398c2006-01-25 18:21:52 +00003654 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3655 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003656 case TargetLowering::Legal: break;
3657 case TargetLowering::Custom:
3658 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003659 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003660 break;
3661 }
3662 break;
3663
Nate Begeman35ef9132006-01-11 21:21:00 +00003664 case ISD::ROTL:
3665 case ISD::ROTR:
3666 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3667 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003668 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003669 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3670 default:
3671 assert(0 && "ROTL/ROTR legalize operation not supported");
3672 break;
3673 case TargetLowering::Legal:
3674 break;
3675 case TargetLowering::Custom:
3676 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003677 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelc9dc1142007-04-02 21:36:32 +00003678 break;
3679 case TargetLowering::Promote:
3680 assert(0 && "Do not know how to promote ROTL/ROTR");
3681 break;
3682 case TargetLowering::Expand:
3683 assert(0 && "Do not know how to expand ROTL/ROTR");
3684 break;
3685 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003686 break;
3687
Nate Begemand88fc032006-01-14 03:14:10 +00003688 case ISD::BSWAP:
3689 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3690 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003691 case TargetLowering::Custom:
3692 assert(0 && "Cannot custom legalize this yet!");
3693 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003694 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003695 break;
3696 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003697 MVT OVT = Tmp1.getValueType();
3698 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3699 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begemand88fc032006-01-14 03:14:10 +00003700
Chris Lattner456a93a2006-01-28 07:39:30 +00003701 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3702 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3703 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3704 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3705 break;
3706 }
3707 case TargetLowering::Expand:
3708 Result = ExpandBSWAP(Tmp1);
3709 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003710 }
3711 break;
3712
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003713 case ISD::CTPOP:
3714 case ISD::CTTZ:
3715 case ISD::CTLZ:
3716 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3717 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003718 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003719 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003720 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003721 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003722 TargetLowering::Custom) {
3723 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003724 if (Tmp1.getNode()) {
Scott Michel335f4f72007-08-02 02:22:46 +00003725 Result = Tmp1;
3726 }
Scott Michel910b66d2007-07-30 21:00:31 +00003727 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003728 break;
3729 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003730 MVT OVT = Tmp1.getValueType();
3731 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003732
3733 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003734 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3735 // Perform the larger operation, then subtract if needed.
3736 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003737 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003738 case ISD::CTPOP:
3739 Result = Tmp1;
3740 break;
3741 case ISD::CTTZ:
3742 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00003743 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003744 DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003745 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003746 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003747 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003748 break;
3749 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003750 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003751 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003752 DAG.getConstant(NVT.getSizeInBits() -
3753 OVT.getSizeInBits(), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003754 break;
3755 }
3756 break;
3757 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003758 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003759 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003760 break;
3761 }
3762 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003763
Chris Lattner2c8086f2005-04-02 05:00:07 +00003764 // Unary operators
3765 case ISD::FABS:
3766 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003767 case ISD::FSQRT:
3768 case ISD::FSIN:
3769 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003770 case ISD::FLOG:
3771 case ISD::FLOG2:
3772 case ISD::FLOG10:
3773 case ISD::FEXP:
3774 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00003775 case ISD::FTRUNC:
3776 case ISD::FFLOOR:
3777 case ISD::FCEIL:
3778 case ISD::FRINT:
3779 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003780 Tmp1 = LegalizeOp(Node->getOperand(0));
3781 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003782 case TargetLowering::Promote:
3783 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003784 isCustom = true;
3785 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003786 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003787 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003788 if (isCustom) {
3789 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003790 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00003791 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003792 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003793 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003794 switch (Node->getOpcode()) {
3795 default: assert(0 && "Unreachable!");
3796 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003797 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3798 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003799 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003800 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003801 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003802 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003803 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003804 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003805 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00003806 ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003807 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3808 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003809 break;
3810 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00003811 case ISD::FSQRT:
3812 case ISD::FSIN:
3813 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003814 case ISD::FLOG:
3815 case ISD::FLOG2:
3816 case ISD::FLOG10:
3817 case ISD::FEXP:
3818 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003819 case ISD::FTRUNC:
3820 case ISD::FFLOOR:
3821 case ISD::FCEIL:
3822 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00003823 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003824 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003825
3826 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003827 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003828 Result = LegalizeOp(UnrollVectorOp(Op));
3829 break;
3830 }
3831
Evan Cheng56966222007-01-12 02:11:51 +00003832 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003833 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003834 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003835 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3836 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003837 break;
3838 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003839 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3840 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003841 break;
3842 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003843 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3844 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003845 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003846 case ISD::FLOG:
3847 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3848 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3849 break;
3850 case ISD::FLOG2:
3851 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3852 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3853 break;
3854 case ISD::FLOG10:
3855 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3856 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3857 break;
3858 case ISD::FEXP:
3859 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3860 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3861 break;
3862 case ISD::FEXP2:
3863 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3864 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3865 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003866 case ISD::FTRUNC:
3867 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3868 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3869 break;
3870 case ISD::FFLOOR:
3871 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3872 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3873 break;
3874 case ISD::FCEIL:
3875 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3876 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3877 break;
3878 case ISD::FRINT:
3879 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3880 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3881 break;
3882 case ISD::FNEARBYINT:
3883 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3884 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3885 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00003886 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003887 default: assert(0 && "Unreachable!");
3888 }
Dan Gohman475871a2008-07-27 21:46:04 +00003889 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003890 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003891 break;
3892 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003893 }
3894 break;
3895 }
3896 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003897 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003898 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003899
3900 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003901 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003902 Result = LegalizeOp(UnrollVectorOp(Op));
3903 break;
3904 }
3905
3906 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003907 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3908 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003909 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003910 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003911 break;
3912 }
Chris Lattner35481892005-12-23 00:16:34 +00003913 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003914 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003915 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3916 Node->getValueType(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003917 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003918 // The input has to be a vector type, we have to either scalarize it, pack
3919 // it, or convert it based on whether the input vector type is legal.
Gabor Greifba36cb52008-08-28 21:40:38 +00003920 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00003921 int InIx = Node->getOperand(0).getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003922 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3923 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohman7f321562007-06-25 16:23:39 +00003924
3925 // Figure out if there is a simple type corresponding to this Vector
3926 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003927 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00003928 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003929 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003930 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3931 LegalizeOp(Node->getOperand(0)));
3932 break;
3933 } else if (NumElems == 1) {
3934 // Turn this into a bit convert of the scalar input.
3935 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3936 ScalarizeVectorOp(Node->getOperand(0)));
3937 break;
3938 } else {
3939 // FIXME: UNIMP! Store then reload
3940 assert(0 && "Cast from unsupported vector type not implemented yet!");
3941 }
Chris Lattner67993f72006-01-23 07:30:46 +00003942 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003943 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3944 Node->getOperand(0).getValueType())) {
3945 default: assert(0 && "Unknown operation action!");
3946 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003947 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3948 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003949 break;
3950 case TargetLowering::Legal:
3951 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003952 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003953 break;
3954 }
3955 }
3956 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003957 case ISD::CONVERT_RNDSAT: {
3958 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3959 switch (CvtCode) {
3960 default: assert(0 && "Unknown cvt code!");
3961 case ISD::CVT_SF:
3962 case ISD::CVT_UF:
Mon P Wang77cdf302008-11-10 20:54:11 +00003963 case ISD::CVT_FF:
Mon P Wang1cd46bb2008-12-09 07:27:39 +00003964 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003965 case ISD::CVT_FS:
3966 case ISD::CVT_FU:
3967 case ISD::CVT_SS:
3968 case ISD::CVT_SU:
3969 case ISD::CVT_US:
3970 case ISD::CVT_UU: {
3971 SDValue DTyOp = Node->getOperand(1);
3972 SDValue STyOp = Node->getOperand(2);
3973 SDValue RndOp = Node->getOperand(3);
3974 SDValue SatOp = Node->getOperand(4);
3975 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3976 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3977 case Legal:
3978 Tmp1 = LegalizeOp(Node->getOperand(0));
3979 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3980 RndOp, SatOp);
3981 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3982 TargetLowering::Custom) {
3983 Tmp1 = TLI.LowerOperation(Result, DAG);
3984 if (Tmp1.getNode()) Result = Tmp1;
3985 }
3986 break;
3987 case Promote:
3988 Result = PromoteOp(Node->getOperand(0));
3989 // For FP, make Op1 a i32
3990
Mon P Wang1cd46bb2008-12-09 07:27:39 +00003991 Result = DAG.getConvertRndSat(Op.getValueType(), Result,
Mon P Wang77cdf302008-11-10 20:54:11 +00003992 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3993 break;
3994 }
3995 break;
3996 }
3997 } // end switch CvtCode
3998 break;
3999 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00004000 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00004001 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004002 case ISD::UINT_TO_FP: {
4003 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman7f8613e2008-08-14 20:04:46 +00004004 Result = LegalizeINT_TO_FP(Result, isSigned,
4005 Node->getValueType(0), Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004006 break;
4007 }
4008 case ISD::TRUNCATE:
4009 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4010 case Legal:
4011 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel546d7b52008-12-02 19:55:08 +00004012 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
4013 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
4014 case TargetLowering::Custom:
Mon P Wangf67303d2008-12-11 00:44:22 +00004015 isCustom = true;
4016 // FALLTHROUGH
Scott Michel546d7b52008-12-02 19:55:08 +00004017 case TargetLowering::Legal:
Mon P Wangf67303d2008-12-11 00:44:22 +00004018 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4019 if (isCustom) {
4020 Tmp1 = TLI.LowerOperation(Result, DAG);
4021 if (Tmp1.getNode()) Result = Tmp1;
4022 }
4023 break;
Mon P Wang9e5ecb82008-12-12 01:25:51 +00004024 case TargetLowering::Expand:
4025 assert(Result.getValueType().isVector() && "must be vector type");
4026 // Unroll the truncate. We should do better.
4027 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Schellerb0a5cdd2008-12-02 12:12:25 +00004028 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004029 break;
4030 case Expand:
4031 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4032
4033 // Since the result is legal, we should just be able to truncate the low
4034 // part of the source.
4035 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
4036 break;
4037 case Promote:
4038 Result = PromoteOp(Node->getOperand(0));
4039 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
4040 break;
4041 }
4042 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004043
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004044 case ISD::FP_TO_SINT:
4045 case ISD::FP_TO_UINT:
4046 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4047 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00004048 Tmp1 = LegalizeOp(Node->getOperand(0));
4049
Chris Lattner1618beb2005-07-29 00:11:56 +00004050 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
4051 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004052 case TargetLowering::Custom:
4053 isCustom = true;
4054 // FALLTHROUGH
4055 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004056 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004057 if (isCustom) {
4058 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004059 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00004060 }
4061 break;
4062 case TargetLowering::Promote:
4063 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
4064 Node->getOpcode() == ISD::FP_TO_SINT);
4065 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00004066 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00004067 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004068 SDValue True, False;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004069 MVT VT = Node->getOperand(0).getValueType();
4070 MVT NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00004071 const uint64_t zero[] = {0, 0};
Duncan Sands83ec4b62008-06-06 12:08:01 +00004072 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
4073 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohmanc7773bf2008-02-29 01:44:25 +00004074 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00004075 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004076 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Nate Begemand2558e32005-08-14 01:20:53 +00004077 Node->getOperand(0), Tmp2, ISD::SETLT);
4078 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
4079 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00004080 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00004081 Tmp2));
4082 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00004083 DAG.getConstant(x, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004084 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
4085 break;
Nate Begemand2558e32005-08-14 01:20:53 +00004086 } else {
4087 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
4088 }
4089 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00004090 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004091 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00004092 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004093 MVT VT = Op.getValueType();
4094 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004095 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004096 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004097 if (Node->getOpcode() == ISD::FP_TO_SINT) {
4098 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
4099 Node->getOperand(0), DAG.getValueType(MVT::f64));
4100 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
4101 DAG.getIntPtrConstant(1));
4102 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
4103 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004104 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4105 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4106 Tmp2 = DAG.getConstantFP(apf, OVT);
4107 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4108 // FIXME: generated code sucks.
4109 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
4110 DAG.getNode(ISD::ADD, MVT::i32,
4111 DAG.getNode(ISD::FP_TO_SINT, VT,
4112 DAG.getNode(ISD::FSUB, OVT,
4113 Node->getOperand(0), Tmp2)),
4114 DAG.getConstant(0x80000000, MVT::i32)),
4115 DAG.getNode(ISD::FP_TO_SINT, VT,
4116 Node->getOperand(0)),
4117 DAG.getCondCode(ISD::SETGE));
4118 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004119 break;
4120 }
Dan Gohmana2e94852008-03-10 23:03:31 +00004121 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsb2ff8852008-07-17 02:36:29 +00004122 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4123 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4124 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman475871a2008-07-27 21:46:04 +00004125 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00004126 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00004127 break;
4128 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004129 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004130 Tmp1 = PromoteOp(Node->getOperand(0));
4131 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4132 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004133 break;
4134 }
4135 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004136
Chris Lattnerf2670a82008-01-16 06:57:07 +00004137 case ISD::FP_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004138 MVT DstVT = Op.getValueType();
4139 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004140 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4141 // The only other way we can lower this is to turn it into a STORE,
4142 // LOAD pair, targetting a temporary location (a stack slot).
4143 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
4144 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00004145 }
4146 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4147 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4148 case Legal:
4149 Tmp1 = LegalizeOp(Node->getOperand(0));
4150 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4151 break;
4152 case Promote:
4153 Tmp1 = PromoteOp(Node->getOperand(0));
4154 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4155 break;
4156 }
4157 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004158 }
Dale Johannesen5411a392007-08-09 01:04:01 +00004159 case ISD::FP_ROUND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004160 MVT DstVT = Op.getValueType();
4161 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004162 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4163 if (SrcVT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00004164 SDValue Lo;
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004165 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00004166 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004167 if (DstVT!=MVT::f64)
4168 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00004169 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00004170 }
Chris Lattner0bd48932008-01-17 07:00:52 +00004171 // The only other way we can lower this is to turn it into a STORE,
4172 // LOAD pair, targetting a temporary location (a stack slot).
4173 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4174 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00004175 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00004176 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4177 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4178 case Legal:
4179 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004180 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004181 break;
4182 case Promote:
4183 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004184 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4185 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004186 break;
4187 }
4188 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004189 }
Chris Lattner13c78e22005-09-02 00:18:10 +00004190 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004191 case ISD::ZERO_EXTEND:
4192 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004193 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00004194 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004195 case Legal:
4196 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel82747a52008-04-30 00:26:38 +00004197 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel0123b7d2008-02-15 23:05:48 +00004198 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4199 TargetLowering::Custom) {
Scott Michel82747a52008-04-30 00:26:38 +00004200 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004201 if (Tmp1.getNode()) Result = Tmp1;
Scott Michel0123b7d2008-02-15 23:05:48 +00004202 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004203 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004204 case Promote:
4205 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00004206 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004207 Tmp1 = PromoteOp(Node->getOperand(0));
4208 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00004209 break;
Chris Lattner1713e732005-01-16 00:38:00 +00004210 case ISD::ZERO_EXTEND:
4211 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004212 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00004213 Result = DAG.getZeroExtendInReg(Result,
4214 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00004215 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004216 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00004217 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004218 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00004219 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004220 Result,
4221 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00004222 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004223 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004224 }
4225 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00004226 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00004227 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00004228 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004229 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00004230
4231 // If this operation is not supported, convert it to a shl/shr or load/store
4232 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004233 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4234 default: assert(0 && "This action not supported for this op yet!");
4235 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004236 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004237 break;
4238 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00004239 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00004240 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00004241 // NOTE: we could fall back on load/store here too for targets without
4242 // SAR. However, it is doubtful that any exist.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004243 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4244 ExtraVT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00004245 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00004246 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4247 Node->getOperand(0), ShiftCst);
4248 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4249 Result, ShiftCst);
4250 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00004251 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00004252 // EXTLOAD pair, targetting a temporary location (a stack slot).
4253
4254 // NOTE: there is a choice here between constantly creating new stack
4255 // slots and always reusing the same one. We currently always create
4256 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00004257 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4258 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00004259 } else {
4260 assert(0 && "Unknown op");
4261 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004262 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00004263 }
Chris Lattner0f69b292005-01-15 06:18:18 +00004264 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004265 }
Duncan Sands36397f52007-07-27 12:58:54 +00004266 case ISD::TRAMPOLINE: {
Dan Gohman475871a2008-07-27 21:46:04 +00004267 SDValue Ops[6];
Duncan Sands36397f52007-07-27 12:58:54 +00004268 for (unsigned i = 0; i != 6; ++i)
4269 Ops[i] = LegalizeOp(Node->getOperand(i));
4270 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4271 // The only option for this node is to custom lower it.
4272 Result = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004273 assert(Result.getNode() && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00004274
4275 // Since trampoline produces two values, make sure to remember that we
4276 // legalized both of them.
4277 Tmp1 = LegalizeOp(Result.getValue(1));
4278 Result = LegalizeOp(Result);
Dan Gohman475871a2008-07-27 21:46:04 +00004279 AddLegalizedOperand(SDValue(Node, 0), Result);
4280 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00004281 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00004282 }
Dan Gohman9c78a392008-05-14 00:43:10 +00004283 case ISD::FLT_ROUNDS_: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004284 MVT VT = Node->getValueType(0);
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004285 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4286 default: assert(0 && "This action not supported for this op yet!");
4287 case TargetLowering::Custom:
4288 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004289 if (Result.getNode()) break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004290 // Fall Thru
4291 case TargetLowering::Legal:
4292 // If this operation is not supported, lower it to constant 1
4293 Result = DAG.getConstant(1, VT);
4294 break;
4295 }
Dan Gohman9ab9ee82008-05-12 16:07:15 +00004296 break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004297 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004298 case ISD::TRAP: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004299 MVT VT = Node->getValueType(0);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004300 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4301 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00004302 case TargetLowering::Legal:
4303 Tmp1 = LegalizeOp(Node->getOperand(0));
4304 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4305 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004306 case TargetLowering::Custom:
4307 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004308 if (Result.getNode()) break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004309 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00004310 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004311 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00004312 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004313 TargetLowering::ArgListTy Args;
Dan Gohman475871a2008-07-27 21:46:04 +00004314 std::pair<SDValue,SDValue> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00004315 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00004316 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00004317 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner034f12e2008-01-15 22:09:33 +00004318 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004319 Result = CallResult.second;
4320 break;
4321 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004322 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004323 }
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004324
Bill Wendling74c37652008-12-09 22:08:41 +00004325 case ISD::SADDO:
4326 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004327 MVT VT = Node->getValueType(0);
4328 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4329 default: assert(0 && "This action not supported for this op yet!");
4330 case TargetLowering::Custom:
4331 Result = TLI.LowerOperation(Op, DAG);
4332 if (Result.getNode()) break;
4333 // FALLTHROUGH
4334 case TargetLowering::Legal: {
4335 SDValue LHS = LegalizeOp(Node->getOperand(0));
4336 SDValue RHS = LegalizeOp(Node->getOperand(1));
4337
Bill Wendling74c37652008-12-09 22:08:41 +00004338 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
4339 ISD::ADD : ISD::SUB, LHS.getValueType(),
4340 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004341 MVT OType = Node->getValueType(1);
4342
Bill Wendlinga6af91a2008-11-25 08:19:22 +00004343 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004344
Bill Wendling740464e2008-11-25 19:40:17 +00004345 // LHSSign -> LHS >= 0
4346 // RHSSign -> RHS >= 0
4347 // SumSign -> Sum >= 0
4348 //
Bill Wendling74c37652008-12-09 22:08:41 +00004349 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00004350 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00004351 // Sub:
4352 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00004353 //
4354 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4355 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
Bill Wendling74c37652008-12-09 22:08:41 +00004356 SDValue SignsMatch = DAG.getSetCC(OType, LHSSign, RHSSign,
4357 Node->getOpcode() == ISD::SADDO ?
4358 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004359
Bill Wendling740464e2008-11-25 19:40:17 +00004360 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4361 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004362
Bill Wendling74c37652008-12-09 22:08:41 +00004363 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004364
4365 MVT ValueVTs[] = { LHS.getValueType(), OType };
4366 SDValue Ops[] = { Sum, Cmp };
4367
Duncan Sandsaaffa052008-12-01 11:41:29 +00004368 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4369 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004370 SDNode *RNode = Result.getNode();
4371 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4372 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4373 break;
4374 }
4375 }
4376
4377 break;
4378 }
Bill Wendling74c37652008-12-09 22:08:41 +00004379 case ISD::UADDO:
4380 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00004381 MVT VT = Node->getValueType(0);
4382 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4383 default: assert(0 && "This action not supported for this op yet!");
4384 case TargetLowering::Custom:
4385 Result = TLI.LowerOperation(Op, DAG);
4386 if (Result.getNode()) break;
4387 // FALLTHROUGH
4388 case TargetLowering::Legal: {
4389 SDValue LHS = LegalizeOp(Node->getOperand(0));
4390 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004391
Bill Wendling74c37652008-12-09 22:08:41 +00004392 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
4393 ISD::ADD : ISD::SUB, LHS.getValueType(),
4394 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004395 MVT OType = Node->getValueType(1);
Bill Wendling74c37652008-12-09 22:08:41 +00004396 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS,
4397 Node->getOpcode () == ISD::UADDO ?
4398 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004399
Bill Wendling41ea7e72008-11-24 19:21:46 +00004400 MVT ValueVTs[] = { LHS.getValueType(), OType };
4401 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004402
Duncan Sandsaaffa052008-12-01 11:41:29 +00004403 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4404 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004405 SDNode *RNode = Result.getNode();
4406 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4407 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4408 break;
4409 }
4410 }
4411
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004412 break;
4413 }
Bill Wendling74c37652008-12-09 22:08:41 +00004414 case ISD::SMULO:
4415 case ISD::UMULO: {
4416 MVT VT = Node->getValueType(0);
4417 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4418 default: assert(0 && "This action is not supported at all!");
4419 case TargetLowering::Custom:
4420 Result = TLI.LowerOperation(Op, DAG);
4421 if (Result.getNode()) break;
4422 // Fall Thru
4423 case TargetLowering::Legal:
4424 // FIXME: According to Hacker's Delight, this can be implemented in
4425 // target independent lowering, but it would be inefficient, since it
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00004426 // requires a division + a branch.
Bill Wendling74c37652008-12-09 22:08:41 +00004427 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4428 break;
4429 }
4430 break;
4431 }
4432
Chris Lattner45b8caf2005-01-15 07:15:18 +00004433 }
Chris Lattner456a93a2006-01-28 07:39:30 +00004434
Chris Lattner4ddd2832006-04-08 04:13:17 +00004435 assert(Result.getValueType() == Op.getValueType() &&
4436 "Bad legalization!");
4437
Chris Lattner456a93a2006-01-28 07:39:30 +00004438 // Make sure that the generated code is itself legal.
4439 if (Result != Op)
4440 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004441
Chris Lattner45982da2005-05-12 16:53:42 +00004442 // Note that LegalizeOp may be reentered even from single-use nodes, which
4443 // means that we always must cache transformed nodes.
4444 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004445 return Result;
4446}
4447
Chris Lattner8b6fa222005-01-15 22:16:26 +00004448/// PromoteOp - Given an operation that produces a value in an invalid type,
4449/// promote it to compute the value into a larger type. The produced value will
4450/// have the correct bits for the low portion of the register, but no guarantee
4451/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman475871a2008-07-27 21:46:04 +00004452SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004453 MVT VT = Op.getValueType();
4454 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004455 assert(getTypeAction(VT) == Promote &&
4456 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004457 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner03c85462005-01-15 05:21:40 +00004458 "Cannot promote to smaller type!");
4459
Dan Gohman475871a2008-07-27 21:46:04 +00004460 SDValue Tmp1, Tmp2, Tmp3;
4461 SDValue Result;
Gabor Greifba36cb52008-08-28 21:40:38 +00004462 SDNode *Node = Op.getNode();
Chris Lattner03c85462005-01-15 05:21:40 +00004463
Dan Gohman475871a2008-07-27 21:46:04 +00004464 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004465 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004466
Chris Lattner03c85462005-01-15 05:21:40 +00004467 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004468 case ISD::CopyFromReg:
4469 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004470 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004471#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004472 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004473#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004474 assert(0 && "Do not know how to promote this operator!");
4475 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004476 case ISD::UNDEF:
4477 Result = DAG.getNode(ISD::UNDEF, NVT);
4478 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004479 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004480 if (VT != MVT::i1)
4481 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4482 else
4483 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004484 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4485 break;
4486 case ISD::ConstantFP:
4487 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4488 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4489 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004490
Chris Lattner82fbfb62005-01-18 02:59:52 +00004491 case ISD::SETCC:
Scott Michel5b8f82e2008-03-10 15:42:14 +00004492 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman5922f562008-03-14 00:53:31 +00004493 && "SetCC type is not legal??");
Scott Michel5b8f82e2008-03-10 15:42:14 +00004494 Result = DAG.getNode(ISD::SETCC,
Nate Begeman5922f562008-03-14 00:53:31 +00004495 TLI.getSetCCResultType(Node->getOperand(0)),
4496 Node->getOperand(0), Node->getOperand(1),
4497 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004498 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00004499
Chris Lattner03c85462005-01-15 05:21:40 +00004500 case ISD::TRUNCATE:
4501 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4502 case Legal:
4503 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004504 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner03c85462005-01-15 05:21:40 +00004505 "This truncation doesn't make sense!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004506 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Chris Lattner03c85462005-01-15 05:21:40 +00004507 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4508 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004509 case Promote:
4510 // The truncation is not required, because we don't guarantee anything
4511 // about high bits anyway.
4512 Result = PromoteOp(Node->getOperand(0));
4513 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004514 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004515 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4516 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004517 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004518 }
4519 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004520 case ISD::SIGN_EXTEND:
4521 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004522 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004523 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4524 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4525 case Legal:
4526 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004527 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004528 break;
4529 case Promote:
4530 // Promote the reg if it's smaller.
4531 Result = PromoteOp(Node->getOperand(0));
4532 // The high bits are not guaranteed to be anything. Insert an extend.
4533 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004534 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004535 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004536 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004537 Result = DAG.getZeroExtendInReg(Result,
4538 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004539 break;
4540 }
4541 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00004542 case ISD::CONVERT_RNDSAT: {
4543 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4544 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4545 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4546 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4547 "can only promote integers");
4548 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4549 Node->getOperand(1), Node->getOperand(2),
4550 Node->getOperand(3), Node->getOperand(4),
4551 CvtCode);
4552 break;
4553
4554 }
Chris Lattner35481892005-12-23 00:16:34 +00004555 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004556 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4557 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004558 Result = PromoteOp(Result);
4559 break;
4560
Chris Lattner8b6fa222005-01-15 22:16:26 +00004561 case ISD::FP_EXTEND:
4562 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4563 case ISD::FP_ROUND:
4564 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4565 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4566 case Promote: assert(0 && "Unreachable with 2 FP types!");
4567 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004568 if (Node->getConstantOperandVal(1) == 0) {
4569 // Input is legal? Do an FP_ROUND_INREG.
4570 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4571 DAG.getValueType(VT));
4572 } else {
4573 // Just remove the truncate, it isn't affecting the value.
4574 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4575 Node->getOperand(1));
4576 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004577 break;
4578 }
4579 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004580 case ISD::SINT_TO_FP:
4581 case ISD::UINT_TO_FP:
4582 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4583 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004584 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004585 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004586 break;
4587
4588 case Promote:
4589 Result = PromoteOp(Node->getOperand(0));
4590 if (Node->getOpcode() == ISD::SINT_TO_FP)
4591 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004592 Result,
4593 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004594 else
Chris Lattner23993562005-04-13 02:38:47 +00004595 Result = DAG.getZeroExtendInReg(Result,
4596 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004597 // No extra round required here.
4598 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004599 break;
4600 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004601 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4602 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00004603 // Round if we cannot tolerate excess precision.
4604 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004605 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4606 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004607 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004608 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004609 break;
4610
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004611 case ISD::SIGN_EXTEND_INREG:
4612 Result = PromoteOp(Node->getOperand(0));
4613 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4614 Node->getOperand(1));
4615 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004616 case ISD::FP_TO_SINT:
4617 case ISD::FP_TO_UINT:
4618 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4619 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004620 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004621 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004622 break;
4623 case Promote:
4624 // The input result is prerounded, so we don't have to do anything
4625 // special.
4626 Tmp1 = PromoteOp(Node->getOperand(0));
4627 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004628 }
Nate Begemand2558e32005-08-14 01:20:53 +00004629 // If we're promoting a UINT to a larger size, check to see if the new node
4630 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4631 // we can use that instead. This allows us to generate better code for
4632 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4633 // legal, such as PowerPC.
4634 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004635 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004636 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4637 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004638 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4639 } else {
4640 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4641 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004642 break;
4643
Chris Lattner2c8086f2005-04-02 05:00:07 +00004644 case ISD::FABS:
4645 case ISD::FNEG:
4646 Tmp1 = PromoteOp(Node->getOperand(0));
4647 assert(Tmp1.getValueType() == NVT);
4648 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4649 // NOTE: we do not have to do any extra rounding here for
4650 // NoExcessFPPrecision, because we know the input will have the appropriate
4651 // precision, and these operations don't modify precision at all.
4652 break;
4653
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004654 case ISD::FLOG:
4655 case ISD::FLOG2:
4656 case ISD::FLOG10:
4657 case ISD::FEXP:
4658 case ISD::FEXP2:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004659 case ISD::FSQRT:
4660 case ISD::FSIN:
4661 case ISD::FCOS:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00004662 case ISD::FTRUNC:
4663 case ISD::FFLOOR:
4664 case ISD::FCEIL:
4665 case ISD::FRINT:
4666 case ISD::FNEARBYINT:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004667 Tmp1 = PromoteOp(Node->getOperand(0));
4668 assert(Tmp1.getValueType() == NVT);
4669 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004670 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004671 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4672 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004673 break;
4674
Evan Cheng9d24ac52008-09-09 23:35:53 +00004675 case ISD::FPOW:
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004676 case ISD::FPOWI: {
Evan Cheng9d24ac52008-09-09 23:35:53 +00004677 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004678 // directly as well, which may be better.
4679 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng9d24ac52008-09-09 23:35:53 +00004680 Tmp2 = Node->getOperand(1);
4681 if (Node->getOpcode() == ISD::FPOW)
4682 Tmp2 = PromoteOp(Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004683 assert(Tmp1.getValueType() == NVT);
Evan Cheng9d24ac52008-09-09 23:35:53 +00004684 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004685 if (NoExcessFPPrecision)
4686 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4687 DAG.getValueType(VT));
4688 break;
4689 }
4690
Dale Johannesene00a8a22008-08-28 02:44:49 +00004691 case ISD::ATOMIC_CMP_SWAP_8:
4692 case ISD::ATOMIC_CMP_SWAP_16:
4693 case ISD::ATOMIC_CMP_SWAP_32:
4694 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang28873102008-06-25 08:15:39 +00004695 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004696 Tmp2 = PromoteOp(Node->getOperand(2));
4697 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang28873102008-06-25 08:15:39 +00004698 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4699 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004700 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004701 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004702 // Remember that we legalized the chain.
4703 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4704 break;
4705 }
Dale Johannesene00a8a22008-08-28 02:44:49 +00004706 case ISD::ATOMIC_LOAD_ADD_8:
4707 case ISD::ATOMIC_LOAD_SUB_8:
4708 case ISD::ATOMIC_LOAD_AND_8:
4709 case ISD::ATOMIC_LOAD_OR_8:
4710 case ISD::ATOMIC_LOAD_XOR_8:
4711 case ISD::ATOMIC_LOAD_NAND_8:
4712 case ISD::ATOMIC_LOAD_MIN_8:
4713 case ISD::ATOMIC_LOAD_MAX_8:
4714 case ISD::ATOMIC_LOAD_UMIN_8:
4715 case ISD::ATOMIC_LOAD_UMAX_8:
4716 case ISD::ATOMIC_SWAP_8:
4717 case ISD::ATOMIC_LOAD_ADD_16:
4718 case ISD::ATOMIC_LOAD_SUB_16:
4719 case ISD::ATOMIC_LOAD_AND_16:
4720 case ISD::ATOMIC_LOAD_OR_16:
4721 case ISD::ATOMIC_LOAD_XOR_16:
4722 case ISD::ATOMIC_LOAD_NAND_16:
4723 case ISD::ATOMIC_LOAD_MIN_16:
4724 case ISD::ATOMIC_LOAD_MAX_16:
4725 case ISD::ATOMIC_LOAD_UMIN_16:
4726 case ISD::ATOMIC_LOAD_UMAX_16:
4727 case ISD::ATOMIC_SWAP_16:
4728 case ISD::ATOMIC_LOAD_ADD_32:
4729 case ISD::ATOMIC_LOAD_SUB_32:
4730 case ISD::ATOMIC_LOAD_AND_32:
4731 case ISD::ATOMIC_LOAD_OR_32:
4732 case ISD::ATOMIC_LOAD_XOR_32:
4733 case ISD::ATOMIC_LOAD_NAND_32:
4734 case ISD::ATOMIC_LOAD_MIN_32:
4735 case ISD::ATOMIC_LOAD_MAX_32:
4736 case ISD::ATOMIC_LOAD_UMIN_32:
4737 case ISD::ATOMIC_LOAD_UMAX_32:
4738 case ISD::ATOMIC_SWAP_32:
4739 case ISD::ATOMIC_LOAD_ADD_64:
4740 case ISD::ATOMIC_LOAD_SUB_64:
4741 case ISD::ATOMIC_LOAD_AND_64:
4742 case ISD::ATOMIC_LOAD_OR_64:
4743 case ISD::ATOMIC_LOAD_XOR_64:
4744 case ISD::ATOMIC_LOAD_NAND_64:
4745 case ISD::ATOMIC_LOAD_MIN_64:
4746 case ISD::ATOMIC_LOAD_MAX_64:
4747 case ISD::ATOMIC_LOAD_UMIN_64:
4748 case ISD::ATOMIC_LOAD_UMAX_64:
4749 case ISD::ATOMIC_SWAP_64: {
Mon P Wang28873102008-06-25 08:15:39 +00004750 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004751 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang28873102008-06-25 08:15:39 +00004752 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4753 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004754 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004755 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004756 // Remember that we legalized the chain.
4757 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4758 break;
4759 }
4760
Chris Lattner03c85462005-01-15 05:21:40 +00004761 case ISD::AND:
4762 case ISD::OR:
4763 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004764 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004765 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004766 case ISD::MUL:
4767 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004768 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004769 // that too is okay if they are integer operations.
4770 Tmp1 = PromoteOp(Node->getOperand(0));
4771 Tmp2 = PromoteOp(Node->getOperand(1));
4772 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4773 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004774 break;
4775 case ISD::FADD:
4776 case ISD::FSUB:
4777 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004778 Tmp1 = PromoteOp(Node->getOperand(0));
4779 Tmp2 = PromoteOp(Node->getOperand(1));
4780 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4781 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4782
4783 // Floating point operations will give excess precision that we may not be
4784 // able to tolerate. If we DO allow excess precision, just leave it,
4785 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004786 // FIXME: Why would we need to round FP ops more than integer ones?
4787 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004788 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004789 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4790 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004791 break;
4792
Chris Lattner8b6fa222005-01-15 22:16:26 +00004793 case ISD::SDIV:
4794 case ISD::SREM:
4795 // These operators require that their input be sign extended.
4796 Tmp1 = PromoteOp(Node->getOperand(0));
4797 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004798 if (NVT.isInteger()) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004799 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4800 DAG.getValueType(VT));
4801 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4802 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004803 }
4804 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4805
4806 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004807 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004808 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4809 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004810 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004811 case ISD::FDIV:
4812 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004813 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004814 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004815 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004816 case Expand: assert(0 && "not implemented");
4817 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4818 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004819 }
4820 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004821 case Expand: assert(0 && "not implemented");
4822 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4823 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004824 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004825 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4826
4827 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004828 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004829 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4830 DAG.getValueType(VT));
4831 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004832
4833 case ISD::UDIV:
4834 case ISD::UREM:
4835 // These operators require that their input be zero extended.
4836 Tmp1 = PromoteOp(Node->getOperand(0));
4837 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004838 assert(NVT.isInteger() && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004839 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4840 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004841 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4842 break;
4843
4844 case ISD::SHL:
4845 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004846 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004847 break;
4848 case ISD::SRA:
4849 // The input value must be properly sign extended.
4850 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004851 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4852 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004853 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004854 break;
4855 case ISD::SRL:
4856 // The input value must be properly zero extended.
4857 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004858 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004859 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004860 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004861
4862 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004863 Tmp1 = Node->getOperand(0); // Get the chain.
4864 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004865 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4866 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands126d9072008-07-04 11:47:58 +00004867 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman0aed7842006-01-28 03:14:31 +00004868 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004869 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004870 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004871 // Increment the pointer, VAList, to the next vaarg
4872 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004873 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman0aed7842006-01-28 03:14:31 +00004874 TLI.getPointerTy()));
4875 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00004876 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004877 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004878 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004879 }
4880 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004881 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004882 break;
4883
Evan Cheng466685d2006-10-09 20:57:25 +00004884 case ISD::LOAD: {
4885 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004886 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4887 ? ISD::EXTLOAD : LD->getExtensionType();
4888 Result = DAG.getExtLoad(ExtType, NVT,
4889 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004890 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004891 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004892 LD->isVolatile(),
4893 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004894 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004895 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004896 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004897 }
Scott Michel8bf61e82008-06-02 22:18:03 +00004898 case ISD::SELECT: {
Chris Lattner03c85462005-01-15 05:21:40 +00004899 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4900 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel8bf61e82008-06-02 22:18:03 +00004901
Duncan Sands83ec4b62008-06-06 12:08:01 +00004902 MVT VT2 = Tmp2.getValueType();
Scott Michel8bf61e82008-06-02 22:18:03 +00004903 assert(VT2 == Tmp3.getValueType()
Scott Michelba12f572008-06-03 19:13:20 +00004904 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4905 // Ensure that the resulting node is at least the same size as the operands'
4906 // value types, because we cannot assume that TLI.getSetCCValueType() is
4907 // constant.
4908 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004909 break;
Scott Michel8bf61e82008-06-02 22:18:03 +00004910 }
Nate Begeman9373a812005-08-10 20:51:12 +00004911 case ISD::SELECT_CC:
4912 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4913 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4914 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004915 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004916 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004917 case ISD::BSWAP:
4918 Tmp1 = Node->getOperand(0);
4919 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4920 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4921 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004922 DAG.getConstant(NVT.getSizeInBits() -
4923 VT.getSizeInBits(),
Nate Begemand88fc032006-01-14 03:14:10 +00004924 TLI.getShiftAmountTy()));
4925 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004926 case ISD::CTPOP:
4927 case ISD::CTTZ:
4928 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004929 // Zero extend the argument
4930 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004931 // Perform the larger operation, then subtract if needed.
4932 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004933 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004934 case ISD::CTPOP:
4935 Result = Tmp1;
4936 break;
4937 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004938 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004939 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004940 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanb55757e2007-05-18 17:52:13 +00004941 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004942 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004943 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004944 break;
4945 case ISD::CTLZ:
4946 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004947 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004948 DAG.getConstant(NVT.getSizeInBits() -
4949 VT.getSizeInBits(), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004950 break;
4951 }
4952 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004953 case ISD::EXTRACT_SUBVECTOR:
4954 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004955 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004956 case ISD::EXTRACT_VECTOR_ELT:
4957 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4958 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004959 }
4960
Gabor Greifba36cb52008-08-28 21:40:38 +00004961 assert(Result.getNode() && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004962
4963 // Make sure the result is itself legal.
4964 Result = LegalizeOp(Result);
4965
4966 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004967 AddPromotedOperand(Op, Result);
4968 return Result;
4969}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004970
Dan Gohman7f321562007-06-25 16:23:39 +00004971/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4972/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4973/// based on the vector type. The return type of this matches the element type
4974/// of the vector, which may not be legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00004975SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004976 // We know that operand #0 is the Vec vector. If the index is a constant
4977 // or if the invec is a supported hardware type, we can use it. Otherwise,
4978 // lower to a store then an indexed load.
Dan Gohman475871a2008-07-27 21:46:04 +00004979 SDValue Vec = Op.getOperand(0);
4980 SDValue Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004981
Duncan Sands83ec4b62008-06-06 12:08:01 +00004982 MVT TVT = Vec.getValueType();
4983 unsigned NumElems = TVT.getVectorNumElements();
Chris Lattner15972212006-03-31 17:55:51 +00004984
Dan Gohman7f321562007-06-25 16:23:39 +00004985 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4986 default: assert(0 && "This action is not supported yet!");
4987 case TargetLowering::Custom: {
4988 Vec = LegalizeOp(Vec);
4989 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004990 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004991 if (Tmp3.getNode())
Dan Gohman7f321562007-06-25 16:23:39 +00004992 return Tmp3;
4993 break;
4994 }
4995 case TargetLowering::Legal:
4996 if (isTypeLegal(TVT)) {
4997 Vec = LegalizeOp(Vec);
4998 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004999 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00005000 }
5001 break;
Mon P Wang0c397192008-10-30 08:01:45 +00005002 case TargetLowering::Promote:
5003 assert(TVT.isVector() && "not vector type");
5004 // fall thru to expand since vectors are by default are promote
Dan Gohman7f321562007-06-25 16:23:39 +00005005 case TargetLowering::Expand:
5006 break;
5007 }
5008
5009 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00005010 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00005011 Op = ScalarizeVectorOp(Vec);
5012 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00005013 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00005014 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00005015 SDValue Lo, Hi;
Chris Lattner15972212006-03-31 17:55:51 +00005016 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005017 if (CIdx->getZExtValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00005018 Vec = Lo;
5019 } else {
5020 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005021 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00005022 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00005023 }
Dan Gohman7f321562007-06-25 16:23:39 +00005024
Chris Lattner15972212006-03-31 17:55:51 +00005025 // It's now an extract from the appropriate high or low part. Recurse.
5026 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00005027 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00005028 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00005029 // Store the value to a temporary stack slot, then LOAD the scalar
5030 // element back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005031 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
5032 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohman7f321562007-06-25 16:23:39 +00005033
5034 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005035 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohman7f321562007-06-25 16:23:39 +00005036 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
5037 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005038
Duncan Sands8e4eb092008-06-08 20:54:56 +00005039 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00005040 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005041 else
Chris Lattnerf185e672007-10-19 16:47:35 +00005042 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005043
Dan Gohman7f321562007-06-25 16:23:39 +00005044 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
5045
5046 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00005047 }
Dan Gohman7f321562007-06-25 16:23:39 +00005048 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00005049}
5050
Dan Gohman7f321562007-06-25 16:23:39 +00005051/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00005052/// we assume the operation can be split if it is not already legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005053SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman65956352007-06-13 15:12:02 +00005054 // We know that operand #0 is the Vec vector. For now we assume the index
5055 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman475871a2008-07-27 21:46:04 +00005056 SDValue Vec = Op.getOperand(0);
5057 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00005058
Duncan Sands83ec4b62008-06-06 12:08:01 +00005059 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohman65956352007-06-13 15:12:02 +00005060
Duncan Sands83ec4b62008-06-06 12:08:01 +00005061 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman65956352007-06-13 15:12:02 +00005062 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00005063 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00005064 }
5065
5066 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00005067 SDValue Lo, Hi;
Dan Gohman65956352007-06-13 15:12:02 +00005068 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005069 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohman65956352007-06-13 15:12:02 +00005070 Vec = Lo;
5071 } else {
5072 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005073 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
5074 Idx.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00005075 }
5076
5077 // It's now an extract from the appropriate high or low part. Recurse.
5078 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00005079 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00005080}
5081
Nate Begeman750ac1b2006-02-01 07:19:44 +00005082/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
5083/// with condition CC on the current target. This usually involves legalizing
5084/// or promoting the arguments. In the case where LHS and RHS must be expanded,
5085/// there may be no choice but to create a new SetCC node to represent the
5086/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00005087/// LHS, and the SDValue returned in RHS has a nil SDNode value.
5088void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
5089 SDValue &RHS,
5090 SDValue &CC) {
5091 SDValue Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005092
5093 switch (getTypeAction(LHS.getValueType())) {
5094 case Legal:
5095 Tmp1 = LegalizeOp(LHS); // LHS
5096 Tmp2 = LegalizeOp(RHS); // RHS
5097 break;
5098 case Promote:
5099 Tmp1 = PromoteOp(LHS); // LHS
5100 Tmp2 = PromoteOp(RHS); // RHS
5101
5102 // If this is an FP compare, the operands have already been extended.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005103 if (LHS.getValueType().isInteger()) {
5104 MVT VT = LHS.getValueType();
5105 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00005106
5107 // Otherwise, we have to insert explicit sign or zero extends. Note
5108 // that we could insert sign extends for ALL conditions, but zero extend
5109 // is cheaper on many machines (an AND instead of two shifts), so prefer
5110 // it.
5111 switch (cast<CondCodeSDNode>(CC)->get()) {
5112 default: assert(0 && "Unknown integer comparison!");
5113 case ISD::SETEQ:
5114 case ISD::SETNE:
5115 case ISD::SETUGE:
5116 case ISD::SETUGT:
5117 case ISD::SETULE:
5118 case ISD::SETULT:
5119 // ALL of these operations will work if we either sign or zero extend
5120 // the operands (including the unsigned comparisons!). Zero extend is
5121 // usually a simpler/cheaper operation, so prefer it.
5122 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
5123 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
5124 break;
5125 case ISD::SETGE:
5126 case ISD::SETGT:
5127 case ISD::SETLT:
5128 case ISD::SETLE:
5129 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
5130 DAG.getValueType(VT));
5131 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
5132 DAG.getValueType(VT));
Evan Chengefa53392008-10-13 18:46:18 +00005133 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5134 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Nate Begeman750ac1b2006-02-01 07:19:44 +00005135 break;
5136 }
5137 }
5138 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005139 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005140 MVT VT = LHS.getValueType();
Evan Cheng2b49c502006-12-15 02:59:56 +00005141 if (VT == MVT::f32 || VT == MVT::f64) {
5142 // Expand into one or more soft-fp libcall(s).
Evan Cheng6518c6e2008-07-01 21:35:46 +00005143 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00005144 switch (cast<CondCodeSDNode>(CC)->get()) {
5145 case ISD::SETEQ:
5146 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00005147 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005148 break;
5149 case ISD::SETNE:
5150 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00005151 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005152 break;
5153 case ISD::SETGE:
5154 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00005155 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005156 break;
5157 case ISD::SETLT:
5158 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00005159 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005160 break;
5161 case ISD::SETLE:
5162 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00005163 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005164 break;
5165 case ISD::SETGT:
5166 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00005167 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005168 break;
5169 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00005170 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5171 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005172 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00005173 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005174 break;
5175 default:
Evan Cheng56966222007-01-12 02:11:51 +00005176 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005177 switch (cast<CondCodeSDNode>(CC)->get()) {
5178 case ISD::SETONE:
5179 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00005180 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005181 // Fallthrough
5182 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00005183 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005184 break;
5185 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00005186 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005187 break;
5188 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00005189 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005190 break;
5191 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00005192 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005193 break;
Evan Cheng56966222007-01-12 02:11:51 +00005194 case ISD::SETUEQ:
5195 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00005196 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005197 default: assert(0 && "Unsupported FP setcc!");
5198 }
5199 }
Duncan Sandsf9516202008-06-30 10:19:09 +00005200
Dan Gohman475871a2008-07-27 21:46:04 +00005201 SDValue Dummy;
5202 SDValue Ops[2] = { LHS, RHS };
Gabor Greifba36cb52008-08-28 21:40:38 +00005203 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005204 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00005205 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00005206 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00005207 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005208 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00005209 CC);
Gabor Greifba36cb52008-08-28 21:40:38 +00005210 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005211 false /*sign irrelevant*/, Dummy);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005212 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00005213 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00005214 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman475871a2008-07-27 21:46:04 +00005215 Tmp2 = SDValue();
Evan Cheng2b49c502006-12-15 02:59:56 +00005216 }
Evan Cheng21cacc42008-07-07 07:18:09 +00005217 LHS = LegalizeOp(Tmp1);
Evan Cheng2b49c502006-12-15 02:59:56 +00005218 RHS = Tmp2;
5219 return;
5220 }
5221
Dan Gohman475871a2008-07-27 21:46:04 +00005222 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005223 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005224 ExpandOp(RHS, RHSLo, RHSHi);
5225 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5226
5227 if (VT==MVT::ppcf128) {
5228 // FIXME: This generated code sucks. We want to generate
Dale Johannesene2f20832008-09-12 00:30:56 +00005229 // FCMPU crN, hi1, hi2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005230 // BNE crN, L:
Dale Johannesene2f20832008-09-12 00:30:56 +00005231 // FCMPU crN, lo1, lo2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005232 // The following can be improved, but not that much.
Dale Johannesene2f20832008-09-12 00:30:56 +00005233 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5234 ISD::SETOEQ);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005235 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005236 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Dale Johannesene2f20832008-09-12 00:30:56 +00005237 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5238 ISD::SETUNE);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005239 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005240 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5241 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00005242 Tmp2 = SDValue();
Dale Johannesen638ccd52007-10-06 01:24:11 +00005243 break;
5244 }
5245
5246 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005247 case ISD::SETEQ:
5248 case ISD::SETNE:
5249 if (RHSLo == RHSHi)
5250 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5251 if (RHSCST->isAllOnesValue()) {
5252 // Comparison to -1.
5253 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5254 Tmp2 = RHSLo;
5255 break;
5256 }
5257
5258 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5259 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5260 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5261 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5262 break;
5263 default:
5264 // If this is a comparison of the sign bit, just look at the top part.
5265 // X > -1, x < 0
5266 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5267 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005268 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00005269 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5270 CST->isAllOnesValue())) { // X > -1
5271 Tmp1 = LHSHi;
5272 Tmp2 = RHSHi;
5273 break;
5274 }
5275
5276 // FIXME: This generated code sucks.
5277 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00005278 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005279 default: assert(0 && "Unknown integer setcc!");
5280 case ISD::SETLT:
5281 case ISD::SETULT: LowCC = ISD::SETULT; break;
5282 case ISD::SETGT:
5283 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5284 case ISD::SETLE:
5285 case ISD::SETULE: LowCC = ISD::SETULE; break;
5286 case ISD::SETGE:
5287 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5288 }
5289
5290 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5291 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5292 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5293
5294 // NOTE: on targets without efficient SELECT of bools, we can always use
5295 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00005296 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005297 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman5922f562008-03-14 00:53:31 +00005298 LowCC, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005299 if (!Tmp1.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00005300 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
5301 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00005302 CCCode, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005303 if (!Tmp2.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00005304 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00005305 RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00005306
Gabor Greifba36cb52008-08-28 21:40:38 +00005307 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5308 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman002e5d02008-03-13 22:13:53 +00005309 if ((Tmp1C && Tmp1C->isNullValue()) ||
5310 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00005311 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5312 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00005313 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00005314 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5315 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5316 // low part is known false, returns high part.
5317 // For LE / GE, if high part is known false, ignore the low part.
5318 // For LT / GT, if high part is known true, ignore the low part.
5319 Tmp1 = Tmp2;
Dan Gohman475871a2008-07-27 21:46:04 +00005320 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005321 } else {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005322 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00005323 ISD::SETEQ, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005324 if (!Result.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00005325 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00005326 ISD::SETEQ);
Evan Cheng2e677812007-02-08 22:16:19 +00005327 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5328 Result, Tmp1, Tmp2));
5329 Tmp1 = Result;
Dan Gohman475871a2008-07-27 21:46:04 +00005330 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005331 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005332 }
5333 }
Evan Cheng2b49c502006-12-15 02:59:56 +00005334 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005335 LHS = Tmp1;
5336 RHS = Tmp2;
5337}
5338
Evan Cheng7f042682008-10-15 02:05:31 +00005339/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5340/// condition code CC on the current target. This routine assumes LHS and rHS
5341/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5342/// illegal condition code into AND / OR of multiple SETCC values.
5343void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5344 SDValue &LHS, SDValue &RHS,
5345 SDValue &CC) {
5346 MVT OpVT = LHS.getValueType();
5347 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5348 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5349 default: assert(0 && "Unknown condition code action!");
5350 case TargetLowering::Legal:
5351 // Nothing to do.
5352 break;
5353 case TargetLowering::Expand: {
5354 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5355 unsigned Opc = 0;
5356 switch (CCCode) {
5357 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00005358 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5359 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5360 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5361 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5362 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5363 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5364 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5365 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5366 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5367 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5368 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5369 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00005370 // FIXME: Implement more expansions.
5371 }
5372
5373 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5374 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5375 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5376 RHS = SDValue();
5377 CC = SDValue();
5378 break;
5379 }
5380 }
5381}
5382
Chris Lattner1401d152008-01-16 07:45:30 +00005383/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5384/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5385/// a load from the stack slot to DestVT, extending it if needed.
5386/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005387SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5388 MVT SlotVT,
5389 MVT DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00005390 // Create the stack frame object.
Mon P Wang364d73d2008-07-05 20:40:31 +00005391 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5392 SrcOp.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00005393 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang364d73d2008-07-05 20:40:31 +00005394
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005395 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005396 int SPFI = StackPtrFI->getIndex();
Mon P Wang364d73d2008-07-05 20:40:31 +00005397
Duncan Sands83ec4b62008-06-06 12:08:01 +00005398 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5399 unsigned SlotSize = SlotVT.getSizeInBits();
5400 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang364d73d2008-07-05 20:40:31 +00005401 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5402 DestVT.getTypeForMVT());
Chris Lattner35481892005-12-23 00:16:34 +00005403
Chris Lattner1401d152008-01-16 07:45:30 +00005404 // Emit a store to the stack slot. Use a truncstore if the input value is
5405 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00005406 SDValue Store;
Mon P Wang364d73d2008-07-05 20:40:31 +00005407
Chris Lattner1401d152008-01-16 07:45:30 +00005408 if (SrcSize > SlotSize)
Dan Gohman69de1932008-02-06 22:27:42 +00005409 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005410 PseudoSourceValue::getFixedStack(SPFI), 0,
5411 SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005412 else {
5413 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman69de1932008-02-06 22:27:42 +00005414 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005415 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang364d73d2008-07-05 20:40:31 +00005416 false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005417 }
5418
Chris Lattner35481892005-12-23 00:16:34 +00005419 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00005420 if (SlotSize == DestSize)
Mon P Wang364d73d2008-07-05 20:40:31 +00005421 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005422
5423 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang364d73d2008-07-05 20:40:31 +00005424 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5425 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00005426}
5427
Dan Gohman475871a2008-07-27 21:46:04 +00005428SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Chris Lattner4352cc92006-04-04 17:23:26 +00005429 // Create a vector sized/aligned stack slot, store the value to element #0,
5430 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005431 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00005432
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005433 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005434 int SPFI = StackPtrFI->getIndex();
5435
Dan Gohman475871a2008-07-27 21:46:04 +00005436 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005437 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00005438 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005439 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00005440}
5441
5442
Chris Lattnerce872152006-03-19 06:31:19 +00005443/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00005444/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00005445SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Chris Lattnerce872152006-03-19 06:31:19 +00005446
5447 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00005448 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00005449 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00005450 bool isOnlyLowElement = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005451 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005452
Dan Gohman475871a2008-07-27 21:46:04 +00005453 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005454 // and use a bitmask instead of a list of elements.
Dan Gohman475871a2008-07-27 21:46:04 +00005455 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00005456 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005457 bool isConstant = true;
5458 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5459 SplatValue.getOpcode() != ISD::UNDEF)
5460 isConstant = false;
5461
Evan Cheng033e6812006-03-24 01:17:21 +00005462 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005463 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00005464 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00005465 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00005466 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00005467 if (SplatValue != V)
Dan Gohman475871a2008-07-27 21:46:04 +00005468 SplatValue = SDValue(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005469
5470 // If this isn't a constant element or an undef, we can't use a constant
5471 // pool load.
5472 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5473 V.getOpcode() != ISD::UNDEF)
5474 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00005475 }
5476
5477 if (isOnlyLowElement) {
5478 // If the low element is an undef too, then this whole things is an undef.
5479 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5480 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5481 // Otherwise, turn this into a scalar_to_vector node.
5482 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5483 Node->getOperand(0));
5484 }
5485
Chris Lattner2eb86532006-03-24 07:29:17 +00005486 // If all elements are constants, create a load from the constant pool.
5487 if (isConstant) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005488 MVT VT = Node->getValueType(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005489 std::vector<Constant*> CV;
5490 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5491 if (ConstantFPSDNode *V =
5492 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005493 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005494 } else if (ConstantSDNode *V =
Chris Lattner02a260a2008-04-20 00:41:09 +00005495 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005496 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005497 } else {
5498 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner02a260a2008-04-20 00:41:09 +00005499 const Type *OpNTy =
Duncan Sands83ec4b62008-06-06 12:08:01 +00005500 Node->getOperand(0).getValueType().getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00005501 CV.push_back(UndefValue::get(OpNTy));
5502 }
5503 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00005504 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005505 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005506 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman69de1932008-02-06 22:27:42 +00005507 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005508 PseudoSourceValue::getConstantPool(), 0,
5509 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00005510 }
5511
Gabor Greifba36cb52008-08-28 21:40:38 +00005512 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00005513 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005514 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman475871a2008-07-27 21:46:04 +00005515 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5516 std::vector<SDValue> ZeroVec(NumElems, Zero);
5517 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005518 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00005519
5520 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005521 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00005522 // Get the splatted value into the low element of a vector register.
Dan Gohman475871a2008-07-27 21:46:04 +00005523 SDValue LowValVec =
Chris Lattner87100e02006-03-20 01:52:29 +00005524 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5525
5526 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5527 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5528 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5529 SplatMask);
5530 }
5531 }
5532
Evan Cheng033e6812006-03-24 01:17:21 +00005533 // If there are only two unique elements, we may be able to turn this into a
5534 // vector shuffle.
5535 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005536 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00005537 SDValue Val1 = Node->getOperand(1);
5538 SDValue Val2;
5539 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005540 if (MI->first != Val1)
5541 Val2 = MI->first;
5542 else
5543 Val2 = (++MI)->first;
5544
5545 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5546 // vector shuffle has the undef vector on the RHS.
5547 if (Val1.getOpcode() == ISD::UNDEF)
5548 std::swap(Val1, Val2);
5549
Evan Cheng033e6812006-03-24 01:17:21 +00005550 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005551 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5552 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005553 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005554
5555 // Set elements of the shuffle mask for Val1.
5556 std::vector<unsigned> &Val1Elts = Values[Val1];
5557 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5558 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5559
5560 // Set elements of the shuffle mask for Val2.
5561 std::vector<unsigned> &Val2Elts = Values[Val2];
5562 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5563 if (Val2.getOpcode() != ISD::UNDEF)
5564 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5565 else
5566 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5567
Dan Gohman475871a2008-07-27 21:46:04 +00005568 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005569 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005570
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005571 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005572 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5573 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005574 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5575 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman475871a2008-07-27 21:46:04 +00005576 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng033e6812006-03-24 01:17:21 +00005577
5578 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005579 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng033e6812006-03-24 01:17:21 +00005580 }
5581 }
Chris Lattnerce872152006-03-19 06:31:19 +00005582
5583 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5584 // aligned object on the stack, store each element into it, then load
5585 // the result as a vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005586 MVT VT = Node->getValueType(0);
Chris Lattnerce872152006-03-19 06:31:19 +00005587 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00005588 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00005589
5590 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00005591 SmallVector<SDValue, 8> Stores;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005592 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005593 // Store (in the right endianness) the elements to memory.
5594 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5595 // Ignore undef elements.
5596 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5597
Chris Lattner841c8822006-03-22 01:46:54 +00005598 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005599
Dan Gohman475871a2008-07-27 21:46:04 +00005600 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Chris Lattnerce872152006-03-19 06:31:19 +00005601 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5602
Evan Cheng786225a2006-10-05 23:01:46 +00005603 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00005604 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00005605 }
5606
Dan Gohman475871a2008-07-27 21:46:04 +00005607 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00005608 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005609 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5610 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005611 else
5612 StoreChain = DAG.getEntryNode();
5613
5614 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00005615 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005616}
5617
Chris Lattner5b359c62005-04-02 04:00:59 +00005618void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman475871a2008-07-27 21:46:04 +00005619 SDValue Op, SDValue Amt,
5620 SDValue &Lo, SDValue &Hi) {
Chris Lattner5b359c62005-04-02 04:00:59 +00005621 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00005622 SDValue LHSL, LHSH;
Chris Lattner5b359c62005-04-02 04:00:59 +00005623 ExpandOp(Op, LHSL, LHSH);
5624
Dan Gohman475871a2008-07-27 21:46:04 +00005625 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005626 MVT VT = LHSL.getValueType();
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00005627 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005628 Hi = Lo.getValue(1);
5629}
5630
5631
Chris Lattnere34b3962005-01-19 04:19:40 +00005632/// ExpandShift - Try to find a clever way to expand this shift operation out to
5633/// smaller elements. If we can't find a way that is more efficient than a
5634/// libcall on this target, return false. Otherwise, return true with the
5635/// low-parts expanded into Lo and Hi.
Dan Gohman475871a2008-07-27 21:46:04 +00005636bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5637 SDValue &Lo, SDValue &Hi) {
Chris Lattnere34b3962005-01-19 04:19:40 +00005638 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5639 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005640
Duncan Sands83ec4b62008-06-06 12:08:01 +00005641 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman475871a2008-07-27 21:46:04 +00005642 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005643 MVT ShTy = ShAmt.getValueType();
5644 unsigned ShBits = ShTy.getSizeInBits();
5645 unsigned VTBits = Op.getValueType().getSizeInBits();
5646 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005647
Chris Lattner7a3c8552007-10-14 20:35:12 +00005648 // Handle the case when Amt is an immediate.
Gabor Greifba36cb52008-08-28 21:40:38 +00005649 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005650 unsigned Cst = CN->getZExtValue();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005651 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005652 SDValue InL, InH;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005653 ExpandOp(Op, InL, InH);
5654 switch(Opc) {
5655 case ISD::SHL:
5656 if (Cst > VTBits) {
5657 Lo = DAG.getConstant(0, NVT);
5658 Hi = DAG.getConstant(0, NVT);
5659 } else if (Cst > NVTBits) {
5660 Lo = DAG.getConstant(0, NVT);
5661 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005662 } else if (Cst == NVTBits) {
5663 Lo = DAG.getConstant(0, NVT);
5664 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005665 } else {
5666 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5667 Hi = DAG.getNode(ISD::OR, NVT,
5668 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5669 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5670 }
5671 return true;
5672 case ISD::SRL:
5673 if (Cst > VTBits) {
5674 Lo = DAG.getConstant(0, NVT);
5675 Hi = DAG.getConstant(0, NVT);
5676 } else if (Cst > NVTBits) {
5677 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5678 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005679 } else if (Cst == NVTBits) {
5680 Lo = InH;
5681 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005682 } else {
5683 Lo = DAG.getNode(ISD::OR, NVT,
5684 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5685 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5686 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5687 }
5688 return true;
5689 case ISD::SRA:
5690 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005691 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005692 DAG.getConstant(NVTBits-1, ShTy));
5693 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005694 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005695 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005696 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005697 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005698 } else if (Cst == NVTBits) {
5699 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005700 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005701 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005702 } else {
5703 Lo = DAG.getNode(ISD::OR, NVT,
5704 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5705 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5706 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5707 }
5708 return true;
5709 }
5710 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005711
5712 // Okay, the shift amount isn't constant. However, if we can tell that it is
5713 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005714 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5715 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005716 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005717
Dan Gohman9e255b72008-02-22 01:12:31 +00005718 // If we know that if any of the high bits of the shift amount are one, then
5719 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005720 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005721 // Mask out the high bit, which we know is set.
5722 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005723 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005724
5725 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005726 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005727 ExpandOp(Op, InL, InH);
5728 switch(Opc) {
5729 case ISD::SHL:
5730 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5731 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5732 return true;
5733 case ISD::SRL:
5734 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5735 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5736 return true;
5737 case ISD::SRA:
5738 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5739 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5740 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5741 return true;
5742 }
5743 }
5744
Dan Gohman9e255b72008-02-22 01:12:31 +00005745 // If we know that the high bits of the shift amount are all zero, then we can
5746 // do this as a couple of simple shifts.
5747 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005748 // Compute 32-amt.
Dan Gohman475871a2008-07-27 21:46:04 +00005749 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005750 DAG.getConstant(NVTBits, Amt.getValueType()),
5751 Amt);
5752
5753 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005754 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005755 ExpandOp(Op, InL, InH);
5756 switch(Opc) {
5757 case ISD::SHL:
5758 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5759 Hi = DAG.getNode(ISD::OR, NVT,
5760 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5761 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5762 return true;
5763 case ISD::SRL:
5764 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5765 Lo = DAG.getNode(ISD::OR, NVT,
5766 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5767 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5768 return true;
5769 case ISD::SRA:
5770 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5771 Lo = DAG.getNode(ISD::OR, NVT,
5772 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5773 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5774 return true;
5775 }
5776 }
5777
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005778 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005779}
Chris Lattner77e77a62005-01-21 06:05:23 +00005780
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005781
Chris Lattner77e77a62005-01-21 06:05:23 +00005782// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5783// does not fit into a register, return the lo part and set the hi part to the
5784// by-reg argument. If it does fit into a single register, return the result
5785// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00005786SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5787 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005788 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5789 // The input chain to this libcall is the entry node of the function.
5790 // Legalizing the call will automatically add the previous call to the
5791 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00005792 SDValue InChain = DAG.getEntryNode();
Chris Lattner6831a812006-02-13 09:18:02 +00005793
Chris Lattner77e77a62005-01-21 06:05:23 +00005794 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005795 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005796 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005797 MVT ArgVT = Node->getOperand(i).getValueType();
5798 const Type *ArgTy = ArgVT.getTypeForMVT();
Reid Spencer47857812006-12-31 05:55:36 +00005799 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005800 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005801 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005802 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005803 }
Bill Wendling056292f2008-09-16 21:48:12 +00005804 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00005805 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005806
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005807 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005808 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman475871a2008-07-27 21:46:04 +00005809 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005810 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5811 CallingConv::C, false, Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005812
Chris Lattner6831a812006-02-13 09:18:02 +00005813 // Legalize the call sequence, starting with the chain. This will advance
5814 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5815 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5816 LegalizeOp(CallInfo.second);
Dan Gohman475871a2008-07-27 21:46:04 +00005817 SDValue Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005818 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005819 default: assert(0 && "Unknown thing");
5820 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005821 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005822 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005823 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005824 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005825 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005826 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005827 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005828}
5829
Dan Gohman7f8613e2008-08-14 20:04:46 +00005830/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5831///
5832SDValue SelectionDAGLegalize::
5833LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5834 bool isCustom = false;
5835 SDValue Tmp1;
5836 switch (getTypeAction(Op.getValueType())) {
5837 case Legal:
5838 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5839 Op.getValueType())) {
5840 default: assert(0 && "Unknown operation action!");
5841 case TargetLowering::Custom:
5842 isCustom = true;
5843 // FALLTHROUGH
5844 case TargetLowering::Legal:
5845 Tmp1 = LegalizeOp(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00005846 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005847 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5848 else
5849 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5850 DestTy, Tmp1);
5851 if (isCustom) {
5852 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005853 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005854 }
5855 break;
5856 case TargetLowering::Expand:
5857 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5858 break;
5859 case TargetLowering::Promote:
5860 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5861 break;
5862 }
5863 break;
5864 case Expand:
5865 Result = ExpandIntToFP(isSigned, DestTy, Op);
5866 break;
5867 case Promote:
5868 Tmp1 = PromoteOp(Op);
5869 if (isSigned) {
5870 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5871 Tmp1, DAG.getValueType(Op.getValueType()));
5872 } else {
5873 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5874 Op.getValueType());
5875 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005876 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005877 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5878 else
5879 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5880 DestTy, Tmp1);
5881 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5882 break;
5883 }
5884 return Result;
5885}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005886
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005887/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5888///
Dan Gohman475871a2008-07-27 21:46:04 +00005889SDValue SelectionDAGLegalize::
5890ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005891 MVT SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005892 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005893
Dan Gohman7f8613e2008-08-14 20:04:46 +00005894 // Expand unsupported int-to-fp vector casts by unrolling them.
5895 if (DestTy.isVector()) {
5896 if (!ExpandSource)
5897 return LegalizeOp(UnrollVectorOp(Source));
5898 MVT DestEltTy = DestTy.getVectorElementType();
5899 if (DestTy.getVectorNumElements() == 1) {
5900 SDValue Scalar = ScalarizeVectorOp(Source);
5901 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5902 DestEltTy, Scalar);
5903 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5904 }
5905 SDValue Lo, Hi;
5906 SplitVectorOp(Source, Lo, Hi);
5907 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5908 DestTy.getVectorNumElements() / 2);
5909 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5910 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengefa53392008-10-13 18:46:18 +00005911 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5912 HiResult));
Dan Gohman7f8613e2008-08-14 20:04:46 +00005913 }
5914
Evan Cheng9845eb52008-04-01 02:18:22 +00005915 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5916 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005917 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005918 // incoming integer is set. To handle this, we dynamically test to see if
5919 // it is set, and, if so, add a fudge factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005920 SDValue Hi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005921 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005922 SDValue Lo;
Dan Gohman034f60e2008-03-11 01:59:03 +00005923 ExpandOp(Source, Lo, Hi);
5924 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5925 } else {
5926 // The comparison for the sign bit will use the entire operand.
5927 Hi = Source;
5928 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005929
Dale Johannesen53997b02008-11-04 20:52:49 +00005930 // Check to see if the target has a custom way to lower this. If so, use
5931 // it. (Note we've already expanded the operand in this case.)
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005932 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5933 default: assert(0 && "This action not implemented for this operation!");
5934 case TargetLowering::Legal:
5935 case TargetLowering::Expand:
5936 break; // This case is handled below.
5937 case TargetLowering::Custom: {
5938 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5939 Source), DAG);
5940 if (NV.getNode())
5941 return LegalizeOp(NV);
5942 break; // The target decided this was legal after all
5943 }
5944 }
5945
Chris Lattner66de05b2005-05-13 04:45:13 +00005946 // If this is unsigned, and not supported, first perform the conversion to
5947 // signed, then adjust the result if the sign bit is set.
Dan Gohman475871a2008-07-27 21:46:04 +00005948 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner66de05b2005-05-13 04:45:13 +00005949
Dan Gohman475871a2008-07-27 21:46:04 +00005950 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005951 DAG.getConstant(0, Hi.getValueType()),
5952 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005953 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5954 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattnere9c35e72005-04-13 05:09:42 +00005955 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005956 uint64_t FF = 0x5f800000ULL;
5957 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohman34bc1782008-03-05 02:07:31 +00005958 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005959
Dan Gohman475871a2008-07-27 21:46:04 +00005960 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005961 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattnere9c35e72005-04-13 05:09:42 +00005962 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005963 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005964 SDValue FudgeInReg;
Chris Lattnere9c35e72005-04-13 05:09:42 +00005965 if (DestTy == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005966 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005967 PseudoSourceValue::getConstantPool(), 0,
5968 false, Alignment);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005969 else if (DestTy.bitsGT(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005970 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005971 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005972 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005973 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005974 MVT::f32, false, Alignment);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005975 else
5976 assert(0 && "Unexpected conversion");
5977
Duncan Sands83ec4b62008-06-06 12:08:01 +00005978 MVT SCVT = SignedConv.getValueType();
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005979 if (SCVT != DestTy) {
5980 // Destination type needs to be expanded as well. The FADD now we are
5981 // constructing will be expanded into a libcall.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005982 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5983 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmand91446d2008-03-05 01:08:17 +00005984 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005985 SignedConv, SignedConv.getValue(1));
5986 }
5987 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5988 }
Chris Lattner473a9902005-09-29 06:44:39 +00005989 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005990 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005991
Chris Lattnera88a2602005-05-14 05:33:54 +00005992 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00005993 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00005994 default: assert(0 && "This action not implemented for this operation!");
5995 case TargetLowering::Legal:
5996 case TargetLowering::Expand:
5997 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005998 case TargetLowering::Custom: {
Dan Gohman475871a2008-07-27 21:46:04 +00005999 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Chris Lattner07dffd62005-08-26 00:14:16 +00006000 Source), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006001 if (NV.getNode())
Chris Lattner07dffd62005-08-26 00:14:16 +00006002 return LegalizeOp(NV);
6003 break; // The target decided this was legal after all
6004 }
Chris Lattnera88a2602005-05-14 05:33:54 +00006005 }
6006
Chris Lattner13689e22005-05-12 07:00:44 +00006007 // Expand the source, then glue it back together for the call. We must expand
6008 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00006009 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00006010 SDValue SrcLo, SrcHi;
Dan Gohman034f60e2008-03-11 01:59:03 +00006011 ExpandOp(Source, SrcLo, SrcHi);
6012 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
6013 }
Chris Lattner13689e22005-05-12 07:00:44 +00006014
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006015 RTLIB::Libcall LC = isSigned ?
6016 RTLIB::getSINTTOFP(SourceVT, DestTy) :
6017 RTLIB::getUINTTOFP(SourceVT, DestTy);
6018 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
6019
Chris Lattner6831a812006-02-13 09:18:02 +00006020 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman475871a2008-07-27 21:46:04 +00006021 SDValue HiPart;
Gabor Greifba36cb52008-08-28 21:40:38 +00006022 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
6023 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmana2e94852008-03-10 23:03:31 +00006024 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
6025 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00006026}
Misha Brukmanedf128a2005-04-21 22:36:52 +00006027
Chris Lattner22cde6a2006-01-28 08:25:58 +00006028/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
6029/// INT_TO_FP operation of the specified operand when the target requests that
6030/// we expand it. At this point, we know that the result and operand types are
6031/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00006032SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
6033 SDValue Op0,
6034 MVT DestVT) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006035 if (Op0.getValueType() == MVT::i32) {
6036 // simple 32-bit [signed|unsigned] integer to float/double expansion
6037
Chris Lattner23594d42008-01-16 07:03:22 +00006038 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00006039 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner23594d42008-01-16 07:03:22 +00006040
Chris Lattner22cde6a2006-01-28 08:25:58 +00006041 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00006042 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00006043 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00006044 SDValue Hi = StackSlot;
6045 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00006046 if (TLI.isLittleEndian())
6047 std::swap(Hi, Lo);
6048
Chris Lattner22cde6a2006-01-28 08:25:58 +00006049 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00006050 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006051 if (isSigned) {
6052 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00006053 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006054 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
6055 } else {
6056 Op0Mapped = Op0;
6057 }
6058 // store the lo of the constructed double - based on integer input
Dan Gohman475871a2008-07-27 21:46:04 +00006059 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00006060 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006061 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00006062 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006063 // store the hi of the constructed double - biased exponent
Dan Gohman475871a2008-07-27 21:46:04 +00006064 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006065 // load the constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00006066 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006067 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00006068 SDValue Bias = DAG.getConstantFP(isSigned ?
Chris Lattner22cde6a2006-01-28 08:25:58 +00006069 BitsToDouble(0x4330000080000000ULL)
6070 : BitsToDouble(0x4330000000000000ULL),
6071 MVT::f64);
6072 // subtract the bias
Dan Gohman475871a2008-07-27 21:46:04 +00006073 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006074 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00006075 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006076 // handle final rounding
6077 if (DestVT == MVT::f64) {
6078 // do nothing
6079 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006080 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00006081 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
6082 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00006083 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesen118cd9d2007-09-16 16:51:49 +00006084 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006085 }
6086 return Result;
6087 }
6088 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman475871a2008-07-27 21:46:04 +00006089 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006090
Dan Gohman475871a2008-07-27 21:46:04 +00006091 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Chris Lattner22cde6a2006-01-28 08:25:58 +00006092 DAG.getConstant(0, Op0.getValueType()),
6093 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00006094 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
6095 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006096 SignSet, Four, Zero);
6097
6098 // If the sign bit of the integer is set, the large number will be treated
6099 // as a negative number. To counteract this, the dynamic code adds an
6100 // offset depending on the data type.
6101 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006102 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006103 default: assert(0 && "Unsupported integer type!");
6104 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6105 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6106 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6107 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6108 }
6109 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00006110 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006111
Dan Gohman475871a2008-07-27 21:46:04 +00006112 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00006113 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006114 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00006115 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00006116 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006117 if (DestVT == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00006118 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00006119 PseudoSourceValue::getConstantPool(), 0,
6120 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006121 else {
Dan Gohman69de1932008-02-06 22:27:42 +00006122 FudgeInReg =
6123 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
6124 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00006125 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00006126 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006127 }
6128
6129 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
6130}
6131
6132/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6133/// *INT_TO_FP operation of the specified operand when the target requests that
6134/// we promote it. At this point, we know that the result and operand types are
6135/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6136/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00006137SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6138 MVT DestVT,
6139 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006140 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006141 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006142
6143 unsigned OpToUse = 0;
6144
6145 // Scan for the appropriate larger type to use.
6146 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006147 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6148 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006149
6150 // If the target supports SINT_TO_FP of this type, use it.
6151 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6152 default: break;
6153 case TargetLowering::Legal:
6154 if (!TLI.isTypeLegal(NewInTy))
6155 break; // Can't use this datatype.
6156 // FALL THROUGH.
6157 case TargetLowering::Custom:
6158 OpToUse = ISD::SINT_TO_FP;
6159 break;
6160 }
6161 if (OpToUse) break;
6162 if (isSigned) continue;
6163
6164 // If the target supports UINT_TO_FP of this type, use it.
6165 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6166 default: break;
6167 case TargetLowering::Legal:
6168 if (!TLI.isTypeLegal(NewInTy))
6169 break; // Can't use this datatype.
6170 // FALL THROUGH.
6171 case TargetLowering::Custom:
6172 OpToUse = ISD::UINT_TO_FP;
6173 break;
6174 }
6175 if (OpToUse) break;
6176
6177 // Otherwise, try a larger type.
6178 }
6179
6180 // Okay, we found the operation and type to use. Zero extend our input to the
6181 // desired type then run the operation on it.
6182 return DAG.getNode(OpToUse, DestVT,
6183 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
6184 NewInTy, LegalOp));
6185}
6186
6187/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6188/// FP_TO_*INT operation of the specified operand when the target requests that
6189/// we promote it. At this point, we know that the result and operand types are
6190/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6191/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00006192SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6193 MVT DestVT,
6194 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006195 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006196 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006197
6198 unsigned OpToUse = 0;
6199
6200 // Scan for the appropriate larger type to use.
6201 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006202 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6203 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006204
6205 // If the target supports FP_TO_SINT returning this type, use it.
6206 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6207 default: break;
6208 case TargetLowering::Legal:
6209 if (!TLI.isTypeLegal(NewOutTy))
6210 break; // Can't use this datatype.
6211 // FALL THROUGH.
6212 case TargetLowering::Custom:
6213 OpToUse = ISD::FP_TO_SINT;
6214 break;
6215 }
6216 if (OpToUse) break;
6217
6218 // If the target supports FP_TO_UINT of this type, use it.
6219 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6220 default: break;
6221 case TargetLowering::Legal:
6222 if (!TLI.isTypeLegal(NewOutTy))
6223 break; // Can't use this datatype.
6224 // FALL THROUGH.
6225 case TargetLowering::Custom:
6226 OpToUse = ISD::FP_TO_UINT;
6227 break;
6228 }
6229 if (OpToUse) break;
6230
6231 // Otherwise, try a larger type.
6232 }
6233
Chris Lattner27a6c732007-11-24 07:07:01 +00006234
6235 // Okay, we found the operation and type to use.
Dan Gohman475871a2008-07-27 21:46:04 +00006236 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00006237
Chris Lattner27a6c732007-11-24 07:07:01 +00006238 // If the operation produces an invalid type, it must be custom lowered. Use
6239 // the target lowering hooks to expand it. Just keep the low part of the
6240 // expanded operation, we know that we're truncating anyway.
6241 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00006242 SmallVector<SDValue, 2> Results;
6243 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6244 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6245 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00006246 }
Duncan Sands126d9072008-07-04 11:47:58 +00006247
Chris Lattner27a6c732007-11-24 07:07:01 +00006248 // Truncate the result of the extended FP_TO_*INT operation to the desired
6249 // size.
6250 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006251}
6252
6253/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6254///
Dan Gohman475871a2008-07-27 21:46:04 +00006255SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006256 MVT VT = Op.getValueType();
6257 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00006258 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006259 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006260 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6261 case MVT::i16:
6262 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6263 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6264 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6265 case MVT::i32:
6266 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6267 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6268 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6269 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6270 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6271 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6272 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6273 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6274 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6275 case MVT::i64:
6276 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6277 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6278 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6279 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6280 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6281 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6282 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6283 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6284 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6285 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6286 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6287 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6288 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6289 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6290 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6291 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6292 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6293 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6294 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6295 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6296 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6297 }
6298}
6299
6300/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6301///
Dan Gohman475871a2008-07-27 21:46:04 +00006302SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006303 switch (Opc) {
6304 default: assert(0 && "Cannot expand this yet!");
6305 case ISD::CTPOP: {
6306 static const uint64_t mask[6] = {
6307 0x5555555555555555ULL, 0x3333333333333333ULL,
6308 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6309 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6310 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00006311 MVT VT = Op.getValueType();
6312 MVT ShVT = TLI.getShiftAmountTy();
6313 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006314 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6315 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman475871a2008-07-27 21:46:04 +00006316 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6317 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006318 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6319 DAG.getNode(ISD::AND, VT,
6320 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6321 }
6322 return Op;
6323 }
6324 case ISD::CTLZ: {
6325 // for now, we do this:
6326 // x = x | (x >> 1);
6327 // x = x | (x >> 2);
6328 // ...
6329 // x = x | (x >>16);
6330 // x = x | (x >>32); // for 64-bit input
6331 // return popcount(~x);
6332 //
6333 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006334 MVT VT = Op.getValueType();
6335 MVT ShVT = TLI.getShiftAmountTy();
6336 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006337 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006338 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006339 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6340 }
6341 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6342 return DAG.getNode(ISD::CTPOP, VT, Op);
6343 }
6344 case ISD::CTTZ: {
6345 // for now, we use: { return popcount(~x & (x - 1)); }
6346 // unless the target has ctlz but not ctpop, in which case we use:
6347 // { return 32 - nlz(~x & (x-1)); }
6348 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006349 MVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00006350 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6351 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00006352 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6353 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6354 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6355 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6356 TLI.isOperationLegal(ISD::CTLZ, VT))
6357 return DAG.getNode(ISD::SUB, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006358 DAG.getConstant(VT.getSizeInBits(), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006359 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6360 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6361 }
6362 }
6363}
Chris Lattnere34b3962005-01-19 04:19:40 +00006364
Dan Gohman475871a2008-07-27 21:46:04 +00006365/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner3e928bb2005-01-07 07:47:09 +00006366/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman929d3eb2008-10-01 15:07:49 +00006367/// LegalizedNodes map is filled in for any results that are not expanded, the
Chris Lattner3e928bb2005-01-07 07:47:09 +00006368/// ExpandedNodes map is filled in for any results that are expanded, and the
6369/// Lo/Hi values are returned.
Dan Gohman475871a2008-07-27 21:46:04 +00006370void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands83ec4b62008-06-06 12:08:01 +00006371 MVT VT = Op.getValueType();
6372 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greifba36cb52008-08-28 21:40:38 +00006373 SDNode *Node = Op.getNode();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006374 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00006375 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands83ec4b62008-06-06 12:08:01 +00006376 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006377
Chris Lattner6fdcb252005-09-02 20:32:45 +00006378 // See if we already expanded it.
Dan Gohman475871a2008-07-27 21:46:04 +00006379 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00006380 = ExpandedNodes.find(Op);
6381 if (I != ExpandedNodes.end()) {
6382 Lo = I->second.first;
6383 Hi = I->second.second;
6384 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006385 }
6386
Chris Lattner3e928bb2005-01-07 07:47:09 +00006387 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006388 case ISD::CopyFromReg:
6389 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006390 case ISD::FP_ROUND_INREG:
6391 if (VT == MVT::ppcf128 &&
6392 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6393 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006394 SDValue SrcLo, SrcHi, Src;
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006395 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6396 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman475871a2008-07-27 21:46:04 +00006397 SDValue Result = TLI.LowerOperation(
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006398 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006399 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6400 Lo = Result.getNode()->getOperand(0);
6401 Hi = Result.getNode()->getOperand(1);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006402 break;
6403 }
6404 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00006405 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006406#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006407 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006408#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00006409 assert(0 && "Do not know how to expand this operator!");
6410 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00006411 case ISD::EXTRACT_ELEMENT:
6412 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006413 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman1953ecb2008-02-27 01:52:30 +00006414 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00006415 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00006416 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen25f1d082007-10-31 00:32:36 +00006417 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6418 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6419 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006420 case ISD::UNDEF:
6421 Lo = DAG.getNode(ISD::UNDEF, NVT);
6422 Hi = DAG.getNode(ISD::UNDEF, NVT);
6423 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006424 case ISD::Constant: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006425 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman050f5502008-03-03 22:20:46 +00006426 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6427 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6428 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006429 break;
6430 }
Evan Cheng00495212006-12-12 21:32:44 +00006431 case ISD::ConstantFP: {
6432 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00006433 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen7111b022008-10-09 18:53:47 +00006434 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesena471c2e2007-10-11 18:07:22 +00006435 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6436 MVT::f64);
6437 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6438 MVT::f64);
6439 break;
6440 }
Evan Cheng279101e2006-12-12 22:19:28 +00006441 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00006442 if (getTypeAction(Lo.getValueType()) == Expand)
6443 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006444 break;
6445 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006446 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006447 // Return the operands.
6448 Lo = Node->getOperand(0);
6449 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006450 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006451
6452 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00006453 if (Node->getNumValues() == 1) {
6454 ExpandOp(Op.getOperand(0), Lo, Hi);
6455 break;
6456 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006457 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif99a6cb92008-08-26 22:36:50 +00006458 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattner27a6c732007-11-24 07:07:01 +00006459 Op.getValue(1).getValueType() == MVT::Other &&
6460 "unhandled MERGE_VALUES");
6461 ExpandOp(Op.getOperand(0), Lo, Hi);
6462 // Remember that we legalized the chain.
6463 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6464 break;
Chris Lattner58f79632005-12-12 22:27:43 +00006465
6466 case ISD::SIGN_EXTEND_INREG:
6467 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00006468 // sext_inreg the low part if needed.
6469 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6470
6471 // The high part gets the sign extension from the lo-part. This handles
6472 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00006473 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006474 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattner58f79632005-12-12 22:27:43 +00006475 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00006476 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006477
Nate Begemand88fc032006-01-14 03:14:10 +00006478 case ISD::BSWAP: {
6479 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006480 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Nate Begemand88fc032006-01-14 03:14:10 +00006481 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6482 Lo = TempLo;
6483 break;
6484 }
6485
Chris Lattneredb1add2005-05-11 04:51:16 +00006486 case ISD::CTPOP:
6487 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00006488 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6489 DAG.getNode(ISD::CTPOP, NVT, Lo),
6490 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00006491 Hi = DAG.getConstant(0, NVT);
6492 break;
6493
Chris Lattner39a8f332005-05-12 19:05:01 +00006494 case ISD::CTLZ: {
6495 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006496 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006497 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6498 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
6499 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00006500 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006501 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Chris Lattner39a8f332005-05-12 19:05:01 +00006502 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6503
6504 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6505 Hi = DAG.getConstant(0, NVT);
6506 break;
6507 }
6508
6509 case ISD::CTTZ: {
6510 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006511 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006512 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6513 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
6514 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00006515 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006516 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00006517 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6518
6519 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6520 Hi = DAG.getConstant(0, NVT);
6521 break;
6522 }
Chris Lattneredb1add2005-05-11 04:51:16 +00006523
Nate Begemanacc398c2006-01-25 18:21:52 +00006524 case ISD::VAARG: {
Dan Gohman475871a2008-07-27 21:46:04 +00006525 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6526 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00006527 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6528 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6529
6530 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00006531 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00006532 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00006533 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00006534 std::swap(Lo, Hi);
6535 break;
6536 }
6537
Chris Lattner3e928bb2005-01-07 07:47:09 +00006538 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00006539 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00006540 SDValue Ch = LD->getChain(); // Legalize the chain.
6541 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Cheng466685d2006-10-09 20:57:25 +00006542 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f8613e2008-08-14 20:04:46 +00006543 const Value *SV = LD->getSrcValue();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006544 int SVOffset = LD->getSrcValueOffset();
6545 unsigned Alignment = LD->getAlignment();
6546 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006547
Evan Cheng466685d2006-10-09 20:57:25 +00006548 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00006549 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006550 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00006551 if (VT == MVT::f32 || VT == MVT::f64) {
6552 // f32->i32 or f64->i64 one to one expansion.
6553 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006554 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00006555 // Recursively expand the new load.
6556 if (getTypeAction(NVT) == Expand)
6557 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006558 break;
6559 }
Chris Lattnerec39a452005-01-19 18:02:17 +00006560
Evan Cheng466685d2006-10-09 20:57:25 +00006561 // Increment the pointer to the other half.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006562 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Evan Cheng466685d2006-10-09 20:57:25 +00006563 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006564 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006565 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006566 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00006567 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006568 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00006569
Evan Cheng466685d2006-10-09 20:57:25 +00006570 // Build a factor node to remember that this load is independent of the
6571 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00006572 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Evan Cheng466685d2006-10-09 20:57:25 +00006573 Hi.getValue(1));
6574
6575 // Remember that we legalized the chain.
6576 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00006577 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00006578 std::swap(Lo, Hi);
6579 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006580 MVT EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00006581
Dale Johannesenb6210fc2007-10-19 20:29:00 +00006582 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6583 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00006584 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman7f8613e2008-08-14 20:04:46 +00006585 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006586 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00006587 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006588 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Evan Cheng548f6112006-12-13 03:19:57 +00006589 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6590 break;
6591 }
Evan Cheng466685d2006-10-09 20:57:25 +00006592
6593 if (EVT == NVT)
Dan Gohman7f8613e2008-08-14 20:04:46 +00006594 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006595 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006596 else
Dan Gohman7f8613e2008-08-14 20:04:46 +00006597 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006598 SVOffset, EVT, isVolatile,
6599 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006600
6601 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006602 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng466685d2006-10-09 20:57:25 +00006603
6604 if (ExtType == ISD::SEXTLOAD) {
6605 // The high part is obtained by SRA'ing all but one of the bits of the
6606 // lo part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006607 unsigned LoSize = Lo.getValueType().getSizeInBits();
Evan Cheng466685d2006-10-09 20:57:25 +00006608 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6609 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6610 } else if (ExtType == ISD::ZEXTLOAD) {
6611 // The high part is just a zero.
6612 Hi = DAG.getConstant(0, NVT);
6613 } else /* if (ExtType == ISD::EXTLOAD) */ {
6614 // The high part is undefined.
6615 Hi = DAG.getNode(ISD::UNDEF, NVT);
6616 }
6617 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006618 break;
6619 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006620 case ISD::AND:
6621 case ISD::OR:
6622 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman475871a2008-07-27 21:46:04 +00006623 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006624 ExpandOp(Node->getOperand(0), LL, LH);
6625 ExpandOp(Node->getOperand(1), RL, RH);
6626 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6627 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6628 break;
6629 }
6630 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006631 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006632 ExpandOp(Node->getOperand(1), LL, LH);
6633 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00006634 if (getTypeAction(NVT) == Expand)
6635 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00006636 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00006637 if (VT != MVT::f32)
6638 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006639 break;
6640 }
Nate Begeman9373a812005-08-10 20:51:12 +00006641 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00006642 SDValue TL, TH, FL, FH;
Nate Begeman9373a812005-08-10 20:51:12 +00006643 ExpandOp(Node->getOperand(2), TL, TH);
6644 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00006645 if (getTypeAction(NVT) == Expand)
6646 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00006647 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6648 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00006649 if (VT != MVT::f32)
6650 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6651 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006652 break;
6653 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006654 case ISD::ANY_EXTEND:
6655 // The low part is any extension of the input (which degenerates to a copy).
6656 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6657 // The high part is undefined.
6658 Hi = DAG.getNode(ISD::UNDEF, NVT);
6659 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006660 case ISD::SIGN_EXTEND: {
6661 // The low part is just a sign extension of the input (which degenerates to
6662 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006663 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006664
Chris Lattner3e928bb2005-01-07 07:47:09 +00006665 // The high part is obtained by SRA'ing all but one of the bits of the lo
6666 // part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006667 unsigned LoSize = Lo.getValueType().getSizeInBits();
Chris Lattner8137c9e2006-01-28 05:07:51 +00006668 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6669 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006670 break;
6671 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006672 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006673 // The low part is just a zero extension of the input (which degenerates to
6674 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006675 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006676
Chris Lattner3e928bb2005-01-07 07:47:09 +00006677 // The high part is just a zero.
6678 Hi = DAG.getConstant(0, NVT);
6679 break;
Chris Lattner35481892005-12-23 00:16:34 +00006680
Chris Lattner4c948eb2007-02-13 23:55:16 +00006681 case ISD::TRUNCATE: {
6682 // The input value must be larger than this value. Expand *it*.
Dan Gohman475871a2008-07-27 21:46:04 +00006683 SDValue NewLo;
Chris Lattner4c948eb2007-02-13 23:55:16 +00006684 ExpandOp(Node->getOperand(0), NewLo, Hi);
6685
6686 // The low part is now either the right size, or it is closer. If not the
6687 // right size, make an illegal truncate so we recursively expand it.
6688 if (NewLo.getValueType() != Node->getValueType(0))
6689 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6690 ExpandOp(NewLo, Lo, Hi);
6691 break;
6692 }
6693
Chris Lattner35481892005-12-23 00:16:34 +00006694 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006695 SDValue Tmp;
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006696 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6697 // If the target wants to, allow it to lower this itself.
6698 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6699 case Expand: assert(0 && "cannot expand FP!");
6700 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6701 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6702 }
6703 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6704 }
6705
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006706 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006707 if (VT == MVT::f32 || VT == MVT::f64) {
6708 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006709 if (getTypeAction(NVT) == Expand)
6710 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006711 break;
6712 }
6713
6714 // If source operand will be expanded to the same type as VT, i.e.
6715 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006716 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006717 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6718 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006719 break;
6720 }
6721
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006722 // Turn this into a load/store pair by default.
Gabor Greifba36cb52008-08-28 21:40:38 +00006723 if (Tmp.getNode() == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00006724 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006725
Chris Lattner35481892005-12-23 00:16:34 +00006726 ExpandOp(Tmp, Lo, Hi);
6727 break;
6728 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006729
Chris Lattner27a6c732007-11-24 07:07:01 +00006730 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006731 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6732 TargetLowering::Custom &&
6733 "Must custom expand ReadCycleCounter");
Dan Gohman475871a2008-07-27 21:46:04 +00006734 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006735 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattner27a6c732007-11-24 07:07:01 +00006736 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006737 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006738 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006739 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006740 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006741
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006742 case ISD::ATOMIC_CMP_SWAP_64: {
6743 // This operation does not need a loop.
6744 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6745 assert(Tmp.getNode() && "Node must be custom expanded!");
6746 ExpandOp(Tmp.getValue(0), Lo, Hi);
6747 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6748 LegalizeOp(Tmp.getValue(1)));
6749 break;
6750 }
6751
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006752 case ISD::ATOMIC_LOAD_ADD_64:
6753 case ISD::ATOMIC_LOAD_SUB_64:
6754 case ISD::ATOMIC_LOAD_AND_64:
6755 case ISD::ATOMIC_LOAD_OR_64:
6756 case ISD::ATOMIC_LOAD_XOR_64:
6757 case ISD::ATOMIC_LOAD_NAND_64:
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006758 case ISD::ATOMIC_SWAP_64: {
6759 // These operations require a loop to be generated. We can't do that yet,
6760 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006761 SDValue In2Lo, In2Hi, In2;
6762 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6763 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006764 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6765 SDValue Replace =
6766 DAG.getAtomic(Op.getOpcode(), Op.getOperand(0), Op.getOperand(1), In2,
6767 Anode->getSrcValue(), Anode->getAlignment());
6768 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006769 ExpandOp(Result.getValue(0), Lo, Hi);
6770 // Remember that we legalized the chain.
6771 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006772 break;
6773 }
6774
Chris Lattner4e6c7462005-01-08 19:27:05 +00006775 // These operators cannot be expanded directly, emit them as calls to
6776 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006777 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006778 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006779 SDValue Op;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006780 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6781 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006782 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6783 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006784 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006785
Chris Lattnerf20d1832005-07-30 01:40:57 +00006786 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6787
Chris Lattner80a3e942005-07-29 00:33:32 +00006788 // Now that the custom expander is done, expand the result, which is still
6789 // VT.
Gabor Greifba36cb52008-08-28 21:40:38 +00006790 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006791 ExpandOp(Op, Lo, Hi);
6792 break;
6793 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006794 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006795
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006796 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6797 VT);
6798 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6799 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006800 break;
Evan Cheng56966222007-01-12 02:11:51 +00006801 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006802
Evan Cheng56966222007-01-12 02:11:51 +00006803 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006804 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006805 SDValue Op;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006806 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6807 case Expand: assert(0 && "cannot expand FP!");
6808 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6809 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6810 }
6811
6812 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6813
6814 // Now that the custom expander is done, expand the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00006815 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006816 ExpandOp(Op, Lo, Hi);
6817 break;
6818 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006819 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006820
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006821 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6822 VT);
6823 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6824 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006825 break;
Evan Cheng56966222007-01-12 02:11:51 +00006826 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006827
Evan Cheng05a2d562006-01-09 18:31:59 +00006828 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006829 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006830 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006831 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006832 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006833 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006834 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006835 // Now that the custom expander is done, expand the result, which is
6836 // still VT.
6837 ExpandOp(Op, Lo, Hi);
6838 break;
6839 }
6840 }
6841
Chris Lattner79980b02006-09-13 03:50:39 +00006842 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6843 // this X << 1 as X+X.
6844 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00006845 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Chris Lattner79980b02006-09-13 03:50:39 +00006846 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006847 SDValue LoOps[2], HiOps[3];
Chris Lattner79980b02006-09-13 03:50:39 +00006848 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6849 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6850 LoOps[1] = LoOps[0];
6851 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6852
6853 HiOps[1] = HiOps[0];
6854 HiOps[2] = Lo.getValue(1);
6855 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6856 break;
6857 }
6858 }
6859
Chris Lattnere34b3962005-01-19 04:19:40 +00006860 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006861 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006862 break;
Chris Lattner47599822005-04-02 03:38:53 +00006863
6864 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006865 TargetLowering::LegalizeAction Action =
6866 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6867 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6868 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006869 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006870 break;
6871 }
6872
Chris Lattnere34b3962005-01-19 04:19:40 +00006873 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006874 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006875 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006876 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006877
Evan Cheng05a2d562006-01-09 18:31:59 +00006878 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006879 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006880 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006881 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006882 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006883 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006884 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006885 // Now that the custom expander is done, expand the result, which is
6886 // still VT.
6887 ExpandOp(Op, Lo, Hi);
6888 break;
6889 }
6890 }
6891
Chris Lattnere34b3962005-01-19 04:19:40 +00006892 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006893 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006894 break;
Chris Lattner47599822005-04-02 03:38:53 +00006895
6896 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006897 TargetLowering::LegalizeAction Action =
6898 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6899 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6900 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006901 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006902 break;
6903 }
6904
Chris Lattnere34b3962005-01-19 04:19:40 +00006905 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006906 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006907 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006908 }
6909
6910 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006911 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006912 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006913 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006914 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006915 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006916 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006917 // Now that the custom expander is done, expand the result, which is
6918 // still VT.
6919 ExpandOp(Op, Lo, Hi);
6920 break;
6921 }
6922 }
6923
Chris Lattnere34b3962005-01-19 04:19:40 +00006924 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006925 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006926 break;
Chris Lattner47599822005-04-02 03:38:53 +00006927
6928 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006929 TargetLowering::LegalizeAction Action =
6930 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6931 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6932 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006933 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006934 break;
6935 }
6936
Chris Lattnere34b3962005-01-19 04:19:40 +00006937 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006938 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006939 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006940 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006941
Misha Brukmanedf128a2005-04-21 22:36:52 +00006942 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006943 case ISD::SUB: {
6944 // If the target wants to custom expand this, let them.
6945 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6946 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006947 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006948 if (Result.getNode()) {
Duncan Sands69bfb152008-06-22 09:42:16 +00006949 ExpandOp(Result, Lo, Hi);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006950 break;
6951 }
6952 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006953 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006954 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006955 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6956 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman475871a2008-07-27 21:46:04 +00006957 SDValue LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006958 LoOps[0] = LHSL;
6959 LoOps[1] = RHSL;
6960 HiOps[0] = LHSH;
6961 HiOps[1] = RHSH;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006962
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006963 //cascaded check to see if any smaller size has a a carry flag.
6964 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6965 bool hasCarry = false;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006966 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6967 MVT AVT = MVT::getIntegerVT(BitSize);
6968 if (TLI.isOperationLegal(OpV, AVT)) {
6969 hasCarry = true;
6970 break;
6971 }
6972 }
6973
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006974 if(hasCarry) {
Evan Cheng637ed032008-12-12 18:49:09 +00006975 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006976 if (Node->getOpcode() == ISD::ADD) {
6977 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6978 HiOps[2] = Lo.getValue(1);
6979 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6980 } else {
6981 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6982 HiOps[2] = Lo.getValue(1);
6983 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6984 }
6985 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006986 } else {
Andrew Lenharth40d51392008-10-07 14:15:42 +00006987 if (Node->getOpcode() == ISD::ADD) {
Evan Cheng637ed032008-12-12 18:49:09 +00006988 Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
6989 Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006990 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6991 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006992 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6993 DAG.getConstant(1, NVT),
6994 DAG.getConstant(0, NVT));
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006995 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6996 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006997 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6998 DAG.getConstant(1, NVT),
6999 Carry1);
7000 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
7001 } else {
Evan Cheng637ed032008-12-12 18:49:09 +00007002 Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
7003 Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007004 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
7005 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
7006 DAG.getConstant(1, NVT),
7007 DAG.getConstant(0, NVT));
7008 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
7009 }
7010 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00007011 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00007012 }
Chris Lattnerb429f732007-05-17 18:15:41 +00007013
7014 case ISD::ADDC:
7015 case ISD::SUBC: {
7016 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00007017 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00007018 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7019 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7020 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00007021 SDValue LoOps[2] = { LHSL, RHSL };
7022 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00007023
7024 if (Node->getOpcode() == ISD::ADDC) {
7025 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
7026 HiOps[2] = Lo.getValue(1);
7027 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
7028 } else {
7029 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
7030 HiOps[2] = Lo.getValue(1);
7031 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
7032 }
7033 // Remember that we legalized the flag.
7034 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7035 break;
7036 }
7037 case ISD::ADDE:
7038 case ISD::SUBE: {
7039 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00007040 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00007041 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7042 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7043 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00007044 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
7045 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00007046
7047 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
7048 HiOps[2] = Lo.getValue(1);
7049 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
7050
7051 // Remember that we legalized the flag.
7052 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7053 break;
7054 }
Nate Begemanc7c16572005-04-11 03:01:51 +00007055 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00007056 // If the target wants to custom expand this, let them.
7057 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00007058 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007059 if (New.getNode()) {
Chris Lattner8829dc82006-09-16 05:08:34 +00007060 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00007061 break;
7062 }
7063 }
7064
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00007065 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
7066 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00007067 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
7068 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
7069 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman475871a2008-07-27 21:46:04 +00007070 SDValue LL, LH, RL, RH;
Nate Begemanc7c16572005-04-11 03:01:51 +00007071 ExpandOp(Node->getOperand(0), LL, LH);
7072 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007073 unsigned OuterBitSize = Op.getValueSizeInBits();
7074 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00007075 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
7076 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00007077 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
7078 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
7079 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00007080 // The inputs are both zero-extended.
7081 if (HasUMUL_LOHI) {
7082 // We can emit a umul_lohi.
7083 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00007084 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00007085 break;
7086 }
7087 if (HasMULHU) {
7088 // We can emit a mulhu+mul.
7089 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7090 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7091 break;
7092 }
Dan Gohman525178c2007-10-08 18:33:35 +00007093 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007094 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00007095 // The input values are both sign-extended.
7096 if (HasSMUL_LOHI) {
7097 // We can emit a smul_lohi.
7098 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00007099 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00007100 break;
7101 }
7102 if (HasMULHS) {
7103 // We can emit a mulhs+mul.
7104 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7105 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
7106 break;
7107 }
7108 }
7109 if (HasUMUL_LOHI) {
7110 // Lo,Hi = umul LHS, RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00007111 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman525178c2007-10-08 18:33:35 +00007112 DAG.getVTList(NVT, NVT), LL, RL);
7113 Lo = UMulLOHI;
7114 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00007115 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7116 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7117 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7118 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00007119 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00007120 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00007121 if (HasMULHU) {
7122 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7123 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7124 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7125 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7126 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7127 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7128 break;
7129 }
Nate Begemanc7c16572005-04-11 03:01:51 +00007130 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00007131
Dan Gohman525178c2007-10-08 18:33:35 +00007132 // If nothing else, we can make a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00007133 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00007134 break;
7135 }
Evan Cheng56966222007-01-12 02:11:51 +00007136 case ISD::SDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007137 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007138 break;
7139 case ISD::UDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007140 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007141 break;
7142 case ISD::SREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007143 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007144 break;
7145 case ISD::UREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007146 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007147 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00007148
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007149 case ISD::FADD:
Duncan Sands460a14e2008-04-12 17:14:18 +00007150 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7151 RTLIB::ADD_F64,
7152 RTLIB::ADD_F80,
7153 RTLIB::ADD_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007154 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007155 break;
7156 case ISD::FSUB:
Duncan Sands460a14e2008-04-12 17:14:18 +00007157 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7158 RTLIB::SUB_F64,
7159 RTLIB::SUB_F80,
7160 RTLIB::SUB_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007161 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007162 break;
7163 case ISD::FMUL:
Duncan Sands460a14e2008-04-12 17:14:18 +00007164 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7165 RTLIB::MUL_F64,
7166 RTLIB::MUL_F80,
7167 RTLIB::MUL_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007168 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007169 break;
7170 case ISD::FDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007171 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7172 RTLIB::DIV_F64,
7173 RTLIB::DIV_F80,
7174 RTLIB::DIV_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007175 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007176 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007177 case ISD::FP_EXTEND: {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007178 if (VT == MVT::ppcf128) {
7179 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7180 Node->getOperand(0).getValueType()==MVT::f64);
7181 const uint64_t zero = 0;
7182 if (Node->getOperand(0).getValueType()==MVT::f32)
7183 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
7184 else
7185 Hi = Node->getOperand(0);
7186 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7187 break;
7188 }
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007189 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7190 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7191 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007192 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007193 }
7194 case ISD::FP_ROUND: {
7195 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7196 VT);
7197 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7198 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007199 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007200 }
Evan Cheng4b887022008-09-09 23:02:14 +00007201 case ISD::FSQRT:
7202 case ISD::FSIN:
7203 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007204 case ISD::FLOG:
7205 case ISD::FLOG2:
7206 case ISD::FLOG10:
7207 case ISD::FEXP:
7208 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007209 case ISD::FTRUNC:
7210 case ISD::FFLOOR:
7211 case ISD::FCEIL:
7212 case ISD::FRINT:
7213 case ISD::FNEARBYINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00007214 case ISD::FPOW:
7215 case ISD::FPOWI: {
Evan Cheng56966222007-01-12 02:11:51 +00007216 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007217 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00007218 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00007219 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7220 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007221 break;
7222 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00007223 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7224 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007225 break;
7226 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00007227 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7228 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007229 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007230 case ISD::FLOG:
7231 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7232 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7233 break;
7234 case ISD::FLOG2:
7235 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7236 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7237 break;
7238 case ISD::FLOG10:
7239 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7240 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7241 break;
7242 case ISD::FEXP:
7243 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7244 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7245 break;
7246 case ISD::FEXP2:
7247 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7248 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7249 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007250 case ISD::FTRUNC:
7251 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7252 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7253 break;
7254 case ISD::FFLOOR:
7255 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7256 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7257 break;
7258 case ISD::FCEIL:
7259 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7260 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7261 break;
7262 case ISD::FRINT:
7263 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7264 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7265 break;
7266 case ISD::FNEARBYINT:
7267 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7268 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7269 break;
Evan Cheng4b887022008-09-09 23:02:14 +00007270 case ISD::FPOW:
7271 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7272 RTLIB::POW_PPCF128);
7273 break;
7274 case ISD::FPOWI:
7275 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7276 RTLIB::POWI_PPCF128);
7277 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007278 default: assert(0 && "Unreachable!");
7279 }
Duncan Sands460a14e2008-04-12 17:14:18 +00007280 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00007281 break;
7282 }
Evan Cheng966bf242006-12-16 00:52:40 +00007283 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007284 if (VT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00007285 SDValue Tmp;
Dale Johannesenf6467742007-10-12 19:02:17 +00007286 ExpandOp(Node->getOperand(0), Lo, Tmp);
7287 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7288 // lo = hi==fabs(hi) ? lo : -lo;
7289 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7290 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7291 DAG.getCondCode(ISD::SETEQ));
7292 break;
7293 }
Dan Gohman475871a2008-07-27 21:46:04 +00007294 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007295 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7296 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7297 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7298 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7299 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7300 if (getTypeAction(NVT) == Expand)
7301 ExpandOp(Lo, Lo, Hi);
7302 break;
7303 }
7304 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007305 if (VT == MVT::ppcf128) {
7306 ExpandOp(Node->getOperand(0), Lo, Hi);
7307 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7308 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7309 break;
7310 }
Dan Gohman475871a2008-07-27 21:46:04 +00007311 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007312 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7313 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7314 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7315 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7316 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7317 if (getTypeAction(NVT) == Expand)
7318 ExpandOp(Lo, Lo, Hi);
7319 break;
7320 }
Evan Cheng912095b2007-01-04 21:56:39 +00007321 case ISD::FCOPYSIGN: {
7322 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7323 if (getTypeAction(NVT) == Expand)
7324 ExpandOp(Lo, Lo, Hi);
7325 break;
7326 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007327 case ISD::SINT_TO_FP:
7328 case ISD::UINT_TO_FP: {
7329 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007330 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00007331
7332 // Promote the operand if needed. Do this before checking for
7333 // ppcf128 so conversions of i16 and i8 work.
7334 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman475871a2008-07-27 21:46:04 +00007335 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesencf498192008-03-18 17:28:38 +00007336 Tmp = isSigned
7337 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7338 DAG.getValueType(SrcVT))
7339 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greifba36cb52008-08-28 21:40:38 +00007340 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesencf498192008-03-18 17:28:38 +00007341 SrcVT = Node->getOperand(0).getValueType();
7342 }
7343
Dan Gohmana2e94852008-03-10 23:03:31 +00007344 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007345 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007346 if (isSigned) {
7347 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7348 Node->getOperand(0)));
7349 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7350 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007351 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007352 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7353 Node->getOperand(0)));
7354 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7355 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00007356 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007357 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7358 DAG.getConstant(0, MVT::i32),
7359 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7360 DAG.getConstantFP(
7361 APFloat(APInt(128, 2, TwoE32)),
7362 MVT::ppcf128)),
7363 Hi,
7364 DAG.getCondCode(ISD::SETLT)),
7365 Lo, Hi);
7366 }
7367 break;
7368 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00007369 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7370 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007371 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen6e63e092007-10-12 17:52:03 +00007372 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7373 Lo, Hi);
7374 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7375 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7376 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7377 DAG.getConstant(0, MVT::i64),
7378 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7379 DAG.getConstantFP(
7380 APFloat(APInt(128, 2, TwoE64)),
7381 MVT::ppcf128)),
7382 Hi,
7383 DAG.getCondCode(ISD::SETLT)),
7384 Lo, Hi);
7385 break;
7386 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007387
Dan Gohmana2e94852008-03-10 23:03:31 +00007388 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7389 Node->getOperand(0));
Evan Cheng110cf482008-04-01 01:50:16 +00007390 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng6ad2f932008-04-01 01:51:26 +00007391 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng110cf482008-04-01 01:50:16 +00007392 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00007393 break;
7394 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00007395 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00007396
Chris Lattner83397362005-12-21 18:02:52 +00007397 // Make sure the resultant values have been legalized themselves, unless this
7398 // is a type that requires multi-step expansion.
7399 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7400 Lo = LegalizeOp(Lo);
Gabor Greifba36cb52008-08-28 21:40:38 +00007401 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00007402 // Don't legalize the high part if it is expanded to a single node.
7403 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00007404 }
Evan Cheng05a2d562006-01-09 18:31:59 +00007405
7406 // Remember in a map if the values will be reused later.
Dan Gohman6b345ee2008-07-07 17:46:23 +00007407 bool isNew =
7408 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng05a2d562006-01-09 18:31:59 +00007409 assert(isNew && "Value already expanded?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007410 isNew = isNew;
Chris Lattner3e928bb2005-01-07 07:47:09 +00007411}
7412
Dan Gohman7f321562007-06-25 16:23:39 +00007413/// SplitVectorOp - Given an operand of vector type, break it down into
7414/// two smaller values, still of vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00007415void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7416 SDValue &Hi) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007417 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007418 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007419 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattnerc7029802006-03-18 01:44:44 +00007420 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00007421
Duncan Sands83ec4b62008-06-06 12:08:01 +00007422 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman5db1afb2007-11-15 21:15:26 +00007423
7424 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7425 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7426
Duncan Sands83ec4b62008-06-06 12:08:01 +00007427 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7428 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007429
Chris Lattnerc7029802006-03-18 01:44:44 +00007430 // See if we already split it.
Dan Gohman475871a2008-07-27 21:46:04 +00007431 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattnerc7029802006-03-18 01:44:44 +00007432 = SplitNodes.find(Op);
7433 if (I != SplitNodes.end()) {
7434 Lo = I->second.first;
7435 Hi = I->second.second;
7436 return;
7437 }
7438
7439 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007440 default:
7441#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007442 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007443#endif
7444 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007445 case ISD::UNDEF:
7446 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7447 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7448 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007449 case ISD::BUILD_PAIR:
7450 Lo = Node->getOperand(0);
7451 Hi = Node->getOperand(1);
7452 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00007453 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman68679912008-04-25 18:07:40 +00007454 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7455 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007456 unsigned Index = Idx->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007457 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman68679912008-04-25 18:07:40 +00007458 if (Index < NewNumElts_Lo)
7459 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7460 DAG.getIntPtrConstant(Index));
7461 else
7462 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7463 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7464 break;
7465 }
Dan Gohman475871a2008-07-27 21:46:04 +00007466 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman68679912008-04-25 18:07:40 +00007467 Node->getOperand(1),
7468 Node->getOperand(2));
7469 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohman9fe46622007-09-28 23:53:40 +00007470 break;
7471 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00007472 case ISD::VECTOR_SHUFFLE: {
7473 // Build the low part.
Dan Gohman475871a2008-07-27 21:46:04 +00007474 SDValue Mask = Node->getOperand(2);
7475 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007476 MVT PtrVT = TLI.getPointerTy();
Chris Lattner6c9c6802007-11-19 21:16:54 +00007477
7478 // Insert all of the elements from the input that are needed. We use
7479 // buildvector of extractelement here because the input vectors will have
7480 // to be legalized, so this makes the code simpler.
7481 for (unsigned i = 0; i != NewNumElts_Lo; ++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 }
7496 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7497 Ops.clear();
7498
7499 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007500 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007501 if (IdxNode.getOpcode() == ISD::UNDEF) {
7502 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7503 continue;
7504 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007505 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007506 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007507 if (Idx >= NumElements) {
7508 InVec = Node->getOperand(1);
7509 Idx -= NumElements;
7510 }
7511 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7512 DAG.getConstant(Idx, PtrVT)));
7513 }
Mon P Wang92879f32008-07-25 01:30:26 +00007514 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007515 break;
7516 }
Dan Gohman7f321562007-06-25 16:23:39 +00007517 case ISD::BUILD_VECTOR: {
Dan Gohman475871a2008-07-27 21:46:04 +00007518 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00007519 Node->op_begin()+NewNumElts_Lo);
7520 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007521
Dan Gohman475871a2008-07-27 21:46:04 +00007522 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00007523 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007524 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007525 break;
7526 }
Dan Gohman7f321562007-06-25 16:23:39 +00007527 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00007528 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00007529 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7530 if (NewNumSubvectors == 1) {
7531 Lo = Node->getOperand(0);
7532 Hi = Node->getOperand(1);
7533 } else {
Mon P Wangaeb06d22008-11-10 04:46:22 +00007534 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7535 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007536 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00007537
Mon P Wangaeb06d22008-11-10 04:46:22 +00007538 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohman7f321562007-06-25 16:23:39 +00007539 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007540 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00007541 }
Dan Gohman65956352007-06-13 15:12:02 +00007542 break;
7543 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00007544 case ISD::EXTRACT_SUBVECTOR: {
7545 SDValue Vec = Op.getOperand(0);
7546 SDValue Idx = Op.getOperand(1);
7547 MVT IdxVT = Idx.getValueType();
7548
7549 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7550 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7551 if (CIdx) {
7552 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7553 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7554 IdxVT));
7555 } else {
7556 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7557 DAG.getConstant(NewNumElts_Lo, IdxVT));
7558 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7559 }
7560 break;
7561 }
Dan Gohmanc6230962007-10-17 14:48:28 +00007562 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007563 SDValue Cond = Node->getOperand(0);
Dan Gohmanc6230962007-10-17 14:48:28 +00007564
Dan Gohman475871a2008-07-27 21:46:04 +00007565 SDValue LL, LH, RL, RH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007566 SplitVectorOp(Node->getOperand(1), LL, LH);
7567 SplitVectorOp(Node->getOperand(2), RL, RH);
7568
Duncan Sands83ec4b62008-06-06 12:08:01 +00007569 if (Cond.getValueType().isVector()) {
Dan Gohmanc6230962007-10-17 14:48:28 +00007570 // Handle a vector merge.
Dan Gohman475871a2008-07-27 21:46:04 +00007571 SDValue CL, CH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007572 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007573 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7574 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007575 } else {
7576 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00007577 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7578 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007579 }
7580 break;
7581 }
Chris Lattner80c1a562008-06-30 02:43:01 +00007582 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007583 SDValue CondLHS = Node->getOperand(0);
7584 SDValue CondRHS = Node->getOperand(1);
7585 SDValue CondCode = Node->getOperand(4);
Chris Lattner80c1a562008-06-30 02:43:01 +00007586
Dan Gohman475871a2008-07-27 21:46:04 +00007587 SDValue LL, LH, RL, RH;
Chris Lattner80c1a562008-06-30 02:43:01 +00007588 SplitVectorOp(Node->getOperand(2), LL, LH);
7589 SplitVectorOp(Node->getOperand(3), RL, RH);
7590
7591 // Handle a simple select with vector operands.
7592 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7593 LL, RL, CondCode);
7594 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7595 LH, RH, CondCode);
7596 break;
7597 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00007598 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007599 SDValue LL, LH, RL, RH;
Nate Begemanb43e9c12008-05-12 19:40:03 +00007600 SplitVectorOp(Node->getOperand(0), LL, LH);
7601 SplitVectorOp(Node->getOperand(1), RL, RH);
7602 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7603 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7604 break;
7605 }
Dan Gohman7f321562007-06-25 16:23:39 +00007606 case ISD::ADD:
7607 case ISD::SUB:
7608 case ISD::MUL:
7609 case ISD::FADD:
7610 case ISD::FSUB:
7611 case ISD::FMUL:
7612 case ISD::SDIV:
7613 case ISD::UDIV:
7614 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00007615 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007616 case ISD::AND:
7617 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00007618 case ISD::XOR:
7619 case ISD::UREM:
7620 case ISD::SREM:
7621 case ISD::FREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00007622 SDValue LL, LH, RL, RH;
Chris Lattnerc7029802006-03-18 01:44:44 +00007623 SplitVectorOp(Node->getOperand(0), LL, LH);
7624 SplitVectorOp(Node->getOperand(1), RL, RH);
7625
Nate Begeman5db1afb2007-11-15 21:15:26 +00007626 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7627 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00007628 break;
7629 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00007630 case ISD::FP_ROUND:
Dan Gohman82669522007-10-11 23:57:53 +00007631 case ISD::FPOWI: {
Dan Gohman475871a2008-07-27 21:46:04 +00007632 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007633 SplitVectorOp(Node->getOperand(0), L, H);
7634
Nate Begeman5db1afb2007-11-15 21:15:26 +00007635 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7636 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00007637 break;
7638 }
7639 case ISD::CTTZ:
7640 case ISD::CTLZ:
7641 case ISD::CTPOP:
7642 case ISD::FNEG:
7643 case ISD::FABS:
7644 case ISD::FSQRT:
7645 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00007646 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007647 case ISD::FLOG:
7648 case ISD::FLOG2:
7649 case ISD::FLOG10:
7650 case ISD::FEXP:
7651 case ISD::FEXP2:
Nate Begemanb348d182007-11-17 03:58:34 +00007652 case ISD::FP_TO_SINT:
7653 case ISD::FP_TO_UINT:
7654 case ISD::SINT_TO_FP:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007655 case ISD::UINT_TO_FP:
7656 case ISD::TRUNCATE:
7657 case ISD::ANY_EXTEND:
7658 case ISD::SIGN_EXTEND:
7659 case ISD::ZERO_EXTEND:
7660 case ISD::FP_EXTEND: {
Dan Gohman475871a2008-07-27 21:46:04 +00007661 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007662 SplitVectorOp(Node->getOperand(0), L, H);
7663
Nate Begeman5db1afb2007-11-15 21:15:26 +00007664 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7665 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00007666 break;
7667 }
Mon P Wang77cdf302008-11-10 20:54:11 +00007668 case ISD::CONVERT_RNDSAT: {
7669 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7670 SDValue L, H;
7671 SplitVectorOp(Node->getOperand(0), L, H);
7672 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7673 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7674 SDValue STyOpL = DAG.getValueType(L.getValueType());
7675 SDValue STyOpH = DAG.getValueType(H.getValueType());
7676
7677 SDValue RndOp = Node->getOperand(3);
7678 SDValue SatOp = Node->getOperand(4);
7679
7680 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7681 RndOp, SatOp, CvtCode);
7682 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7683 RndOp, SatOp, CvtCode);
7684 break;
7685 }
Dan Gohman7f321562007-06-25 16:23:39 +00007686 case ISD::LOAD: {
7687 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007688 SDValue Ch = LD->getChain();
7689 SDValue Ptr = LD->getBasePtr();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007690 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007691 const Value *SV = LD->getSrcValue();
7692 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007693 MVT MemoryVT = LD->getMemoryVT();
Dan Gohman7f321562007-06-25 16:23:39 +00007694 unsigned Alignment = LD->getAlignment();
7695 bool isVolatile = LD->isVolatile();
7696
Dan Gohman7f8613e2008-08-14 20:04:46 +00007697 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7698 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7699
7700 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7701 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7702 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7703
7704 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7705 NewVT_Lo, Ch, Ptr, Offset,
7706 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7707 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00007708 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00007709 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00007710 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00007711 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00007712 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7713 NewVT_Hi, Ch, Ptr, Offset,
7714 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00007715
7716 // Build a factor node to remember that this load is independent of the
7717 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00007718 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Chris Lattnerc7029802006-03-18 01:44:44 +00007719 Hi.getValue(1));
7720
7721 // Remember that we legalized the chain.
7722 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00007723 break;
7724 }
Dan Gohman7f321562007-06-25 16:23:39 +00007725 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00007726 // We know the result is a vector. The input may be either a vector or a
7727 // scalar value.
Dan Gohman475871a2008-07-27 21:46:04 +00007728 SDValue InOp = Node->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007729 if (!InOp.getValueType().isVector() ||
7730 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman10a7aa62007-06-29 00:09:08 +00007731 // The input is a scalar or single-element vector.
7732 // Lower to a store/load so that it can be split.
7733 // FIXME: this could be improved probably.
Mon P Wang2920d2b2008-07-15 05:28:34 +00007734 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7735 Op.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00007736 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greifba36cb52008-08-28 21:40:38 +00007737 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Chris Lattner7692eb42006-03-23 21:16:34 +00007738
Dan Gohman475871a2008-07-27 21:46:04 +00007739 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00007740 InOp, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007741 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00007742 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007743 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00007744 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00007745 // Split the vector and convert each of the pieces now.
7746 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007747 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7748 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00007749 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00007750 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007751 }
7752
7753 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00007754 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00007755 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00007756 assert(isNew && "Value already split?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007757 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007758}
7759
7760
Dan Gohman89b20c02007-06-27 14:06:22 +00007761/// ScalarizeVectorOp - Given an operand of single-element vector type
7762/// (e.g. v1f32), convert it into the equivalent operation that returns a
7763/// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +00007764SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007765 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007766 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007767 MVT NewVT = Op.getValueType().getVectorElementType();
7768 assert(Op.getValueType().getVectorNumElements() == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00007769
Dan Gohman7f321562007-06-25 16:23:39 +00007770 // See if we already scalarized it.
Dan Gohman475871a2008-07-27 21:46:04 +00007771 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohman7f321562007-06-25 16:23:39 +00007772 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00007773
Dan Gohman475871a2008-07-27 21:46:04 +00007774 SDValue Result;
Chris Lattnerc7029802006-03-18 01:44:44 +00007775 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00007776 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007777#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007778 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007779#endif
Dan Gohman7f321562007-06-25 16:23:39 +00007780 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7781 case ISD::ADD:
7782 case ISD::FADD:
7783 case ISD::SUB:
7784 case ISD::FSUB:
7785 case ISD::MUL:
7786 case ISD::FMUL:
7787 case ISD::SDIV:
7788 case ISD::UDIV:
7789 case ISD::FDIV:
7790 case ISD::SREM:
7791 case ISD::UREM:
7792 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00007793 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007794 case ISD::AND:
7795 case ISD::OR:
7796 case ISD::XOR:
7797 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00007798 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007799 ScalarizeVectorOp(Node->getOperand(0)),
7800 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00007801 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007802 case ISD::FNEG:
7803 case ISD::FABS:
7804 case ISD::FSQRT:
7805 case ISD::FSIN:
7806 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007807 case ISD::FLOG:
7808 case ISD::FLOG2:
7809 case ISD::FLOG10:
7810 case ISD::FEXP:
7811 case ISD::FEXP2:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007812 case ISD::FP_TO_SINT:
7813 case ISD::FP_TO_UINT:
7814 case ISD::SINT_TO_FP:
7815 case ISD::UINT_TO_FP:
7816 case ISD::SIGN_EXTEND:
7817 case ISD::ZERO_EXTEND:
7818 case ISD::ANY_EXTEND:
7819 case ISD::TRUNCATE:
7820 case ISD::FP_EXTEND:
Dan Gohman7f321562007-06-25 16:23:39 +00007821 Result = DAG.getNode(Node->getOpcode(),
7822 NewVT,
7823 ScalarizeVectorOp(Node->getOperand(0)));
7824 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00007825 case ISD::CONVERT_RNDSAT: {
7826 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7827 Result = DAG.getConvertRndSat(NewVT, Op0,
7828 DAG.getValueType(NewVT),
7829 DAG.getValueType(Op0.getValueType()),
7830 Node->getOperand(3),
7831 Node->getOperand(4),
7832 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7833 break;
7834 }
Dan Gohman9e04c822007-10-12 14:13:46 +00007835 case ISD::FPOWI:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007836 case ISD::FP_ROUND:
Dan Gohman9e04c822007-10-12 14:13:46 +00007837 Result = DAG.getNode(Node->getOpcode(),
7838 NewVT,
7839 ScalarizeVectorOp(Node->getOperand(0)),
7840 Node->getOperand(1));
7841 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007842 case ISD::LOAD: {
7843 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007844 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7845 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman7f8613e2008-08-14 20:04:46 +00007846 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007847 const Value *SV = LD->getSrcValue();
7848 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007849 MVT MemoryVT = LD->getMemoryVT();
7850 unsigned Alignment = LD->getAlignment();
7851 bool isVolatile = LD->isVolatile();
7852
7853 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7854 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7855
7856 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7857 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7858 MemoryVT.getVectorElementType(),
7859 isVolatile, Alignment);
Dan Gohman7f321562007-06-25 16:23:39 +00007860
Chris Lattnerc7029802006-03-18 01:44:44 +00007861 // Remember that we legalized the chain.
7862 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7863 break;
7864 }
Dan Gohman7f321562007-06-25 16:23:39 +00007865 case ISD::BUILD_VECTOR:
7866 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007867 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007868 case ISD::INSERT_VECTOR_ELT:
7869 // Returning the inserted scalar element.
7870 Result = Node->getOperand(1);
7871 break;
7872 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007873 assert(Node->getOperand(0).getValueType() == NewVT &&
7874 "Concat of non-legal vectors not yet supported!");
7875 Result = Node->getOperand(0);
7876 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007877 case ISD::VECTOR_SHUFFLE: {
7878 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman475871a2008-07-27 21:46:04 +00007879 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007880 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohman7f321562007-06-25 16:23:39 +00007881 Result = ScalarizeVectorOp(Node->getOperand(1));
7882 else
7883 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007884 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007885 }
7886 case ISD::EXTRACT_SUBVECTOR:
Mon P Wange0b436a2008-11-06 22:52:21 +00007887 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wangaeb06d22008-11-10 04:46:22 +00007888 Node->getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00007889 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007890 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007891 SDValue Op0 = Op.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007892 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng446efdd2008-05-16 17:19:05 +00007893 Op0 = ScalarizeVectorOp(Op0);
7894 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Chris Lattner5b2316e2006-03-28 20:24:43 +00007895 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007896 }
Dan Gohman7f321562007-06-25 16:23:39 +00007897 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007898 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007899 ScalarizeVectorOp(Op.getOperand(1)),
7900 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007901 break;
Chris Lattner80c1a562008-06-30 02:43:01 +00007902 case ISD::SELECT_CC:
7903 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7904 Node->getOperand(1),
7905 ScalarizeVectorOp(Op.getOperand(2)),
7906 ScalarizeVectorOp(Op.getOperand(3)),
7907 Node->getOperand(4));
7908 break;
Nate Begeman0d1704b2008-05-12 23:09:43 +00007909 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007910 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7911 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Nate Begeman0d1704b2008-05-12 23:09:43 +00007912 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7913 Op.getOperand(2));
7914 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7915 DAG.getConstant(-1ULL, NewVT),
7916 DAG.getConstant(0ULL, NewVT));
7917 break;
7918 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007919 }
7920
7921 if (TLI.isTypeLegal(NewVT))
7922 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007923 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7924 assert(isNew && "Value already scalarized?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007925 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007926 return Result;
7927}
7928
Chris Lattner3e928bb2005-01-07 07:47:09 +00007929
Mon P Wang0c397192008-10-30 08:01:45 +00007930SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7931 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7932 if (I != WidenNodes.end()) return I->second;
7933
7934 MVT VT = Op.getValueType();
7935 assert(VT.isVector() && "Cannot widen non-vector type!");
7936
7937 SDValue Result;
7938 SDNode *Node = Op.getNode();
7939 MVT EVT = VT.getVectorElementType();
7940
7941 unsigned NumElts = VT.getVectorNumElements();
7942 unsigned NewNumElts = WidenVT.getVectorNumElements();
7943 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7944 assert(NewNumElts < 17);
7945
7946 // When widen is called, it is assumed that it is more efficient to use a
7947 // wide type. The default action is to widen to operation to a wider legal
7948 // vector type and then do the operation if it is legal by calling LegalizeOp
7949 // again. If there is no vector equivalent, we will unroll the operation, do
7950 // it, and rebuild the vector. If most of the operations are vectorizible to
7951 // the legal type, the resulting code will be more efficient. If this is not
7952 // the case, the resulting code will preform badly as we end up generating
7953 // code to pack/unpack the results. It is the function that calls widen
Mon P Wangf007a8b2008-11-06 05:31:54 +00007954 // that is responsible for seeing this doesn't happen.
Mon P Wang0c397192008-10-30 08:01:45 +00007955 switch (Node->getOpcode()) {
7956 default:
7957#ifndef NDEBUG
7958 Node->dump(&DAG);
7959#endif
7960 assert(0 && "Unexpected operation in WidenVectorOp!");
7961 break;
7962 case ISD::CopyFromReg:
Mon P Wang49292f12008-11-15 06:05:52 +00007963 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang0c397192008-10-30 08:01:45 +00007964 case ISD::Constant:
7965 case ISD::ConstantFP:
7966 // To build a vector of these elements, clients should call BuildVector
7967 // and with each element instead of creating a node with a vector type
7968 assert(0 && "Unexpected operation in WidenVectorOp!");
7969 case ISD::VAARG:
7970 // Variable Arguments with vector types doesn't make any sense to me
7971 assert(0 && "Unexpected operation in WidenVectorOp!");
7972 break;
Mon P Wang49292f12008-11-15 06:05:52 +00007973 case ISD::UNDEF:
7974 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7975 break;
Mon P Wang0c397192008-10-30 08:01:45 +00007976 case ISD::BUILD_VECTOR: {
7977 // Build a vector with undefined for the new nodes
7978 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7979 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7980 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7981 }
7982 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7983 break;
7984 }
7985 case ISD::INSERT_VECTOR_ELT: {
7986 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7987 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7988 Node->getOperand(1), Node->getOperand(2));
7989 break;
7990 }
7991 case ISD::VECTOR_SHUFFLE: {
7992 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7993 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7994 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7995 // used as permutation array. We build the vector here instead of widening
7996 // because we don't want to legalize and have it turned to something else.
7997 SDValue PermOp = Node->getOperand(2);
7998 SDValueVector NewOps;
7999 MVT PVT = PermOp.getValueType().getVectorElementType();
8000 for (unsigned i = 0; i < NumElts; ++i) {
8001 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
8002 NewOps.push_back(PermOp.getOperand(i));
8003 } else {
8004 unsigned Idx =
8005 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
8006 if (Idx < NumElts) {
8007 NewOps.push_back(PermOp.getOperand(i));
8008 }
8009 else {
8010 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
8011 PermOp.getOperand(i).getValueType()));
8012 }
8013 }
8014 }
8015 for (unsigned i = NumElts; i < NewNumElts; ++i) {
8016 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
8017 }
8018
8019 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
8020 MVT::getVectorVT(PVT, NewOps.size()),
8021 &NewOps[0], NewOps.size());
8022
8023 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
8024 break;
8025 }
8026 case ISD::LOAD: {
8027 // If the load widen returns true, we can use a single load for the
8028 // vector. Otherwise, it is returning a token factor for multiple
8029 // loads.
8030 SDValue TFOp;
8031 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
8032 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
8033 else
8034 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
8035 break;
8036 }
8037
8038 case ISD::BIT_CONVERT: {
8039 SDValue Tmp1 = Node->getOperand(0);
8040 // Converts between two different types so we need to determine
8041 // the correct widen type for the input operand.
8042 MVT TVT = Tmp1.getValueType();
8043 assert(TVT.isVector() && "can not widen non vector type");
8044 MVT TEVT = TVT.getVectorElementType();
8045 assert(WidenVT.getSizeInBits() % EVT.getSizeInBits() == 0 &&
8046 "can not widen bit bit convert that are not multiple of element type");
8047 MVT TWidenVT = MVT::getVectorVT(TEVT,
8048 WidenVT.getSizeInBits()/EVT.getSizeInBits());
8049 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
8050 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
8051 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
8052
8053 TargetLowering::LegalizeAction action =
8054 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8055 switch (action) {
8056 default: assert(0 && "action not supported");
8057 case TargetLowering::Legal:
8058 break;
8059 case TargetLowering::Promote:
8060 // We defer the promotion to when we legalize the op
8061 break;
8062 case TargetLowering::Expand:
8063 // Expand the operation into a bunch of nasty scalar code.
8064 Result = LegalizeOp(UnrollVectorOp(Result));
8065 break;
8066 }
8067 break;
8068 }
8069
8070 case ISD::SINT_TO_FP:
8071 case ISD::UINT_TO_FP:
8072 case ISD::FP_TO_SINT:
8073 case ISD::FP_TO_UINT: {
8074 SDValue Tmp1 = Node->getOperand(0);
8075 // Converts between two different types so we need to determine
8076 // the correct widen type for the input operand.
8077 MVT TVT = Tmp1.getValueType();
8078 assert(TVT.isVector() && "can not widen non vector type");
8079 MVT TEVT = TVT.getVectorElementType();
8080 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
8081 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
8082 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
8083 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00008084 break;
8085 }
8086
8087 case ISD::FP_EXTEND:
8088 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
8089 case ISD::TRUNCATE:
8090 case ISD::SIGN_EXTEND:
8091 case ISD::ZERO_EXTEND:
8092 case ISD::ANY_EXTEND:
8093 case ISD::FP_ROUND:
8094 case ISD::SIGN_EXTEND_INREG:
8095 case ISD::FABS:
8096 case ISD::FNEG:
8097 case ISD::FSQRT:
8098 case ISD::FSIN:
Mon P Wang49292f12008-11-15 06:05:52 +00008099 case ISD::FCOS:
8100 case ISD::CTPOP:
8101 case ISD::CTTZ:
8102 case ISD::CTLZ: {
Mon P Wang0c397192008-10-30 08:01:45 +00008103 // Unary op widening
8104 SDValue Tmp1;
Mon P Wang0c397192008-10-30 08:01:45 +00008105 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8106 assert(Tmp1.getValueType() == WidenVT);
8107 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00008108 break;
8109 }
Mon P Wang77cdf302008-11-10 20:54:11 +00008110 case ISD::CONVERT_RNDSAT: {
8111 SDValue RndOp = Node->getOperand(3);
8112 SDValue SatOp = Node->getOperand(4);
Mon P Wang77cdf302008-11-10 20:54:11 +00008113 SDValue SrcOp = Node->getOperand(0);
8114
8115 // Converts between two different types so we need to determine
8116 // the correct widen type for the input operand.
8117 MVT SVT = SrcOp.getValueType();
8118 assert(SVT.isVector() && "can not widen non vector type");
8119 MVT SEVT = SVT.getVectorElementType();
8120 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8121
8122 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8123 assert(SrcOp.getValueType() == WidenVT);
8124 SDValue DTyOp = DAG.getValueType(WidenVT);
8125 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8126 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8127
8128 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
8129 RndOp, SatOp, CvtCode);
Mon P Wang77cdf302008-11-10 20:54:11 +00008130 break;
8131 }
Mon P Wang0c397192008-10-30 08:01:45 +00008132 case ISD::FPOW:
8133 case ISD::FPOWI:
8134 case ISD::ADD:
8135 case ISD::SUB:
8136 case ISD::MUL:
8137 case ISD::MULHS:
8138 case ISD::MULHU:
8139 case ISD::AND:
8140 case ISD::OR:
8141 case ISD::XOR:
8142 case ISD::FADD:
8143 case ISD::FSUB:
8144 case ISD::FMUL:
8145 case ISD::SDIV:
8146 case ISD::SREM:
8147 case ISD::FDIV:
8148 case ISD::FREM:
8149 case ISD::FCOPYSIGN:
8150 case ISD::UDIV:
8151 case ISD::UREM:
8152 case ISD::BSWAP: {
8153 // Binary op widening
Mon P Wang0c397192008-10-30 08:01:45 +00008154 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8155 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8156 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8157 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008158 break;
8159 }
8160
8161 case ISD::SHL:
8162 case ISD::SRA:
8163 case ISD::SRL: {
Mon P Wang0c397192008-10-30 08:01:45 +00008164 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8165 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangfb13f002008-12-02 07:35:08 +00008166 SDValue ShOp = Node->getOperand(1);
8167 MVT ShVT = ShOp.getValueType();
8168 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8169 WidenVT.getVectorNumElements());
8170 ShOp = WidenVectorOp(ShOp, NewShVT);
8171 assert(ShOp.getValueType() == NewShVT);
8172 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, ShOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008173 break;
8174 }
Mon P Wangfb13f002008-12-02 07:35:08 +00008175
Mon P Wang0c397192008-10-30 08:01:45 +00008176 case ISD::EXTRACT_VECTOR_ELT: {
8177 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8178 assert(Tmp1.getValueType() == WidenVT);
8179 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8180 break;
8181 }
8182 case ISD::CONCAT_VECTORS: {
8183 // We concurrently support only widen on a multiple of the incoming vector.
8184 // We could widen on a multiple of the incoming operand if necessary.
8185 unsigned NumConcat = NewNumElts / NumElts;
8186 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Mon P Wangfb13f002008-12-02 07:35:08 +00008187 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
Mon P Wang0c397192008-10-30 08:01:45 +00008188 SmallVector<SDValue, 8> MOps;
8189 MOps.push_back(Op);
8190 for (unsigned i = 1; i != NumConcat; ++i) {
8191 MOps.push_back(UndefVal);
8192 }
8193 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8194 &MOps[0], MOps.size()));
8195 break;
8196 }
8197 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang49292f12008-11-15 06:05:52 +00008198 SDValue Tmp1 = Node->getOperand(0);
8199 SDValue Idx = Node->getOperand(1);
8200 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8201 if (CIdx && CIdx->getZExtValue() == 0) {
8202 // Since we are access the start of the vector, the incoming
8203 // vector type might be the proper.
8204 MVT Tmp1VT = Tmp1.getValueType();
8205 if (Tmp1VT == WidenVT)
8206 return Tmp1;
8207 else {
8208 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8209 if (Tmp1VTNumElts < NewNumElts)
8210 Result = WidenVectorOp(Tmp1, WidenVT);
8211 else
8212 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8213 }
8214 } else if (NewNumElts % NumElts == 0) {
8215 // Widen the extracted subvector.
8216 unsigned NumConcat = NewNumElts / NumElts;
8217 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8218 SmallVector<SDValue, 8> MOps;
8219 MOps.push_back(Op);
8220 for (unsigned i = 1; i != NumConcat; ++i) {
8221 MOps.push_back(UndefVal);
8222 }
8223 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8224 &MOps[0], MOps.size()));
8225 } else {
8226 assert(0 && "can not widen extract subvector");
8227 // This could be implemented using insert and build vector but I would
8228 // like to see when this happens.
8229 }
Mon P Wang0c397192008-10-30 08:01:45 +00008230 break;
8231 }
8232
8233 case ISD::SELECT: {
Mon P Wang0c397192008-10-30 08:01:45 +00008234 // Determine new condition widen type and widen
8235 SDValue Cond1 = Node->getOperand(0);
8236 MVT CondVT = Cond1.getValueType();
8237 assert(CondVT.isVector() && "can not widen non vector type");
8238 MVT CondEVT = CondVT.getVectorElementType();
8239 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8240 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8241 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8242
8243 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8244 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8245 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8246 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008247 break;
8248 }
8249
8250 case ISD::SELECT_CC: {
Mon P Wang0c397192008-10-30 08:01:45 +00008251 // Determine new condition widen type and widen
8252 SDValue Cond1 = Node->getOperand(0);
8253 SDValue Cond2 = Node->getOperand(1);
8254 MVT CondVT = Cond1.getValueType();
8255 assert(CondVT.isVector() && "can not widen non vector type");
8256 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8257 MVT CondEVT = CondVT.getVectorElementType();
8258 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8259 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8260 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8261 assert(Cond1.getValueType() == CondWidenVT &&
8262 Cond2.getValueType() == CondWidenVT && "condition not widen");
8263
8264 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8265 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8266 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8267 "operands not widen");
8268 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8269 Tmp2, Node->getOperand(4));
Mon P Wang0c397192008-10-30 08:01:45 +00008270 break;
Mon P Wang2eb13c32008-10-30 18:21:52 +00008271 }
8272 case ISD::VSETCC: {
8273 // Determine widen for the operand
8274 SDValue Tmp1 = Node->getOperand(0);
8275 MVT TmpVT = Tmp1.getValueType();
8276 assert(TmpVT.isVector() && "can not widen non vector type");
8277 MVT TmpEVT = TmpVT.getVectorElementType();
8278 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8279 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8280 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
8281 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
8282 Node->getOperand(2));
Mon P Wang0c397192008-10-30 08:01:45 +00008283 break;
8284 }
Mon P Wang0c397192008-10-30 08:01:45 +00008285 case ISD::ATOMIC_CMP_SWAP_8:
8286 case ISD::ATOMIC_CMP_SWAP_16:
8287 case ISD::ATOMIC_CMP_SWAP_32:
8288 case ISD::ATOMIC_CMP_SWAP_64:
8289 case ISD::ATOMIC_LOAD_ADD_8:
8290 case ISD::ATOMIC_LOAD_SUB_8:
8291 case ISD::ATOMIC_LOAD_AND_8:
8292 case ISD::ATOMIC_LOAD_OR_8:
8293 case ISD::ATOMIC_LOAD_XOR_8:
8294 case ISD::ATOMIC_LOAD_NAND_8:
8295 case ISD::ATOMIC_LOAD_MIN_8:
8296 case ISD::ATOMIC_LOAD_MAX_8:
8297 case ISD::ATOMIC_LOAD_UMIN_8:
8298 case ISD::ATOMIC_LOAD_UMAX_8:
8299 case ISD::ATOMIC_SWAP_8:
8300 case ISD::ATOMIC_LOAD_ADD_16:
8301 case ISD::ATOMIC_LOAD_SUB_16:
8302 case ISD::ATOMIC_LOAD_AND_16:
8303 case ISD::ATOMIC_LOAD_OR_16:
8304 case ISD::ATOMIC_LOAD_XOR_16:
8305 case ISD::ATOMIC_LOAD_NAND_16:
8306 case ISD::ATOMIC_LOAD_MIN_16:
8307 case ISD::ATOMIC_LOAD_MAX_16:
8308 case ISD::ATOMIC_LOAD_UMIN_16:
8309 case ISD::ATOMIC_LOAD_UMAX_16:
8310 case ISD::ATOMIC_SWAP_16:
8311 case ISD::ATOMIC_LOAD_ADD_32:
8312 case ISD::ATOMIC_LOAD_SUB_32:
8313 case ISD::ATOMIC_LOAD_AND_32:
8314 case ISD::ATOMIC_LOAD_OR_32:
8315 case ISD::ATOMIC_LOAD_XOR_32:
8316 case ISD::ATOMIC_LOAD_NAND_32:
8317 case ISD::ATOMIC_LOAD_MIN_32:
8318 case ISD::ATOMIC_LOAD_MAX_32:
8319 case ISD::ATOMIC_LOAD_UMIN_32:
8320 case ISD::ATOMIC_LOAD_UMAX_32:
8321 case ISD::ATOMIC_SWAP_32:
8322 case ISD::ATOMIC_LOAD_ADD_64:
8323 case ISD::ATOMIC_LOAD_SUB_64:
8324 case ISD::ATOMIC_LOAD_AND_64:
8325 case ISD::ATOMIC_LOAD_OR_64:
8326 case ISD::ATOMIC_LOAD_XOR_64:
8327 case ISD::ATOMIC_LOAD_NAND_64:
8328 case ISD::ATOMIC_LOAD_MIN_64:
8329 case ISD::ATOMIC_LOAD_MAX_64:
8330 case ISD::ATOMIC_LOAD_UMIN_64:
8331 case ISD::ATOMIC_LOAD_UMAX_64:
8332 case ISD::ATOMIC_SWAP_64: {
8333 // For now, we assume that using vectors for these operations don't make
8334 // much sense so we just split it. We return an empty result
8335 SDValue X, Y;
8336 SplitVectorOp(Op, X, Y);
8337 return Result;
8338 break;
8339 }
8340
8341 } // end switch (Node->getOpcode())
8342
8343 assert(Result.getNode() && "Didn't set a result!");
8344 if (Result != Op)
8345 Result = LegalizeOp(Result);
8346
Mon P Wangf007a8b2008-11-06 05:31:54 +00008347 AddWidenedOperand(Op, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00008348 return Result;
8349}
8350
8351// Utility function to find a legal vector type and its associated element
8352// type from a preferred width and whose vector type must be the same size
8353// as the VVT.
8354// TLI: Target lowering used to determine legal types
8355// Width: Preferred width of element type
8356// VVT: Vector value type whose size we must match.
8357// Returns VecEVT and EVT - the vector type and its associated element type
8358static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8359 MVT& EVT, MVT& VecEVT) {
8360 // We start with the preferred width, make it a power of 2 and see if
8361 // we can find a vector type of that width. If not, we reduce it by
8362 // another power of 2. If we have widen the type, a vector of bytes should
8363 // always be legal.
8364 assert(TLI.isTypeLegal(VVT));
8365 unsigned EWidth = Width + 1;
8366 do {
8367 assert(EWidth > 0);
8368 EWidth = (1 << Log2_32(EWidth-1));
8369 EVT = MVT::getIntegerVT(EWidth);
8370 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8371 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8372 } while (!TLI.isTypeLegal(VecEVT) ||
8373 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8374}
8375
8376SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8377 SDValue Chain,
8378 SDValue BasePtr,
8379 const Value *SV,
8380 int SVOffset,
8381 unsigned Alignment,
8382 bool isVolatile,
8383 unsigned LdWidth,
8384 MVT ResType) {
8385 // We assume that we have good rules to handle loading power of two loads so
8386 // we break down the operations to power of 2 loads. The strategy is to
8387 // load the largest power of 2 that we can easily transform to a legal vector
8388 // and then insert into that vector, and the cast the result into the legal
8389 // vector that we want. This avoids unnecessary stack converts.
8390 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8391 // the load is nonvolatile, we an use a wider load for the value.
8392 // Find a vector length we can load a large chunk
8393 MVT EVT, VecEVT;
8394 unsigned EVTWidth;
8395 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8396 EVTWidth = EVT.getSizeInBits();
8397
8398 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8399 isVolatile, Alignment);
8400 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8401 LdChain.push_back(LdOp.getValue(1));
8402
8403 // Check if we can load the element with one instruction
8404 if (LdWidth == EVTWidth) {
8405 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8406 }
8407
8408 // The vector element order is endianness dependent.
8409 unsigned Idx = 1;
8410 LdWidth -= EVTWidth;
8411 unsigned Offset = 0;
8412
8413 while (LdWidth > 0) {
8414 unsigned Increment = EVTWidth / 8;
8415 Offset += Increment;
8416 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8417 DAG.getIntPtrConstant(Increment));
8418
8419 if (LdWidth < EVTWidth) {
8420 // Our current type we are using is too large, use a smaller size by
8421 // using a smaller power of 2
8422 unsigned oEVTWidth = EVTWidth;
8423 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8424 EVTWidth = EVT.getSizeInBits();
8425 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008426 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang0c397192008-10-30 08:01:45 +00008427 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8428 }
8429
8430 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8431 SVOffset+Offset, isVolatile,
8432 MinAlign(Alignment, Offset));
8433 LdChain.push_back(LdOp.getValue(1));
8434 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8435 DAG.getIntPtrConstant(Idx++));
8436
8437 LdWidth -= EVTWidth;
8438 }
8439
8440 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8441}
8442
8443bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8444 SDValue& TFOp,
8445 SDValue Op,
8446 MVT NVT) {
8447 // TODO: Add support for ConcatVec and the ability to load many vector
8448 // types (e.g., v4i8). This will not work when a vector register
8449 // to memory mapping is strange (e.g., vector elements are not
8450 // stored in some sequential order).
8451
8452 // It must be true that the widen vector type is bigger than where
8453 // we need to load from.
8454 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8455 MVT LdVT = LD->getMemoryVT();
8456 assert(LdVT.isVector() && NVT.isVector());
8457 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8458
8459 // Load information
8460 SDValue Chain = LD->getChain();
8461 SDValue BasePtr = LD->getBasePtr();
8462 int SVOffset = LD->getSrcValueOffset();
8463 unsigned Alignment = LD->getAlignment();
8464 bool isVolatile = LD->isVolatile();
8465 const Value *SV = LD->getSrcValue();
8466 unsigned int LdWidth = LdVT.getSizeInBits();
8467
8468 // Load value as a large register
8469 SDValueVector LdChain;
8470 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8471 Alignment, isVolatile, LdWidth, NVT);
8472
8473 if (LdChain.size() == 1) {
8474 TFOp = LdChain[0];
8475 return true;
8476 }
8477 else {
8478 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8479 return false;
8480 }
8481}
8482
8483
8484void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8485 SDValue Chain,
8486 SDValue BasePtr,
8487 const Value *SV,
8488 int SVOffset,
8489 unsigned Alignment,
8490 bool isVolatile,
Mon P Wang49292f12008-11-15 06:05:52 +00008491 SDValue ValOp,
Mon P Wang0c397192008-10-30 08:01:45 +00008492 unsigned StWidth) {
8493 // Breaks the stores into a series of power of 2 width stores. For any
8494 // width, we convert the vector to the vector of element size that we
8495 // want to store. This avoids requiring a stack convert.
8496
8497 // Find a width of the element type we can store with
8498 MVT VVT = ValOp.getValueType();
8499 MVT EVT, VecEVT;
8500 unsigned EVTWidth;
8501 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8502 EVTWidth = EVT.getSizeInBits();
8503
8504 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8505 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wange0b436a2008-11-06 22:52:21 +00008506 DAG.getIntPtrConstant(0));
Mon P Wang0c397192008-10-30 08:01:45 +00008507 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8508 isVolatile, Alignment);
8509 StChain.push_back(StOp);
8510
8511 // Check if we are done
8512 if (StWidth == EVTWidth) {
8513 return;
8514 }
8515
8516 unsigned Idx = 1;
8517 StWidth -= EVTWidth;
8518 unsigned Offset = 0;
8519
8520 while (StWidth > 0) {
8521 unsigned Increment = EVTWidth / 8;
8522 Offset += Increment;
8523 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8524 DAG.getIntPtrConstant(Increment));
8525
8526 if (StWidth < EVTWidth) {
8527 // Our current type we are using is too large, use a smaller size by
8528 // using a smaller power of 2
8529 unsigned oEVTWidth = EVTWidth;
8530 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8531 EVTWidth = EVT.getSizeInBits();
8532 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008533 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang0c397192008-10-30 08:01:45 +00008534 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8535 }
8536
8537 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang49292f12008-11-15 06:05:52 +00008538 DAG.getIntPtrConstant(Idx++));
Mon P Wang0c397192008-10-30 08:01:45 +00008539 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8540 SVOffset + Offset, isVolatile,
8541 MinAlign(Alignment, Offset)));
8542 StWidth -= EVTWidth;
8543 }
8544}
8545
8546
8547SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8548 SDValue Chain,
8549 SDValue BasePtr) {
8550 // TODO: It might be cleaner if we can use SplitVector and have more legal
8551 // vector types that can be stored into memory (e.g., v4xi8 can
8552 // be stored as a word). This will not work when a vector register
8553 // to memory mapping is strange (e.g., vector elements are not
8554 // stored in some sequential order).
8555
8556 MVT StVT = ST->getMemoryVT();
8557 SDValue ValOp = ST->getValue();
8558
8559 // Check if we have widen this node with another value
8560 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8561 if (I != WidenNodes.end())
8562 ValOp = I->second;
8563
8564 MVT VVT = ValOp.getValueType();
8565
8566 // It must be true that we the widen vector type is bigger than where
8567 // we need to store.
8568 assert(StVT.isVector() && VVT.isVector());
8569 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8570 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8571
8572 // Store value
8573 SDValueVector StChain;
8574 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8575 ST->getSrcValueOffset(), ST->getAlignment(),
8576 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8577 if (StChain.size() == 1)
8578 return StChain[0];
8579 else
8580 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8581}
8582
8583
Chris Lattner3e928bb2005-01-07 07:47:09 +00008584// SelectionDAG::Legalize - This is the entry point for the file.
8585//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00008586void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00008587 /// run - This is the main entry point to this class.
8588 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00008589 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00008590}
8591