blob: 4a2af06966d322491e59f094500b017231ce2f65 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000019#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga448bc42007-08-16 23:50:06 +000020#include "llvm/Target/TargetFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Target/TargetLowering.h"
22#include "llvm/Target/TargetData.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetOptions.h"
Dan Gohmane8b391e2008-04-12 04:36:06 +000025#include "llvm/Target/TargetSubtarget.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/CallingConv.h"
27#include "llvm/Constants.h"
28#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/Compiler.h"
Duncan Sandsa3691432007-10-28 12:59:45 +000031#include "llvm/Support/MathExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/ADT/DenseMap.h"
33#include "llvm/ADT/SmallVector.h"
34#include "llvm/ADT/SmallPtrSet.h"
35#include <map>
36using namespace llvm;
37
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038//===----------------------------------------------------------------------===//
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 {
51class VISIBILITY_HIDDEN SelectionDAGLegalize {
52 TargetLowering &TLI;
53 SelectionDAG &DAG;
Duncan Sandse016a2e2008-12-14 09:43:15 +000054 bool TypesNeedLegalizing;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055
56 // Libcall insertion helpers.
57
58 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
59 /// legalized. We use this to ensure that calls are properly serialized
60 /// against each other, including inserted libcalls.
Dan Gohman8181bd12008-07-27 21:46:04 +000061 SDValue LastCALLSEQ_END;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062
63 /// IsLegalizingCall - This member is used *only* for purposes of providing
64 /// helpful assertions that a libcall isn't created while another call is
65 /// being legalized (which could lead to non-serialized call sequences).
66 bool IsLegalizingCall;
67
68 enum LegalizeAction {
69 Legal, // The target natively supports this operation.
70 Promote, // This operation should be executed in a larger type.
71 Expand // Try to expand this to other ops, otherwise use a libcall.
72 };
73
74 /// ValueTypeActions - This is a bitvector that contains two bits for each
75 /// value type, where the two bits correspond to the LegalizeAction enum.
76 /// This can be queried with "getTypeAction(VT)".
77 TargetLowering::ValueTypeActionImpl ValueTypeActions;
78
79 /// LegalizedNodes - For nodes that are of legal width, and that have more
80 /// than one use, this map indicates what regularized operand to use. This
81 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000082 DenseMap<SDValue, SDValue> LegalizedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083
84 /// PromotedNodes - For nodes that are below legal width, and that have more
85 /// than one use, this map indicates what promoted value to use. This allows
86 /// us to avoid promoting the same thing more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000087 DenseMap<SDValue, SDValue> PromotedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088
89 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang1448aad2008-10-30 08:01:45 +000090 /// which operands are the expanded version of the input. This allows
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 /// us to avoid expanding the same node more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000092 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093
94 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang1448aad2008-10-30 08:01:45 +000095 /// which operands are the split version of the input. This allows us
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 /// to avoid splitting the same node more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000097 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098
99 /// ScalarizedNodes - For nodes that need to be converted from vector types to
100 /// scalar types, this contains the mapping of ones we have already
101 /// processed to the result.
Dan Gohman8181bd12008-07-27 21:46:04 +0000102 std::map<SDValue, SDValue> ScalarizedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103
Mon P Wanga5a239f2008-11-06 05:31:54 +0000104 /// WidenNodes - For nodes that need to be widened from one vector type to
105 /// another, this contains the mapping of those that we have already widen.
106 /// This allows us to avoid widening more than once.
Mon P Wang1448aad2008-10-30 08:01:45 +0000107 std::map<SDValue, SDValue> WidenNodes;
108
Dan Gohman8181bd12008-07-27 21:46:04 +0000109 void AddLegalizedOperand(SDValue From, SDValue To) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 LegalizedNodes.insert(std::make_pair(From, To));
111 // If someone requests legalization of the new node, return itself.
112 if (From != To)
113 LegalizedNodes.insert(std::make_pair(To, To));
114 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000115 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman55d19662008-07-07 17:46:23 +0000116 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 assert(isNew && "Got into the map somehow?");
Evan Chengcf576fd2008-11-24 07:09:49 +0000118 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 // If someone requests legalization of the new node, return itself.
120 LegalizedNodes.insert(std::make_pair(To, To));
121 }
Mon P Wanga5a239f2008-11-06 05:31:54 +0000122 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang1448aad2008-10-30 08:01:45 +0000123 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
124 assert(isNew && "Got into the map somehow?");
Evan Chengcf576fd2008-11-24 07:09:49 +0000125 isNew = isNew;
Mon P Wang1448aad2008-10-30 08:01:45 +0000126 // If someone requests legalization of the new node, return itself.
127 LegalizedNodes.insert(std::make_pair(To, To));
128 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129
130public:
Duncan Sandse016a2e2008-12-14 09:43:15 +0000131 explicit SelectionDAGLegalize(SelectionDAG &DAG, bool TypesNeedLegalizing);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132
133 /// getTypeAction - Return how we should legalize values of this type, either
134 /// it is already legal or we need to expand it into multiple registers of
135 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands92c43912008-06-06 12:08:01 +0000136 LegalizeAction getTypeAction(MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
138 }
139
140 /// isTypeLegal - Return true if this type is legal on this target.
141 ///
Duncan Sands92c43912008-06-06 12:08:01 +0000142 bool isTypeLegal(MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 return getTypeAction(VT) == Legal;
144 }
145
146 void LegalizeDAG();
147
148private:
149 /// HandleOp - Legalize, Promote, or Expand the specified operand as
150 /// appropriate for its type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000151 void HandleOp(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152
153 /// LegalizeOp - We know that the specified value has a legal type.
154 /// Recursively ensure that the operands have legal types, then return the
155 /// result.
Dan Gohman8181bd12008-07-27 21:46:04 +0000156 SDValue LegalizeOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157
Dan Gohman6d05cac2007-10-11 23:57:53 +0000158 /// UnrollVectorOp - We know that the given vector has a legal type, however
159 /// the operation it performs is not legal and is an operation that we have
160 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
161 /// operating on each element individually.
Dan Gohman8181bd12008-07-27 21:46:04 +0000162 SDValue UnrollVectorOp(SDValue O);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000163
164 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
165 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
166 /// is necessary to spill the vector being inserted into to memory, perform
167 /// the insert there, and then read the result back.
Dan Gohman8181bd12008-07-27 21:46:04 +0000168 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
169 SDValue Idx);
Dan Gohman6d05cac2007-10-11 23:57:53 +0000170
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 /// PromoteOp - Given an operation that produces a value in an invalid type,
172 /// promote it to compute the value into a larger type. The produced value
173 /// will have the correct bits for the low portion of the register, but no
174 /// guarantee is made about the top bits: it may be zero, sign-extended, or
175 /// garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +0000176 SDValue PromoteOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177
Dan Gohman8181bd12008-07-27 21:46:04 +0000178 /// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman4fc03742008-10-01 15:07:49 +0000180 /// the LegalizedNodes map is filled in for any results that are not expanded,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 /// the ExpandedNodes map is filled in for any results that are expanded, and
182 /// the Lo/Hi values are returned. This applies to integer types and Vector
183 /// types.
Dan Gohman8181bd12008-07-27 21:46:04 +0000184 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185
Mon P Wanga5a239f2008-11-06 05:31:54 +0000186 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
187 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
188 /// for the existing elements but no guarantee is made about the new elements
189 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
190 /// when we have an instruction operating on an illegal vector type and we
191 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang1448aad2008-10-30 08:01:45 +0000192 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
193
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 /// SplitVectorOp - Given an operand of vector type, break it down into
195 /// two smaller values.
Dan Gohman8181bd12008-07-27 21:46:04 +0000196 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197
198 /// ScalarizeVectorOp - Given an operand of single-element vector type
199 /// (e.g. v1f32), convert it into the equivalent operation that returns a
200 /// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +0000201 SDValue ScalarizeVectorOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202
Mon P Wanga5a239f2008-11-06 05:31:54 +0000203 /// Useful 16 element vector type that is used to pass operands for widening.
Mon P Wang1448aad2008-10-30 08:01:45 +0000204 typedef SmallVector<SDValue, 16> SDValueVector;
205
206 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
207 /// the LdChain contains a single load and false if it contains a token
208 /// factor for multiple loads. It takes
209 /// Result: location to return the result
210 /// LdChain: location to return the load chain
211 /// Op: load operation to widen
212 /// NVT: widen vector result type we want for the load
213 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
214 SDValue Op, MVT NVT);
215
216 /// Helper genWidenVectorLoads - Helper function to generate a set of
217 /// loads to load a vector with a resulting wider type. It takes
218 /// LdChain: list of chains for the load we have generated
219 /// Chain: incoming chain for the ld vector
220 /// BasePtr: base pointer to load from
221 /// SV: memory disambiguation source value
222 /// SVOffset: memory disambiugation offset
223 /// Alignment: alignment of the memory
224 /// isVolatile: volatile load
225 /// LdWidth: width of memory that we want to load
226 /// ResType: the wider result result type for the resulting loaded vector
227 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
228 SDValue BasePtr, const Value *SV,
229 int SVOffset, unsigned Alignment,
230 bool isVolatile, unsigned LdWidth,
231 MVT ResType);
232
233 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
234 /// location. It takes
235 /// ST: store node that we want to replace
236 /// Chain: incoming store chain
237 /// BasePtr: base address of where we want to store into
238 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
239 SDValue BasePtr);
240
241 /// Helper genWidenVectorStores - Helper function to generate a set of
242 /// stores to store a widen vector into non widen memory
243 // It takes
244 // StChain: list of chains for the stores we have generated
245 // Chain: incoming chain for the ld vector
246 // BasePtr: base pointer to load from
247 // SV: memory disambiguation source value
248 // SVOffset: memory disambiugation offset
249 // Alignment: alignment of the memory
250 // isVolatile: volatile lod
251 // ValOp: value to store
252 // StWidth: width of memory that we want to store
253 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
254 SDValue BasePtr, const Value *SV,
255 int SVOffset, unsigned Alignment,
256 bool isVolatile, SDValue ValOp,
257 unsigned StWidth);
258
Duncan Sandsd3ace282008-07-21 10:20:31 +0000259 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 /// specified mask and type. Targets can specify exactly which masks they
261 /// support and the code generator is tasked with not creating illegal masks.
262 ///
263 /// Note that this will also return true for shuffles that are promoted to a
264 /// different type.
265 ///
266 /// If this is a legal shuffle, this method returns the (possibly promoted)
267 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman8181bd12008-07-27 21:46:04 +0000268 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269
270 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
271 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
272
Dan Gohman8181bd12008-07-27 21:46:04 +0000273 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Evan Cheng71343822008-10-15 02:05:31 +0000274 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC);
275 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC) {
276 LegalizeSetCCOperands(LHS, RHS, CC);
277 LegalizeSetCCCondCode(VT, LHS, RHS, CC);
278 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279
Dan Gohman8181bd12008-07-27 21:46:04 +0000280 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
281 SDValue &Hi);
282 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283
Dan Gohman8181bd12008-07-27 21:46:04 +0000284 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
285 SDValue ExpandBUILD_VECTOR(SDNode *Node);
286 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dan Gohman29c3cef2008-08-14 20:04:46 +0000287 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op);
Dan Gohman8181bd12008-07-27 21:46:04 +0000288 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
289 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
290 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291
Dan Gohman8181bd12008-07-27 21:46:04 +0000292 SDValue ExpandBSWAP(SDValue Op);
293 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
294 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
295 SDValue &Lo, SDValue &Hi);
296 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
297 SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298
Dan Gohman8181bd12008-07-27 21:46:04 +0000299 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
300 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Mon P Wang9901e732008-12-09 05:46:39 +0000301
302 // Returns the legalized (truncated or extended) shift amount.
303 SDValue LegalizeShiftAmount(SDValue ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304};
305}
306
307/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
308/// specified mask and type. Targets can specify exactly which masks they
309/// support and the code generator is tasked with not creating illegal masks.
310///
311/// Note that this will also return true for shuffles that are promoted to a
312/// different type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000313SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
315 default: return 0;
316 case TargetLowering::Legal:
317 case TargetLowering::Custom:
318 break;
319 case TargetLowering::Promote: {
320 // If this is promoted to a different type, convert the shuffle mask and
321 // ask if it is legal in the promoted type!
Duncan Sands92c43912008-06-06 12:08:01 +0000322 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd3ace282008-07-21 10:20:31 +0000323 MVT EltVT = NVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324
325 // If we changed # elements, change the shuffle mask.
326 unsigned NumEltsGrowth =
Duncan Sands92c43912008-06-06 12:08:01 +0000327 NVT.getVectorNumElements() / VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
329 if (NumEltsGrowth > 1) {
330 // Renumber the elements.
Dan Gohman8181bd12008-07-27 21:46:04 +0000331 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000333 SDValue InOp = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
335 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd3ace282008-07-21 10:20:31 +0000336 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000338 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd3ace282008-07-21 10:20:31 +0000339 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 }
341 }
342 }
343 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
344 }
345 VT = NVT;
346 break;
347 }
348 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000349 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350}
351
Duncan Sandse016a2e2008-12-14 09:43:15 +0000352SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag, bool types)
353 : TLI(dag.getTargetLoweringInfo()), DAG(dag), TypesNeedLegalizing(types),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 ValueTypeActions(TLI.getValueTypeActions()) {
355 assert(MVT::LAST_VALUETYPE <= 32 &&
356 "Too many value types for ValueTypeActions to hold!");
357}
358
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359void SelectionDAGLegalize::LegalizeDAG() {
360 LastCALLSEQ_END = DAG.getEntryNode();
361 IsLegalizingCall = false;
362
363 // The legalize process is inherently a bottom-up recursive process (users
364 // legalize their uses before themselves). Given infinite stack space, we
365 // could just start legalizing on the root and traverse the whole graph. In
366 // practice however, this causes us to run out of stack space on large basic
367 // blocks. To avoid this problem, compute an ordering of the nodes where each
368 // node is only legalized after all of its operands are legalized.
Dan Gohman2d2a7a32008-09-30 18:30:35 +0000369 DAG.AssignTopologicalOrder();
370 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
371 E = prior(DAG.allnodes_end()); I != next(E); ++I)
372 HandleOp(SDValue(I, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373
374 // Finally, it's possible the root changed. Get the new root.
Dan Gohman8181bd12008-07-27 21:46:04 +0000375 SDValue OldRoot = DAG.getRoot();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
377 DAG.setRoot(LegalizedNodes[OldRoot]);
378
379 ExpandedNodes.clear();
380 LegalizedNodes.clear();
381 PromotedNodes.clear();
382 SplitNodes.clear();
383 ScalarizedNodes.clear();
Mon P Wang1448aad2008-10-30 08:01:45 +0000384 WidenNodes.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385
386 // Remove dead nodes now.
387 DAG.RemoveDeadNodes();
388}
389
390
391/// FindCallEndFromCallStart - Given a chained node that is part of a call
392/// sequence, find the CALLSEQ_END node that terminates the call sequence.
393static SDNode *FindCallEndFromCallStart(SDNode *Node) {
394 if (Node->getOpcode() == ISD::CALLSEQ_END)
395 return Node;
396 if (Node->use_empty())
397 return 0; // No CallSeqEnd
398
399 // The chain is usually at the end.
Dan Gohman8181bd12008-07-27 21:46:04 +0000400 SDValue TheChain(Node, Node->getNumValues()-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 if (TheChain.getValueType() != MVT::Other) {
402 // Sometimes it's at the beginning.
Dan Gohman8181bd12008-07-27 21:46:04 +0000403 TheChain = SDValue(Node, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 if (TheChain.getValueType() != MVT::Other) {
405 // Otherwise, hunt for it.
406 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
407 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000408 TheChain = SDValue(Node, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 break;
410 }
411
412 // Otherwise, we walked into a node without a chain.
413 if (TheChain.getValueType() != MVT::Other)
414 return 0;
415 }
416 }
417
418 for (SDNode::use_iterator UI = Node->use_begin(),
419 E = Node->use_end(); UI != E; ++UI) {
420
421 // Make sure to only follow users of our token chain.
Dan Gohman0c97f1d2008-07-27 20:43:25 +0000422 SDNode *User = *UI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
424 if (User->getOperand(i) == TheChain)
425 if (SDNode *Result = FindCallEndFromCallStart(User))
426 return Result;
427 }
428 return 0;
429}
430
431/// FindCallStartFromCallEnd - Given a chained node that is part of a call
432/// sequence, find the CALLSEQ_START node that initiates the call sequence.
433static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
434 assert(Node && "Didn't find callseq_start for a call??");
435 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
436
437 assert(Node->getOperand(0).getValueType() == MVT::Other &&
438 "Node doesn't have a token chain argument!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000439 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440}
441
442/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
443/// see if any uses can reach Dest. If no dest operands can get to dest,
444/// legalize them, legalize ourself, and return false, otherwise, return true.
445///
446/// Keep track of the nodes we fine that actually do lead to Dest in
447/// NodesLeadingTo. This avoids retraversing them exponential number of times.
448///
449bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
450 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
451 if (N == Dest) return true; // N certainly leads to Dest :)
452
453 // If we've already processed this node and it does lead to Dest, there is no
454 // need to reprocess it.
455 if (NodesLeadingTo.count(N)) return true;
456
457 // If the first result of this node has been already legalized, then it cannot
458 // reach N.
459 switch (getTypeAction(N->getValueType(0))) {
460 case Legal:
Dan Gohman8181bd12008-07-27 21:46:04 +0000461 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462 break;
463 case Promote:
Dan Gohman8181bd12008-07-27 21:46:04 +0000464 if (PromotedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 break;
466 case Expand:
Dan Gohman8181bd12008-07-27 21:46:04 +0000467 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 break;
469 }
470
471 // Okay, this node has not already been legalized. Check and legalize all
472 // operands. If none lead to Dest, then we can legalize this node.
473 bool OperandsLeadToDest = false;
474 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
475 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greif1c80d112008-08-28 21:40:38 +0000476 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477
478 if (OperandsLeadToDest) {
479 NodesLeadingTo.insert(N);
480 return true;
481 }
482
483 // Okay, this node looks safe, legalize it and return false.
Dan Gohman8181bd12008-07-27 21:46:04 +0000484 HandleOp(SDValue(N, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 return false;
486}
487
Mon P Wang1448aad2008-10-30 08:01:45 +0000488/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489/// appropriate for its type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000490void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000491 MVT VT = Op.getValueType();
Duncan Sandse016a2e2008-12-14 09:43:15 +0000492 // If the type legalizer was run then we should never see any illegal result
493 // types here except for target constants (the type legalizer does not touch
Mon P Wang26342922008-12-18 20:03:17 +0000494 // those) or for build vector used as a mask for a vector shuffle.
495 // FIXME: We can removed the BUILD_VECTOR case when we fix PR2957.
Duncan Sandse016a2e2008-12-14 09:43:15 +0000496 assert((TypesNeedLegalizing || getTypeAction(VT) == Legal ||
Mon P Wang26342922008-12-18 20:03:17 +0000497 Op.getOpcode() == ISD::TargetConstant ||
498 Op.getOpcode() == ISD::BUILD_VECTOR) &&
Duncan Sandse016a2e2008-12-14 09:43:15 +0000499 "Illegal type introduced after type legalization?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 switch (getTypeAction(VT)) {
501 default: assert(0 && "Bad type action!");
502 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang1448aad2008-10-30 08:01:45 +0000503 case Promote:
504 if (!VT.isVector()) {
505 (void)PromoteOp(Op);
506 break;
507 }
508 else {
509 // See if we can widen otherwise use Expand to either scalarize or split
510 MVT WidenVT = TLI.getWidenVectorType(VT);
511 if (WidenVT != MVT::Other) {
512 (void) WidenVectorOp(Op, WidenVT);
513 break;
514 }
515 // else fall thru to expand since we can't widen the vector
516 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +0000518 if (!VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 // If this is an illegal scalar, expand it into its two component
520 // pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +0000521 SDValue X, Y;
Chris Lattnerdad577b2007-08-25 01:00:22 +0000522 if (Op.getOpcode() == ISD::TargetConstant)
523 break; // Allow illegal target nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 ExpandOp(Op, X, Y);
Duncan Sands92c43912008-06-06 12:08:01 +0000525 } else if (VT.getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 // If this is an illegal single element vector, convert it to a
527 // scalar operation.
528 (void)ScalarizeVectorOp(Op);
529 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +0000530 // This is an illegal multiple element vector.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 // Split it in half and legalize both parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000532 SDValue X, Y;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 SplitVectorOp(Op, X, Y);
534 }
535 break;
536 }
537}
538
539/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
540/// a load from the constant pool.
Dan Gohman8181bd12008-07-27 21:46:04 +0000541static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542 SelectionDAG &DAG, TargetLowering &TLI) {
543 bool Extend = false;
544
545 // If a FP immediate is precise when represented as a float and if the
546 // target can do an extending load from float to double, we put it into
547 // the constant pool as a float, even if it's is statically typed as a
Chris Lattnere718cc52008-03-05 06:46:58 +0000548 // double. This shrinks FP constants and canonicalizes them for targets where
549 // an FP extending load is the same cost as a normal load (such as on the x87
550 // fp stack or PPC FP unit).
Duncan Sands92c43912008-06-06 12:08:01 +0000551 MVT VT = CFP->getValueType(0);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000552 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 if (!UseCP) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000554 if (VT!=MVT::f64 && VT!=MVT::f32)
555 assert(0 && "Invalid type expansion");
Dale Johannesen49cc7ce2008-10-09 18:53:47 +0000556 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Cheng354be062008-03-04 08:05:30 +0000557 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558 }
559
Duncan Sands92c43912008-06-06 12:08:01 +0000560 MVT OrigVT = VT;
561 MVT SVT = VT;
Evan Cheng354be062008-03-04 08:05:30 +0000562 while (SVT != MVT::f32) {
Duncan Sands92c43912008-06-06 12:08:01 +0000563 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Cheng354be062008-03-04 08:05:30 +0000564 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
565 // Only do this if the target has a native EXTLOAD instruction from
566 // smaller type.
Evan Cheng08c171a2008-10-14 21:26:46 +0000567 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattnere718cc52008-03-05 06:46:58 +0000568 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000569 const Type *SType = SVT.getTypeForMVT();
Evan Cheng354be062008-03-04 08:05:30 +0000570 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
571 VT = SVT;
572 Extend = true;
573 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 }
575
Dan Gohman8181bd12008-07-27 21:46:04 +0000576 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +0000577 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Cheng354be062008-03-04 08:05:30 +0000578 if (Extend)
579 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohmanfb020b62008-02-07 18:41:25 +0000580 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman04637d12008-09-16 22:05:41 +0000581 0, VT, false, Alignment);
Evan Cheng354be062008-03-04 08:05:30 +0000582 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +0000583 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584}
585
586
587/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
588/// operations.
589static
Dan Gohman8181bd12008-07-27 21:46:04 +0000590SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
591 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +0000592 MVT VT = Node->getValueType(0);
593 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
595 "fcopysign expansion only supported for f32 and f64");
Duncan Sands92c43912008-06-06 12:08:01 +0000596 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597
598 // First get the sign bit of second operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000599 SDValue Mask1 = (SrcVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
601 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
602 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000603 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
605 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands92c43912008-06-06 12:08:01 +0000606 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 if (SizeDiff > 0) {
608 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
609 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
610 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnere6fa1452008-07-10 23:46:13 +0000611 } else if (SizeDiff < 0) {
612 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
613 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
614 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
615 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616
617 // Clear the sign bit of first operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000618 SDValue Mask2 = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
620 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
621 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman8181bd12008-07-27 21:46:04 +0000622 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
624
625 // Or the value with the sign bit.
626 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
627 return Result;
628}
629
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000630/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
631static
Dan Gohman8181bd12008-07-27 21:46:04 +0000632SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
633 TargetLowering &TLI) {
634 SDValue Chain = ST->getChain();
635 SDValue Ptr = ST->getBasePtr();
636 SDValue Val = ST->getValue();
Duncan Sands92c43912008-06-06 12:08:01 +0000637 MVT VT = Val.getValueType();
Dale Johannesen08275382007-09-08 19:29:23 +0000638 int Alignment = ST->getAlignment();
639 int SVOffset = ST->getSrcValueOffset();
Duncan Sands92c43912008-06-06 12:08:01 +0000640 if (ST->getMemoryVT().isFloatingPoint() ||
641 ST->getMemoryVT().isVector()) {
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000642 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
643 if (TLI.isTypeLegal(intVT)) {
644 // Expand to a bitconvert of the value to the integer type of the
645 // same size, then a (misaligned) int store.
646 // FIXME: Does not handle truncating floating point stores!
647 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
648 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
649 SVOffset, ST->isVolatile(), Alignment);
650 } else {
651 // Do a (aligned) store to a stack slot, then copy from the stack slot
652 // to the final destination using (unaligned) integer loads and stores.
653 MVT StoredVT = ST->getMemoryVT();
654 MVT RegVT =
655 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
656 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
657 unsigned RegBytes = RegVT.getSizeInBits() / 8;
658 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen08275382007-09-08 19:29:23 +0000659
Duncan Sands734f49b2008-12-13 07:18:38 +0000660 // Make sure the stack slot is also aligned for the register type.
661 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
662
663 // Perform the original store, only redirected to the stack slot.
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000664 SDValue Store = DAG.getTruncStore(Chain, Val, StackPtr, NULL, 0,StoredVT);
665 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
666 SmallVector<SDValue, 8> Stores;
667 unsigned Offset = 0;
668
669 // Do all but one copies using the full register width.
670 for (unsigned i = 1; i < NumRegs; i++) {
671 // Load one integer register's worth from the stack slot.
672 SDValue Load = DAG.getLoad(RegVT, Store, StackPtr, NULL, 0);
673 // Store it to the final location. Remember the store.
674 Stores.push_back(DAG.getStore(Load.getValue(1), Load, Ptr,
675 ST->getSrcValue(), SVOffset + Offset,
676 ST->isVolatile(),
677 MinAlign(ST->getAlignment(), Offset)));
678 // Increment the pointers.
679 Offset += RegBytes;
680 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
681 Increment);
682 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
683 }
684
Duncan Sands734f49b2008-12-13 07:18:38 +0000685 // The last store may be partial. Do a truncating store. On big-endian
686 // machines this requires an extending load from the stack slot to ensure
687 // that the bits are in the right place.
688 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000689
Duncan Sands734f49b2008-12-13 07:18:38 +0000690 // Load from the stack slot.
691 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Store, StackPtr,
692 NULL, 0, MemVT);
693
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000694 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, Ptr,
695 ST->getSrcValue(), SVOffset + Offset,
Duncan Sands734f49b2008-12-13 07:18:38 +0000696 MemVT, ST->isVolatile(),
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000697 MinAlign(ST->getAlignment(), Offset)));
698 // The order of the stores doesn't matter - say it with a TokenFactor.
699 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
700 Stores.size());
701 }
Dale Johannesen08275382007-09-08 19:29:23 +0000702 }
Duncan Sands92c43912008-06-06 12:08:01 +0000703 assert(ST->getMemoryVT().isInteger() &&
704 !ST->getMemoryVT().isVector() &&
Dale Johannesen08275382007-09-08 19:29:23 +0000705 "Unaligned store of unknown type.");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000706 // Get the half-size VT
Duncan Sands92c43912008-06-06 12:08:01 +0000707 MVT NewStoredVT =
708 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
709 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000710 int IncrementSize = NumBits / 8;
711
712 // Divide the stored value in two parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000713 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
714 SDValue Lo = Val;
715 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000716
717 // Store the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000718 SDValue Store1, Store2;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000719 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
720 ST->getSrcValue(), SVOffset, NewStoredVT,
721 ST->isVolatile(), Alignment);
722 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
723 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsa3691432007-10-28 12:59:45 +0000724 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000725 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
726 ST->getSrcValue(), SVOffset + IncrementSize,
727 NewStoredVT, ST->isVolatile(), Alignment);
728
729 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
730}
731
732/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
733static
Dan Gohman8181bd12008-07-27 21:46:04 +0000734SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
735 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000736 int SVOffset = LD->getSrcValueOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +0000737 SDValue Chain = LD->getChain();
738 SDValue Ptr = LD->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +0000739 MVT VT = LD->getValueType(0);
740 MVT LoadedVT = LD->getMemoryVT();
741 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000742 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
743 if (TLI.isTypeLegal(intVT)) {
744 // Expand to a (misaligned) integer load of the same size,
745 // then bitconvert to floating point or vector.
746 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
747 SVOffset, LD->isVolatile(),
Dale Johannesen08275382007-09-08 19:29:23 +0000748 LD->getAlignment());
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000749 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
750 if (VT.isFloatingPoint() && LoadedVT != VT)
751 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
Dale Johannesen08275382007-09-08 19:29:23 +0000752
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000753 SDValue Ops[] = { Result, Chain };
754 return DAG.getMergeValues(Ops, 2);
755 } else {
756 // Copy the value to a (aligned) stack slot using (unaligned) integer
757 // loads and stores, then do a (aligned) load from the stack slot.
758 MVT RegVT = TLI.getRegisterType(intVT);
759 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
760 unsigned RegBytes = RegVT.getSizeInBits() / 8;
761 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
762
Duncan Sands734f49b2008-12-13 07:18:38 +0000763 // Make sure the stack slot is also aligned for the register type.
764 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
765
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000766 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
767 SmallVector<SDValue, 8> Stores;
768 SDValue StackPtr = StackBase;
769 unsigned Offset = 0;
770
771 // Do all but one copies using the full register width.
772 for (unsigned i = 1; i < NumRegs; i++) {
773 // Load one integer register's worth from the original location.
774 SDValue Load = DAG.getLoad(RegVT, Chain, Ptr, LD->getSrcValue(),
775 SVOffset + Offset, LD->isVolatile(),
776 MinAlign(LD->getAlignment(), Offset));
777 // Follow the load with a store to the stack slot. Remember the store.
778 Stores.push_back(DAG.getStore(Load.getValue(1), Load, StackPtr,
779 NULL, 0));
780 // Increment the pointers.
781 Offset += RegBytes;
782 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
783 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
784 Increment);
785 }
786
787 // The last copy may be partial. Do an extending load.
Duncan Sands734f49b2008-12-13 07:18:38 +0000788 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000789 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Chain, Ptr,
790 LD->getSrcValue(), SVOffset + Offset,
Duncan Sands734f49b2008-12-13 07:18:38 +0000791 MemVT, LD->isVolatile(),
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000792 MinAlign(LD->getAlignment(), Offset));
793 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sands734f49b2008-12-13 07:18:38 +0000794 // On big-endian machines this requires a truncating store to ensure
795 // that the bits end up in the right place.
796 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, StackPtr,
797 NULL, 0, MemVT));
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000798
799 // The order of the stores doesn't matter - say it with a TokenFactor.
800 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
801 Stores.size());
802
803 // Finally, perform the original load only redirected to the stack slot.
804 Load = DAG.getExtLoad(LD->getExtensionType(), VT, TF, StackBase,
805 NULL, 0, LoadedVT);
806
807 // Callers expect a MERGE_VALUES node.
808 SDValue Ops[] = { Load, TF };
809 return DAG.getMergeValues(Ops, 2);
810 }
Dale Johannesen08275382007-09-08 19:29:23 +0000811 }
Duncan Sands92c43912008-06-06 12:08:01 +0000812 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000813 "Unaligned load of unsupported type.");
814
Dale Johannesendc0ee192008-02-27 22:36:00 +0000815 // Compute the new VT that is half the size of the old one. This is an
816 // integer MVT.
Duncan Sands92c43912008-06-06 12:08:01 +0000817 unsigned NumBits = LoadedVT.getSizeInBits();
818 MVT NewLoadedVT;
819 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000820 NumBits >>= 1;
821
822 unsigned Alignment = LD->getAlignment();
823 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000824 ISD::LoadExtType HiExtType = LD->getExtensionType();
825
826 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
827 if (HiExtType == ISD::NON_EXTLOAD)
828 HiExtType = ISD::ZEXTLOAD;
829
830 // Load the value in two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000831 SDValue Lo, Hi;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000832 if (TLI.isLittleEndian()) {
833 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
834 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
835 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
836 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
837 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
838 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000839 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000840 } else {
841 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
842 NewLoadedVT,LD->isVolatile(), Alignment);
843 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
844 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
845 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
846 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000847 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000848 }
849
850 // aggregate the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000851 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
852 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000853 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
854
Dan Gohman8181bd12008-07-27 21:46:04 +0000855 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000856 Hi.getValue(1));
857
Dan Gohman8181bd12008-07-27 21:46:04 +0000858 SDValue Ops[] = { Result, TF };
Duncan Sands698842f2008-07-02 17:40:58 +0000859 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000860}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861
Dan Gohman6d05cac2007-10-11 23:57:53 +0000862/// UnrollVectorOp - We know that the given vector has a legal type, however
863/// the operation it performs is not legal and is an operation that we have
864/// no way of lowering. "Unroll" the vector, splitting out the scalars and
865/// operating on each element individually.
Dan Gohman8181bd12008-07-27 21:46:04 +0000866SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000867 MVT VT = Op.getValueType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000868 assert(isTypeLegal(VT) &&
869 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000870 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman6d05cac2007-10-11 23:57:53 +0000871 "Can't unroll a vector with multiple results!");
Duncan Sands92c43912008-06-06 12:08:01 +0000872 unsigned NE = VT.getVectorNumElements();
873 MVT EltVT = VT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000874
Dan Gohman8181bd12008-07-27 21:46:04 +0000875 SmallVector<SDValue, 8> Scalars;
876 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman6d05cac2007-10-11 23:57:53 +0000877 for (unsigned i = 0; i != NE; ++i) {
878 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000879 SDValue Operand = Op.getOperand(j);
Duncan Sands92c43912008-06-06 12:08:01 +0000880 MVT OperandVT = Operand.getValueType();
881 if (OperandVT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +0000882 // A vector operand; extract a single element.
Duncan Sands92c43912008-06-06 12:08:01 +0000883 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000884 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
885 OperandEltVT,
886 Operand,
887 DAG.getConstant(i, MVT::i32));
888 } else {
889 // A scalar operand; just use it as is.
890 Operands[j] = Operand;
891 }
892 }
Mon P Wang9901e732008-12-09 05:46:39 +0000893
894 switch (Op.getOpcode()) {
895 default:
896 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
897 &Operands[0], Operands.size()));
898 break;
899 case ISD::SHL:
900 case ISD::SRA:
901 case ISD::SRL:
902 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT, Operands[0],
903 LegalizeShiftAmount(Operands[1])));
904 break;
905 }
Dan Gohman6d05cac2007-10-11 23:57:53 +0000906 }
907
908 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
909}
910
Duncan Sands37a3f472008-01-10 10:28:30 +0000911/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands92c43912008-06-06 12:08:01 +0000912static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands37a3f472008-01-10 10:28:30 +0000913 RTLIB::Libcall Call_F32,
914 RTLIB::Libcall Call_F64,
915 RTLIB::Libcall Call_F80,
916 RTLIB::Libcall Call_PPCF128) {
917 return
918 VT == MVT::f32 ? Call_F32 :
919 VT == MVT::f64 ? Call_F64 :
920 VT == MVT::f80 ? Call_F80 :
921 VT == MVT::ppcf128 ? Call_PPCF128 :
922 RTLIB::UNKNOWN_LIBCALL;
923}
924
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000925/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
926/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
927/// is necessary to spill the vector being inserted into to memory, perform
928/// the insert there, and then read the result back.
Dan Gohman8181bd12008-07-27 21:46:04 +0000929SDValue SelectionDAGLegalize::
930PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
931 SDValue Tmp1 = Vec;
932 SDValue Tmp2 = Val;
933 SDValue Tmp3 = Idx;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000934
935 // If the target doesn't support this, we have to spill the input vector
936 // to a temporary stack slot, update the element, then reload it. This is
937 // badness. We could also load the value into a vector register (either
938 // with a "move to register" or "extload into register" instruction, then
939 // permute it into place, if the idx is a constant and if the idx is
940 // supported by the target.
Duncan Sands92c43912008-06-06 12:08:01 +0000941 MVT VT = Tmp1.getValueType();
942 MVT EltVT = VT.getVectorElementType();
943 MVT IdxVT = Tmp3.getValueType();
944 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +0000945 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000946
Gabor Greif1c80d112008-08-28 21:40:38 +0000947 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000948
949 // Store the vector.
Dan Gohman8181bd12008-07-27 21:46:04 +0000950 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang1448aad2008-10-30 08:01:45 +0000951 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000952
953 // Truncate or zero extend offset to target pointer type.
Duncan Sandsec142ee2008-06-08 20:54:56 +0000954 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000955 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
956 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +0000957 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000958 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman8181bd12008-07-27 21:46:04 +0000959 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000960 // Store the scalar value.
961 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000962 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000963 // Load the updated vector.
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000964 return DAG.getLoad(VT, Ch, StackPtr,
965 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000966}
967
Mon P Wang9901e732008-12-09 05:46:39 +0000968SDValue SelectionDAGLegalize::LegalizeShiftAmount(SDValue ShiftAmt) {
969 if (TLI.getShiftAmountTy().bitsLT(ShiftAmt.getValueType()))
970 return DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), ShiftAmt);
971
972 if (TLI.getShiftAmountTy().bitsGT(ShiftAmt.getValueType()))
973 return DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), ShiftAmt);
974
975 return ShiftAmt;
976}
977
978
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000979/// LegalizeOp - We know that the specified value has a legal type, and
980/// that its operands are legal. Now ensure that the operation itself
981/// is legal, recursively ensuring that the operands' operations remain
982/// legal.
Dan Gohman8181bd12008-07-27 21:46:04 +0000983SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattnerdad577b2007-08-25 01:00:22 +0000984 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
985 return Op;
986
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000987 assert(isTypeLegal(Op.getValueType()) &&
988 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000989 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000990
991 // If this operation defines any values that cannot be represented in a
992 // register on this target, make sure to expand or promote them.
993 if (Node->getNumValues() > 1) {
994 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
995 if (getTypeAction(Node->getValueType(i)) != Legal) {
996 HandleOp(Op.getValue(i));
997 assert(LegalizedNodes.count(Op) &&
998 "Handling didn't add legal operands!");
999 return LegalizedNodes[Op];
1000 }
1001 }
1002
1003 // Note that LegalizeOp may be reentered even from single-use nodes, which
1004 // means that we always must cache transformed nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00001005 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006 if (I != LegalizedNodes.end()) return I->second;
1007
Dan Gohman8181bd12008-07-27 21:46:04 +00001008 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
1009 SDValue Result = Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 bool isCustom = false;
1011
1012 switch (Node->getOpcode()) {
1013 case ISD::FrameIndex:
1014 case ISD::EntryToken:
1015 case ISD::Register:
1016 case ISD::BasicBlock:
1017 case ISD::TargetFrameIndex:
1018 case ISD::TargetJumpTable:
1019 case ISD::TargetConstant:
1020 case ISD::TargetConstantFP:
1021 case ISD::TargetConstantPool:
1022 case ISD::TargetGlobalAddress:
1023 case ISD::TargetGlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +00001024 case ISD::TargetExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 case ISD::VALUETYPE:
1026 case ISD::SRCVALUE:
Dan Gohman12a9c082008-02-06 22:27:42 +00001027 case ISD::MEMOPERAND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001028 case ISD::CONDCODE:
Duncan Sandsc93fae32008-03-21 09:14:45 +00001029 case ISD::ARG_FLAGS:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00001031 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032 "This must be legal!");
1033 break;
1034 default:
1035 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1036 // If this is a target node, legalize it by legalizing the operands then
1037 // passing it through.
Dan Gohman8181bd12008-07-27 21:46:04 +00001038 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1040 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1041
1042 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
1043
1044 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1045 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00001046 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047 }
1048 // Otherwise this is an unhandled builtin node. splat.
1049#ifndef NDEBUG
1050 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
1051#endif
1052 assert(0 && "Do not know how to legalize this operator!");
1053 abort();
1054 case ISD::GLOBAL_OFFSET_TABLE:
1055 case ISD::GlobalAddress:
1056 case ISD::GlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +00001057 case ISD::ExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 case ISD::ConstantPool:
1059 case ISD::JumpTable: // Nothing to do.
1060 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1061 default: assert(0 && "This action is not supported yet!");
1062 case TargetLowering::Custom:
1063 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001064 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001065 // FALLTHROUGH if the target doesn't want to lower this op after all.
1066 case TargetLowering::Legal:
1067 break;
1068 }
1069 break;
1070 case ISD::FRAMEADDR:
1071 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001072 // The only option for these nodes is to custom lower them. If the target
1073 // does not custom lower them, then return zero.
1074 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001075 if (Tmp1.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001076 Result = Tmp1;
1077 else
1078 Result = DAG.getConstant(0, TLI.getPointerTy());
1079 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +00001080 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands92c43912008-06-06 12:08:01 +00001081 MVT VT = Node->getValueType(0);
Anton Korobeynikov09386bd2007-08-29 19:28:29 +00001082 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1083 default: assert(0 && "This action is not supported yet!");
1084 case TargetLowering::Custom:
1085 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001086 if (Result.getNode()) break;
Anton Korobeynikov09386bd2007-08-29 19:28:29 +00001087 // Fall Thru
1088 case TargetLowering::Legal:
1089 Result = DAG.getConstant(0, VT);
1090 break;
1091 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +00001092 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +00001093 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001094 case ISD::EXCEPTIONADDR: {
1095 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00001096 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001097 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1098 default: assert(0 && "This action is not supported yet!");
1099 case TargetLowering::Expand: {
1100 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001101 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001102 }
1103 break;
1104 case TargetLowering::Custom:
1105 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001106 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001107 // Fall Thru
1108 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001109 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands698842f2008-07-02 17:40:58 +00001110 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001111 break;
1112 }
1113 }
1114 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001115 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001116
Gabor Greif1c80d112008-08-28 21:40:38 +00001117 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001118 "Cannot return more than two values!");
1119
1120 // Since we produced two values, make sure to remember that we
1121 // legalized both of them.
1122 Tmp1 = LegalizeOp(Result);
1123 Tmp2 = LegalizeOp(Result.getValue(1));
1124 AddLegalizedOperand(Op.getValue(0), Tmp1);
1125 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001126 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001127 case ISD::EHSELECTION: {
1128 Tmp1 = LegalizeOp(Node->getOperand(0));
1129 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00001130 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001131 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1132 default: assert(0 && "This action is not supported yet!");
1133 case TargetLowering::Expand: {
1134 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001135 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001136 }
1137 break;
1138 case TargetLowering::Custom:
1139 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001140 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001141 // Fall Thru
1142 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001143 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands698842f2008-07-02 17:40:58 +00001144 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145 break;
1146 }
1147 }
1148 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001149 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001150
Gabor Greif1c80d112008-08-28 21:40:38 +00001151 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001152 "Cannot return more than two values!");
1153
1154 // Since we produced two values, make sure to remember that we
1155 // legalized both of them.
1156 Tmp1 = LegalizeOp(Result);
1157 Tmp2 = LegalizeOp(Result.getValue(1));
1158 AddLegalizedOperand(Op.getValue(0), Tmp1);
1159 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001160 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001161 case ISD::EH_RETURN: {
Duncan Sands92c43912008-06-06 12:08:01 +00001162 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001163 // The only "good" option for this node is to custom lower it.
1164 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1165 default: assert(0 && "This action is not supported at all!");
1166 case TargetLowering::Custom:
1167 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001168 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001169 // Fall Thru
1170 case TargetLowering::Legal:
1171 // Target does not know, how to lower this, lower to noop
1172 Result = LegalizeOp(Node->getOperand(0));
1173 break;
1174 }
1175 }
1176 break;
1177 case ISD::AssertSext:
1178 case ISD::AssertZext:
1179 Tmp1 = LegalizeOp(Node->getOperand(0));
1180 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1181 break;
1182 case ISD::MERGE_VALUES:
1183 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif46bf5472008-08-26 22:36:50 +00001184 Result = Node->getOperand(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001185 break;
1186 case ISD::CopyFromReg:
1187 Tmp1 = LegalizeOp(Node->getOperand(0));
1188 Result = Op.getValue(0);
1189 if (Node->getNumValues() == 2) {
1190 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1191 } else {
1192 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
1193 if (Node->getNumOperands() == 3) {
1194 Tmp2 = LegalizeOp(Node->getOperand(2));
1195 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1196 } else {
1197 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1198 }
1199 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1200 }
1201 // Since CopyFromReg produces two values, make sure to remember that we
1202 // legalized both of them.
1203 AddLegalizedOperand(Op.getValue(0), Result);
1204 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001205 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001206 case ISD::UNDEF: {
Duncan Sands92c43912008-06-06 12:08:01 +00001207 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001208 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
1209 default: assert(0 && "This action is not supported yet!");
1210 case TargetLowering::Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00001211 if (VT.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001212 Result = DAG.getConstant(0, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00001213 else if (VT.isFloatingPoint())
1214 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesen20b76352007-09-26 17:26:49 +00001215 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001216 else
1217 assert(0 && "Unknown value type!");
1218 break;
1219 case TargetLowering::Legal:
1220 break;
1221 }
1222 break;
1223 }
1224
1225 case ISD::INTRINSIC_W_CHAIN:
1226 case ISD::INTRINSIC_WO_CHAIN:
1227 case ISD::INTRINSIC_VOID: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001228 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001229 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1230 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1231 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1232
1233 // Allow the target to custom lower its intrinsics if it wants to.
1234 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1235 TargetLowering::Custom) {
1236 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001237 if (Tmp3.getNode()) Result = Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001238 }
1239
Gabor Greif1c80d112008-08-28 21:40:38 +00001240 if (Result.getNode()->getNumValues() == 1) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001241
1242 // Must have return value and chain result.
Gabor Greif1c80d112008-08-28 21:40:38 +00001243 assert(Result.getNode()->getNumValues() == 2 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 "Cannot return more than two values!");
1245
1246 // Since loads produce two values, make sure to remember that we
1247 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001248 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1249 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001250 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001251 }
1252
Dan Gohman472d12c2008-06-30 20:59:49 +00001253 case ISD::DBG_STOPPOINT:
1254 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001255 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1256
Dan Gohman472d12c2008-06-30 20:59:49 +00001257 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001258 case TargetLowering::Promote:
1259 default: assert(0 && "This action is not supported yet!");
1260 case TargetLowering::Expand: {
1261 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1262 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001263 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264
Dan Gohman472d12c2008-06-30 20:59:49 +00001265 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001266 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman472d12c2008-06-30 20:59:49 +00001267 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1268 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269
Dan Gohman472d12c2008-06-30 20:59:49 +00001270 unsigned Line = DSP->getLine();
1271 unsigned Col = DSP->getColumn();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001272
1273 if (useDEBUG_LOC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001274 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Chengd6f57682008-07-08 20:06:39 +00001275 DAG.getConstant(Col, MVT::i32),
1276 DAG.getConstant(SrcFile, MVT::i32) };
1277 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001278 } else {
Evan Cheng69eda822008-02-01 02:05:57 +00001279 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001280 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001281 }
1282 } else {
1283 Result = Tmp1; // chain
1284 }
1285 break;
1286 }
Evan Chengd6f57682008-07-08 20:06:39 +00001287 case TargetLowering::Legal: {
1288 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1289 if (Action == Legal && Tmp1 == Node->getOperand(0))
1290 break;
1291
Dan Gohman8181bd12008-07-27 21:46:04 +00001292 SmallVector<SDValue, 8> Ops;
Evan Chengd6f57682008-07-08 20:06:39 +00001293 Ops.push_back(Tmp1);
1294 if (Action == Legal) {
1295 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1296 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1297 } else {
1298 // Otherwise promote them.
1299 Ops.push_back(PromoteOp(Node->getOperand(1)));
1300 Ops.push_back(PromoteOp(Node->getOperand(2)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001301 }
Evan Chengd6f57682008-07-08 20:06:39 +00001302 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1303 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1304 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001305 break;
1306 }
Evan Chengd6f57682008-07-08 20:06:39 +00001307 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001308 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001309
1310 case ISD::DECLARE:
1311 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1312 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1313 default: assert(0 && "This action is not supported yet!");
1314 case TargetLowering::Legal:
1315 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1316 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1317 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1318 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1319 break;
Chris Lattner203cd052008-02-28 05:53:40 +00001320 case TargetLowering::Expand:
1321 Result = LegalizeOp(Node->getOperand(0));
1322 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001323 }
1324 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001325
1326 case ISD::DEBUG_LOC:
1327 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1328 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1329 default: assert(0 && "This action is not supported yet!");
Evan Chengd6f57682008-07-08 20:06:39 +00001330 case TargetLowering::Legal: {
1331 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001332 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Chengd6f57682008-07-08 20:06:39 +00001333 if (Action == Legal && Tmp1 == Node->getOperand(0))
1334 break;
1335 if (Action == Legal) {
1336 Tmp2 = Node->getOperand(1);
1337 Tmp3 = Node->getOperand(2);
1338 Tmp4 = Node->getOperand(3);
1339 } else {
1340 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1341 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1342 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1343 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001344 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1345 break;
1346 }
Evan Chengd6f57682008-07-08 20:06:39 +00001347 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001348 break;
1349
Dan Gohmanfa607c92008-07-01 00:05:16 +00001350 case ISD::DBG_LABEL:
1351 case ISD::EH_LABEL:
1352 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1353 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001354 default: assert(0 && "This action is not supported yet!");
1355 case TargetLowering::Legal:
1356 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohmanfa607c92008-07-01 00:05:16 +00001357 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001358 break;
1359 case TargetLowering::Expand:
1360 Result = LegalizeOp(Node->getOperand(0));
1361 break;
1362 }
1363 break;
1364
Evan Chengd1d68072008-03-08 00:58:38 +00001365 case ISD::PREFETCH:
1366 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1367 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1368 default: assert(0 && "This action is not supported yet!");
1369 case TargetLowering::Legal:
1370 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1371 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1372 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1373 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1374 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1375 break;
1376 case TargetLowering::Expand:
1377 // It's a noop.
1378 Result = LegalizeOp(Node->getOperand(0));
1379 break;
1380 }
1381 break;
1382
Andrew Lenharth785610d2008-02-16 01:24:58 +00001383 case ISD::MEMBARRIER: {
1384 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001385 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1386 default: assert(0 && "This action is not supported yet!");
1387 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001388 SDValue Ops[6];
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001389 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands3ee041a2008-02-27 08:53:44 +00001390 for (int x = 1; x < 6; ++x) {
1391 Ops[x] = Node->getOperand(x);
1392 if (!isTypeLegal(Ops[x].getValueType()))
1393 Ops[x] = PromoteOp(Ops[x]);
1394 }
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001395 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1396 break;
1397 }
1398 case TargetLowering::Expand:
1399 //There is no libgcc call for this op
1400 Result = Node->getOperand(0); // Noop
1401 break;
1402 }
Andrew Lenharth785610d2008-02-16 01:24:58 +00001403 break;
1404 }
1405
Dale Johannesenbc187662008-08-28 02:44:49 +00001406 case ISD::ATOMIC_CMP_SWAP_8:
1407 case ISD::ATOMIC_CMP_SWAP_16:
1408 case ISD::ATOMIC_CMP_SWAP_32:
1409 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001410 unsigned int num_operands = 4;
1411 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001412 SDValue Ops[4];
Mon P Wang078a62d2008-05-05 19:05:59 +00001413 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001414 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang078a62d2008-05-05 19:05:59 +00001415 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1416
1417 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1418 default: assert(0 && "This action is not supported yet!");
1419 case TargetLowering::Custom:
1420 Result = TLI.LowerOperation(Result, DAG);
1421 break;
1422 case TargetLowering::Legal:
1423 break;
1424 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001425 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1426 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001427 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001428 }
Dale Johannesenbc187662008-08-28 02:44:49 +00001429 case ISD::ATOMIC_LOAD_ADD_8:
1430 case ISD::ATOMIC_LOAD_SUB_8:
1431 case ISD::ATOMIC_LOAD_AND_8:
1432 case ISD::ATOMIC_LOAD_OR_8:
1433 case ISD::ATOMIC_LOAD_XOR_8:
1434 case ISD::ATOMIC_LOAD_NAND_8:
1435 case ISD::ATOMIC_LOAD_MIN_8:
1436 case ISD::ATOMIC_LOAD_MAX_8:
1437 case ISD::ATOMIC_LOAD_UMIN_8:
1438 case ISD::ATOMIC_LOAD_UMAX_8:
1439 case ISD::ATOMIC_SWAP_8:
1440 case ISD::ATOMIC_LOAD_ADD_16:
1441 case ISD::ATOMIC_LOAD_SUB_16:
1442 case ISD::ATOMIC_LOAD_AND_16:
1443 case ISD::ATOMIC_LOAD_OR_16:
1444 case ISD::ATOMIC_LOAD_XOR_16:
1445 case ISD::ATOMIC_LOAD_NAND_16:
1446 case ISD::ATOMIC_LOAD_MIN_16:
1447 case ISD::ATOMIC_LOAD_MAX_16:
1448 case ISD::ATOMIC_LOAD_UMIN_16:
1449 case ISD::ATOMIC_LOAD_UMAX_16:
1450 case ISD::ATOMIC_SWAP_16:
1451 case ISD::ATOMIC_LOAD_ADD_32:
1452 case ISD::ATOMIC_LOAD_SUB_32:
1453 case ISD::ATOMIC_LOAD_AND_32:
1454 case ISD::ATOMIC_LOAD_OR_32:
1455 case ISD::ATOMIC_LOAD_XOR_32:
1456 case ISD::ATOMIC_LOAD_NAND_32:
1457 case ISD::ATOMIC_LOAD_MIN_32:
1458 case ISD::ATOMIC_LOAD_MAX_32:
1459 case ISD::ATOMIC_LOAD_UMIN_32:
1460 case ISD::ATOMIC_LOAD_UMAX_32:
1461 case ISD::ATOMIC_SWAP_32:
1462 case ISD::ATOMIC_LOAD_ADD_64:
1463 case ISD::ATOMIC_LOAD_SUB_64:
1464 case ISD::ATOMIC_LOAD_AND_64:
1465 case ISD::ATOMIC_LOAD_OR_64:
1466 case ISD::ATOMIC_LOAD_XOR_64:
1467 case ISD::ATOMIC_LOAD_NAND_64:
1468 case ISD::ATOMIC_LOAD_MIN_64:
1469 case ISD::ATOMIC_LOAD_MAX_64:
1470 case ISD::ATOMIC_LOAD_UMIN_64:
1471 case ISD::ATOMIC_LOAD_UMAX_64:
1472 case ISD::ATOMIC_SWAP_64: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001473 unsigned int num_operands = 3;
1474 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001475 SDValue Ops[3];
Mon P Wang078a62d2008-05-05 19:05:59 +00001476 for (unsigned int x = 0; x < num_operands; ++x)
1477 Ops[x] = LegalizeOp(Node->getOperand(x));
1478 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sandsac496a12008-07-04 11:47:58 +00001479
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001480 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001481 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001482 case TargetLowering::Custom:
1483 Result = TLI.LowerOperation(Result, DAG);
1484 break;
1485 case TargetLowering::Legal:
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001486 break;
1487 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001488 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1489 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001490 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001491 }
Scott Michelf2e2b702007-08-08 23:23:31 +00001492 case ISD::Constant: {
1493 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1494 unsigned opAction =
1495 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1496
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001497 // We know we don't need to expand constants here, constants only have one
1498 // value and we check that it is fine above.
1499
Scott Michelf2e2b702007-08-08 23:23:31 +00001500 if (opAction == TargetLowering::Custom) {
1501 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001502 if (Tmp1.getNode())
Scott Michelf2e2b702007-08-08 23:23:31 +00001503 Result = Tmp1;
1504 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001505 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001506 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001507 case ISD::ConstantFP: {
1508 // Spill FP immediates to the constant pool if the target cannot directly
1509 // codegen them. Targets often have some immediate values that can be
1510 // efficiently generated into an FP register without a load. We explicitly
1511 // leave these constants as ConstantFP nodes for the target to deal with.
1512 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1513
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001514 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1515 default: assert(0 && "This action is not supported yet!");
Nate Begemane2ba64f2008-02-14 08:57:00 +00001516 case TargetLowering::Legal:
1517 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518 case TargetLowering::Custom:
1519 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001520 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001521 Result = Tmp3;
1522 break;
1523 }
1524 // FALLTHROUGH
Nate Begemane2ba64f2008-02-14 08:57:00 +00001525 case TargetLowering::Expand: {
1526 // Check to see if this FP immediate is already legal.
1527 bool isLegal = false;
1528 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1529 E = TLI.legal_fpimm_end(); I != E; ++I) {
1530 if (CFP->isExactlyValue(*I)) {
1531 isLegal = true;
1532 break;
1533 }
1534 }
1535 // If this is a legal constant, turn it into a TargetConstantFP node.
1536 if (isLegal)
1537 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001538 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1539 }
Nate Begemane2ba64f2008-02-14 08:57:00 +00001540 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001541 break;
1542 }
1543 case ISD::TokenFactor:
1544 if (Node->getNumOperands() == 2) {
1545 Tmp1 = LegalizeOp(Node->getOperand(0));
1546 Tmp2 = LegalizeOp(Node->getOperand(1));
1547 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1548 } else if (Node->getNumOperands() == 3) {
1549 Tmp1 = LegalizeOp(Node->getOperand(0));
1550 Tmp2 = LegalizeOp(Node->getOperand(1));
1551 Tmp3 = LegalizeOp(Node->getOperand(2));
1552 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1553 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00001554 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001555 // Legalize the operands.
1556 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1557 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1558 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1559 }
1560 break;
1561
1562 case ISD::FORMAL_ARGUMENTS:
1563 case ISD::CALL:
1564 // The only option for this is to custom lower it.
1565 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001566 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesenac246272008-03-05 19:14:03 +00001567 // A call within a calling sequence must be legalized to something
1568 // other than the normal CALLSEQ_END. Violating this gets Legalize
1569 // into an infinite loop.
1570 assert ((!IsLegalizingCall ||
1571 Node->getOpcode() != ISD::CALL ||
Gabor Greif1c80d112008-08-28 21:40:38 +00001572 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesenac246272008-03-05 19:14:03 +00001573 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001574
1575 // The number of incoming and outgoing values should match; unless the final
1576 // outgoing value is a flag.
Gabor Greif1c80d112008-08-28 21:40:38 +00001577 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1578 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1579 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling22f8deb2007-11-13 00:44:25 +00001580 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001581 "Lowering call/formal_arguments produced unexpected # results!");
1582
1583 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1584 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greif1c80d112008-08-28 21:40:38 +00001585 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1586 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling22f8deb2007-11-13 00:44:25 +00001587 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001588 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00001589 if (Op.getResNo() == i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001590 Tmp2 = Tmp1;
Dan Gohman8181bd12008-07-27 21:46:04 +00001591 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001592 }
1593 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001594 case ISD::EXTRACT_SUBREG: {
1595 Tmp1 = LegalizeOp(Node->getOperand(0));
1596 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1597 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001598 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001599 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1600 }
1601 break;
1602 case ISD::INSERT_SUBREG: {
1603 Tmp1 = LegalizeOp(Node->getOperand(0));
1604 Tmp2 = LegalizeOp(Node->getOperand(1));
1605 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1606 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001607 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001608 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1609 }
1610 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001611 case ISD::BUILD_VECTOR:
1612 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1613 default: assert(0 && "This action is not supported yet!");
1614 case TargetLowering::Custom:
1615 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001616 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001617 Result = Tmp3;
1618 break;
1619 }
1620 // FALLTHROUGH
1621 case TargetLowering::Expand:
Gabor Greif1c80d112008-08-28 21:40:38 +00001622 Result = ExpandBUILD_VECTOR(Result.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001623 break;
1624 }
1625 break;
1626 case ISD::INSERT_VECTOR_ELT:
1627 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001628 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001629
1630 // The type of the value to insert may not be legal, even though the vector
1631 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1632 // here.
1633 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1634 default: assert(0 && "Cannot expand insert element operand");
1635 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1636 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang1448aad2008-10-30 08:01:45 +00001637 case Expand:
1638 // FIXME: An alternative would be to check to see if the target is not
1639 // going to custom lower this operation, we could bitcast to half elt
1640 // width and perform two inserts at that width, if that is legal.
1641 Tmp2 = Node->getOperand(1);
1642 break;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001643 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001644 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1645
1646 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1647 Node->getValueType(0))) {
1648 default: assert(0 && "This action is not supported yet!");
1649 case TargetLowering::Legal:
1650 break;
1651 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001652 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001653 if (Tmp4.getNode()) {
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001654 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001655 break;
1656 }
1657 // FALLTHROUGH
Mon P Wang1448aad2008-10-30 08:01:45 +00001658 case TargetLowering::Promote:
1659 // Fall thru for vector case
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001660 case TargetLowering::Expand: {
1661 // If the insert index is a constant, codegen this as a scalar_to_vector,
1662 // then a shuffle that inserts it into the right position in the vector.
1663 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001664 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1665 // match the element type of the vector being created.
1666 if (Tmp2.getValueType() ==
Duncan Sands92c43912008-06-06 12:08:01 +00001667 Op.getValueType().getVectorElementType()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001668 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001669 Tmp1.getValueType(), Tmp2);
1670
Duncan Sands92c43912008-06-06 12:08:01 +00001671 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1672 MVT ShufMaskVT =
1673 MVT::getIntVectorWithNumElements(NumElts);
1674 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001675
1676 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1677 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1678 // elt 0 of the RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00001679 SmallVector<SDValue, 8> ShufOps;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001680 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001681 if (i != InsertPos->getZExtValue())
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001682 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1683 else
1684 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1685 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001686 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001687 &ShufOps[0], ShufOps.size());
1688
1689 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1690 Tmp1, ScVec, ShufMask);
1691 Result = LegalizeOp(Result);
1692 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001693 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001694 }
Nate Begeman7c9e4b72008-04-25 18:07:40 +00001695 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001696 break;
1697 }
1698 }
1699 break;
1700 case ISD::SCALAR_TO_VECTOR:
1701 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1702 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1703 break;
1704 }
1705
1706 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1707 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1708 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1709 Node->getValueType(0))) {
1710 default: assert(0 && "This action is not supported yet!");
1711 case TargetLowering::Legal:
1712 break;
1713 case TargetLowering::Custom:
1714 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001715 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001716 Result = Tmp3;
1717 break;
1718 }
1719 // FALLTHROUGH
1720 case TargetLowering::Expand:
1721 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1722 break;
1723 }
1724 break;
1725 case ISD::VECTOR_SHUFFLE:
1726 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1727 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1728 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1729
1730 // Allow targets to custom lower the SHUFFLEs they support.
1731 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1732 default: assert(0 && "Unknown operation action!");
1733 case TargetLowering::Legal:
1734 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1735 "vector shuffle should not be created if not legal!");
1736 break;
1737 case TargetLowering::Custom:
1738 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001739 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001740 Result = Tmp3;
1741 break;
1742 }
1743 // FALLTHROUGH
1744 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00001745 MVT VT = Node->getValueType(0);
1746 MVT EltVT = VT.getVectorElementType();
1747 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00001748 SDValue Mask = Node->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001749 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00001750 SmallVector<SDValue,8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001751 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001752 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001753 if (Arg.getOpcode() == ISD::UNDEF) {
1754 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1755 } else {
1756 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001757 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001758 if (Idx < NumElems)
1759 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1760 DAG.getConstant(Idx, PtrVT)));
1761 else
1762 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1763 DAG.getConstant(Idx - NumElems, PtrVT)));
1764 }
1765 }
1766 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1767 break;
1768 }
1769 case TargetLowering::Promote: {
1770 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00001771 MVT OVT = Node->getValueType(0);
1772 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001773
1774 // Cast the two input vectors.
1775 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1776 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1777
1778 // Convert the shuffle mask to the right # elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00001779 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001780 assert(Tmp3.getNode() && "Shuffle not legal?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001781 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1782 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1783 break;
1784 }
1785 }
1786 break;
1787
1788 case ISD::EXTRACT_VECTOR_ELT:
1789 Tmp1 = Node->getOperand(0);
1790 Tmp2 = LegalizeOp(Node->getOperand(1));
1791 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1792 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1793 break;
1794
1795 case ISD::EXTRACT_SUBVECTOR:
1796 Tmp1 = Node->getOperand(0);
1797 Tmp2 = LegalizeOp(Node->getOperand(1));
1798 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1799 Result = ExpandEXTRACT_SUBVECTOR(Result);
1800 break;
1801
Mon P Wang1448aad2008-10-30 08:01:45 +00001802 case ISD::CONCAT_VECTORS: {
1803 // Use extract/insert/build vector for now. We might try to be
1804 // more clever later.
1805 MVT PtrVT = TLI.getPointerTy();
1806 SmallVector<SDValue, 8> Ops;
1807 unsigned NumOperands = Node->getNumOperands();
1808 for (unsigned i=0; i < NumOperands; ++i) {
1809 SDValue SubOp = Node->getOperand(i);
1810 MVT VVT = SubOp.getNode()->getValueType(0);
1811 MVT EltVT = VVT.getVectorElementType();
1812 unsigned NumSubElem = VVT.getVectorNumElements();
1813 for (unsigned j=0; j < NumSubElem; ++j) {
1814 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1815 DAG.getConstant(j, PtrVT)));
1816 }
1817 }
1818 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1819 &Ops[0], Ops.size()));
1820 }
1821
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001822 case ISD::CALLSEQ_START: {
1823 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1824
1825 // Recursively Legalize all of the inputs of the call end that do not lead
1826 // to this call start. This ensures that any libcalls that need be inserted
1827 // are inserted *before* the CALLSEQ_START.
1828 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1829 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greif1c80d112008-08-28 21:40:38 +00001830 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001831 NodesLeadingTo);
1832 }
1833
1834 // Now that we legalized all of the inputs (which may have inserted
1835 // libcalls) create the new CALLSEQ_START node.
1836 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1837
1838 // Merge in the last call, to ensure that this call start after the last
1839 // call ended.
1840 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1841 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1842 Tmp1 = LegalizeOp(Tmp1);
1843 }
1844
1845 // Do not try to legalize the target-specific arguments (#1+).
1846 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001847 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001848 Ops[0] = Tmp1;
1849 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1850 }
1851
1852 // Remember that the CALLSEQ_START is legalized.
1853 AddLegalizedOperand(Op.getValue(0), Result);
1854 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1855 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1856
1857 // Now that the callseq_start and all of the non-call nodes above this call
1858 // sequence have been legalized, legalize the call itself. During this
1859 // process, no libcalls can/will be inserted, guaranteeing that no calls
1860 // can overlap.
1861 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001862 // Note that we are selecting this call!
Dan Gohman8181bd12008-07-27 21:46:04 +00001863 LastCALLSEQ_END = SDValue(CallEnd, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001864 IsLegalizingCall = true;
1865
1866 // Legalize the call, starting from the CALLSEQ_END.
1867 LegalizeOp(LastCALLSEQ_END);
1868 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1869 return Result;
1870 }
1871 case ISD::CALLSEQ_END:
1872 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1873 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greif1c80d112008-08-28 21:40:38 +00001874 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001875 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1876 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001877 assert(I != LegalizedNodes.end() &&
1878 "Legalizing the call start should have legalized this node!");
1879 return I->second;
1880 }
1881
1882 // Otherwise, the call start has been legalized and everything is going
1883 // according to plan. Just legalize ourselves normally here.
1884 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1885 // Do not try to legalize the target-specific arguments (#1+), except for
1886 // an optional flag input.
1887 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1888 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001889 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001890 Ops[0] = Tmp1;
1891 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1892 }
1893 } else {
1894 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1895 if (Tmp1 != Node->getOperand(0) ||
1896 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001897 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001898 Ops[0] = Tmp1;
1899 Ops.back() = Tmp2;
1900 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1901 }
1902 }
1903 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1904 // This finishes up call legalization.
1905 IsLegalizingCall = false;
1906
1907 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001908 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001909 if (Node->getNumValues() == 2)
Dan Gohman8181bd12008-07-27 21:46:04 +00001910 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001911 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001912 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands92c43912008-06-06 12:08:01 +00001913 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001914 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1915 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1916 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1917 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1918
1919 Tmp1 = Result.getValue(0);
1920 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001921 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001922 default: assert(0 && "This action is not supported yet!");
1923 case TargetLowering::Expand: {
1924 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1925 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1926 " not tell us which reg is the stack pointer!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001927 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001928
1929 // Chain the dynamic stack allocation so that it doesn't modify the stack
1930 // pointer when other instructions are using the stack.
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001931 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling22f8deb2007-11-13 00:44:25 +00001932
Dan Gohman8181bd12008-07-27 21:46:04 +00001933 SDValue Size = Tmp2.getOperand(1);
1934 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Chenga448bc42007-08-16 23:50:06 +00001935 Chain = SP.getValue(1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001936 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Chenga448bc42007-08-16 23:50:06 +00001937 unsigned StackAlign =
1938 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1939 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001940 SP = DAG.getNode(ISD::AND, VT, SP,
1941 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001942 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling22f8deb2007-11-13 00:44:25 +00001943 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1944
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001945 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1946 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling22f8deb2007-11-13 00:44:25 +00001947
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001948 Tmp1 = LegalizeOp(Tmp1);
1949 Tmp2 = LegalizeOp(Tmp2);
1950 break;
1951 }
1952 case TargetLowering::Custom:
1953 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001954 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001955 Tmp1 = LegalizeOp(Tmp3);
1956 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1957 }
1958 break;
1959 case TargetLowering::Legal:
1960 break;
1961 }
1962 // Since this op produce two values, make sure to remember that we
1963 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001964 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1965 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001966 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001967 }
1968 case ISD::INLINEASM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001969 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001970 bool Changed = false;
1971 // Legalize all of the operands of the inline asm, in case they are nodes
1972 // that need to be expanded or something. Note we skip the asm string and
1973 // all of the TargetConstant flags.
Dan Gohman8181bd12008-07-27 21:46:04 +00001974 SDValue Op = LegalizeOp(Ops[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001975 Changed = Op != Ops[0];
1976 Ops[0] = Op;
1977
1978 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1979 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001980 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001981 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001982 SDValue Op = LegalizeOp(Ops[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001983 if (Op != Ops[i]) {
1984 Changed = true;
1985 Ops[i] = Op;
1986 }
1987 }
1988 }
1989
1990 if (HasInFlag) {
1991 Op = LegalizeOp(Ops.back());
1992 Changed |= Op != Ops.back();
1993 Ops.back() = Op;
1994 }
1995
1996 if (Changed)
1997 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1998
1999 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman8181bd12008-07-27 21:46:04 +00002000 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2001 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00002002 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002003 }
2004 case ISD::BR:
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 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2012 break;
2013 case ISD::BRIND:
2014 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2015 // Ensure that libcalls are emitted before a branch.
2016 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2017 Tmp1 = LegalizeOp(Tmp1);
2018 LastCALLSEQ_END = DAG.getEntryNode();
2019
2020 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2021 default: assert(0 && "Indirect target must be legal type (pointer)!");
2022 case Legal:
2023 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2024 break;
2025 }
2026 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2027 break;
2028 case ISD::BR_JT:
2029 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2030 // Ensure that libcalls are emitted before a branch.
2031 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2032 Tmp1 = LegalizeOp(Tmp1);
2033 LastCALLSEQ_END = DAG.getEntryNode();
2034
2035 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
2036 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2037
2038 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
2039 default: assert(0 && "This action is not supported yet!");
2040 case TargetLowering::Legal: break;
2041 case TargetLowering::Custom:
2042 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002043 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002044 break;
2045 case TargetLowering::Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002046 SDValue Chain = Result.getOperand(0);
2047 SDValue Table = Result.getOperand(1);
2048 SDValue Index = Result.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002049
Duncan Sands92c43912008-06-06 12:08:01 +00002050 MVT PTy = TLI.getPointerTy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002051 MachineFunction &MF = DAG.getMachineFunction();
2052 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
2053 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman8181bd12008-07-27 21:46:04 +00002054 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002055
Duncan Sands12ddc802008-12-12 08:13:38 +00002056 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
2057 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, Chain, Addr,
2058 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Cheng6fb06762007-11-09 01:32:10 +00002059 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002060 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2061 // For PIC, the sequence is:
2062 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00002063 // RelocBase can be JumpTable, GOT or some sort of global base.
Evan Cheng6fb06762007-11-09 01:32:10 +00002064 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
2065 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002066 }
Evan Cheng6fb06762007-11-09 01:32:10 +00002067 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002068 }
2069 }
2070 break;
2071 case ISD::BRCOND:
2072 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2073 // Ensure that libcalls are emitted before a return.
2074 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2075 Tmp1 = LegalizeOp(Tmp1);
2076 LastCALLSEQ_END = DAG.getEntryNode();
2077
2078 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2079 case Expand: assert(0 && "It's impossible to expand bools");
2080 case Legal:
2081 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2082 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002083 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002084 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
2085
2086 // The top bits of the promoted condition are not necessarily zero, ensure
2087 // that the value is properly zero extended.
Dan Gohman07961cd2008-02-25 21:11:39 +00002088 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002089 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman07961cd2008-02-25 21:11:39 +00002090 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002091 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
2092 break;
2093 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002094 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002095
2096 // Basic block destination (Op#2) is always legal.
2097 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2098
2099 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
2100 default: assert(0 && "This action is not supported yet!");
2101 case TargetLowering::Legal: break;
2102 case TargetLowering::Custom:
2103 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002104 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002105 break;
2106 case TargetLowering::Expand:
2107 // Expand brcond's setcc into its constituent parts and create a BR_CC
2108 // Node.
2109 if (Tmp2.getOpcode() == ISD::SETCC) {
2110 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
2111 Tmp2.getOperand(0), Tmp2.getOperand(1),
2112 Node->getOperand(2));
2113 } else {
2114 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
2115 DAG.getCondCode(ISD::SETNE), Tmp2,
2116 DAG.getConstant(0, Tmp2.getValueType()),
2117 Node->getOperand(2));
2118 }
2119 break;
2120 }
2121 break;
2122 case ISD::BR_CC:
2123 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2124 // Ensure that libcalls are emitted before a branch.
2125 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2126 Tmp1 = LegalizeOp(Tmp1);
2127 Tmp2 = Node->getOperand(2); // LHS
2128 Tmp3 = Node->getOperand(3); // RHS
2129 Tmp4 = Node->getOperand(1); // CC
2130
Dale Johannesen32100b22008-11-07 22:54:33 +00002131 LegalizeSetCC(TLI.getSetCCResultType(Tmp2), Tmp2, Tmp3, Tmp4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002132 LastCALLSEQ_END = DAG.getEntryNode();
2133
Evan Cheng71343822008-10-15 02:05:31 +00002134 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002135 // the LHS is a legal SETCC itself. In this case, we need to compare
2136 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00002137 if (Tmp3.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002138 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2139 Tmp4 = DAG.getCondCode(ISD::SETNE);
2140 }
2141
2142 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2143 Node->getOperand(4));
2144
2145 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2146 default: assert(0 && "Unexpected action for BR_CC!");
2147 case TargetLowering::Legal: break;
2148 case TargetLowering::Custom:
2149 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002150 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002151 break;
2152 }
2153 break;
2154 case ISD::LOAD: {
2155 LoadSDNode *LD = cast<LoadSDNode>(Node);
2156 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2157 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
2158
2159 ISD::LoadExtType ExtType = LD->getExtensionType();
2160 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands92c43912008-06-06 12:08:01 +00002161 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002162 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2163 Tmp3 = Result.getValue(0);
2164 Tmp4 = Result.getValue(1);
2165
2166 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2167 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002168 case TargetLowering::Legal:
2169 // If this is an unaligned load and the target doesn't support it,
2170 // expand it.
2171 if (!TLI.allowsUnalignedMemoryAccesses()) {
2172 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002173 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002174 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002175 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002176 TLI);
2177 Tmp3 = Result.getOperand(0);
2178 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00002179 Tmp3 = LegalizeOp(Tmp3);
2180 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002181 }
2182 }
2183 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002184 case TargetLowering::Custom:
2185 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002186 if (Tmp1.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002187 Tmp3 = LegalizeOp(Tmp1);
2188 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2189 }
2190 break;
2191 case TargetLowering::Promote: {
2192 // Only promote a load of vector type to another.
Duncan Sands92c43912008-06-06 12:08:01 +00002193 assert(VT.isVector() && "Cannot promote this load!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002194 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002195 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002196
2197 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
2198 LD->getSrcValueOffset(),
2199 LD->isVolatile(), LD->getAlignment());
2200 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2201 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2202 break;
2203 }
2204 }
2205 // Since loads produce two values, make sure to remember that we
2206 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002207 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2208 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif46bf5472008-08-26 22:36:50 +00002209 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002210 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00002211 MVT SrcVT = LD->getMemoryVT();
2212 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sands082524c2008-01-23 20:39:46 +00002213 int SVOffset = LD->getSrcValueOffset();
2214 unsigned Alignment = LD->getAlignment();
2215 bool isVolatile = LD->isVolatile();
2216
Duncan Sands92c43912008-06-06 12:08:01 +00002217 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002218 // Some targets pretend to have an i1 loading operation, and actually
2219 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2220 // bits are guaranteed to be zero; it helps the optimizers understand
2221 // that these bits are zero. It is also useful for EXTLOAD, since it
2222 // tells the optimizers that those bits are undefined. It would be
2223 // nice to have an effective generic way of getting these benefits...
2224 // Until such a way is found, don't insist on promoting i1 here.
2225 (SrcVT != MVT::i1 ||
Evan Cheng08c171a2008-10-14 21:26:46 +00002226 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002227 // Promote to a byte-sized load if not loading an integral number of
2228 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands92c43912008-06-06 12:08:01 +00002229 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2230 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002231 SDValue Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002232
2233 // The extra bits are guaranteed to be zero, since we stored them that
2234 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2235
2236 ISD::LoadExtType NewExtType =
2237 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2238
2239 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2240 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2241 NVT, isVolatile, Alignment);
2242
2243 Ch = Result.getValue(1); // The chain.
2244
2245 if (ExtType == ISD::SEXTLOAD)
2246 // Having the top bits zero doesn't help when sign extending.
2247 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2248 Result, DAG.getValueType(SrcVT));
2249 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2250 // All the top bits are guaranteed to be zero - inform the optimizers.
2251 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2252 DAG.getValueType(SrcVT));
2253
2254 Tmp1 = LegalizeOp(Result);
2255 Tmp2 = LegalizeOp(Ch);
2256 } else if (SrcWidth & (SrcWidth - 1)) {
2257 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands92c43912008-06-06 12:08:01 +00002258 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002259 "Unsupported extload!");
2260 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2261 assert(RoundWidth < SrcWidth);
2262 unsigned ExtraWidth = SrcWidth - RoundWidth;
2263 assert(ExtraWidth < RoundWidth);
2264 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2265 "Load size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002266 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2267 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002268 SDValue Lo, Hi, Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002269 unsigned IncrementSize;
2270
2271 if (TLI.isLittleEndian()) {
2272 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2273 // Load the bottom RoundWidth bits.
2274 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2275 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2276 Alignment);
2277
2278 // Load the remaining ExtraWidth bits.
2279 IncrementSize = RoundWidth / 8;
2280 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2281 DAG.getIntPtrConstant(IncrementSize));
2282 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2283 LD->getSrcValue(), SVOffset + IncrementSize,
2284 ExtraVT, isVolatile,
2285 MinAlign(Alignment, IncrementSize));
2286
2287 // Build a factor node to remember that this load is independent of the
2288 // other one.
2289 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2290 Hi.getValue(1));
2291
2292 // Move the top bits to the right place.
2293 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2294 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2295
2296 // Join the hi and lo parts.
2297 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002298 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00002299 // Big endian - avoid unaligned loads.
2300 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2301 // Load the top RoundWidth bits.
2302 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2303 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2304 Alignment);
2305
2306 // Load the remaining ExtraWidth bits.
2307 IncrementSize = RoundWidth / 8;
2308 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2309 DAG.getIntPtrConstant(IncrementSize));
2310 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2311 LD->getSrcValue(), SVOffset + IncrementSize,
2312 ExtraVT, isVolatile,
2313 MinAlign(Alignment, IncrementSize));
2314
2315 // Build a factor node to remember that this load is independent of the
2316 // other one.
2317 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2318 Hi.getValue(1));
2319
2320 // Move the top bits to the right place.
2321 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2322 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2323
2324 // Join the hi and lo parts.
2325 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2326 }
2327
2328 Tmp1 = LegalizeOp(Result);
2329 Tmp2 = LegalizeOp(Ch);
2330 } else {
Evan Cheng08c171a2008-10-14 21:26:46 +00002331 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002332 default: assert(0 && "This action is not supported yet!");
2333 case TargetLowering::Custom:
2334 isCustom = true;
2335 // FALLTHROUGH
2336 case TargetLowering::Legal:
2337 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2338 Tmp1 = Result.getValue(0);
2339 Tmp2 = Result.getValue(1);
2340
2341 if (isCustom) {
2342 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002343 if (Tmp3.getNode()) {
Duncan Sands082524c2008-01-23 20:39:46 +00002344 Tmp1 = LegalizeOp(Tmp3);
2345 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2346 }
2347 } else {
2348 // If this is an unaligned load and the target doesn't support it,
2349 // expand it.
2350 if (!TLI.allowsUnalignedMemoryAccesses()) {
2351 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002352 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sands082524c2008-01-23 20:39:46 +00002353 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002354 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sands082524c2008-01-23 20:39:46 +00002355 TLI);
2356 Tmp1 = Result.getOperand(0);
2357 Tmp2 = Result.getOperand(1);
2358 Tmp1 = LegalizeOp(Tmp1);
2359 Tmp2 = LegalizeOp(Tmp2);
2360 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002361 }
2362 }
Duncan Sands082524c2008-01-23 20:39:46 +00002363 break;
2364 case TargetLowering::Expand:
2365 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2366 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002367 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sands082524c2008-01-23 20:39:46 +00002368 LD->getSrcValueOffset(),
2369 LD->isVolatile(), LD->getAlignment());
2370 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2371 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2372 Tmp2 = LegalizeOp(Load.getValue(1));
2373 break;
2374 }
2375 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2376 // Turn the unsupported load into an EXTLOAD followed by an explicit
2377 // zero/sign extend inreg.
2378 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2379 Tmp1, Tmp2, LD->getSrcValue(),
2380 LD->getSrcValueOffset(), SrcVT,
2381 LD->isVolatile(), LD->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00002382 SDValue ValRes;
Duncan Sands082524c2008-01-23 20:39:46 +00002383 if (ExtType == ISD::SEXTLOAD)
2384 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2385 Result, DAG.getValueType(SrcVT));
2386 else
2387 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2388 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2389 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002390 break;
2391 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002392 }
Duncan Sands082524c2008-01-23 20:39:46 +00002393
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002394 // Since loads produce two values, make sure to remember that we legalized
2395 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002396 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2397 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002398 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002399 }
2400 }
2401 case ISD::EXTRACT_ELEMENT: {
Duncan Sands92c43912008-06-06 12:08:01 +00002402 MVT OpTy = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002403 switch (getTypeAction(OpTy)) {
2404 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2405 case Legal:
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002406 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002407 // 1 -> Hi
2408 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands92c43912008-06-06 12:08:01 +00002409 DAG.getConstant(OpTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002410 TLI.getShiftAmountTy()));
2411 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2412 } else {
2413 // 0 -> Lo
2414 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2415 Node->getOperand(0));
2416 }
2417 break;
2418 case Expand:
2419 // Get both the low and high parts.
2420 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002421 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002422 Result = Tmp2; // 1 -> Hi
2423 else
2424 Result = Tmp1; // 0 -> Lo
2425 break;
2426 }
2427 break;
2428 }
2429
2430 case ISD::CopyToReg:
2431 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2432
2433 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2434 "Register type must be legal!");
2435 // Legalize the incoming value (must be a legal type).
2436 Tmp2 = LegalizeOp(Node->getOperand(2));
2437 if (Node->getNumValues() == 1) {
2438 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2439 } else {
2440 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2441 if (Node->getNumOperands() == 4) {
2442 Tmp3 = LegalizeOp(Node->getOperand(3));
2443 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2444 Tmp3);
2445 } else {
2446 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2447 }
2448
2449 // Since this produces two values, make sure to remember that we legalized
2450 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002451 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2452 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002453 return Result;
2454 }
2455 break;
2456
2457 case ISD::RET:
2458 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2459
2460 // Ensure that libcalls are emitted before a return.
2461 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2462 Tmp1 = LegalizeOp(Tmp1);
2463 LastCALLSEQ_END = DAG.getEntryNode();
2464
2465 switch (Node->getNumOperands()) {
2466 case 3: // ret val
2467 Tmp2 = Node->getOperand(1);
2468 Tmp3 = Node->getOperand(2); // Signness
2469 switch (getTypeAction(Tmp2.getValueType())) {
2470 case Legal:
2471 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2472 break;
2473 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00002474 if (!Tmp2.getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002475 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002476 ExpandOp(Tmp2, Lo, Hi);
2477
2478 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002479 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002480 std::swap(Lo, Hi);
2481
Gabor Greif1c80d112008-08-28 21:40:38 +00002482 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002483 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2484 else
2485 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2486 Result = LegalizeOp(Result);
2487 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +00002488 SDNode *InVal = Tmp2.getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002489 int InIx = Tmp2.getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002490 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2491 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002492
2493 // Figure out if there is a simple type corresponding to this Vector
2494 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002495 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002496 if (TLI.isTypeLegal(TVT)) {
2497 // Turn this into a return of the vector type.
2498 Tmp2 = LegalizeOp(Tmp2);
2499 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2500 } else if (NumElems == 1) {
2501 // Turn this into a return of the scalar type.
2502 Tmp2 = ScalarizeVectorOp(Tmp2);
2503 Tmp2 = LegalizeOp(Tmp2);
2504 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2505
2506 // FIXME: Returns of gcc generic vectors smaller than a legal type
2507 // should be returned in integer registers!
2508
2509 // The scalarized value type may not be legal, e.g. it might require
2510 // promotion or expansion. Relegalize the return.
2511 Result = LegalizeOp(Result);
2512 } else {
2513 // FIXME: Returns of gcc generic vectors larger than a legal vector
2514 // type should be returned by reference!
Dan Gohman8181bd12008-07-27 21:46:04 +00002515 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002516 SplitVectorOp(Tmp2, Lo, Hi);
2517 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2518 Result = LegalizeOp(Result);
2519 }
2520 }
2521 break;
2522 case Promote:
2523 Tmp2 = PromoteOp(Node->getOperand(1));
2524 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2525 Result = LegalizeOp(Result);
2526 break;
2527 }
2528 break;
2529 case 1: // ret void
2530 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2531 break;
2532 default: { // ret <values>
Dan Gohman8181bd12008-07-27 21:46:04 +00002533 SmallVector<SDValue, 8> NewValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002534 NewValues.push_back(Tmp1);
2535 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2536 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2537 case Legal:
2538 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2539 NewValues.push_back(Node->getOperand(i+1));
2540 break;
2541 case Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002542 SDValue Lo, Hi;
Duncan Sands92c43912008-06-06 12:08:01 +00002543 assert(!Node->getOperand(i).getValueType().isExtended() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002544 "FIXME: TODO: implement returning non-legal vector types!");
2545 ExpandOp(Node->getOperand(i), Lo, Hi);
2546 NewValues.push_back(Lo);
2547 NewValues.push_back(Node->getOperand(i+1));
Gabor Greif1c80d112008-08-28 21:40:38 +00002548 if (Hi.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002549 NewValues.push_back(Hi);
2550 NewValues.push_back(Node->getOperand(i+1));
2551 }
2552 break;
2553 }
2554 case Promote:
2555 assert(0 && "Can't promote multiple return value yet!");
2556 }
2557
2558 if (NewValues.size() == Node->getNumOperands())
2559 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2560 else
2561 Result = DAG.getNode(ISD::RET, MVT::Other,
2562 &NewValues[0], NewValues.size());
2563 break;
2564 }
2565 }
2566
2567 if (Result.getOpcode() == ISD::RET) {
2568 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2569 default: assert(0 && "This action is not supported yet!");
2570 case TargetLowering::Legal: break;
2571 case TargetLowering::Custom:
2572 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002573 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002574 break;
2575 }
2576 }
2577 break;
2578 case ISD::STORE: {
2579 StoreSDNode *ST = cast<StoreSDNode>(Node);
2580 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2581 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2582 int SVOffset = ST->getSrcValueOffset();
2583 unsigned Alignment = ST->getAlignment();
2584 bool isVolatile = ST->isVolatile();
2585
2586 if (!ST->isTruncatingStore()) {
2587 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2588 // FIXME: We shouldn't do this for TargetConstantFP's.
2589 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2590 // to phase ordering between legalized code and the dag combiner. This
2591 // probably means that we need to integrate dag combiner and legalizer
2592 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002593 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002594 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002595 if (CFP->getValueType(0) == MVT::f32 &&
2596 getTypeAction(MVT::i32) == Legal) {
Dan Gohman39509762008-03-11 00:11:06 +00002597 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002598 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen1616e902007-09-11 18:32:33 +00002599 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002600 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2601 SVOffset, isVolatile, Alignment);
2602 break;
2603 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002604 // If this target supports 64-bit registers, do a single 64-bit store.
2605 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002606 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman39509762008-03-11 00:11:06 +00002607 zextOrTrunc(64), MVT::i64);
Chris Lattner19f229a2007-10-15 05:46:06 +00002608 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2609 SVOffset, isVolatile, Alignment);
2610 break;
Duncan Sands2418bec2008-06-13 19:07:40 +00002611 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002612 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2613 // stores. If the target supports neither 32- nor 64-bits, this
2614 // xform is certainly not worth it.
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002615 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman8181bd12008-07-27 21:46:04 +00002616 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2617 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002618 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002619
2620 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2621 SVOffset, isVolatile, Alignment);
2622 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002623 DAG.getIntPtrConstant(4));
Chris Lattner19f229a2007-10-15 05:46:06 +00002624 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002625 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002626
2627 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2628 break;
2629 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002630 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002631 }
2632
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002633 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002634 case Legal: {
2635 Tmp3 = LegalizeOp(ST->getValue());
2636 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2637 ST->getOffset());
2638
Duncan Sands92c43912008-06-06 12:08:01 +00002639 MVT VT = Tmp3.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002640 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2641 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002642 case TargetLowering::Legal:
2643 // If this is an unaligned store and the target doesn't support it,
2644 // expand it.
2645 if (!TLI.allowsUnalignedMemoryAccesses()) {
2646 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002647 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002648 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002649 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002650 TLI);
2651 }
2652 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002653 case TargetLowering::Custom:
2654 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002655 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002656 break;
2657 case TargetLowering::Promote:
Duncan Sands92c43912008-06-06 12:08:01 +00002658 assert(VT.isVector() && "Unknown legal promote case!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002659 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2660 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2661 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2662 ST->getSrcValue(), SVOffset, isVolatile,
2663 Alignment);
2664 break;
2665 }
2666 break;
2667 }
2668 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002669 if (!ST->getMemoryVT().isVector()) {
2670 // Truncate the value and store the result.
2671 Tmp3 = PromoteOp(ST->getValue());
2672 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2673 SVOffset, ST->getMemoryVT(),
2674 isVolatile, Alignment);
2675 break;
2676 }
2677 // Fall thru to expand for vector
2678 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002679 unsigned IncrementSize = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002680 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002681
2682 // If this is a vector type, then we have to calculate the increment as
2683 // the product of the element size in bytes, and the number of elements
2684 // in the high half of the vector.
Duncan Sands92c43912008-06-06 12:08:01 +00002685 if (ST->getValue().getValueType().isVector()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002686 SDNode *InVal = ST->getValue().getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002687 int InIx = ST->getValue().getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002688 MVT InVT = InVal->getValueType(InIx);
2689 unsigned NumElems = InVT.getVectorNumElements();
2690 MVT EVT = InVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002691
2692 // Figure out if there is a simple type corresponding to this Vector
2693 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002694 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002695 if (TLI.isTypeLegal(TVT)) {
2696 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002697 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002698 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2699 SVOffset, isVolatile, Alignment);
2700 Result = LegalizeOp(Result);
2701 break;
2702 } else if (NumElems == 1) {
2703 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002704 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002705 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2706 SVOffset, isVolatile, Alignment);
2707 // The scalarized value type may not be legal, e.g. it might require
2708 // promotion or expansion. Relegalize the scalar store.
2709 Result = LegalizeOp(Result);
2710 break;
2711 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +00002712 // Check if we have widen this node with another value
2713 std::map<SDValue, SDValue>::iterator I =
2714 WidenNodes.find(ST->getValue());
2715 if (I != WidenNodes.end()) {
2716 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2717 break;
2718 }
2719 else {
2720 SplitVectorOp(ST->getValue(), Lo, Hi);
2721 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2722 EVT.getSizeInBits()/8;
2723 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002724 }
2725 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002726 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greif1c80d112008-08-28 21:40:38 +00002727 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002728
Richard Pennington73ae9e42008-09-25 16:15:10 +00002729 if (Hi.getNode() && TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002730 std::swap(Lo, Hi);
2731 }
2732
2733 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2734 SVOffset, isVolatile, Alignment);
2735
Gabor Greif1c80d112008-08-28 21:40:38 +00002736 if (Hi.getNode() == NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002737 // Must be int <-> float one-to-one expansion.
2738 Result = Lo;
2739 break;
2740 }
2741
2742 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002743 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002744 assert(isTypeLegal(Tmp2.getValueType()) &&
2745 "Pointers must be legal!");
2746 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002747 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002748 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2749 SVOffset, isVolatile, Alignment);
2750 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2751 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00002752 } // case Expand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002753 }
2754 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002755 switch (getTypeAction(ST->getValue().getValueType())) {
2756 case Legal:
2757 Tmp3 = LegalizeOp(ST->getValue());
2758 break;
2759 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002760 if (!ST->getValue().getValueType().isVector()) {
2761 // We can promote the value, the truncstore will still take care of it.
2762 Tmp3 = PromoteOp(ST->getValue());
2763 break;
2764 }
2765 // Vector case falls through to expand
Chris Lattner3bc08502008-01-17 19:59:44 +00002766 case Expand:
2767 // Just store the low part. This may become a non-trunc store, so make
2768 // sure to use getTruncStore, not UpdateNodeOperands below.
2769 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2770 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2771 SVOffset, MVT::i8, isVolatile, Alignment);
2772 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002773
Duncan Sands92c43912008-06-06 12:08:01 +00002774 MVT StVT = ST->getMemoryVT();
2775 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands40676662008-01-22 07:17:34 +00002776
Duncan Sands92c43912008-06-06 12:08:01 +00002777 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands40676662008-01-22 07:17:34 +00002778 // Promote to a byte-sized store with upper bits zero if not
2779 // storing an integral number of bytes. For example, promote
2780 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands92c43912008-06-06 12:08:01 +00002781 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands40676662008-01-22 07:17:34 +00002782 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2783 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2784 SVOffset, NVT, isVolatile, Alignment);
2785 } else if (StWidth & (StWidth - 1)) {
2786 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands92c43912008-06-06 12:08:01 +00002787 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands40676662008-01-22 07:17:34 +00002788 "Unsupported truncstore!");
2789 unsigned RoundWidth = 1 << Log2_32(StWidth);
2790 assert(RoundWidth < StWidth);
2791 unsigned ExtraWidth = StWidth - RoundWidth;
2792 assert(ExtraWidth < RoundWidth);
2793 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2794 "Store size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002795 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2796 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002797 SDValue Lo, Hi;
Duncan Sands40676662008-01-22 07:17:34 +00002798 unsigned IncrementSize;
2799
2800 if (TLI.isLittleEndian()) {
2801 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2802 // Store the bottom RoundWidth bits.
2803 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2804 SVOffset, RoundVT,
2805 isVolatile, Alignment);
2806
2807 // Store the remaining ExtraWidth bits.
2808 IncrementSize = RoundWidth / 8;
2809 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2810 DAG.getIntPtrConstant(IncrementSize));
2811 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2812 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2813 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2814 SVOffset + IncrementSize, ExtraVT, isVolatile,
2815 MinAlign(Alignment, IncrementSize));
2816 } else {
2817 // Big endian - avoid unaligned stores.
2818 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2819 // Store the top RoundWidth bits.
2820 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2821 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2822 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2823 RoundVT, isVolatile, Alignment);
2824
2825 // Store the remaining ExtraWidth bits.
2826 IncrementSize = RoundWidth / 8;
2827 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2828 DAG.getIntPtrConstant(IncrementSize));
2829 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2830 SVOffset + IncrementSize, ExtraVT, isVolatile,
2831 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002832 }
Duncan Sands40676662008-01-22 07:17:34 +00002833
2834 // The order of the stores doesn't matter.
2835 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2836 } else {
2837 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2838 Tmp2 != ST->getBasePtr())
2839 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2840 ST->getOffset());
2841
2842 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2843 default: assert(0 && "This action is not supported yet!");
2844 case TargetLowering::Legal:
2845 // If this is an unaligned store and the target doesn't support it,
2846 // expand it.
2847 if (!TLI.allowsUnalignedMemoryAccesses()) {
2848 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002849 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands40676662008-01-22 07:17:34 +00002850 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002851 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands40676662008-01-22 07:17:34 +00002852 TLI);
2853 }
2854 break;
2855 case TargetLowering::Custom:
2856 Result = TLI.LowerOperation(Result, DAG);
2857 break;
2858 case Expand:
2859 // TRUNCSTORE:i16 i32 -> STORE i16
2860 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2861 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2862 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2863 isVolatile, Alignment);
2864 break;
2865 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002866 }
2867 }
2868 break;
2869 }
2870 case ISD::PCMARKER:
2871 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2872 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2873 break;
2874 case ISD::STACKSAVE:
2875 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2876 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2877 Tmp1 = Result.getValue(0);
2878 Tmp2 = Result.getValue(1);
2879
2880 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2881 default: assert(0 && "This action is not supported yet!");
2882 case TargetLowering::Legal: break;
2883 case TargetLowering::Custom:
2884 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002885 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002886 Tmp1 = LegalizeOp(Tmp3);
2887 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2888 }
2889 break;
2890 case TargetLowering::Expand:
2891 // Expand to CopyFromReg if the target set
2892 // StackPointerRegisterToSaveRestore.
2893 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2894 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2895 Node->getValueType(0));
2896 Tmp2 = Tmp1.getValue(1);
2897 } else {
2898 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2899 Tmp2 = Node->getOperand(0);
2900 }
2901 break;
2902 }
2903
2904 // Since stacksave produce two values, make sure to remember that we
2905 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002906 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2907 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002908 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002909
2910 case ISD::STACKRESTORE:
2911 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2912 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2913 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2914
2915 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2916 default: assert(0 && "This action is not supported yet!");
2917 case TargetLowering::Legal: break;
2918 case TargetLowering::Custom:
2919 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002920 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002921 break;
2922 case TargetLowering::Expand:
2923 // Expand to CopyToReg if the target set
2924 // StackPointerRegisterToSaveRestore.
2925 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2926 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2927 } else {
2928 Result = Tmp1;
2929 }
2930 break;
2931 }
2932 break;
2933
2934 case ISD::READCYCLECOUNTER:
2935 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2936 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2937 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2938 Node->getValueType(0))) {
2939 default: assert(0 && "This action is not supported yet!");
2940 case TargetLowering::Legal:
2941 Tmp1 = Result.getValue(0);
2942 Tmp2 = Result.getValue(1);
2943 break;
2944 case TargetLowering::Custom:
2945 Result = TLI.LowerOperation(Result, DAG);
2946 Tmp1 = LegalizeOp(Result.getValue(0));
2947 Tmp2 = LegalizeOp(Result.getValue(1));
2948 break;
2949 }
2950
2951 // Since rdcc produce two values, make sure to remember that we legalized
2952 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002953 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2954 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002955 return Result;
2956
2957 case ISD::SELECT:
2958 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2959 case Expand: assert(0 && "It's impossible to expand bools");
2960 case Legal:
2961 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2962 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002963 case Promote: {
Mon P Wang1448aad2008-10-30 08:01:45 +00002964 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002965 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2966 // Make sure the condition is either zero or one.
Dan Gohman07961cd2008-02-25 21:11:39 +00002967 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002968 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman07961cd2008-02-25 21:11:39 +00002969 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002970 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2971 break;
2972 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002973 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002974 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2975 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2976
2977 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2978
2979 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2980 default: assert(0 && "This action is not supported yet!");
2981 case TargetLowering::Legal: break;
2982 case TargetLowering::Custom: {
2983 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002984 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002985 break;
2986 }
2987 case TargetLowering::Expand:
2988 if (Tmp1.getOpcode() == ISD::SETCC) {
2989 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2990 Tmp2, Tmp3,
2991 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2992 } else {
2993 Result = DAG.getSelectCC(Tmp1,
2994 DAG.getConstant(0, Tmp1.getValueType()),
2995 Tmp2, Tmp3, ISD::SETNE);
2996 }
2997 break;
2998 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00002999 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003000 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
3001 unsigned ExtOp, TruncOp;
Duncan Sands92c43912008-06-06 12:08:01 +00003002 if (Tmp2.getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003003 ExtOp = ISD::BIT_CONVERT;
3004 TruncOp = ISD::BIT_CONVERT;
Duncan Sands92c43912008-06-06 12:08:01 +00003005 } else if (Tmp2.getValueType().isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003006 ExtOp = ISD::ANY_EXTEND;
3007 TruncOp = ISD::TRUNCATE;
3008 } else {
3009 ExtOp = ISD::FP_EXTEND;
3010 TruncOp = ISD::FP_ROUND;
3011 }
3012 // Promote each of the values to the new type.
3013 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
3014 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
3015 // Perform the larger operation, then round down.
3016 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00003017 if (TruncOp != ISD::FP_ROUND)
3018 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
3019 else
3020 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
3021 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003022 break;
3023 }
3024 }
3025 break;
3026 case ISD::SELECT_CC: {
3027 Tmp1 = Node->getOperand(0); // LHS
3028 Tmp2 = Node->getOperand(1); // RHS
3029 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
3030 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman8181bd12008-07-27 21:46:04 +00003031 SDValue CC = Node->getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003032
Dale Johannesen32100b22008-11-07 22:54:33 +00003033 LegalizeSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2, CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003034
Evan Cheng71343822008-10-15 02:05:31 +00003035 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003036 // the LHS is a legal SETCC itself. In this case, we need to compare
3037 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00003038 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003039 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3040 CC = DAG.getCondCode(ISD::SETNE);
3041 }
3042 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
3043
3044 // Everything is legal, see if we should expand this op or something.
3045 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
3046 default: assert(0 && "This action is not supported yet!");
3047 case TargetLowering::Legal: break;
3048 case TargetLowering::Custom:
3049 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003050 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003051 break;
3052 }
3053 break;
3054 }
3055 case ISD::SETCC:
3056 Tmp1 = Node->getOperand(0);
3057 Tmp2 = Node->getOperand(1);
3058 Tmp3 = Node->getOperand(2);
Evan Cheng71343822008-10-15 02:05:31 +00003059 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003060
3061 // If we had to Expand the SetCC operands into a SELECT node, then it may
3062 // not always be possible to return a true LHS & RHS. In this case, just
3063 // return the value we legalized, returned in the LHS
Gabor Greif1c80d112008-08-28 21:40:38 +00003064 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065 Result = Tmp1;
3066 break;
3067 }
3068
3069 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
3070 default: assert(0 && "Cannot handle this action for SETCC yet!");
3071 case TargetLowering::Custom:
3072 isCustom = true;
3073 // FALLTHROUGH.
3074 case TargetLowering::Legal:
3075 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3076 if (isCustom) {
3077 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003078 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003079 }
3080 break;
3081 case TargetLowering::Promote: {
3082 // First step, figure out the appropriate operation to use.
3083 // Allow SETCC to not be supported for all legal data types
3084 // Mostly this targets FP
Duncan Sands92c43912008-06-06 12:08:01 +00003085 MVT NewInTy = Node->getOperand(0).getValueType();
3086 MVT OldVT = NewInTy; OldVT = OldVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003087
3088 // Scan for the appropriate larger type to use.
3089 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00003090 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003091
Duncan Sands92c43912008-06-06 12:08:01 +00003092 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003093 "Fell off of the edge of the integer world");
Duncan Sands92c43912008-06-06 12:08:01 +00003094 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003095 "Fell off of the edge of the floating point world");
3096
3097 // If the target supports SETCC of this type, use it.
3098 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
3099 break;
3100 }
Duncan Sands92c43912008-06-06 12:08:01 +00003101 if (NewInTy.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003102 assert(0 && "Cannot promote Legal Integer SETCC yet");
3103 else {
3104 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
3105 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
3106 }
3107 Tmp1 = LegalizeOp(Tmp1);
3108 Tmp2 = LegalizeOp(Tmp2);
3109 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3110 Result = LegalizeOp(Result);
3111 break;
3112 }
3113 case TargetLowering::Expand:
3114 // Expand a setcc node into a select_cc of the same condition, lhs, and
3115 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands92c43912008-06-06 12:08:01 +00003116 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003117 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3118 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3119 Tmp3);
3120 break;
3121 }
3122 break;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003123 case ISD::VSETCC: {
3124 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3125 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman8181bd12008-07-27 21:46:04 +00003126 SDValue CC = Node->getOperand(2);
Nate Begeman9a1ce152008-05-12 19:40:03 +00003127
3128 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3129
3130 // Everything is legal, see if we should expand this op or something.
3131 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3132 default: assert(0 && "This action is not supported yet!");
3133 case TargetLowering::Legal: break;
3134 case TargetLowering::Custom:
3135 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003136 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003137 break;
Mon P Wangec428ad2008-12-13 08:15:14 +00003138 case TargetLowering::Expand: {
3139 // Unroll into a nasty set of scalar code for now.
3140 MVT VT = Node->getValueType(0);
3141 unsigned NumElems = VT.getVectorNumElements();
3142 MVT EltVT = VT.getVectorElementType();
3143 MVT TmpEltVT = Tmp1.getValueType().getVectorElementType();
3144 SmallVector<SDValue, 8> Ops(NumElems);
3145 for (unsigned i = 0; i < NumElems; ++i) {
3146 SDValue In1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3147 Tmp1, DAG.getIntPtrConstant(i));
3148 Ops[i] = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(In1), In1,
Mon P Wang77bc9cd2008-12-17 08:49:47 +00003149 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3150 Tmp2, DAG.getIntPtrConstant(i)),
3151 CC);
3152 Ops[i] = DAG.getNode(ISD::SELECT, EltVT, Ops[i],
3153 DAG.getConstant(EltVT.getIntegerVTBitMask(),EltVT),
3154 DAG.getConstant(0, EltVT));
Mon P Wangec428ad2008-12-13 08:15:14 +00003155 }
3156 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], NumElems);
3157 break;
3158 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00003159 }
3160 break;
3161 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003162
3163 case ISD::SHL_PARTS:
3164 case ISD::SRA_PARTS:
3165 case ISD::SRL_PARTS: {
Dan Gohman8181bd12008-07-27 21:46:04 +00003166 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003167 bool Changed = false;
3168 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3169 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3170 Changed |= Ops.back() != Node->getOperand(i);
3171 }
3172 if (Changed)
3173 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
3174
3175 switch (TLI.getOperationAction(Node->getOpcode(),
3176 Node->getValueType(0))) {
3177 default: assert(0 && "This action is not supported yet!");
3178 case TargetLowering::Legal: break;
3179 case TargetLowering::Custom:
3180 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003181 if (Tmp1.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003182 SDValue Tmp2, RetVal(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003183 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
3184 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman8181bd12008-07-27 21:46:04 +00003185 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00003186 if (i == Op.getResNo())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003187 RetVal = Tmp2;
3188 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003189 assert(RetVal.getNode() && "Illegal result number");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003190 return RetVal;
3191 }
3192 break;
3193 }
3194
3195 // Since these produce multiple values, make sure to remember that we
3196 // legalized all of them.
3197 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman8181bd12008-07-27 21:46:04 +00003198 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00003199 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003200 }
3201
3202 // Binary operators
3203 case ISD::ADD:
3204 case ISD::SUB:
3205 case ISD::MUL:
3206 case ISD::MULHS:
3207 case ISD::MULHU:
3208 case ISD::UDIV:
3209 case ISD::SDIV:
3210 case ISD::AND:
3211 case ISD::OR:
3212 case ISD::XOR:
3213 case ISD::SHL:
3214 case ISD::SRL:
3215 case ISD::SRA:
3216 case ISD::FADD:
3217 case ISD::FSUB:
3218 case ISD::FMUL:
3219 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00003220 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003221 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3222 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3223 case Expand: assert(0 && "Not possible");
3224 case Legal:
3225 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3226 break;
3227 case Promote:
3228 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3229 break;
3230 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003231
3232 if ((Node->getOpcode() == ISD::SHL ||
3233 Node->getOpcode() == ISD::SRL ||
3234 Node->getOpcode() == ISD::SRA) &&
3235 !Node->getValueType(0).isVector()) {
Mon P Wang9901e732008-12-09 05:46:39 +00003236 Tmp2 = LegalizeShiftAmount(Tmp2);
Mon P Wangec428ad2008-12-13 08:15:14 +00003237 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003238
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003239 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangbff5d9c2008-11-10 04:46:22 +00003240
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003241 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3242 default: assert(0 && "BinOp legalize operation not supported");
3243 case TargetLowering::Legal: break;
3244 case TargetLowering::Custom:
3245 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003246 if (Tmp1.getNode()) {
Nate Begemanbb1ce942008-07-29 15:49:41 +00003247 Result = Tmp1;
3248 break;
Nate Begeman7569e762008-07-29 19:07:27 +00003249 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003250 // Fall through if the custom lower can't deal with the operation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003251 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00003252 MVT VT = Op.getValueType();
Mon P Wang1448aad2008-10-30 08:01:45 +00003253
Dan Gohman5a199552007-10-08 18:33:35 +00003254 // See if multiply or divide can be lowered using two-result operations.
3255 SDVTList VTs = DAG.getVTList(VT, VT);
3256 if (Node->getOpcode() == ISD::MUL) {
3257 // We just need the low half of the multiply; try both the signed
3258 // and unsigned forms. If the target supports both SMUL_LOHI and
3259 // UMUL_LOHI, form a preference by checking which forms of plain
3260 // MULH it supports.
3261 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3262 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3263 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3264 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3265 unsigned OpToUse = 0;
3266 if (HasSMUL_LOHI && !HasMULHS) {
3267 OpToUse = ISD::SMUL_LOHI;
3268 } else if (HasUMUL_LOHI && !HasMULHU) {
3269 OpToUse = ISD::UMUL_LOHI;
3270 } else if (HasSMUL_LOHI) {
3271 OpToUse = ISD::SMUL_LOHI;
3272 } else if (HasUMUL_LOHI) {
3273 OpToUse = ISD::UMUL_LOHI;
3274 }
3275 if (OpToUse) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003276 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003277 break;
3278 }
3279 }
3280 if (Node->getOpcode() == ISD::MULHS &&
3281 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003282 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3283 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003284 break;
3285 }
3286 if (Node->getOpcode() == ISD::MULHU &&
3287 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003288 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3289 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003290 break;
3291 }
3292 if (Node->getOpcode() == ISD::SDIV &&
3293 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003294 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3295 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003296 break;
3297 }
3298 if (Node->getOpcode() == ISD::UDIV &&
3299 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003300 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3301 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003302 break;
3303 }
Mon P Wang26342922008-12-18 20:03:17 +00003304
Dan Gohman6d05cac2007-10-11 23:57:53 +00003305 // Check to see if we have a libcall for this operator.
3306 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3307 bool isSigned = false;
3308 switch (Node->getOpcode()) {
3309 case ISD::UDIV:
3310 case ISD::SDIV:
3311 if (VT == MVT::i32) {
3312 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang1448aad2008-10-30 08:01:45 +00003313 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003314 isSigned = Node->getOpcode() == ISD::SDIV;
3315 }
3316 break;
Chris Lattner48188652008-10-04 21:27:46 +00003317 case ISD::MUL:
3318 if (VT == MVT::i32)
3319 LC = RTLIB::MUL_I32;
3320 break;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003321 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00003322 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3323 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003324 break;
3325 default: break;
3326 }
3327 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003328 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003329 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003330 break;
3331 }
Mon P Wang1448aad2008-10-30 08:01:45 +00003332
Duncan Sands92c43912008-06-06 12:08:01 +00003333 assert(Node->getValueType(0).isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003334 "Cannot expand this binary operator!");
3335 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003336 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003337 break;
3338 }
3339 case TargetLowering::Promote: {
3340 switch (Node->getOpcode()) {
3341 default: assert(0 && "Do not know how to promote this BinOp!");
3342 case ISD::AND:
3343 case ISD::OR:
3344 case ISD::XOR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003345 MVT OVT = Node->getValueType(0);
3346 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3347 assert(OVT.isVector() && "Cannot promote this BinOp!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003348 // Bit convert each of the values to the new type.
3349 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3350 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3351 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3352 // Bit convert the result back the original type.
3353 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3354 break;
3355 }
3356 }
3357 }
3358 }
3359 break;
3360
Dan Gohman475cd732007-10-05 14:17:22 +00003361 case ISD::SMUL_LOHI:
3362 case ISD::UMUL_LOHI:
3363 case ISD::SDIVREM:
3364 case ISD::UDIVREM:
3365 // These nodes will only be produced by target-specific lowering, so
3366 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003367 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003368 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003369
3370 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3371 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3372 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003373 break;
3374
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003375 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3376 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3377 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3378 case Expand: assert(0 && "Not possible");
3379 case Legal:
3380 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3381 break;
3382 case Promote:
3383 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3384 break;
3385 }
3386
3387 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3388
3389 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3390 default: assert(0 && "Operation not supported");
3391 case TargetLowering::Custom:
3392 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003393 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003394 break;
3395 case TargetLowering::Legal: break;
3396 case TargetLowering::Expand: {
3397 // If this target supports fabs/fneg natively and select is cheap,
3398 // do this efficiently.
3399 if (!TLI.isSelectExpensive() &&
3400 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3401 TargetLowering::Legal &&
3402 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3403 TargetLowering::Legal) {
3404 // Get the sign bit of the RHS.
Duncan Sands92c43912008-06-06 12:08:01 +00003405 MVT IVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003406 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman8181bd12008-07-27 21:46:04 +00003407 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel502151f2008-03-10 15:42:14 +00003408 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003409 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3410 // Get the absolute value of the result.
Dan Gohman8181bd12008-07-27 21:46:04 +00003411 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003412 // Select between the nabs and abs value based on the sign bit of
3413 // the input.
3414 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3415 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3416 AbsVal),
3417 AbsVal);
3418 Result = LegalizeOp(Result);
3419 break;
3420 }
3421
3422 // Otherwise, do bitwise ops!
Duncan Sands92c43912008-06-06 12:08:01 +00003423 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003424 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3425 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3426 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3427 Result = LegalizeOp(Result);
3428 break;
3429 }
3430 }
3431 break;
3432
3433 case ISD::ADDC:
3434 case ISD::SUBC:
3435 Tmp1 = LegalizeOp(Node->getOperand(0));
3436 Tmp2 = LegalizeOp(Node->getOperand(1));
3437 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003438 Tmp3 = Result.getValue(0);
3439 Tmp4 = Result.getValue(1);
3440
3441 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3442 default: assert(0 && "This action is not supported yet!");
3443 case TargetLowering::Legal:
3444 break;
3445 case TargetLowering::Custom:
3446 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3447 if (Tmp1.getNode() != NULL) {
Sanjiv Guptad57f2e12008-11-27 05:58:04 +00003448 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003449 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3450 }
3451 break;
3452 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003453 // Since this produces two values, make sure to remember that we legalized
3454 // both of them.
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003455 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3456 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3457 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003458
3459 case ISD::ADDE:
3460 case ISD::SUBE:
3461 Tmp1 = LegalizeOp(Node->getOperand(0));
3462 Tmp2 = LegalizeOp(Node->getOperand(1));
3463 Tmp3 = LegalizeOp(Node->getOperand(2));
3464 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003465 Tmp3 = Result.getValue(0);
3466 Tmp4 = Result.getValue(1);
3467
3468 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3469 default: assert(0 && "This action is not supported yet!");
3470 case TargetLowering::Legal:
3471 break;
3472 case TargetLowering::Custom:
3473 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3474 if (Tmp1.getNode() != NULL) {
Sanjiv Guptad57f2e12008-11-27 05:58:04 +00003475 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003476 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3477 }
3478 break;
3479 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003480 // Since this produces two values, make sure to remember that we legalized
3481 // both of them.
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003482 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3483 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3484 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003485
3486 case ISD::BUILD_PAIR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003487 MVT PairTy = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003488 // TODO: handle the case where the Lo and Hi operands are not of legal type
3489 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3490 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3491 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3492 case TargetLowering::Promote:
3493 case TargetLowering::Custom:
3494 assert(0 && "Cannot promote/custom this yet!");
3495 case TargetLowering::Legal:
3496 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3497 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3498 break;
3499 case TargetLowering::Expand:
3500 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3501 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3502 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003503 DAG.getConstant(PairTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003504 TLI.getShiftAmountTy()));
3505 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3506 break;
3507 }
3508 break;
3509 }
3510
3511 case ISD::UREM:
3512 case ISD::SREM:
3513 case ISD::FREM:
3514 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3515 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3516
3517 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3518 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3519 case TargetLowering::Custom:
3520 isCustom = true;
3521 // FALLTHROUGH
3522 case TargetLowering::Legal:
3523 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3524 if (isCustom) {
3525 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003526 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003527 }
3528 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003529 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003530 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3531 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands92c43912008-06-06 12:08:01 +00003532 MVT VT = Node->getValueType(0);
Dan Gohman5a199552007-10-08 18:33:35 +00003533
3534 // See if remainder can be lowered using two-result operations.
3535 SDVTList VTs = DAG.getVTList(VT, VT);
3536 if (Node->getOpcode() == ISD::SREM &&
3537 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003538 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003539 break;
3540 }
3541 if (Node->getOpcode() == ISD::UREM &&
3542 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003543 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003544 break;
3545 }
3546
Duncan Sands92c43912008-06-06 12:08:01 +00003547 if (VT.isInteger()) {
Dan Gohman5a199552007-10-08 18:33:35 +00003548 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003549 TargetLowering::Legal) {
3550 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003551 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3552 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3553 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands92c43912008-06-06 12:08:01 +00003554 } else if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003555 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003556 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003557 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003558 "Cannot expand this binary operator!");
3559 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3560 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman8181bd12008-07-27 21:46:04 +00003561 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003562 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003563 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003564 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00003565 assert(VT.isFloatingPoint() &&
Dan Gohman59b4b102007-11-06 22:11:54 +00003566 "remainder op must have integer or floating-point type");
Duncan Sands92c43912008-06-06 12:08:01 +00003567 if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003568 Result = LegalizeOp(UnrollVectorOp(Op));
3569 } else {
3570 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003571 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3572 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003573 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003574 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003575 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003576 }
3577 break;
3578 }
Dan Gohman5a199552007-10-08 18:33:35 +00003579 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003580 break;
3581 case ISD::VAARG: {
3582 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3583 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3584
Duncan Sands92c43912008-06-06 12:08:01 +00003585 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003586 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3587 default: assert(0 && "This action is not supported yet!");
3588 case TargetLowering::Custom:
3589 isCustom = true;
3590 // FALLTHROUGH
3591 case TargetLowering::Legal:
3592 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3593 Result = Result.getValue(0);
3594 Tmp1 = Result.getValue(1);
3595
3596 if (isCustom) {
3597 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003598 if (Tmp2.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003599 Result = LegalizeOp(Tmp2);
3600 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3601 }
3602 }
3603 break;
3604 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003605 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003606 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003607 // Increment the pointer, VAList, to the next vaarg
Duncan Sands55a4c232008-11-03 11:51:11 +00003608 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3609 DAG.getConstant(TLI.getTargetData()->getABITypeSize(VT.getTypeForMVT()),
3610 TLI.getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003611 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00003612 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003613 // Load the actual argument out of the pointer VAList
3614 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3615 Tmp1 = LegalizeOp(Result.getValue(1));
3616 Result = LegalizeOp(Result);
3617 break;
3618 }
3619 }
3620 // Since VAARG produces two values, make sure to remember that we
3621 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003622 AddLegalizedOperand(SDValue(Node, 0), Result);
3623 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00003624 return Op.getResNo() ? Tmp1 : Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003625 }
3626
3627 case ISD::VACOPY:
3628 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3629 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3630 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3631
3632 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3633 default: assert(0 && "This action is not supported yet!");
3634 case TargetLowering::Custom:
3635 isCustom = true;
3636 // FALLTHROUGH
3637 case TargetLowering::Legal:
3638 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3639 Node->getOperand(3), Node->getOperand(4));
3640 if (isCustom) {
3641 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003642 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003643 }
3644 break;
3645 case TargetLowering::Expand:
3646 // This defaults to loading a pointer from the input and storing it to the
3647 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003648 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3649 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman6b9a08e2008-04-17 02:09:26 +00003650 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3651 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003652 break;
3653 }
3654 break;
3655
3656 case ISD::VAEND:
3657 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3658 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3659
3660 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3661 default: assert(0 && "This action is not supported yet!");
3662 case TargetLowering::Custom:
3663 isCustom = true;
3664 // FALLTHROUGH
3665 case TargetLowering::Legal:
3666 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3667 if (isCustom) {
3668 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003669 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003670 }
3671 break;
3672 case TargetLowering::Expand:
3673 Result = Tmp1; // Default to a no-op, return the chain
3674 break;
3675 }
3676 break;
3677
3678 case ISD::VASTART:
3679 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3680 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3681
3682 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3683
3684 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3685 default: assert(0 && "This action is not supported yet!");
3686 case TargetLowering::Legal: break;
3687 case TargetLowering::Custom:
3688 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003689 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003690 break;
3691 }
3692 break;
3693
3694 case ISD::ROTL:
3695 case ISD::ROTR:
3696 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3697 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3698 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3699 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3700 default:
3701 assert(0 && "ROTL/ROTR legalize operation not supported");
3702 break;
3703 case TargetLowering::Legal:
3704 break;
3705 case TargetLowering::Custom:
3706 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003707 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003708 break;
3709 case TargetLowering::Promote:
3710 assert(0 && "Do not know how to promote ROTL/ROTR");
3711 break;
3712 case TargetLowering::Expand:
3713 assert(0 && "Do not know how to expand ROTL/ROTR");
3714 break;
3715 }
3716 break;
3717
3718 case ISD::BSWAP:
3719 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3720 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3721 case TargetLowering::Custom:
3722 assert(0 && "Cannot custom legalize this yet!");
3723 case TargetLowering::Legal:
3724 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3725 break;
3726 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003727 MVT OVT = Tmp1.getValueType();
3728 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3729 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003730
3731 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3732 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3733 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3734 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3735 break;
3736 }
3737 case TargetLowering::Expand:
3738 Result = ExpandBSWAP(Tmp1);
3739 break;
3740 }
3741 break;
3742
3743 case ISD::CTPOP:
3744 case ISD::CTTZ:
3745 case ISD::CTLZ:
3746 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3747 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003748 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003749 case TargetLowering::Legal:
3750 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003751 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003752 TargetLowering::Custom) {
3753 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003754 if (Tmp1.getNode()) {
Scott Michelbc62b412007-08-02 02:22:46 +00003755 Result = Tmp1;
3756 }
Scott Michel48b63e62007-07-30 21:00:31 +00003757 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003758 break;
3759 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003760 MVT OVT = Tmp1.getValueType();
3761 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003762
3763 // Zero extend the argument.
3764 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3765 // Perform the larger operation, then subtract if needed.
3766 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3767 switch (Node->getOpcode()) {
3768 case ISD::CTPOP:
3769 Result = Tmp1;
3770 break;
3771 case ISD::CTTZ:
3772 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00003773 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003774 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003775 ISD::SETEQ);
3776 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003777 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003778 break;
3779 case ISD::CTLZ:
3780 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3781 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003782 DAG.getConstant(NVT.getSizeInBits() -
3783 OVT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003784 break;
3785 }
3786 break;
3787 }
3788 case TargetLowering::Expand:
3789 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3790 break;
3791 }
3792 break;
3793
3794 // Unary operators
3795 case ISD::FABS:
3796 case ISD::FNEG:
3797 case ISD::FSQRT:
3798 case ISD::FSIN:
3799 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003800 case ISD::FLOG:
3801 case ISD::FLOG2:
3802 case ISD::FLOG10:
3803 case ISD::FEXP:
3804 case ISD::FEXP2:
Dan Gohmanc8b20e22008-08-21 17:55:02 +00003805 case ISD::FTRUNC:
3806 case ISD::FFLOOR:
3807 case ISD::FCEIL:
3808 case ISD::FRINT:
3809 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003810 Tmp1 = LegalizeOp(Node->getOperand(0));
3811 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3812 case TargetLowering::Promote:
3813 case TargetLowering::Custom:
3814 isCustom = true;
3815 // FALLTHROUGH
3816 case TargetLowering::Legal:
3817 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3818 if (isCustom) {
3819 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003820 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003821 }
3822 break;
3823 case TargetLowering::Expand:
3824 switch (Node->getOpcode()) {
3825 default: assert(0 && "Unreachable!");
3826 case ISD::FNEG:
3827 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3828 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3829 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3830 break;
3831 case ISD::FABS: {
3832 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands92c43912008-06-06 12:08:01 +00003833 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003834 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel502151f2008-03-10 15:42:14 +00003835 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00003836 ISD::SETUGT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003837 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3838 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3839 break;
3840 }
Evan Cheng1fac6952008-09-09 23:35:53 +00003841 case ISD::FSQRT:
3842 case ISD::FSIN:
3843 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003844 case ISD::FLOG:
3845 case ISD::FLOG2:
3846 case ISD::FLOG10:
3847 case ISD::FEXP:
3848 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00003849 case ISD::FTRUNC:
3850 case ISD::FFLOOR:
3851 case ISD::FCEIL:
3852 case ISD::FRINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00003853 case ISD::FNEARBYINT: {
Duncan Sands92c43912008-06-06 12:08:01 +00003854 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003855
3856 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003857 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003858 Result = LegalizeOp(UnrollVectorOp(Op));
3859 break;
3860 }
3861
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003862 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3863 switch(Node->getOpcode()) {
3864 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003865 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3866 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003867 break;
3868 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003869 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3870 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003871 break;
3872 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003873 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3874 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003875 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00003876 case ISD::FLOG:
3877 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3878 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3879 break;
3880 case ISD::FLOG2:
3881 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3882 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3883 break;
3884 case ISD::FLOG10:
3885 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3886 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3887 break;
3888 case ISD::FEXP:
3889 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3890 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3891 break;
3892 case ISD::FEXP2:
3893 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3894 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3895 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00003896 case ISD::FTRUNC:
3897 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3898 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3899 break;
3900 case ISD::FFLOOR:
3901 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3902 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3903 break;
3904 case ISD::FCEIL:
3905 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3906 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3907 break;
3908 case ISD::FRINT:
3909 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3910 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3911 break;
3912 case ISD::FNEARBYINT:
3913 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3914 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3915 break;
Evan Cheng1fac6952008-09-09 23:35:53 +00003916 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003917 default: assert(0 && "Unreachable!");
3918 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003919 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003920 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003921 break;
3922 }
3923 }
3924 break;
3925 }
3926 break;
3927 case ISD::FPOWI: {
Duncan Sands92c43912008-06-06 12:08:01 +00003928 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003929
3930 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003931 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003932 Result = LegalizeOp(UnrollVectorOp(Op));
3933 break;
3934 }
3935
3936 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003937 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3938 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003939 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003940 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003941 break;
3942 }
3943 case ISD::BIT_CONVERT:
3944 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003945 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3946 Node->getValueType(0));
Duncan Sands92c43912008-06-06 12:08:01 +00003947 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003948 // The input has to be a vector type, we have to either scalarize it, pack
3949 // it, or convert it based on whether the input vector type is legal.
Gabor Greif1c80d112008-08-28 21:40:38 +00003950 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00003951 int InIx = Node->getOperand(0).getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00003952 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3953 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003954
3955 // Figure out if there is a simple type corresponding to this Vector
3956 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00003957 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003958 if (TLI.isTypeLegal(TVT)) {
3959 // Turn this into a bit convert of the vector input.
3960 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3961 LegalizeOp(Node->getOperand(0)));
3962 break;
3963 } else if (NumElems == 1) {
3964 // Turn this into a bit convert of the scalar input.
3965 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3966 ScalarizeVectorOp(Node->getOperand(0)));
3967 break;
3968 } else {
3969 // FIXME: UNIMP! Store then reload
3970 assert(0 && "Cast from unsupported vector type not implemented yet!");
3971 }
3972 } else {
3973 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3974 Node->getOperand(0).getValueType())) {
3975 default: assert(0 && "Unknown operation action!");
3976 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003977 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3978 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003979 break;
3980 case TargetLowering::Legal:
3981 Tmp1 = LegalizeOp(Node->getOperand(0));
3982 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3983 break;
3984 }
3985 }
3986 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003987 case ISD::CONVERT_RNDSAT: {
3988 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3989 switch (CvtCode) {
3990 default: assert(0 && "Unknown cvt code!");
3991 case ISD::CVT_SF:
3992 case ISD::CVT_UF:
Mon P Wang73d31542008-11-10 20:54:11 +00003993 case ISD::CVT_FF:
Mon P Wang2fc3f9e2008-12-09 07:27:39 +00003994 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003995 case ISD::CVT_FS:
3996 case ISD::CVT_FU:
3997 case ISD::CVT_SS:
3998 case ISD::CVT_SU:
3999 case ISD::CVT_US:
4000 case ISD::CVT_UU: {
4001 SDValue DTyOp = Node->getOperand(1);
4002 SDValue STyOp = Node->getOperand(2);
4003 SDValue RndOp = Node->getOperand(3);
4004 SDValue SatOp = Node->getOperand(4);
4005 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4006 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4007 case Legal:
4008 Tmp1 = LegalizeOp(Node->getOperand(0));
4009 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
4010 RndOp, SatOp);
4011 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4012 TargetLowering::Custom) {
4013 Tmp1 = TLI.LowerOperation(Result, DAG);
4014 if (Tmp1.getNode()) Result = Tmp1;
4015 }
4016 break;
4017 case Promote:
4018 Result = PromoteOp(Node->getOperand(0));
4019 // For FP, make Op1 a i32
4020
Mon P Wang2fc3f9e2008-12-09 07:27:39 +00004021 Result = DAG.getConvertRndSat(Op.getValueType(), Result,
Mon P Wang73d31542008-11-10 20:54:11 +00004022 DTyOp, STyOp, RndOp, SatOp, CvtCode);
4023 break;
4024 }
4025 break;
4026 }
4027 } // end switch CvtCode
4028 break;
4029 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004030 // Conversion operators. The source and destination have different types.
4031 case ISD::SINT_TO_FP:
4032 case ISD::UINT_TO_FP: {
4033 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman29c3cef2008-08-14 20:04:46 +00004034 Result = LegalizeINT_TO_FP(Result, isSigned,
4035 Node->getValueType(0), Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004036 break;
4037 }
4038 case ISD::TRUNCATE:
4039 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4040 case Legal:
4041 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michele9b8a402008-12-02 19:55:08 +00004042 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
4043 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
4044 case TargetLowering::Custom:
Mon P Wang72fe5462008-12-11 00:44:22 +00004045 isCustom = true;
4046 // FALLTHROUGH
Scott Michele9b8a402008-12-02 19:55:08 +00004047 case TargetLowering::Legal:
Mon P Wang72fe5462008-12-11 00:44:22 +00004048 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4049 if (isCustom) {
4050 Tmp1 = TLI.LowerOperation(Result, DAG);
4051 if (Tmp1.getNode()) Result = Tmp1;
4052 }
4053 break;
Mon P Wang83edba52008-12-12 01:25:51 +00004054 case TargetLowering::Expand:
4055 assert(Result.getValueType().isVector() && "must be vector type");
4056 // Unroll the truncate. We should do better.
4057 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Schellerbfc55ee2008-12-02 12:12:25 +00004058 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004059 break;
4060 case Expand:
4061 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4062
4063 // Since the result is legal, we should just be able to truncate the low
4064 // part of the source.
4065 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
4066 break;
4067 case Promote:
4068 Result = PromoteOp(Node->getOperand(0));
4069 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
4070 break;
4071 }
4072 break;
4073
4074 case ISD::FP_TO_SINT:
4075 case ISD::FP_TO_UINT:
4076 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4077 case Legal:
4078 Tmp1 = LegalizeOp(Node->getOperand(0));
4079
4080 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
4081 default: assert(0 && "Unknown operation action!");
4082 case TargetLowering::Custom:
4083 isCustom = true;
4084 // FALLTHROUGH
4085 case TargetLowering::Legal:
4086 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4087 if (isCustom) {
4088 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004089 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004090 }
4091 break;
4092 case TargetLowering::Promote:
4093 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
4094 Node->getOpcode() == ISD::FP_TO_SINT);
4095 break;
4096 case TargetLowering::Expand:
4097 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004098 SDValue True, False;
Duncan Sands92c43912008-06-06 12:08:01 +00004099 MVT VT = Node->getOperand(0).getValueType();
4100 MVT NVT = Node->getValueType(0);
Dale Johannesen958b08b2007-09-19 23:55:34 +00004101 const uint64_t zero[] = {0, 0};
Duncan Sands92c43912008-06-06 12:08:01 +00004102 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
4103 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohman88ae8c52008-02-29 01:44:25 +00004104 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00004105 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel502151f2008-03-10 15:42:14 +00004106 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004107 Node->getOperand(0), Tmp2, ISD::SETLT);
4108 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
4109 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
4110 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
4111 Tmp2));
4112 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman88ae8c52008-02-29 01:44:25 +00004113 DAG.getConstant(x, NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004114 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
4115 break;
4116 } else {
4117 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
4118 }
4119 break;
4120 }
4121 break;
4122 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00004123 MVT VT = Op.getValueType();
4124 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00004125 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004126 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00004127 if (Node->getOpcode() == ISD::FP_TO_SINT) {
4128 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
4129 Node->getOperand(0), DAG.getValueType(MVT::f64));
4130 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
4131 DAG.getIntPtrConstant(1));
4132 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
4133 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00004134 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4135 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4136 Tmp2 = DAG.getConstantFP(apf, OVT);
4137 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4138 // FIXME: generated code sucks.
4139 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
4140 DAG.getNode(ISD::ADD, MVT::i32,
4141 DAG.getNode(ISD::FP_TO_SINT, VT,
4142 DAG.getNode(ISD::FSUB, OVT,
4143 Node->getOperand(0), Tmp2)),
4144 DAG.getConstant(0x80000000, MVT::i32)),
4145 DAG.getNode(ISD::FP_TO_SINT, VT,
4146 Node->getOperand(0)),
4147 DAG.getCondCode(ISD::SETGE));
4148 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004149 break;
4150 }
Dan Gohmanec51f642008-03-10 23:03:31 +00004151 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsf68dffb2008-07-17 02:36:29 +00004152 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4153 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4154 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman8181bd12008-07-27 21:46:04 +00004155 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00004156 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004157 break;
4158 }
4159 case Promote:
4160 Tmp1 = PromoteOp(Node->getOperand(0));
4161 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4162 Result = LegalizeOp(Result);
4163 break;
4164 }
4165 break;
4166
Chris Lattner56ecde32008-01-16 06:57:07 +00004167 case ISD::FP_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004168 MVT DstVT = Op.getValueType();
4169 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004170 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4171 // 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), SrcVT, DstVT);
4174 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00004175 }
4176 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));
4180 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4181 break;
4182 case Promote:
4183 Tmp1 = PromoteOp(Node->getOperand(0));
4184 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4185 break;
4186 }
4187 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004188 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004189 case ISD::FP_ROUND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004190 MVT DstVT = Op.getValueType();
4191 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004192 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4193 if (SrcVT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004194 SDValue Lo;
Dale Johannesena0d36082008-01-20 01:18:38 +00004195 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00004196 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00004197 if (DstVT!=MVT::f64)
4198 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00004199 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004200 }
Chris Lattner5872a362008-01-17 07:00:52 +00004201 // The only other way we can lower this is to turn it into a STORE,
4202 // LOAD pair, targetting a temporary location (a stack slot).
4203 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4204 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004205 }
Chris Lattner56ecde32008-01-16 06:57:07 +00004206 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4207 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4208 case Legal:
4209 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004210 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004211 break;
4212 case Promote:
4213 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004214 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4215 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004216 break;
4217 }
4218 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004219 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004220 case ISD::ANY_EXTEND:
4221 case ISD::ZERO_EXTEND:
4222 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004223 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4224 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4225 case Legal:
4226 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac54d002008-04-30 00:26:38 +00004227 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michelac7091c2008-02-15 23:05:48 +00004228 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4229 TargetLowering::Custom) {
Scott Michelac54d002008-04-30 00:26:38 +00004230 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004231 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelac7091c2008-02-15 23:05:48 +00004232 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004233 break;
4234 case Promote:
4235 switch (Node->getOpcode()) {
4236 case ISD::ANY_EXTEND:
4237 Tmp1 = PromoteOp(Node->getOperand(0));
4238 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
4239 break;
4240 case ISD::ZERO_EXTEND:
4241 Result = PromoteOp(Node->getOperand(0));
4242 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4243 Result = DAG.getZeroExtendInReg(Result,
4244 Node->getOperand(0).getValueType());
4245 break;
4246 case ISD::SIGN_EXTEND:
4247 Result = PromoteOp(Node->getOperand(0));
4248 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4249 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4250 Result,
4251 DAG.getValueType(Node->getOperand(0).getValueType()));
4252 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004253 }
4254 }
4255 break;
4256 case ISD::FP_ROUND_INREG:
4257 case ISD::SIGN_EXTEND_INREG: {
4258 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004259 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004260
4261 // If this operation is not supported, convert it to a shl/shr or load/store
4262 // pair.
4263 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4264 default: assert(0 && "This action not supported for this op yet!");
4265 case TargetLowering::Legal:
4266 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
4267 break;
4268 case TargetLowering::Expand:
4269 // If this is an integer extend and shifts are supported, do that.
4270 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
4271 // NOTE: we could fall back on load/store here too for targets without
4272 // SAR. However, it is doubtful that any exist.
Duncan Sands92c43912008-06-06 12:08:01 +00004273 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4274 ExtraVT.getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00004275 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004276 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4277 Node->getOperand(0), ShiftCst);
4278 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4279 Result, ShiftCst);
4280 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
4281 // The only way we can lower this is to turn it into a TRUNCSTORE,
4282 // EXTLOAD pair, targetting a temporary location (a stack slot).
4283
4284 // NOTE: there is a choice here between constantly creating new stack
4285 // slots and always reusing the same one. We currently always create
4286 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00004287 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4288 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004289 } else {
4290 assert(0 && "Unknown op");
4291 }
4292 break;
4293 }
4294 break;
4295 }
Duncan Sands38947cd2007-07-27 12:58:54 +00004296 case ISD::TRAMPOLINE: {
Dan Gohman8181bd12008-07-27 21:46:04 +00004297 SDValue Ops[6];
Duncan Sands38947cd2007-07-27 12:58:54 +00004298 for (unsigned i = 0; i != 6; ++i)
4299 Ops[i] = LegalizeOp(Node->getOperand(i));
4300 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4301 // The only option for this node is to custom lower it.
4302 Result = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004303 assert(Result.getNode() && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00004304
4305 // Since trampoline produces two values, make sure to remember that we
4306 // legalized both of them.
4307 Tmp1 = LegalizeOp(Result.getValue(1));
4308 Result = LegalizeOp(Result);
Dan Gohman8181bd12008-07-27 21:46:04 +00004309 AddLegalizedOperand(SDValue(Node, 0), Result);
4310 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00004311 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00004312 }
Dan Gohmane8e4a412008-05-14 00:43:10 +00004313 case ISD::FLT_ROUNDS_: {
Duncan Sands92c43912008-06-06 12:08:01 +00004314 MVT VT = Node->getValueType(0);
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004315 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4316 default: assert(0 && "This action not supported for this op yet!");
4317 case TargetLowering::Custom:
4318 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004319 if (Result.getNode()) break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004320 // Fall Thru
4321 case TargetLowering::Legal:
4322 // If this operation is not supported, lower it to constant 1
4323 Result = DAG.getConstant(1, VT);
4324 break;
4325 }
Dan Gohmane09dc8c2008-05-12 16:07:15 +00004326 break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004327 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004328 case ISD::TRAP: {
Duncan Sands92c43912008-06-06 12:08:01 +00004329 MVT VT = Node->getValueType(0);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004330 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4331 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00004332 case TargetLowering::Legal:
4333 Tmp1 = LegalizeOp(Node->getOperand(0));
4334 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4335 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004336 case TargetLowering::Custom:
4337 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004338 if (Result.getNode()) break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004339 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00004340 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004341 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00004342 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004343 TargetLowering::ArgListTy Args;
Dan Gohman8181bd12008-07-27 21:46:04 +00004344 std::pair<SDValue,SDValue> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004345 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00004346 false, false, false, false, CallingConv::C, false,
Bill Wendlingfef06052008-09-16 21:48:12 +00004347 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner88e03932008-01-15 22:09:33 +00004348 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004349 Result = CallResult.second;
4350 break;
4351 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004352 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004353 }
Bill Wendling913dcf32008-11-22 00:22:52 +00004354
Bill Wendling7e04be62008-12-09 22:08:41 +00004355 case ISD::SADDO:
4356 case ISD::SSUBO: {
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004357 MVT VT = Node->getValueType(0);
4358 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4359 default: assert(0 && "This action not supported for this op yet!");
4360 case TargetLowering::Custom:
4361 Result = TLI.LowerOperation(Op, DAG);
4362 if (Result.getNode()) break;
4363 // FALLTHROUGH
4364 case TargetLowering::Legal: {
4365 SDValue LHS = LegalizeOp(Node->getOperand(0));
4366 SDValue RHS = LegalizeOp(Node->getOperand(1));
4367
Bill Wendling7e04be62008-12-09 22:08:41 +00004368 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
4369 ISD::ADD : ISD::SUB, LHS.getValueType(),
4370 LHS, RHS);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004371 MVT OType = Node->getValueType(1);
4372
Bill Wendlingc65e6e42008-11-25 08:19:22 +00004373 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004374
Bill Wendlingcf4de122008-11-25 19:40:17 +00004375 // LHSSign -> LHS >= 0
4376 // RHSSign -> RHS >= 0
4377 // SumSign -> Sum >= 0
4378 //
Bill Wendling7e04be62008-12-09 22:08:41 +00004379 // Add:
Bill Wendlingcf4de122008-11-25 19:40:17 +00004380 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling7e04be62008-12-09 22:08:41 +00004381 // Sub:
4382 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendlingcf4de122008-11-25 19:40:17 +00004383 //
4384 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4385 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
Bill Wendling7e04be62008-12-09 22:08:41 +00004386 SDValue SignsMatch = DAG.getSetCC(OType, LHSSign, RHSSign,
4387 Node->getOpcode() == ISD::SADDO ?
4388 ISD::SETEQ : ISD::SETNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004389
Bill Wendlingcf4de122008-11-25 19:40:17 +00004390 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4391 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004392
Bill Wendling7e04be62008-12-09 22:08:41 +00004393 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsMatch, SumSignNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004394
4395 MVT ValueVTs[] = { LHS.getValueType(), OType };
4396 SDValue Ops[] = { Sum, Cmp };
4397
Duncan Sands42d7bb82008-12-01 11:41:29 +00004398 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4399 &Ops[0], 2);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004400 SDNode *RNode = Result.getNode();
4401 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4402 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4403 break;
4404 }
4405 }
4406
4407 break;
4408 }
Bill Wendling7e04be62008-12-09 22:08:41 +00004409 case ISD::UADDO:
4410 case ISD::USUBO: {
Bill Wendling4c134df2008-11-24 19:21:46 +00004411 MVT VT = Node->getValueType(0);
4412 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4413 default: assert(0 && "This action not supported for this op yet!");
4414 case TargetLowering::Custom:
4415 Result = TLI.LowerOperation(Op, DAG);
4416 if (Result.getNode()) break;
4417 // FALLTHROUGH
4418 case TargetLowering::Legal: {
4419 SDValue LHS = LegalizeOp(Node->getOperand(0));
4420 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling913dcf32008-11-22 00:22:52 +00004421
Bill Wendling7e04be62008-12-09 22:08:41 +00004422 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
4423 ISD::ADD : ISD::SUB, LHS.getValueType(),
4424 LHS, RHS);
Bill Wendling4c134df2008-11-24 19:21:46 +00004425 MVT OType = Node->getValueType(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00004426 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS,
4427 Node->getOpcode () == ISD::UADDO ?
4428 ISD::SETULT : ISD::SETUGT);
Bill Wendling913dcf32008-11-22 00:22:52 +00004429
Bill Wendling4c134df2008-11-24 19:21:46 +00004430 MVT ValueVTs[] = { LHS.getValueType(), OType };
4431 SDValue Ops[] = { Sum, Cmp };
Bill Wendling913dcf32008-11-22 00:22:52 +00004432
Duncan Sands42d7bb82008-12-01 11:41:29 +00004433 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4434 &Ops[0], 2);
Bill Wendling4c134df2008-11-24 19:21:46 +00004435 SDNode *RNode = Result.getNode();
4436 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4437 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4438 break;
4439 }
4440 }
4441
Bill Wendling913dcf32008-11-22 00:22:52 +00004442 break;
4443 }
Bill Wendling7e04be62008-12-09 22:08:41 +00004444 case ISD::SMULO:
4445 case ISD::UMULO: {
4446 MVT VT = Node->getValueType(0);
4447 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4448 default: assert(0 && "This action is not supported at all!");
4449 case TargetLowering::Custom:
4450 Result = TLI.LowerOperation(Op, DAG);
4451 if (Result.getNode()) break;
4452 // Fall Thru
4453 case TargetLowering::Legal:
4454 // FIXME: According to Hacker's Delight, this can be implemented in
4455 // target independent lowering, but it would be inefficient, since it
Bill Wendling35f1a9d2008-12-10 02:01:32 +00004456 // requires a division + a branch.
Bill Wendling7e04be62008-12-09 22:08:41 +00004457 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4458 break;
4459 }
4460 break;
4461 }
4462
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004463 }
4464
4465 assert(Result.getValueType() == Op.getValueType() &&
4466 "Bad legalization!");
4467
4468 // Make sure that the generated code is itself legal.
4469 if (Result != Op)
4470 Result = LegalizeOp(Result);
4471
4472 // Note that LegalizeOp may be reentered even from single-use nodes, which
4473 // means that we always must cache transformed nodes.
4474 AddLegalizedOperand(Op, Result);
4475 return Result;
4476}
4477
4478/// PromoteOp - Given an operation that produces a value in an invalid type,
4479/// promote it to compute the value into a larger type. The produced value will
4480/// have the correct bits for the low portion of the register, but no guarantee
4481/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +00004482SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00004483 MVT VT = Op.getValueType();
4484 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485 assert(getTypeAction(VT) == Promote &&
4486 "Caller should expand or legalize operands that are not promotable!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004487 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004488 "Cannot promote to smaller type!");
4489
Dan Gohman8181bd12008-07-27 21:46:04 +00004490 SDValue Tmp1, Tmp2, Tmp3;
4491 SDValue Result;
Gabor Greif1c80d112008-08-28 21:40:38 +00004492 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004493
Dan Gohman8181bd12008-07-27 21:46:04 +00004494 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004495 if (I != PromotedNodes.end()) return I->second;
4496
4497 switch (Node->getOpcode()) {
4498 case ISD::CopyFromReg:
4499 assert(0 && "CopyFromReg must be legal!");
4500 default:
4501#ifndef NDEBUG
4502 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4503#endif
4504 assert(0 && "Do not know how to promote this operator!");
4505 abort();
4506 case ISD::UNDEF:
4507 Result = DAG.getNode(ISD::UNDEF, NVT);
4508 break;
4509 case ISD::Constant:
4510 if (VT != MVT::i1)
4511 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4512 else
4513 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
4514 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4515 break;
4516 case ISD::ConstantFP:
4517 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4518 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4519 break;
4520
4521 case ISD::SETCC:
Scott Michel502151f2008-03-10 15:42:14 +00004522 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004523 && "SetCC type is not legal??");
Scott Michel502151f2008-03-10 15:42:14 +00004524 Result = DAG.getNode(ISD::SETCC,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004525 TLI.getSetCCResultType(Node->getOperand(0)),
4526 Node->getOperand(0), Node->getOperand(1),
4527 Node->getOperand(2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004528 break;
4529
4530 case ISD::TRUNCATE:
4531 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4532 case Legal:
4533 Result = LegalizeOp(Node->getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00004534 assert(Result.getValueType().bitsGE(NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004535 "This truncation doesn't make sense!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004536 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004537 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4538 break;
4539 case Promote:
4540 // The truncation is not required, because we don't guarantee anything
4541 // about high bits anyway.
4542 Result = PromoteOp(Node->getOperand(0));
4543 break;
4544 case Expand:
4545 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4546 // Truncate the low part of the expanded value to the result type
4547 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4548 }
4549 break;
4550 case ISD::SIGN_EXTEND:
4551 case ISD::ZERO_EXTEND:
4552 case ISD::ANY_EXTEND:
4553 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4554 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4555 case Legal:
4556 // Input is legal? Just do extend all the way to the larger type.
4557 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4558 break;
4559 case Promote:
4560 // Promote the reg if it's smaller.
4561 Result = PromoteOp(Node->getOperand(0));
4562 // The high bits are not guaranteed to be anything. Insert an extend.
4563 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4564 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4565 DAG.getValueType(Node->getOperand(0).getValueType()));
4566 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4567 Result = DAG.getZeroExtendInReg(Result,
4568 Node->getOperand(0).getValueType());
4569 break;
4570 }
4571 break;
Mon P Wang73d31542008-11-10 20:54:11 +00004572 case ISD::CONVERT_RNDSAT: {
4573 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4574 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4575 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4576 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4577 "can only promote integers");
4578 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4579 Node->getOperand(1), Node->getOperand(2),
4580 Node->getOperand(3), Node->getOperand(4),
4581 CvtCode);
4582 break;
4583
4584 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004586 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4587 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004588 Result = PromoteOp(Result);
4589 break;
4590
4591 case ISD::FP_EXTEND:
4592 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4593 case ISD::FP_ROUND:
4594 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4595 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4596 case Promote: assert(0 && "Unreachable with 2 FP types!");
4597 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004598 if (Node->getConstantOperandVal(1) == 0) {
4599 // Input is legal? Do an FP_ROUND_INREG.
4600 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4601 DAG.getValueType(VT));
4602 } else {
4603 // Just remove the truncate, it isn't affecting the value.
4604 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4605 Node->getOperand(1));
4606 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004607 break;
4608 }
4609 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004610 case ISD::SINT_TO_FP:
4611 case ISD::UINT_TO_FP:
4612 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4613 case Legal:
4614 // No extra round required here.
4615 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4616 break;
4617
4618 case Promote:
4619 Result = PromoteOp(Node->getOperand(0));
4620 if (Node->getOpcode() == ISD::SINT_TO_FP)
4621 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4622 Result,
4623 DAG.getValueType(Node->getOperand(0).getValueType()));
4624 else
4625 Result = DAG.getZeroExtendInReg(Result,
4626 Node->getOperand(0).getValueType());
4627 // No extra round required here.
4628 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4629 break;
4630 case Expand:
4631 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4632 Node->getOperand(0));
4633 // Round if we cannot tolerate excess precision.
4634 if (NoExcessFPPrecision)
4635 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4636 DAG.getValueType(VT));
4637 break;
4638 }
4639 break;
4640
4641 case ISD::SIGN_EXTEND_INREG:
4642 Result = PromoteOp(Node->getOperand(0));
4643 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4644 Node->getOperand(1));
4645 break;
4646 case ISD::FP_TO_SINT:
4647 case ISD::FP_TO_UINT:
4648 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4649 case Legal:
4650 case Expand:
4651 Tmp1 = Node->getOperand(0);
4652 break;
4653 case Promote:
4654 // The input result is prerounded, so we don't have to do anything
4655 // special.
4656 Tmp1 = PromoteOp(Node->getOperand(0));
4657 break;
4658 }
4659 // If we're promoting a UINT to a larger size, check to see if the new node
4660 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4661 // we can use that instead. This allows us to generate better code for
4662 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4663 // legal, such as PowerPC.
4664 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4665 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4666 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4667 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4668 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4669 } else {
4670 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4671 }
4672 break;
4673
4674 case ISD::FABS:
4675 case ISD::FNEG:
4676 Tmp1 = PromoteOp(Node->getOperand(0));
4677 assert(Tmp1.getValueType() == NVT);
4678 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4679 // NOTE: we do not have to do any extra rounding here for
4680 // NoExcessFPPrecision, because we know the input will have the appropriate
4681 // precision, and these operations don't modify precision at all.
4682 break;
4683
Dale Johannesen92b33082008-09-04 00:47:13 +00004684 case ISD::FLOG:
4685 case ISD::FLOG2:
4686 case ISD::FLOG10:
4687 case ISD::FEXP:
4688 case ISD::FEXP2:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004689 case ISD::FSQRT:
4690 case ISD::FSIN:
4691 case ISD::FCOS:
Dan Gohmanb2158232008-08-21 18:38:14 +00004692 case ISD::FTRUNC:
4693 case ISD::FFLOOR:
4694 case ISD::FCEIL:
4695 case ISD::FRINT:
4696 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004697 Tmp1 = PromoteOp(Node->getOperand(0));
4698 assert(Tmp1.getValueType() == NVT);
4699 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4700 if (NoExcessFPPrecision)
4701 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4702 DAG.getValueType(VT));
4703 break;
4704
Evan Cheng1fac6952008-09-09 23:35:53 +00004705 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004706 case ISD::FPOWI: {
Evan Cheng1fac6952008-09-09 23:35:53 +00004707 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004708 // directly as well, which may be better.
4709 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng1fac6952008-09-09 23:35:53 +00004710 Tmp2 = Node->getOperand(1);
4711 if (Node->getOpcode() == ISD::FPOW)
4712 Tmp2 = PromoteOp(Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004713 assert(Tmp1.getValueType() == NVT);
Evan Cheng1fac6952008-09-09 23:35:53 +00004714 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004715 if (NoExcessFPPrecision)
4716 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4717 DAG.getValueType(VT));
4718 break;
4719 }
4720
Dale Johannesenbc187662008-08-28 02:44:49 +00004721 case ISD::ATOMIC_CMP_SWAP_8:
4722 case ISD::ATOMIC_CMP_SWAP_16:
4723 case ISD::ATOMIC_CMP_SWAP_32:
4724 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004725 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004726 Tmp2 = PromoteOp(Node->getOperand(2));
4727 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004728 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4729 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004730 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004731 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004732 // Remember that we legalized the chain.
4733 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4734 break;
4735 }
Dale Johannesenbc187662008-08-28 02:44:49 +00004736 case ISD::ATOMIC_LOAD_ADD_8:
4737 case ISD::ATOMIC_LOAD_SUB_8:
4738 case ISD::ATOMIC_LOAD_AND_8:
4739 case ISD::ATOMIC_LOAD_OR_8:
4740 case ISD::ATOMIC_LOAD_XOR_8:
4741 case ISD::ATOMIC_LOAD_NAND_8:
4742 case ISD::ATOMIC_LOAD_MIN_8:
4743 case ISD::ATOMIC_LOAD_MAX_8:
4744 case ISD::ATOMIC_LOAD_UMIN_8:
4745 case ISD::ATOMIC_LOAD_UMAX_8:
4746 case ISD::ATOMIC_SWAP_8:
4747 case ISD::ATOMIC_LOAD_ADD_16:
4748 case ISD::ATOMIC_LOAD_SUB_16:
4749 case ISD::ATOMIC_LOAD_AND_16:
4750 case ISD::ATOMIC_LOAD_OR_16:
4751 case ISD::ATOMIC_LOAD_XOR_16:
4752 case ISD::ATOMIC_LOAD_NAND_16:
4753 case ISD::ATOMIC_LOAD_MIN_16:
4754 case ISD::ATOMIC_LOAD_MAX_16:
4755 case ISD::ATOMIC_LOAD_UMIN_16:
4756 case ISD::ATOMIC_LOAD_UMAX_16:
4757 case ISD::ATOMIC_SWAP_16:
4758 case ISD::ATOMIC_LOAD_ADD_32:
4759 case ISD::ATOMIC_LOAD_SUB_32:
4760 case ISD::ATOMIC_LOAD_AND_32:
4761 case ISD::ATOMIC_LOAD_OR_32:
4762 case ISD::ATOMIC_LOAD_XOR_32:
4763 case ISD::ATOMIC_LOAD_NAND_32:
4764 case ISD::ATOMIC_LOAD_MIN_32:
4765 case ISD::ATOMIC_LOAD_MAX_32:
4766 case ISD::ATOMIC_LOAD_UMIN_32:
4767 case ISD::ATOMIC_LOAD_UMAX_32:
4768 case ISD::ATOMIC_SWAP_32:
4769 case ISD::ATOMIC_LOAD_ADD_64:
4770 case ISD::ATOMIC_LOAD_SUB_64:
4771 case ISD::ATOMIC_LOAD_AND_64:
4772 case ISD::ATOMIC_LOAD_OR_64:
4773 case ISD::ATOMIC_LOAD_XOR_64:
4774 case ISD::ATOMIC_LOAD_NAND_64:
4775 case ISD::ATOMIC_LOAD_MIN_64:
4776 case ISD::ATOMIC_LOAD_MAX_64:
4777 case ISD::ATOMIC_LOAD_UMIN_64:
4778 case ISD::ATOMIC_LOAD_UMAX_64:
4779 case ISD::ATOMIC_SWAP_64: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004780 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004781 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004782 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4783 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004784 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004785 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004786 // Remember that we legalized the chain.
4787 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4788 break;
4789 }
4790
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004791 case ISD::AND:
4792 case ISD::OR:
4793 case ISD::XOR:
4794 case ISD::ADD:
4795 case ISD::SUB:
4796 case ISD::MUL:
4797 // The input may have strange things in the top bits of the registers, but
4798 // these operations don't care. They may have weird bits going out, but
4799 // that too is okay if they are integer operations.
4800 Tmp1 = PromoteOp(Node->getOperand(0));
4801 Tmp2 = PromoteOp(Node->getOperand(1));
4802 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4803 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4804 break;
4805 case ISD::FADD:
4806 case ISD::FSUB:
4807 case ISD::FMUL:
4808 Tmp1 = PromoteOp(Node->getOperand(0));
4809 Tmp2 = PromoteOp(Node->getOperand(1));
4810 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4811 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4812
4813 // Floating point operations will give excess precision that we may not be
4814 // able to tolerate. If we DO allow excess precision, just leave it,
4815 // otherwise excise it.
4816 // FIXME: Why would we need to round FP ops more than integer ones?
4817 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4818 if (NoExcessFPPrecision)
4819 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4820 DAG.getValueType(VT));
4821 break;
4822
4823 case ISD::SDIV:
4824 case ISD::SREM:
4825 // These operators require that their input be sign extended.
4826 Tmp1 = PromoteOp(Node->getOperand(0));
4827 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004828 if (NVT.isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004829 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4830 DAG.getValueType(VT));
4831 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4832 DAG.getValueType(VT));
4833 }
4834 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4835
4836 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands92c43912008-06-06 12:08:01 +00004837 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004838 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4839 DAG.getValueType(VT));
4840 break;
4841 case ISD::FDIV:
4842 case ISD::FREM:
4843 case ISD::FCOPYSIGN:
4844 // These operators require that their input be fp extended.
4845 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004846 case Expand: assert(0 && "not implemented");
4847 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4848 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004849 }
4850 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004851 case Expand: assert(0 && "not implemented");
4852 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4853 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004854 }
4855 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4856
4857 // Perform FP_ROUND: this is probably overly pessimistic.
4858 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4859 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4860 DAG.getValueType(VT));
4861 break;
4862
4863 case ISD::UDIV:
4864 case ISD::UREM:
4865 // These operators require that their input be zero extended.
4866 Tmp1 = PromoteOp(Node->getOperand(0));
4867 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004868 assert(NVT.isInteger() && "Operators don't apply to FP!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004869 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4870 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4871 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4872 break;
4873
4874 case ISD::SHL:
4875 Tmp1 = PromoteOp(Node->getOperand(0));
4876 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4877 break;
4878 case ISD::SRA:
4879 // The input value must be properly sign extended.
4880 Tmp1 = PromoteOp(Node->getOperand(0));
4881 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4882 DAG.getValueType(VT));
4883 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4884 break;
4885 case ISD::SRL:
4886 // The input value must be properly zero extended.
4887 Tmp1 = PromoteOp(Node->getOperand(0));
4888 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4889 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4890 break;
4891
4892 case ISD::VAARG:
4893 Tmp1 = Node->getOperand(0); // Get the chain.
4894 Tmp2 = Node->getOperand(1); // Get the pointer.
4895 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4896 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sandsac496a12008-07-04 11:47:58 +00004897 Result = TLI.LowerOperation(Tmp3, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004898 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004899 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00004900 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004901 // Increment the pointer, VAList, to the next vaarg
4902 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands92c43912008-06-06 12:08:01 +00004903 DAG.getConstant(VT.getSizeInBits()/8,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004904 TLI.getPointerTy()));
4905 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00004906 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004907 // Load the actual argument out of the pointer VAList
4908 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4909 }
4910 // Remember that we legalized the chain.
4911 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4912 break;
4913
4914 case ISD::LOAD: {
4915 LoadSDNode *LD = cast<LoadSDNode>(Node);
4916 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4917 ? ISD::EXTLOAD : LD->getExtensionType();
4918 Result = DAG.getExtLoad(ExtType, NVT,
4919 LD->getChain(), LD->getBasePtr(),
4920 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004921 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004922 LD->isVolatile(),
4923 LD->getAlignment());
4924 // Remember that we legalized the chain.
4925 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4926 break;
4927 }
Scott Michel67224b22008-06-02 22:18:03 +00004928 case ISD::SELECT: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004929 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4930 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel67224b22008-06-02 22:18:03 +00004931
Duncan Sands92c43912008-06-06 12:08:01 +00004932 MVT VT2 = Tmp2.getValueType();
Scott Michel67224b22008-06-02 22:18:03 +00004933 assert(VT2 == Tmp3.getValueType()
Scott Michel7b54de02008-06-03 19:13:20 +00004934 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4935 // Ensure that the resulting node is at least the same size as the operands'
4936 // value types, because we cannot assume that TLI.getSetCCValueType() is
4937 // constant.
4938 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004939 break;
Scott Michel67224b22008-06-02 22:18:03 +00004940 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004941 case ISD::SELECT_CC:
4942 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4943 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4944 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4945 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4946 break;
4947 case ISD::BSWAP:
4948 Tmp1 = Node->getOperand(0);
4949 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4950 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4951 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004952 DAG.getConstant(NVT.getSizeInBits() -
4953 VT.getSizeInBits(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004954 TLI.getShiftAmountTy()));
4955 break;
4956 case ISD::CTPOP:
4957 case ISD::CTTZ:
4958 case ISD::CTLZ:
4959 // Zero extend the argument
4960 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4961 // Perform the larger operation, then subtract if needed.
4962 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4963 switch(Node->getOpcode()) {
4964 case ISD::CTPOP:
4965 Result = Tmp1;
4966 break;
4967 case ISD::CTTZ:
4968 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00004969 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004970 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004971 ISD::SETEQ);
4972 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00004973 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004974 break;
4975 case ISD::CTLZ:
4976 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4977 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004978 DAG.getConstant(NVT.getSizeInBits() -
4979 VT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004980 break;
4981 }
4982 break;
4983 case ISD::EXTRACT_SUBVECTOR:
4984 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4985 break;
4986 case ISD::EXTRACT_VECTOR_ELT:
4987 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4988 break;
4989 }
4990
Gabor Greif1c80d112008-08-28 21:40:38 +00004991 assert(Result.getNode() && "Didn't set a result!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004992
4993 // Make sure the result is itself legal.
4994 Result = LegalizeOp(Result);
4995
4996 // Remember that we promoted this!
4997 AddPromotedOperand(Op, Result);
4998 return Result;
4999}
5000
5001/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
5002/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
5003/// based on the vector type. The return type of this matches the element type
5004/// of the vector, which may not be legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00005005SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005006 // We know that operand #0 is the Vec vector. If the index is a constant
5007 // or if the invec is a supported hardware type, we can use it. Otherwise,
5008 // lower to a store then an indexed load.
Dan Gohman8181bd12008-07-27 21:46:04 +00005009 SDValue Vec = Op.getOperand(0);
5010 SDValue Idx = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005011
Duncan Sands92c43912008-06-06 12:08:01 +00005012 MVT TVT = Vec.getValueType();
5013 unsigned NumElems = TVT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005014
5015 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
5016 default: assert(0 && "This action is not supported yet!");
5017 case TargetLowering::Custom: {
5018 Vec = LegalizeOp(Vec);
5019 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00005020 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005021 if (Tmp3.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005022 return Tmp3;
5023 break;
5024 }
5025 case TargetLowering::Legal:
5026 if (isTypeLegal(TVT)) {
5027 Vec = LegalizeOp(Vec);
5028 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00005029 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005030 }
5031 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00005032 case TargetLowering::Promote:
5033 assert(TVT.isVector() && "not vector type");
5034 // fall thru to expand since vectors are by default are promote
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005035 case TargetLowering::Expand:
5036 break;
5037 }
5038
5039 if (NumElems == 1) {
5040 // This must be an access of the only element. Return it.
5041 Op = ScalarizeVectorOp(Vec);
5042 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00005043 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005044 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00005045 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005046 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005047 if (CIdx->getZExtValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005048 Vec = Lo;
5049 } else {
5050 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005051 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005052 Idx.getValueType());
5053 }
5054
5055 // It's now an extract from the appropriate high or low part. Recurse.
5056 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
5057 Op = ExpandEXTRACT_VECTOR_ELT(Op);
5058 } else {
5059 // Store the value to a temporary stack slot, then LOAD the scalar
5060 // element back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00005061 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
5062 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005063
5064 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +00005065 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005066 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
5067 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00005068
Duncan Sandsec142ee2008-06-08 20:54:56 +00005069 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00005070 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00005071 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00005072 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00005073
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005074 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
5075
5076 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
5077 }
5078 return Op;
5079}
5080
5081/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
5082/// we assume the operation can be split if it is not already legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00005083SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005084 // We know that operand #0 is the Vec vector. For now we assume the index
5085 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman8181bd12008-07-27 21:46:04 +00005086 SDValue Vec = Op.getOperand(0);
5087 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005088
Duncan Sands92c43912008-06-06 12:08:01 +00005089 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005090
Duncan Sands92c43912008-06-06 12:08:01 +00005091 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005092 // This must be an access of the desired vector length. Return it.
5093 return Vec;
5094 }
5095
5096 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00005097 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005098 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005099 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005100 Vec = Lo;
5101 } else {
5102 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005103 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
5104 Idx.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005105 }
5106
5107 // It's now an extract from the appropriate high or low part. Recurse.
5108 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
5109 return ExpandEXTRACT_SUBVECTOR(Op);
5110}
5111
5112/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
5113/// with condition CC on the current target. This usually involves legalizing
5114/// or promoting the arguments. In the case where LHS and RHS must be expanded,
5115/// there may be no choice but to create a new SetCC node to represent the
5116/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman8181bd12008-07-27 21:46:04 +00005117/// LHS, and the SDValue returned in RHS has a nil SDNode value.
5118void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
5119 SDValue &RHS,
5120 SDValue &CC) {
5121 SDValue Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005122
5123 switch (getTypeAction(LHS.getValueType())) {
5124 case Legal:
5125 Tmp1 = LegalizeOp(LHS); // LHS
5126 Tmp2 = LegalizeOp(RHS); // RHS
5127 break;
5128 case Promote:
5129 Tmp1 = PromoteOp(LHS); // LHS
5130 Tmp2 = PromoteOp(RHS); // RHS
5131
5132 // If this is an FP compare, the operands have already been extended.
Duncan Sands92c43912008-06-06 12:08:01 +00005133 if (LHS.getValueType().isInteger()) {
5134 MVT VT = LHS.getValueType();
5135 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005136
5137 // Otherwise, we have to insert explicit sign or zero extends. Note
5138 // that we could insert sign extends for ALL conditions, but zero extend
5139 // is cheaper on many machines (an AND instead of two shifts), so prefer
5140 // it.
5141 switch (cast<CondCodeSDNode>(CC)->get()) {
5142 default: assert(0 && "Unknown integer comparison!");
5143 case ISD::SETEQ:
5144 case ISD::SETNE:
5145 case ISD::SETUGE:
5146 case ISD::SETUGT:
5147 case ISD::SETULE:
5148 case ISD::SETULT:
5149 // ALL of these operations will work if we either sign or zero extend
5150 // the operands (including the unsigned comparisons!). Zero extend is
5151 // usually a simpler/cheaper operation, so prefer it.
5152 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
5153 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
5154 break;
5155 case ISD::SETGE:
5156 case ISD::SETGT:
5157 case ISD::SETLT:
5158 case ISD::SETLE:
5159 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
5160 DAG.getValueType(VT));
5161 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
5162 DAG.getValueType(VT));
Evan Chengd901b662008-10-13 18:46:18 +00005163 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5164 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005165 break;
5166 }
5167 }
5168 break;
5169 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00005170 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005171 if (VT == MVT::f32 || VT == MVT::f64) {
5172 // Expand into one or more soft-fp libcall(s).
Evan Cheng24108632008-07-01 21:35:46 +00005173 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005174 switch (cast<CondCodeSDNode>(CC)->get()) {
5175 case ISD::SETEQ:
5176 case ISD::SETOEQ:
5177 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5178 break;
5179 case ISD::SETNE:
5180 case ISD::SETUNE:
5181 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
5182 break;
5183 case ISD::SETGE:
5184 case ISD::SETOGE:
5185 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5186 break;
5187 case ISD::SETLT:
5188 case ISD::SETOLT:
5189 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5190 break;
5191 case ISD::SETLE:
5192 case ISD::SETOLE:
5193 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5194 break;
5195 case ISD::SETGT:
5196 case ISD::SETOGT:
5197 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
5198 break;
5199 case ISD::SETUO:
5200 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5201 break;
5202 case ISD::SETO:
5203 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
5204 break;
5205 default:
5206 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5207 switch (cast<CondCodeSDNode>(CC)->get()) {
5208 case ISD::SETONE:
5209 // SETONE = SETOLT | SETOGT
5210 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5211 // Fallthrough
5212 case ISD::SETUGT:
5213 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
5214 break;
5215 case ISD::SETUGE:
5216 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5217 break;
5218 case ISD::SETULT:
5219 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5220 break;
5221 case ISD::SETULE:
5222 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5223 break;
5224 case ISD::SETUEQ:
5225 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5226 break;
5227 default: assert(0 && "Unsupported FP setcc!");
5228 }
5229 }
Duncan Sandsf19591c2008-06-30 10:19:09 +00005230
Dan Gohman8181bd12008-07-27 21:46:04 +00005231 SDValue Dummy;
5232 SDValue Ops[2] = { LHS, RHS };
Gabor Greif1c80d112008-08-28 21:40:38 +00005233 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005234 false /*sign irrelevant*/, Dummy);
5235 Tmp2 = DAG.getConstant(0, MVT::i32);
5236 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
5237 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel502151f2008-03-10 15:42:14 +00005238 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005239 CC);
Gabor Greif1c80d112008-08-28 21:40:38 +00005240 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005241 false /*sign irrelevant*/, Dummy);
Scott Michel502151f2008-03-10 15:42:14 +00005242 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005243 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
5244 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005245 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005246 }
Evan Cheng18a1ab12008-07-07 07:18:09 +00005247 LHS = LegalizeOp(Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005248 RHS = Tmp2;
5249 return;
5250 }
5251
Dan Gohman8181bd12008-07-27 21:46:04 +00005252 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005253 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005254 ExpandOp(RHS, RHSLo, RHSHi);
5255 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5256
5257 if (VT==MVT::ppcf128) {
5258 // FIXME: This generated code sucks. We want to generate
Dale Johannesen26317b62008-09-12 00:30:56 +00005259 // FCMPU crN, hi1, hi2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005260 // BNE crN, L:
Dale Johannesen26317b62008-09-12 00:30:56 +00005261 // FCMPU crN, lo1, lo2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005262 // The following can be improved, but not that much.
Dale Johannesen26317b62008-09-12 00:30:56 +00005263 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5264 ISD::SETOEQ);
Scott Michel502151f2008-03-10 15:42:14 +00005265 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005266 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Dale Johannesen26317b62008-09-12 00:30:56 +00005267 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5268 ISD::SETUNE);
Scott Michel502151f2008-03-10 15:42:14 +00005269 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005270 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5271 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00005272 Tmp2 = SDValue();
Dale Johannesen472d15d2007-10-06 01:24:11 +00005273 break;
5274 }
5275
5276 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005277 case ISD::SETEQ:
5278 case ISD::SETNE:
5279 if (RHSLo == RHSHi)
5280 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5281 if (RHSCST->isAllOnesValue()) {
5282 // Comparison to -1.
5283 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5284 Tmp2 = RHSLo;
5285 break;
5286 }
5287
5288 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5289 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5290 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5291 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5292 break;
5293 default:
5294 // If this is a comparison of the sign bit, just look at the top part.
5295 // X > -1, x < 0
5296 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5297 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00005298 CST->isNullValue()) || // X < 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005299 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5300 CST->isAllOnesValue())) { // X > -1
5301 Tmp1 = LHSHi;
5302 Tmp2 = RHSHi;
5303 break;
5304 }
5305
5306 // FIXME: This generated code sucks.
5307 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005308 switch (CCCode) {
5309 default: assert(0 && "Unknown integer setcc!");
5310 case ISD::SETLT:
5311 case ISD::SETULT: LowCC = ISD::SETULT; break;
5312 case ISD::SETGT:
5313 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5314 case ISD::SETLE:
5315 case ISD::SETULE: LowCC = ISD::SETULE; break;
5316 case ISD::SETGE:
5317 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5318 }
5319
5320 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5321 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5322 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5323
5324 // NOTE: on targets without efficient SELECT of bools, we can always use
5325 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
5326 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel502151f2008-03-10 15:42:14 +00005327 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005328 LowCC, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005329 if (!Tmp1.getNode())
Scott Michel502151f2008-03-10 15:42:14 +00005330 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
5331 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005332 CCCode, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005333 if (!Tmp2.getNode())
Scott Michel502151f2008-03-10 15:42:14 +00005334 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005335 RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005336
Gabor Greif1c80d112008-08-28 21:40:38 +00005337 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5338 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman9d24dc72008-03-13 22:13:53 +00005339 if ((Tmp1C && Tmp1C->isNullValue()) ||
5340 (Tmp2C && Tmp2C->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005341 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5342 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman9d24dc72008-03-13 22:13:53 +00005343 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005344 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5345 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5346 // low part is known false, returns high part.
5347 // For LE / GE, if high part is known false, ignore the low part.
5348 // For LT / GT, if high part is known true, ignore the low part.
5349 Tmp1 = Tmp2;
Dan Gohman8181bd12008-07-27 21:46:04 +00005350 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005351 } else {
Scott Michel502151f2008-03-10 15:42:14 +00005352 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005353 ISD::SETEQ, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005354 if (!Result.getNode())
Scott Michel502151f2008-03-10 15:42:14 +00005355 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005356 ISD::SETEQ);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005357 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5358 Result, Tmp1, Tmp2));
5359 Tmp1 = Result;
Dan Gohman8181bd12008-07-27 21:46:04 +00005360 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005361 }
5362 }
5363 }
5364 }
5365 LHS = Tmp1;
5366 RHS = Tmp2;
5367}
5368
Evan Cheng71343822008-10-15 02:05:31 +00005369/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5370/// condition code CC on the current target. This routine assumes LHS and rHS
5371/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5372/// illegal condition code into AND / OR of multiple SETCC values.
5373void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5374 SDValue &LHS, SDValue &RHS,
5375 SDValue &CC) {
5376 MVT OpVT = LHS.getValueType();
5377 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5378 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5379 default: assert(0 && "Unknown condition code action!");
5380 case TargetLowering::Legal:
5381 // Nothing to do.
5382 break;
5383 case TargetLowering::Expand: {
5384 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5385 unsigned Opc = 0;
5386 switch (CCCode) {
5387 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohman2b5b9ca2008-10-21 03:12:54 +00005388 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5389 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5390 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5391 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5392 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5393 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5394 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5395 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5396 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5397 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5398 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5399 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng71343822008-10-15 02:05:31 +00005400 // FIXME: Implement more expansions.
5401 }
5402
5403 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5404 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5405 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5406 RHS = SDValue();
5407 CC = SDValue();
5408 break;
5409 }
5410 }
5411}
5412
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005413/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5414/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5415/// a load from the stack slot to DestVT, extending it if needed.
5416/// The resultant code need not be legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00005417SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5418 MVT SlotVT,
5419 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005420 // Create the stack frame object.
Mon P Wang55854cc2008-07-05 20:40:31 +00005421 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5422 SrcOp.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00005423 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang55854cc2008-07-05 20:40:31 +00005424
Dan Gohman20e37962008-02-11 18:58:42 +00005425 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005426 int SPFI = StackPtrFI->getIndex();
Mon P Wang55854cc2008-07-05 20:40:31 +00005427
Duncan Sands92c43912008-06-06 12:08:01 +00005428 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5429 unsigned SlotSize = SlotVT.getSizeInBits();
5430 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang55854cc2008-07-05 20:40:31 +00005431 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5432 DestVT.getTypeForMVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005433
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005434 // Emit a store to the stack slot. Use a truncstore if the input value is
5435 // later than DestVT.
Dan Gohman8181bd12008-07-27 21:46:04 +00005436 SDValue Store;
Mon P Wang55854cc2008-07-05 20:40:31 +00005437
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005438 if (SrcSize > SlotSize)
Dan Gohman12a9c082008-02-06 22:27:42 +00005439 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005440 PseudoSourceValue::getFixedStack(SPFI), 0,
5441 SlotVT, false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005442 else {
5443 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman12a9c082008-02-06 22:27:42 +00005444 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005445 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang55854cc2008-07-05 20:40:31 +00005446 false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005447 }
5448
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005449 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005450 if (SlotSize == DestSize)
Mon P Wang55854cc2008-07-05 20:40:31 +00005451 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005452
5453 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang55854cc2008-07-05 20:40:31 +00005454 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5455 false, DestAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005456}
5457
Dan Gohman8181bd12008-07-27 21:46:04 +00005458SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005459 // Create a vector sized/aligned stack slot, store the value to element #0,
5460 // then load the whole vector back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00005461 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman12a9c082008-02-06 22:27:42 +00005462
Dan Gohman20e37962008-02-11 18:58:42 +00005463 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005464 int SPFI = StackPtrFI->getIndex();
5465
Dan Gohman8181bd12008-07-27 21:46:04 +00005466 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005467 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00005468 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005469 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005470}
5471
5472
5473/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
5474/// support the operation, but do support the resultant vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00005475SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005476
5477 // If the only non-undef value is the low element, turn this into a
5478 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
5479 unsigned NumElems = Node->getNumOperands();
5480 bool isOnlyLowElement = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005481 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005482
Dan Gohman8181bd12008-07-27 21:46:04 +00005483 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerd8cee732008-03-09 00:29:42 +00005484 // and use a bitmask instead of a list of elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00005485 std::map<SDValue, std::vector<unsigned> > Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005486 Values[SplatValue].push_back(0);
5487 bool isConstant = true;
5488 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5489 SplatValue.getOpcode() != ISD::UNDEF)
5490 isConstant = false;
5491
5492 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005493 SDValue V = Node->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005494 Values[V].push_back(i);
5495 if (V.getOpcode() != ISD::UNDEF)
5496 isOnlyLowElement = false;
5497 if (SplatValue != V)
Dan Gohman8181bd12008-07-27 21:46:04 +00005498 SplatValue = SDValue(0,0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005499
5500 // If this isn't a constant element or an undef, we can't use a constant
5501 // pool load.
5502 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5503 V.getOpcode() != ISD::UNDEF)
5504 isConstant = false;
5505 }
5506
5507 if (isOnlyLowElement) {
5508 // If the low element is an undef too, then this whole things is an undef.
5509 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5510 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5511 // Otherwise, turn this into a scalar_to_vector node.
5512 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5513 Node->getOperand(0));
5514 }
5515
5516 // If all elements are constants, create a load from the constant pool.
5517 if (isConstant) {
Duncan Sands92c43912008-06-06 12:08:01 +00005518 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005519 std::vector<Constant*> CV;
5520 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5521 if (ConstantFPSDNode *V =
5522 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005523 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005524 } else if (ConstantSDNode *V =
Chris Lattner5e0610f2008-04-20 00:41:09 +00005525 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005526 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005527 } else {
5528 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner5e0610f2008-04-20 00:41:09 +00005529 const Type *OpNTy =
Duncan Sands92c43912008-06-06 12:08:01 +00005530 Node->getOperand(0).getValueType().getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005531 CV.push_back(UndefValue::get(OpNTy));
5532 }
5533 }
5534 Constant *CP = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005535 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005536 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman12a9c082008-02-06 22:27:42 +00005537 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005538 PseudoSourceValue::getConstantPool(), 0,
5539 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005540 }
5541
Gabor Greif1c80d112008-08-28 21:40:38 +00005542 if (SplatValue.getNode()) { // Splat of one value?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005543 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands92c43912008-06-06 12:08:01 +00005544 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman8181bd12008-07-27 21:46:04 +00005545 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5546 std::vector<SDValue> ZeroVec(NumElems, Zero);
5547 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005548 &ZeroVec[0], ZeroVec.size());
5549
5550 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
5551 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
5552 // Get the splatted value into the low element of a vector register.
Dan Gohman8181bd12008-07-27 21:46:04 +00005553 SDValue LowValVec =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005554 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5555
5556 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5557 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5558 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5559 SplatMask);
5560 }
5561 }
5562
5563 // If there are only two unique elements, we may be able to turn this into a
5564 // vector shuffle.
5565 if (Values.size() == 2) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005566 // Get the two values in deterministic order.
Dan Gohman8181bd12008-07-27 21:46:04 +00005567 SDValue Val1 = Node->getOperand(1);
5568 SDValue Val2;
5569 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerd8cee732008-03-09 00:29:42 +00005570 if (MI->first != Val1)
5571 Val2 = MI->first;
5572 else
5573 Val2 = (++MI)->first;
5574
5575 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5576 // vector shuffle has the undef vector on the RHS.
5577 if (Val1.getOpcode() == ISD::UNDEF)
5578 std::swap(Val1, Val2);
5579
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005580 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands92c43912008-06-06 12:08:01 +00005581 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5582 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005583 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005584
5585 // Set elements of the shuffle mask for Val1.
5586 std::vector<unsigned> &Val1Elts = Values[Val1];
5587 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5588 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5589
5590 // Set elements of the shuffle mask for Val2.
5591 std::vector<unsigned> &Val2Elts = Values[Val2];
5592 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5593 if (Val2.getOpcode() != ISD::UNDEF)
5594 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5595 else
5596 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5597
Dan Gohman8181bd12008-07-27 21:46:04 +00005598 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005599 &MaskVec[0], MaskVec.size());
5600
Chris Lattnerd8cee732008-03-09 00:29:42 +00005601 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005602 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5603 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005604 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5605 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005606 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005607
5608 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerd8cee732008-03-09 00:29:42 +00005609 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005610 }
5611 }
5612
5613 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5614 // aligned object on the stack, store each element into it, then load
5615 // the result as a vector.
Duncan Sands92c43912008-06-06 12:08:01 +00005616 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005617 // Create the stack frame object.
Dan Gohman8181bd12008-07-27 21:46:04 +00005618 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005619
5620 // Emit a store of each element to the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00005621 SmallVector<SDValue, 8> Stores;
Duncan Sands92c43912008-06-06 12:08:01 +00005622 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005623 // Store (in the right endianness) the elements to memory.
5624 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5625 // Ignore undef elements.
5626 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5627
5628 unsigned Offset = TypeByteSize*i;
5629
Dan Gohman8181bd12008-07-27 21:46:04 +00005630 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005631 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5632
5633 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
5634 NULL, 0));
5635 }
5636
Dan Gohman8181bd12008-07-27 21:46:04 +00005637 SDValue StoreChain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005638 if (!Stores.empty()) // Not all undef elements?
5639 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5640 &Stores[0], Stores.size());
5641 else
5642 StoreChain = DAG.getEntryNode();
5643
5644 // Result is a load from the stack slot.
5645 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
5646}
5647
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005648void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman8181bd12008-07-27 21:46:04 +00005649 SDValue Op, SDValue Amt,
5650 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005651 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00005652 SDValue LHSL, LHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005653 ExpandOp(Op, LHSL, LHSH);
5654
Dan Gohman8181bd12008-07-27 21:46:04 +00005655 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands92c43912008-06-06 12:08:01 +00005656 MVT VT = LHSL.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005657 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5658 Hi = Lo.getValue(1);
5659}
5660
5661
5662/// ExpandShift - Try to find a clever way to expand this shift operation out to
5663/// smaller elements. If we can't find a way that is more efficient than a
5664/// libcall on this target, return false. Otherwise, return true with the
5665/// low-parts expanded into Lo and Hi.
Dan Gohman8181bd12008-07-27 21:46:04 +00005666bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5667 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005668 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5669 "This is not a shift!");
5670
Duncan Sands92c43912008-06-06 12:08:01 +00005671 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman8181bd12008-07-27 21:46:04 +00005672 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands92c43912008-06-06 12:08:01 +00005673 MVT ShTy = ShAmt.getValueType();
5674 unsigned ShBits = ShTy.getSizeInBits();
5675 unsigned VTBits = Op.getValueType().getSizeInBits();
5676 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005677
Chris Lattner8c931452007-10-14 20:35:12 +00005678 // Handle the case when Amt is an immediate.
Gabor Greif1c80d112008-08-28 21:40:38 +00005679 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005680 unsigned Cst = CN->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005681 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005682 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005683 ExpandOp(Op, InL, InH);
5684 switch(Opc) {
5685 case ISD::SHL:
5686 if (Cst > VTBits) {
5687 Lo = DAG.getConstant(0, NVT);
5688 Hi = DAG.getConstant(0, NVT);
5689 } else if (Cst > NVTBits) {
5690 Lo = DAG.getConstant(0, NVT);
5691 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5692 } else if (Cst == NVTBits) {
5693 Lo = DAG.getConstant(0, NVT);
5694 Hi = InL;
5695 } else {
5696 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5697 Hi = DAG.getNode(ISD::OR, NVT,
5698 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5699 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5700 }
5701 return true;
5702 case ISD::SRL:
5703 if (Cst > VTBits) {
5704 Lo = DAG.getConstant(0, NVT);
5705 Hi = DAG.getConstant(0, NVT);
5706 } else if (Cst > NVTBits) {
5707 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5708 Hi = DAG.getConstant(0, NVT);
5709 } else if (Cst == NVTBits) {
5710 Lo = InH;
5711 Hi = DAG.getConstant(0, NVT);
5712 } else {
5713 Lo = DAG.getNode(ISD::OR, NVT,
5714 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5715 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5716 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5717 }
5718 return true;
5719 case ISD::SRA:
5720 if (Cst > VTBits) {
5721 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5722 DAG.getConstant(NVTBits-1, ShTy));
5723 } else if (Cst > NVTBits) {
5724 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5725 DAG.getConstant(Cst-NVTBits, ShTy));
5726 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5727 DAG.getConstant(NVTBits-1, ShTy));
5728 } else if (Cst == NVTBits) {
5729 Lo = InH;
5730 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5731 DAG.getConstant(NVTBits-1, ShTy));
5732 } else {
5733 Lo = DAG.getNode(ISD::OR, NVT,
5734 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5735 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5736 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5737 }
5738 return true;
5739 }
5740 }
5741
5742 // Okay, the shift amount isn't constant. However, if we can tell that it is
5743 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005744 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5745 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005746 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5747
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005748 // If we know that if any of the high bits of the shift amount are one, then
5749 // we can do this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005750 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005751 // Mask out the high bit, which we know is set.
5752 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005753 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005754
5755 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005756 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005757 ExpandOp(Op, InL, InH);
5758 switch(Opc) {
5759 case ISD::SHL:
5760 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5761 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5762 return true;
5763 case ISD::SRL:
5764 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5765 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5766 return true;
5767 case ISD::SRA:
5768 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5769 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5770 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5771 return true;
5772 }
5773 }
5774
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005775 // If we know that the high bits of the shift amount are all zero, then we can
5776 // do this as a couple of simple shifts.
5777 if ((KnownZero & Mask) == Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005778 // Compute 32-amt.
Dan Gohman8181bd12008-07-27 21:46:04 +00005779 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005780 DAG.getConstant(NVTBits, Amt.getValueType()),
5781 Amt);
5782
5783 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005784 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005785 ExpandOp(Op, InL, InH);
5786 switch(Opc) {
5787 case ISD::SHL:
5788 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5789 Hi = DAG.getNode(ISD::OR, NVT,
5790 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5791 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5792 return true;
5793 case ISD::SRL:
5794 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5795 Lo = DAG.getNode(ISD::OR, NVT,
5796 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5797 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5798 return true;
5799 case ISD::SRA:
5800 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5801 Lo = DAG.getNode(ISD::OR, NVT,
5802 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5803 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5804 return true;
5805 }
5806 }
5807
5808 return false;
5809}
5810
5811
5812// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5813// does not fit into a register, return the lo part and set the hi part to the
5814// by-reg argument. If it does fit into a single register, return the result
5815// and leave the Hi part unset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005816SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5817 bool isSigned, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005818 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5819 // The input chain to this libcall is the entry node of the function.
5820 // Legalizing the call will automatically add the previous call to the
5821 // dependence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005822 SDValue InChain = DAG.getEntryNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005823
5824 TargetLowering::ArgListTy Args;
5825 TargetLowering::ArgListEntry Entry;
5826 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00005827 MVT ArgVT = Node->getOperand(i).getValueType();
5828 const Type *ArgTy = ArgVT.getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005829 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5830 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005831 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005832 Args.push_back(Entry);
5833 }
Bill Wendlingfef06052008-09-16 21:48:12 +00005834 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang1448aad2008-10-30 08:01:45 +00005835 TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005836
5837 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands92c43912008-06-06 12:08:01 +00005838 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman8181bd12008-07-27 21:46:04 +00005839 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen67cc9b62008-09-26 19:31:26 +00005840 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5841 CallingConv::C, false, Callee, Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005842
5843 // Legalize the call sequence, starting with the chain. This will advance
5844 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5845 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5846 LegalizeOp(CallInfo.second);
Dan Gohman8181bd12008-07-27 21:46:04 +00005847 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005848 switch (getTypeAction(CallInfo.first.getValueType())) {
5849 default: assert(0 && "Unknown thing");
5850 case Legal:
5851 Result = CallInfo.first;
5852 break;
5853 case Expand:
5854 ExpandOp(CallInfo.first, Result, Hi);
5855 break;
5856 }
5857 return Result;
5858}
5859
Dan Gohman29c3cef2008-08-14 20:04:46 +00005860/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5861///
5862SDValue SelectionDAGLegalize::
5863LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5864 bool isCustom = false;
5865 SDValue Tmp1;
5866 switch (getTypeAction(Op.getValueType())) {
5867 case Legal:
5868 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5869 Op.getValueType())) {
5870 default: assert(0 && "Unknown operation action!");
5871 case TargetLowering::Custom:
5872 isCustom = true;
5873 // FALLTHROUGH
5874 case TargetLowering::Legal:
5875 Tmp1 = LegalizeOp(Op);
Gabor Greif1c80d112008-08-28 21:40:38 +00005876 if (Result.getNode())
Dan Gohman29c3cef2008-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 if (isCustom) {
5882 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005883 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman29c3cef2008-08-14 20:04:46 +00005884 }
5885 break;
5886 case TargetLowering::Expand:
5887 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5888 break;
5889 case TargetLowering::Promote:
5890 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5891 break;
5892 }
5893 break;
5894 case Expand:
5895 Result = ExpandIntToFP(isSigned, DestTy, Op);
5896 break;
5897 case Promote:
5898 Tmp1 = PromoteOp(Op);
5899 if (isSigned) {
5900 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5901 Tmp1, DAG.getValueType(Op.getValueType()));
5902 } else {
5903 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5904 Op.getValueType());
5905 }
Gabor Greif1c80d112008-08-28 21:40:38 +00005906 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005907 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5908 else
5909 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5910 DestTy, Tmp1);
5911 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5912 break;
5913 }
5914 return Result;
5915}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005916
5917/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5918///
Dan Gohman8181bd12008-07-27 21:46:04 +00005919SDValue SelectionDAGLegalize::
5920ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands92c43912008-06-06 12:08:01 +00005921 MVT SourceVT = Source.getValueType();
Dan Gohman8b232ff2008-03-11 01:59:03 +00005922 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005923
Dan Gohman29c3cef2008-08-14 20:04:46 +00005924 // Expand unsupported int-to-fp vector casts by unrolling them.
5925 if (DestTy.isVector()) {
5926 if (!ExpandSource)
5927 return LegalizeOp(UnrollVectorOp(Source));
5928 MVT DestEltTy = DestTy.getVectorElementType();
5929 if (DestTy.getVectorNumElements() == 1) {
5930 SDValue Scalar = ScalarizeVectorOp(Source);
5931 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5932 DestEltTy, Scalar);
5933 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5934 }
5935 SDValue Lo, Hi;
5936 SplitVectorOp(Source, Lo, Hi);
5937 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5938 DestTy.getVectorNumElements() / 2);
5939 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5940 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengd901b662008-10-13 18:46:18 +00005941 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5942 HiResult));
Dan Gohman29c3cef2008-08-14 20:04:46 +00005943 }
5944
Evan Chengf99a7752008-04-01 02:18:22 +00005945 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5946 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmana193dba2008-03-05 02:07:31 +00005947 // The integer value loaded will be incorrectly if the 'sign bit' of the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005948 // incoming integer is set. To handle this, we dynamically test to see if
5949 // it is set, and, if so, add a fudge factor.
Dan Gohman8181bd12008-07-27 21:46:04 +00005950 SDValue Hi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005951 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005952 SDValue Lo;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005953 ExpandOp(Source, Lo, Hi);
5954 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5955 } else {
5956 // The comparison for the sign bit will use the entire operand.
5957 Hi = Source;
5958 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005959
Dale Johannesen96db7962008-11-04 20:52:49 +00005960 // Check to see if the target has a custom way to lower this. If so, use
5961 // it. (Note we've already expanded the operand in this case.)
Dale Johannesena359b8b2008-10-21 20:50:01 +00005962 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5963 default: assert(0 && "This action not implemented for this operation!");
5964 case TargetLowering::Legal:
5965 case TargetLowering::Expand:
5966 break; // This case is handled below.
5967 case TargetLowering::Custom: {
5968 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5969 Source), DAG);
5970 if (NV.getNode())
5971 return LegalizeOp(NV);
5972 break; // The target decided this was legal after all
5973 }
5974 }
5975
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005976 // If this is unsigned, and not supported, first perform the conversion to
5977 // signed, then adjust the result if the sign bit is set.
Dan Gohman8181bd12008-07-27 21:46:04 +00005978 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005979
Dan Gohman8181bd12008-07-27 21:46:04 +00005980 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005981 DAG.getConstant(0, Hi.getValueType()),
5982 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005983 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5984 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005985 SignSet, Four, Zero);
5986 uint64_t FF = 0x5f800000ULL;
5987 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmana193dba2008-03-05 02:07:31 +00005988 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005989
Dan Gohman8181bd12008-07-27 21:46:04 +00005990 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005991 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005992 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00005993 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00005994 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005995 if (DestTy == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005996 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005997 PseudoSourceValue::getConstantPool(), 0,
5998 false, Alignment);
Duncan Sandsec142ee2008-06-08 20:54:56 +00005999 else if (DestTy.bitsGT(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006000 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00006001 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006002 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006003 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00006004 MVT::f32, false, Alignment);
Dale Johannesen2fc20782007-09-14 22:26:36 +00006005 else
6006 assert(0 && "Unexpected conversion");
6007
Duncan Sands92c43912008-06-06 12:08:01 +00006008 MVT SCVT = SignedConv.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006009 if (SCVT != DestTy) {
6010 // Destination type needs to be expanded as well. The FADD now we are
6011 // constructing will be expanded into a libcall.
Duncan Sands92c43912008-06-06 12:08:01 +00006012 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
6013 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmanc98645c2008-03-05 01:08:17 +00006014 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006015 SignedConv, SignedConv.getValue(1));
6016 }
6017 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
6018 }
6019 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
6020 }
6021
6022 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmanc98645c2008-03-05 01:08:17 +00006023 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006024 default: assert(0 && "This action not implemented for this operation!");
6025 case TargetLowering::Legal:
6026 case TargetLowering::Expand:
6027 break; // This case is handled below.
6028 case TargetLowering::Custom: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006029 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006030 Source), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006031 if (NV.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006032 return LegalizeOp(NV);
6033 break; // The target decided this was legal after all
6034 }
6035 }
6036
6037 // Expand the source, then glue it back together for the call. We must expand
6038 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman8b232ff2008-03-11 01:59:03 +00006039 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006040 SDValue SrcLo, SrcHi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00006041 ExpandOp(Source, SrcLo, SrcHi);
6042 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
6043 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006044
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006045 RTLIB::Libcall LC = isSigned ?
6046 RTLIB::getSINTTOFP(SourceVT, DestTy) :
6047 RTLIB::getUINTTOFP(SourceVT, DestTy);
6048 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
6049
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006050 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman8181bd12008-07-27 21:46:04 +00006051 SDValue HiPart;
Gabor Greif1c80d112008-08-28 21:40:38 +00006052 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
6053 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmanec51f642008-03-10 23:03:31 +00006054 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
6055 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006056}
6057
6058/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
6059/// INT_TO_FP operation of the specified operand when the target requests that
6060/// we expand it. At this point, we know that the result and operand types are
6061/// legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00006062SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
6063 SDValue Op0,
6064 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006065 if (Op0.getValueType() == MVT::i32) {
6066 // simple 32-bit [signed|unsigned] integer to float/double expansion
6067
Chris Lattner0aeb1d02008-01-16 07:03:22 +00006068 // Get the stack frame index of a 8 byte buffer.
Dan Gohman8181bd12008-07-27 21:46:04 +00006069 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner0aeb1d02008-01-16 07:03:22 +00006070
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006071 // word offset constant for Hi/Lo address computation
Dan Gohman8181bd12008-07-27 21:46:04 +00006072 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006073 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman8181bd12008-07-27 21:46:04 +00006074 SDValue Hi = StackSlot;
6075 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006076 if (TLI.isLittleEndian())
6077 std::swap(Hi, Lo);
6078
6079 // if signed map to unsigned space
Dan Gohman8181bd12008-07-27 21:46:04 +00006080 SDValue Op0Mapped;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006081 if (isSigned) {
6082 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman8181bd12008-07-27 21:46:04 +00006083 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006084 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
6085 } else {
6086 Op0Mapped = Op0;
6087 }
6088 // store the lo of the constructed double - based on integer input
Dan Gohman8181bd12008-07-27 21:46:04 +00006089 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006090 Op0Mapped, Lo, NULL, 0);
6091 // initial hi portion of constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00006092 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006093 // store the hi of the constructed double - biased exponent
Dan Gohman8181bd12008-07-27 21:46:04 +00006094 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006095 // load the constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00006096 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006097 // FP constant to bias correct the final result
Dan Gohman8181bd12008-07-27 21:46:04 +00006098 SDValue Bias = DAG.getConstantFP(isSigned ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006099 BitsToDouble(0x4330000080000000ULL)
6100 : BitsToDouble(0x4330000000000000ULL),
6101 MVT::f64);
6102 // subtract the bias
Dan Gohman8181bd12008-07-27 21:46:04 +00006103 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006104 // final result
Dan Gohman8181bd12008-07-27 21:46:04 +00006105 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006106 // handle final rounding
6107 if (DestVT == MVT::f64) {
6108 // do nothing
6109 Result = Sub;
Duncan Sandsec142ee2008-06-08 20:54:56 +00006110 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner5872a362008-01-17 07:00:52 +00006111 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
6112 DAG.getIntPtrConstant(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00006113 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenb17a7a22007-09-16 16:51:49 +00006114 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006115 }
6116 return Result;
6117 }
6118 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman8181bd12008-07-27 21:46:04 +00006119 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006120
Dan Gohman8181bd12008-07-27 21:46:04 +00006121 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006122 DAG.getConstant(0, Op0.getValueType()),
6123 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00006124 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
6125 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006126 SignSet, Four, Zero);
6127
6128 // If the sign bit of the integer is set, the large number will be treated
6129 // as a negative number. To counteract this, the dynamic code adds an
6130 // offset depending on the data type.
6131 uint64_t FF;
Duncan Sands92c43912008-06-06 12:08:01 +00006132 switch (Op0.getValueType().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006133 default: assert(0 && "Unsupported integer type!");
6134 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6135 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6136 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6137 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6138 }
6139 if (TLI.isLittleEndian()) FF <<= 32;
6140 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
6141
Dan Gohman8181bd12008-07-27 21:46:04 +00006142 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00006143 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006144 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00006145 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00006146 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006147 if (DestVT == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00006148 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00006149 PseudoSourceValue::getConstantPool(), 0,
6150 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006151 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00006152 FudgeInReg =
6153 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
6154 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006155 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00006156 MVT::f32, false, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006157 }
6158
6159 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
6160}
6161
6162/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6163/// *INT_TO_FP operation of the specified operand when the target requests that
6164/// we promote it. At this point, we know that the result and operand types are
6165/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6166/// operation that takes a larger input.
Dan Gohman8181bd12008-07-27 21:46:04 +00006167SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6168 MVT DestVT,
6169 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006170 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00006171 MVT NewInTy = LegalOp.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006172
6173 unsigned OpToUse = 0;
6174
6175 // Scan for the appropriate larger type to use.
6176 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00006177 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6178 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006179
6180 // If the target supports SINT_TO_FP of this type, use it.
6181 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6182 default: break;
6183 case TargetLowering::Legal:
6184 if (!TLI.isTypeLegal(NewInTy))
6185 break; // Can't use this datatype.
6186 // FALL THROUGH.
6187 case TargetLowering::Custom:
6188 OpToUse = ISD::SINT_TO_FP;
6189 break;
6190 }
6191 if (OpToUse) break;
6192 if (isSigned) continue;
6193
6194 // If the target supports UINT_TO_FP of this type, use it.
6195 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6196 default: break;
6197 case TargetLowering::Legal:
6198 if (!TLI.isTypeLegal(NewInTy))
6199 break; // Can't use this datatype.
6200 // FALL THROUGH.
6201 case TargetLowering::Custom:
6202 OpToUse = ISD::UINT_TO_FP;
6203 break;
6204 }
6205 if (OpToUse) break;
6206
6207 // Otherwise, try a larger type.
6208 }
6209
6210 // Okay, we found the operation and type to use. Zero extend our input to the
6211 // desired type then run the operation on it.
6212 return DAG.getNode(OpToUse, DestVT,
6213 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
6214 NewInTy, LegalOp));
6215}
6216
6217/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6218/// FP_TO_*INT operation of the specified operand when the target requests that
6219/// we promote it. At this point, we know that the result and operand types are
6220/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6221/// operation that returns a larger result.
Dan Gohman8181bd12008-07-27 21:46:04 +00006222SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6223 MVT DestVT,
6224 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006225 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00006226 MVT NewOutTy = DestVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006227
6228 unsigned OpToUse = 0;
6229
6230 // Scan for the appropriate larger type to use.
6231 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00006232 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6233 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006234
6235 // If the target supports FP_TO_SINT returning this type, use it.
6236 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6237 default: break;
6238 case TargetLowering::Legal:
6239 if (!TLI.isTypeLegal(NewOutTy))
6240 break; // Can't use this datatype.
6241 // FALL THROUGH.
6242 case TargetLowering::Custom:
6243 OpToUse = ISD::FP_TO_SINT;
6244 break;
6245 }
6246 if (OpToUse) break;
6247
6248 // If the target supports FP_TO_UINT of this type, use it.
6249 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6250 default: break;
6251 case TargetLowering::Legal:
6252 if (!TLI.isTypeLegal(NewOutTy))
6253 break; // Can't use this datatype.
6254 // FALL THROUGH.
6255 case TargetLowering::Custom:
6256 OpToUse = ISD::FP_TO_UINT;
6257 break;
6258 }
6259 if (OpToUse) break;
6260
6261 // Otherwise, try a larger type.
6262 }
6263
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006264
6265 // Okay, we found the operation and type to use.
Dan Gohman8181bd12008-07-27 21:46:04 +00006266 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sandsac496a12008-07-04 11:47:58 +00006267
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006268 // If the operation produces an invalid type, it must be custom lowered. Use
6269 // the target lowering hooks to expand it. Just keep the low part of the
6270 // expanded operation, we know that we're truncating anyway.
6271 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006272 SmallVector<SDValue, 2> Results;
6273 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6274 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6275 Operation = Results[0];
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006276 }
Duncan Sandsac496a12008-07-04 11:47:58 +00006277
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006278 // Truncate the result of the extended FP_TO_*INT operation to the desired
6279 // size.
6280 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006281}
6282
6283/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6284///
Dan Gohman8181bd12008-07-27 21:46:04 +00006285SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00006286 MVT VT = Op.getValueType();
6287 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00006288 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands92c43912008-06-06 12:08:01 +00006289 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006290 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6291 case MVT::i16:
6292 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6293 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6294 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6295 case MVT::i32:
6296 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6297 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6298 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6299 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6300 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6301 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6302 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6303 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6304 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6305 case MVT::i64:
6306 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6307 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6308 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6309 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6310 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6311 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6312 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6313 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6314 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6315 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6316 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6317 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6318 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6319 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6320 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6321 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6322 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6323 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6324 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6325 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6326 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6327 }
6328}
6329
6330/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6331///
Dan Gohman8181bd12008-07-27 21:46:04 +00006332SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006333 switch (Opc) {
6334 default: assert(0 && "Cannot expand this yet!");
6335 case ISD::CTPOP: {
6336 static const uint64_t mask[6] = {
6337 0x5555555555555555ULL, 0x3333333333333333ULL,
6338 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6339 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6340 };
Duncan Sands92c43912008-06-06 12:08:01 +00006341 MVT VT = Op.getValueType();
6342 MVT ShVT = TLI.getShiftAmountTy();
6343 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006344 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6345 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman8181bd12008-07-27 21:46:04 +00006346 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6347 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006348 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6349 DAG.getNode(ISD::AND, VT,
6350 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6351 }
6352 return Op;
6353 }
6354 case ISD::CTLZ: {
6355 // for now, we do this:
6356 // x = x | (x >> 1);
6357 // x = x | (x >> 2);
6358 // ...
6359 // x = x | (x >>16);
6360 // x = x | (x >>32); // for 64-bit input
6361 // return popcount(~x);
6362 //
6363 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006364 MVT VT = Op.getValueType();
6365 MVT ShVT = TLI.getShiftAmountTy();
6366 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006367 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006368 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006369 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6370 }
6371 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6372 return DAG.getNode(ISD::CTPOP, VT, Op);
6373 }
6374 case ISD::CTTZ: {
6375 // for now, we use: { return popcount(~x & (x - 1)); }
6376 // unless the target has ctlz but not ctpop, in which case we use:
6377 // { return 32 - nlz(~x & (x-1)); }
6378 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006379 MVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006380 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6381 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006382 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6383 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6384 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6385 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6386 TLI.isOperationLegal(ISD::CTLZ, VT))
6387 return DAG.getNode(ISD::SUB, VT,
Duncan Sands92c43912008-06-06 12:08:01 +00006388 DAG.getConstant(VT.getSizeInBits(), VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006389 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6390 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6391 }
6392 }
6393}
6394
Dan Gohman8181bd12008-07-27 21:46:04 +00006395/// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006396/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman4fc03742008-10-01 15:07:49 +00006397/// LegalizedNodes map is filled in for any results that are not expanded, the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006398/// ExpandedNodes map is filled in for any results that are expanded, and the
6399/// Lo/Hi values are returned.
Dan Gohman8181bd12008-07-27 21:46:04 +00006400void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands92c43912008-06-06 12:08:01 +00006401 MVT VT = Op.getValueType();
6402 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greif1c80d112008-08-28 21:40:38 +00006403 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006404 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00006405 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands92c43912008-06-06 12:08:01 +00006406 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006407
6408 // See if we already expanded it.
Dan Gohman8181bd12008-07-27 21:46:04 +00006409 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006410 = ExpandedNodes.find(Op);
6411 if (I != ExpandedNodes.end()) {
6412 Lo = I->second.first;
6413 Hi = I->second.second;
6414 return;
6415 }
6416
6417 switch (Node->getOpcode()) {
6418 case ISD::CopyFromReg:
6419 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006420 case ISD::FP_ROUND_INREG:
6421 if (VT == MVT::ppcf128 &&
6422 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6423 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006424 SDValue SrcLo, SrcHi, Src;
Dale Johannesend3b6af32007-10-11 23:32:15 +00006425 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6426 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006427 SDValue Result = TLI.LowerOperation(
Dale Johannesend3b6af32007-10-11 23:32:15 +00006428 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006429 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6430 Lo = Result.getNode()->getOperand(0);
6431 Hi = Result.getNode()->getOperand(1);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006432 break;
6433 }
6434 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006435 default:
6436#ifndef NDEBUG
6437 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
6438#endif
6439 assert(0 && "Do not know how to expand this operator!");
6440 abort();
Dan Gohman550c8462008-02-27 01:52:30 +00006441 case ISD::EXTRACT_ELEMENT:
6442 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006443 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman550c8462008-02-27 01:52:30 +00006444 return ExpandOp(Hi, Lo, Hi);
Dan Gohman7e7aa2c2008-02-27 19:44:57 +00006445 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006446 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006447 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6448 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6449 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006450 case ISD::UNDEF:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006451 Lo = DAG.getNode(ISD::UNDEF, NVT);
6452 Hi = DAG.getNode(ISD::UNDEF, NVT);
6453 break;
6454 case ISD::Constant: {
Duncan Sands92c43912008-06-06 12:08:01 +00006455 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman97f1f8e2008-03-03 22:20:46 +00006456 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6457 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6458 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006459 break;
6460 }
6461 case ISD::ConstantFP: {
6462 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00006463 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00006464 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen2aef5692007-10-11 18:07:22 +00006465 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6466 MVT::f64);
6467 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6468 MVT::f64);
6469 break;
6470 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006471 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
6472 if (getTypeAction(Lo.getValueType()) == Expand)
6473 ExpandOp(Lo, Lo, Hi);
6474 break;
6475 }
6476 case ISD::BUILD_PAIR:
6477 // Return the operands.
6478 Lo = Node->getOperand(0);
6479 Hi = Node->getOperand(1);
6480 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006481
6482 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00006483 if (Node->getNumValues() == 1) {
6484 ExpandOp(Op.getOperand(0), Lo, Hi);
6485 break;
6486 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006487 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif46bf5472008-08-26 22:36:50 +00006488 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006489 Op.getValue(1).getValueType() == MVT::Other &&
6490 "unhandled MERGE_VALUES");
6491 ExpandOp(Op.getOperand(0), Lo, Hi);
6492 // Remember that we legalized the chain.
6493 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6494 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006495
6496 case ISD::SIGN_EXTEND_INREG:
6497 ExpandOp(Node->getOperand(0), Lo, Hi);
6498 // sext_inreg the low part if needed.
6499 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6500
6501 // The high part gets the sign extension from the lo-part. This handles
6502 // things like sextinreg V:i64 from i8.
6503 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands92c43912008-06-06 12:08:01 +00006504 DAG.getConstant(NVT.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006505 TLI.getShiftAmountTy()));
6506 break;
6507
6508 case ISD::BSWAP: {
6509 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006510 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006511 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6512 Lo = TempLo;
6513 break;
6514 }
6515
6516 case ISD::CTPOP:
6517 ExpandOp(Node->getOperand(0), Lo, Hi);
6518 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6519 DAG.getNode(ISD::CTPOP, NVT, Lo),
6520 DAG.getNode(ISD::CTPOP, NVT, Hi));
6521 Hi = DAG.getConstant(0, NVT);
6522 break;
6523
6524 case ISD::CTLZ: {
6525 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
6526 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006527 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6528 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
6529 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006530 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006531 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006532 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6533
6534 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6535 Hi = DAG.getConstant(0, NVT);
6536 break;
6537 }
6538
6539 case ISD::CTTZ: {
6540 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
6541 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006542 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6543 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
6544 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006545 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006546 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006547 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6548
6549 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6550 Hi = DAG.getConstant(0, NVT);
6551 break;
6552 }
6553
6554 case ISD::VAARG: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006555 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6556 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006557 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6558 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6559
6560 // Remember that we legalized the chain.
6561 Hi = LegalizeOp(Hi);
6562 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006563 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006564 std::swap(Lo, Hi);
6565 break;
6566 }
6567
6568 case ISD::LOAD: {
6569 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00006570 SDValue Ch = LD->getChain(); // Legalize the chain.
6571 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006572 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman29c3cef2008-08-14 20:04:46 +00006573 const Value *SV = LD->getSrcValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006574 int SVOffset = LD->getSrcValueOffset();
6575 unsigned Alignment = LD->getAlignment();
6576 bool isVolatile = LD->isVolatile();
6577
6578 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman29c3cef2008-08-14 20:04:46 +00006579 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006580 isVolatile, Alignment);
6581 if (VT == MVT::f32 || VT == MVT::f64) {
6582 // f32->i32 or f64->i64 one to one expansion.
6583 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006584 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006585 // Recursively expand the new load.
6586 if (getTypeAction(NVT) == Expand)
6587 ExpandOp(Lo, Lo, Hi);
6588 break;
6589 }
6590
6591 // Increment the pointer to the other half.
Duncan Sands92c43912008-06-06 12:08:01 +00006592 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006593 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006594 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006595 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006596 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00006597 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006598 isVolatile, Alignment);
6599
6600 // Build a factor node to remember that this load is independent of the
6601 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00006602 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006603 Hi.getValue(1));
6604
6605 // Remember that we legalized the chain.
6606 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006607 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006608 std::swap(Lo, Hi);
6609 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00006610 MVT EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006611
Dale Johannesen2550e3a2007-10-19 20:29:00 +00006612 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6613 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006614 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman29c3cef2008-08-14 20:04:46 +00006615 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006616 SVOffset, isVolatile, Alignment);
6617 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006618 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006619 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6620 break;
6621 }
6622
6623 if (EVT == NVT)
Dan Gohman29c3cef2008-08-14 20:04:46 +00006624 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006625 SVOffset, isVolatile, Alignment);
6626 else
Dan Gohman29c3cef2008-08-14 20:04:46 +00006627 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006628 SVOffset, EVT, isVolatile,
6629 Alignment);
6630
6631 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006632 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006633
6634 if (ExtType == ISD::SEXTLOAD) {
6635 // The high part is obtained by SRA'ing all but one of the bits of the
6636 // lo part.
Duncan Sands92c43912008-06-06 12:08:01 +00006637 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006638 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6639 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6640 } else if (ExtType == ISD::ZEXTLOAD) {
6641 // The high part is just a zero.
6642 Hi = DAG.getConstant(0, NVT);
6643 } else /* if (ExtType == ISD::EXTLOAD) */ {
6644 // The high part is undefined.
6645 Hi = DAG.getNode(ISD::UNDEF, NVT);
6646 }
6647 }
6648 break;
6649 }
6650 case ISD::AND:
6651 case ISD::OR:
6652 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +00006653 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006654 ExpandOp(Node->getOperand(0), LL, LH);
6655 ExpandOp(Node->getOperand(1), RL, RH);
6656 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6657 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6658 break;
6659 }
6660 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006661 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006662 ExpandOp(Node->getOperand(1), LL, LH);
6663 ExpandOp(Node->getOperand(2), RL, RH);
6664 if (getTypeAction(NVT) == Expand)
6665 NVT = TLI.getTypeToExpandTo(NVT);
6666 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
6667 if (VT != MVT::f32)
6668 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
6669 break;
6670 }
6671 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006672 SDValue TL, TH, FL, FH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006673 ExpandOp(Node->getOperand(2), TL, TH);
6674 ExpandOp(Node->getOperand(3), FL, FH);
6675 if (getTypeAction(NVT) == Expand)
6676 NVT = TLI.getTypeToExpandTo(NVT);
6677 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6678 Node->getOperand(1), TL, FL, Node->getOperand(4));
6679 if (VT != MVT::f32)
6680 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6681 Node->getOperand(1), TH, FH, Node->getOperand(4));
6682 break;
6683 }
6684 case ISD::ANY_EXTEND:
6685 // The low part is any extension of the input (which degenerates to a copy).
6686 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6687 // The high part is undefined.
6688 Hi = DAG.getNode(ISD::UNDEF, NVT);
6689 break;
6690 case ISD::SIGN_EXTEND: {
6691 // The low part is just a sign extension of the input (which degenerates to
6692 // a copy).
6693 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
6694
6695 // The high part is obtained by SRA'ing all but one of the bits of the lo
6696 // part.
Duncan Sands92c43912008-06-06 12:08:01 +00006697 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006698 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6699 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6700 break;
6701 }
6702 case ISD::ZERO_EXTEND:
6703 // The low part is just a zero extension of the input (which degenerates to
6704 // a copy).
6705 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
6706
6707 // The high part is just a zero.
6708 Hi = DAG.getConstant(0, NVT);
6709 break;
6710
6711 case ISD::TRUNCATE: {
6712 // The input value must be larger than this value. Expand *it*.
Dan Gohman8181bd12008-07-27 21:46:04 +00006713 SDValue NewLo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006714 ExpandOp(Node->getOperand(0), NewLo, Hi);
6715
6716 // The low part is now either the right size, or it is closer. If not the
6717 // right size, make an illegal truncate so we recursively expand it.
6718 if (NewLo.getValueType() != Node->getValueType(0))
6719 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6720 ExpandOp(NewLo, Lo, Hi);
6721 break;
6722 }
6723
6724 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006725 SDValue Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006726 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6727 // If the target wants to, allow it to lower this itself.
6728 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6729 case Expand: assert(0 && "cannot expand FP!");
6730 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6731 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6732 }
6733 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6734 }
6735
6736 // f32 / f64 must be expanded to i32 / i64.
6737 if (VT == MVT::f32 || VT == MVT::f64) {
6738 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6739 if (getTypeAction(NVT) == Expand)
6740 ExpandOp(Lo, Lo, Hi);
6741 break;
6742 }
6743
6744 // If source operand will be expanded to the same type as VT, i.e.
6745 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands92c43912008-06-06 12:08:01 +00006746 MVT VT0 = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006747 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6748 ExpandOp(Node->getOperand(0), Lo, Hi);
6749 break;
6750 }
6751
6752 // Turn this into a load/store pair by default.
Gabor Greif1c80d112008-08-28 21:40:38 +00006753 if (Tmp.getNode() == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00006754 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006755
6756 ExpandOp(Tmp, Lo, Hi);
6757 break;
6758 }
6759
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006760 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006761 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6762 TargetLowering::Custom &&
6763 "Must custom expand ReadCycleCounter");
Dan Gohman8181bd12008-07-27 21:46:04 +00006764 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006765 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006766 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006767 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006768 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006769 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006770 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006771
Dale Johannesen44eb5372008-10-03 19:41:08 +00006772 case ISD::ATOMIC_CMP_SWAP_64: {
6773 // This operation does not need a loop.
6774 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6775 assert(Tmp.getNode() && "Node must be custom expanded!");
6776 ExpandOp(Tmp.getValue(0), Lo, Hi);
6777 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6778 LegalizeOp(Tmp.getValue(1)));
6779 break;
6780 }
6781
Dale Johannesenf160d802008-10-02 18:53:47 +00006782 case ISD::ATOMIC_LOAD_ADD_64:
6783 case ISD::ATOMIC_LOAD_SUB_64:
6784 case ISD::ATOMIC_LOAD_AND_64:
6785 case ISD::ATOMIC_LOAD_OR_64:
6786 case ISD::ATOMIC_LOAD_XOR_64:
6787 case ISD::ATOMIC_LOAD_NAND_64:
Dale Johannesen44eb5372008-10-03 19:41:08 +00006788 case ISD::ATOMIC_SWAP_64: {
6789 // These operations require a loop to be generated. We can't do that yet,
6790 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesenf160d802008-10-02 18:53:47 +00006791 SDValue In2Lo, In2Hi, In2;
6792 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6793 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006794 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6795 SDValue Replace =
6796 DAG.getAtomic(Op.getOpcode(), Op.getOperand(0), Op.getOperand(1), In2,
6797 Anode->getSrcValue(), Anode->getAlignment());
6798 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006799 ExpandOp(Result.getValue(0), Lo, Hi);
6800 // Remember that we legalized the chain.
6801 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharth81580822008-03-05 01:15:49 +00006802 break;
6803 }
6804
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006805 // These operators cannot be expanded directly, emit them as calls to
6806 // library functions.
6807 case ISD::FP_TO_SINT: {
6808 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006809 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006810 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6811 case Expand: assert(0 && "cannot expand FP!");
6812 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6813 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6814 }
6815
6816 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6817
6818 // Now that the custom expander is done, expand the result, which is still
6819 // VT.
Gabor Greif1c80d112008-08-28 21:40:38 +00006820 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006821 ExpandOp(Op, Lo, Hi);
6822 break;
6823 }
6824 }
6825
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006826 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6827 VT);
6828 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6829 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006830 break;
6831 }
6832
6833 case ISD::FP_TO_UINT: {
6834 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006835 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006836 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6837 case Expand: assert(0 && "cannot expand FP!");
6838 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6839 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6840 }
6841
6842 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6843
6844 // Now that the custom expander is done, expand the result.
Gabor Greif1c80d112008-08-28 21:40:38 +00006845 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006846 ExpandOp(Op, Lo, Hi);
6847 break;
6848 }
6849 }
6850
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006851 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6852 VT);
6853 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6854 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006855 break;
6856 }
6857
6858 case ISD::SHL: {
6859 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006860 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006861 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006862 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006863 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006864 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006865 // Now that the custom expander is done, expand the result, which is
6866 // still VT.
6867 ExpandOp(Op, Lo, Hi);
6868 break;
6869 }
6870 }
6871
6872 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6873 // this X << 1 as X+X.
6874 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00006875 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006876 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006877 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006878 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6879 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6880 LoOps[1] = LoOps[0];
6881 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6882
6883 HiOps[1] = HiOps[0];
6884 HiOps[2] = Lo.getValue(1);
6885 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6886 break;
6887 }
6888 }
6889
6890 // If we can emit an efficient shift operation, do so now.
6891 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6892 break;
6893
6894 // If this target supports SHL_PARTS, use it.
6895 TargetLowering::LegalizeAction Action =
6896 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6897 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6898 Action == TargetLowering::Custom) {
6899 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6900 break;
6901 }
6902
6903 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006904 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006905 break;
6906 }
6907
6908 case ISD::SRA: {
6909 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006910 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006911 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006912 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006913 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006914 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006915 // Now that the custom expander is done, expand the result, which is
6916 // still VT.
6917 ExpandOp(Op, Lo, Hi);
6918 break;
6919 }
6920 }
6921
6922 // If we can emit an efficient shift operation, do so now.
6923 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6924 break;
6925
6926 // If this target supports SRA_PARTS, use it.
6927 TargetLowering::LegalizeAction Action =
6928 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6929 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6930 Action == TargetLowering::Custom) {
6931 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6932 break;
6933 }
6934
6935 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006936 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006937 break;
6938 }
6939
6940 case ISD::SRL: {
6941 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006942 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006943 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006944 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006945 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006946 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006947 // Now that the custom expander is done, expand the result, which is
6948 // still VT.
6949 ExpandOp(Op, Lo, Hi);
6950 break;
6951 }
6952 }
6953
6954 // If we can emit an efficient shift operation, do so now.
6955 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6956 break;
6957
6958 // If this target supports SRL_PARTS, use it.
6959 TargetLowering::LegalizeAction Action =
6960 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6961 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6962 Action == TargetLowering::Custom) {
6963 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6964 break;
6965 }
6966
6967 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006968 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006969 break;
6970 }
6971
6972 case ISD::ADD:
6973 case ISD::SUB: {
6974 // If the target wants to custom expand this, let them.
6975 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6976 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006977 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006978 if (Result.getNode()) {
Duncan Sands4c3885b2008-06-22 09:42:16 +00006979 ExpandOp(Result, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006980 break;
6981 }
6982 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006983 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006984 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006985 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6986 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman8181bd12008-07-27 21:46:04 +00006987 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006988 LoOps[0] = LHSL;
6989 LoOps[1] = RHSL;
6990 HiOps[0] = LHSH;
6991 HiOps[1] = RHSH;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006992
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006993 //cascaded check to see if any smaller size has a a carry flag.
6994 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6995 bool hasCarry = false;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006996 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6997 MVT AVT = MVT::getIntegerVT(BitSize);
6998 if (TLI.isOperationLegal(OpV, AVT)) {
6999 hasCarry = true;
7000 break;
7001 }
7002 }
7003
Andrew Lenhartha23d6992008-10-07 17:03:15 +00007004 if(hasCarry) {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00007005 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth5e814462008-10-07 14:15:42 +00007006 if (Node->getOpcode() == ISD::ADD) {
7007 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
7008 HiOps[2] = Lo.getValue(1);
7009 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
7010 } else {
7011 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
7012 HiOps[2] = Lo.getValue(1);
7013 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
7014 }
7015 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007016 } else {
Andrew Lenharth5e814462008-10-07 14:15:42 +00007017 if (Node->getOpcode() == ISD::ADD) {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00007018 Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
7019 Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
Andrew Lenhartha23d6992008-10-07 17:03:15 +00007020 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
7021 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00007022 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
7023 DAG.getConstant(1, NVT),
7024 DAG.getConstant(0, NVT));
Andrew Lenhartha23d6992008-10-07 17:03:15 +00007025 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
7026 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00007027 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
7028 DAG.getConstant(1, NVT),
7029 Carry1);
7030 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
7031 } else {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00007032 Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
7033 Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
Andrew Lenharth5e814462008-10-07 14:15:42 +00007034 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
7035 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
7036 DAG.getConstant(1, NVT),
7037 DAG.getConstant(0, NVT));
7038 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
7039 }
7040 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007041 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007042 }
7043
7044 case ISD::ADDC:
7045 case ISD::SUBC: {
7046 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00007047 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007048 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7049 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7050 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00007051 SDValue LoOps[2] = { LHSL, RHSL };
7052 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007053
7054 if (Node->getOpcode() == ISD::ADDC) {
7055 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
7056 HiOps[2] = Lo.getValue(1);
7057 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
7058 } else {
7059 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
7060 HiOps[2] = Lo.getValue(1);
7061 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
7062 }
7063 // Remember that we legalized the flag.
7064 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7065 break;
7066 }
7067 case ISD::ADDE:
7068 case ISD::SUBE: {
7069 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00007070 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007071 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7072 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7073 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00007074 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
7075 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007076
7077 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
7078 HiOps[2] = Lo.getValue(1);
7079 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
7080
7081 // Remember that we legalized the flag.
7082 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7083 break;
7084 }
7085 case ISD::MUL: {
7086 // If the target wants to custom expand this, let them.
7087 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007088 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00007089 if (New.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007090 ExpandOp(New, Lo, Hi);
7091 break;
7092 }
7093 }
7094
7095 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
7096 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00007097 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
7098 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
7099 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007100 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007101 ExpandOp(Node->getOperand(0), LL, LH);
7102 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman07961cd2008-02-25 21:11:39 +00007103 unsigned OuterBitSize = Op.getValueSizeInBits();
7104 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman5a199552007-10-08 18:33:35 +00007105 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
7106 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman2594d942008-03-10 20:42:19 +00007107 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
7108 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
7109 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman5a199552007-10-08 18:33:35 +00007110 // The inputs are both zero-extended.
7111 if (HasUMUL_LOHI) {
7112 // We can emit a umul_lohi.
7113 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00007114 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00007115 break;
7116 }
7117 if (HasMULHU) {
7118 // We can emit a mulhu+mul.
7119 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7120 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7121 break;
7122 }
Dan Gohman5a199552007-10-08 18:33:35 +00007123 }
Dan Gohman07961cd2008-02-25 21:11:39 +00007124 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman5a199552007-10-08 18:33:35 +00007125 // The input values are both sign-extended.
7126 if (HasSMUL_LOHI) {
7127 // We can emit a smul_lohi.
7128 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00007129 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00007130 break;
7131 }
7132 if (HasMULHS) {
7133 // We can emit a mulhs+mul.
7134 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7135 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
7136 break;
7137 }
7138 }
7139 if (HasUMUL_LOHI) {
7140 // Lo,Hi = umul LHS, RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00007141 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman5a199552007-10-08 18:33:35 +00007142 DAG.getVTList(NVT, NVT), LL, RL);
7143 Lo = UMulLOHI;
7144 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007145 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7146 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7147 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7148 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7149 break;
7150 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00007151 if (HasMULHU) {
7152 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7153 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7154 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7155 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7156 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7157 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7158 break;
7159 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007160 }
7161
Dan Gohman5a199552007-10-08 18:33:35 +00007162 // If nothing else, we can make a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007163 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007164 break;
7165 }
7166 case ISD::SDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007167 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007168 break;
7169 case ISD::UDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007170 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007171 break;
7172 case ISD::SREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007173 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007174 break;
7175 case ISD::UREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007176 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007177 break;
7178
7179 case ISD::FADD:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007180 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7181 RTLIB::ADD_F64,
7182 RTLIB::ADD_F80,
7183 RTLIB::ADD_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007184 Node, false, Hi);
7185 break;
7186 case ISD::FSUB:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007187 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7188 RTLIB::SUB_F64,
7189 RTLIB::SUB_F80,
7190 RTLIB::SUB_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007191 Node, false, Hi);
7192 break;
7193 case ISD::FMUL:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007194 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7195 RTLIB::MUL_F64,
7196 RTLIB::MUL_F80,
7197 RTLIB::MUL_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007198 Node, false, Hi);
7199 break;
7200 case ISD::FDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007201 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7202 RTLIB::DIV_F64,
7203 RTLIB::DIV_F80,
7204 RTLIB::DIV_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007205 Node, false, Hi);
7206 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007207 case ISD::FP_EXTEND: {
Dale Johannesen4c14d512007-10-12 01:37:08 +00007208 if (VT == MVT::ppcf128) {
7209 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7210 Node->getOperand(0).getValueType()==MVT::f64);
7211 const uint64_t zero = 0;
7212 if (Node->getOperand(0).getValueType()==MVT::f32)
7213 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
7214 else
7215 Hi = Node->getOperand(0);
7216 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7217 break;
7218 }
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007219 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7220 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7221 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007222 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007223 }
7224 case ISD::FP_ROUND: {
7225 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7226 VT);
7227 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7228 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007229 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007230 }
Evan Cheng5316b392008-09-09 23:02:14 +00007231 case ISD::FSQRT:
7232 case ISD::FSIN:
7233 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007234 case ISD::FLOG:
7235 case ISD::FLOG2:
7236 case ISD::FLOG10:
7237 case ISD::FEXP:
7238 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00007239 case ISD::FTRUNC:
7240 case ISD::FFLOOR:
7241 case ISD::FCEIL:
7242 case ISD::FRINT:
7243 case ISD::FNEARBYINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00007244 case ISD::FPOW:
7245 case ISD::FPOWI: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007246 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
7247 switch(Node->getOpcode()) {
7248 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00007249 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7250 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007251 break;
7252 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00007253 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7254 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007255 break;
7256 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00007257 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7258 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007259 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00007260 case ISD::FLOG:
7261 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7262 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7263 break;
7264 case ISD::FLOG2:
7265 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7266 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7267 break;
7268 case ISD::FLOG10:
7269 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7270 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7271 break;
7272 case ISD::FEXP:
7273 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7274 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7275 break;
7276 case ISD::FEXP2:
7277 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7278 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7279 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00007280 case ISD::FTRUNC:
7281 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7282 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7283 break;
7284 case ISD::FFLOOR:
7285 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7286 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7287 break;
7288 case ISD::FCEIL:
7289 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7290 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7291 break;
7292 case ISD::FRINT:
7293 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7294 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7295 break;
7296 case ISD::FNEARBYINT:
7297 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7298 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7299 break;
Evan Cheng5316b392008-09-09 23:02:14 +00007300 case ISD::FPOW:
7301 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7302 RTLIB::POW_PPCF128);
7303 break;
7304 case ISD::FPOWI:
7305 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7306 RTLIB::POWI_PPCF128);
7307 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007308 default: assert(0 && "Unreachable!");
7309 }
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007310 Lo = ExpandLibCall(LC, Node, false, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007311 break;
7312 }
7313 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007314 if (VT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007315 SDValue Tmp;
Dale Johannesen5707ef82007-10-12 19:02:17 +00007316 ExpandOp(Node->getOperand(0), Lo, Tmp);
7317 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7318 // lo = hi==fabs(hi) ? lo : -lo;
7319 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7320 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7321 DAG.getCondCode(ISD::SETEQ));
7322 break;
7323 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007324 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007325 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7326 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7327 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7328 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7329 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7330 if (getTypeAction(NVT) == Expand)
7331 ExpandOp(Lo, Lo, Hi);
7332 break;
7333 }
7334 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007335 if (VT == MVT::ppcf128) {
7336 ExpandOp(Node->getOperand(0), Lo, Hi);
7337 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7338 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7339 break;
7340 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007341 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007342 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7343 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7344 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7345 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7346 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7347 if (getTypeAction(NVT) == Expand)
7348 ExpandOp(Lo, Lo, Hi);
7349 break;
7350 }
7351 case ISD::FCOPYSIGN: {
7352 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7353 if (getTypeAction(NVT) == Expand)
7354 ExpandOp(Lo, Lo, Hi);
7355 break;
7356 }
7357 case ISD::SINT_TO_FP:
7358 case ISD::UINT_TO_FP: {
7359 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands92c43912008-06-06 12:08:01 +00007360 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007361
7362 // Promote the operand if needed. Do this before checking for
7363 // ppcf128 so conversions of i16 and i8 work.
7364 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007365 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesen6a779c82008-03-18 17:28:38 +00007366 Tmp = isSigned
7367 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7368 DAG.getValueType(SrcVT))
7369 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greif1c80d112008-08-28 21:40:38 +00007370 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007371 SrcVT = Node->getOperand(0).getValueType();
7372 }
7373
Dan Gohmanec51f642008-03-10 23:03:31 +00007374 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman84d00962008-02-25 21:39:34 +00007375 static const uint64_t zero = 0;
Dale Johannesen4c14d512007-10-12 01:37:08 +00007376 if (isSigned) {
7377 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7378 Node->getOperand(0)));
7379 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7380 } else {
Dan Gohman84d00962008-02-25 21:39:34 +00007381 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen4c14d512007-10-12 01:37:08 +00007382 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7383 Node->getOperand(0)));
7384 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7385 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007386 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00007387 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7388 DAG.getConstant(0, MVT::i32),
7389 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7390 DAG.getConstantFP(
7391 APFloat(APInt(128, 2, TwoE32)),
7392 MVT::ppcf128)),
7393 Hi,
7394 DAG.getCondCode(ISD::SETLT)),
7395 Lo, Hi);
7396 }
7397 break;
7398 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007399 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7400 // si64->ppcf128 done by libcall, below
Dan Gohman84d00962008-02-25 21:39:34 +00007401 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007402 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7403 Lo, Hi);
7404 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7405 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7406 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7407 DAG.getConstant(0, MVT::i64),
7408 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7409 DAG.getConstantFP(
7410 APFloat(APInt(128, 2, TwoE64)),
7411 MVT::ppcf128)),
7412 Hi,
7413 DAG.getCondCode(ISD::SETLT)),
7414 Lo, Hi);
7415 break;
7416 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007417
Dan Gohmanec51f642008-03-10 23:03:31 +00007418 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7419 Node->getOperand(0));
Evan Chenga8740032008-04-01 01:50:16 +00007420 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng4a2f6df2008-04-01 01:51:26 +00007421 // float to i32 etc. can be 'expanded' to a single node.
Evan Chenga8740032008-04-01 01:50:16 +00007422 ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007423 break;
7424 }
7425 }
7426
7427 // Make sure the resultant values have been legalized themselves, unless this
7428 // is a type that requires multi-step expansion.
7429 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7430 Lo = LegalizeOp(Lo);
Gabor Greif1c80d112008-08-28 21:40:38 +00007431 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007432 // Don't legalize the high part if it is expanded to a single node.
7433 Hi = LegalizeOp(Hi);
7434 }
7435
7436 // Remember in a map if the values will be reused later.
Dan Gohman55d19662008-07-07 17:46:23 +00007437 bool isNew =
7438 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007439 assert(isNew && "Value already expanded?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007440 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007441}
7442
7443/// SplitVectorOp - Given an operand of vector type, break it down into
7444/// two smaller values, still of vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00007445void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7446 SDValue &Hi) {
Duncan Sands92c43912008-06-06 12:08:01 +00007447 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007448 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007449 unsigned NumElements = Op.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007450 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00007451
Duncan Sands92c43912008-06-06 12:08:01 +00007452 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman4a365ad2007-11-15 21:15:26 +00007453
7454 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7455 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7456
Duncan Sands92c43912008-06-06 12:08:01 +00007457 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7458 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007459
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007460 // See if we already split it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007461 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007462 = SplitNodes.find(Op);
7463 if (I != SplitNodes.end()) {
7464 Lo = I->second.first;
7465 Hi = I->second.second;
7466 return;
7467 }
7468
7469 switch (Node->getOpcode()) {
7470 default:
7471#ifndef NDEBUG
7472 Node->dump(&DAG);
7473#endif
7474 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00007475 case ISD::UNDEF:
7476 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7477 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7478 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007479 case ISD::BUILD_PAIR:
7480 Lo = Node->getOperand(0);
7481 Hi = Node->getOperand(1);
7482 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007483 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007484 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7485 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007486 unsigned Index = Idx->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007487 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007488 if (Index < NewNumElts_Lo)
7489 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7490 DAG.getIntPtrConstant(Index));
7491 else
7492 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7493 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7494 break;
7495 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007496 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007497 Node->getOperand(1),
7498 Node->getOperand(2));
7499 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007500 break;
7501 }
Chris Lattner587c46d2007-11-19 21:16:54 +00007502 case ISD::VECTOR_SHUFFLE: {
7503 // Build the low part.
Dan Gohman8181bd12008-07-27 21:46:04 +00007504 SDValue Mask = Node->getOperand(2);
7505 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +00007506 MVT PtrVT = TLI.getPointerTy();
Chris Lattner587c46d2007-11-19 21:16:54 +00007507
7508 // Insert all of the elements from the input that are needed. We use
7509 // buildvector of extractelement here because the input vectors will have
7510 // to be legalized, so this makes the code simpler.
7511 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007512 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007513 if (IdxNode.getOpcode() == ISD::UNDEF) {
7514 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7515 continue;
7516 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007517 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007518 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007519 if (Idx >= NumElements) {
7520 InVec = Node->getOperand(1);
7521 Idx -= NumElements;
7522 }
7523 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7524 DAG.getConstant(Idx, PtrVT)));
7525 }
7526 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7527 Ops.clear();
7528
7529 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007530 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007531 if (IdxNode.getOpcode() == ISD::UNDEF) {
7532 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7533 continue;
7534 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007535 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007536 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007537 if (Idx >= NumElements) {
7538 InVec = Node->getOperand(1);
7539 Idx -= NumElements;
7540 }
7541 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7542 DAG.getConstant(Idx, PtrVT)));
7543 }
Mon P Wang2e89b112008-07-25 01:30:26 +00007544 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner587c46d2007-11-19 21:16:54 +00007545 break;
7546 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007547 case ISD::BUILD_VECTOR: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007548 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00007549 Node->op_begin()+NewNumElts_Lo);
7550 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007551
Dan Gohman8181bd12008-07-27 21:46:04 +00007552 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007553 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007554 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007555 break;
7556 }
7557 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00007558 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007559 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7560 if (NewNumSubvectors == 1) {
7561 Lo = Node->getOperand(0);
7562 Hi = Node->getOperand(1);
7563 } else {
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007564 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7565 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007566 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007567
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007568 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007569 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007570 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007571 }
7572 break;
7573 }
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007574 case ISD::EXTRACT_SUBVECTOR: {
7575 SDValue Vec = Op.getOperand(0);
7576 SDValue Idx = Op.getOperand(1);
7577 MVT IdxVT = Idx.getValueType();
7578
7579 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7580 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7581 if (CIdx) {
7582 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7583 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7584 IdxVT));
7585 } else {
7586 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7587 DAG.getConstant(NewNumElts_Lo, IdxVT));
7588 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7589 }
7590 break;
7591 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00007592 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007593 SDValue Cond = Node->getOperand(0);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007594
Dan Gohman8181bd12008-07-27 21:46:04 +00007595 SDValue LL, LH, RL, RH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007596 SplitVectorOp(Node->getOperand(1), LL, LH);
7597 SplitVectorOp(Node->getOperand(2), RL, RH);
7598
Duncan Sands92c43912008-06-06 12:08:01 +00007599 if (Cond.getValueType().isVector()) {
Dan Gohmand5d4c872007-10-17 14:48:28 +00007600 // Handle a vector merge.
Dan Gohman8181bd12008-07-27 21:46:04 +00007601 SDValue CL, CH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007602 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007603 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7604 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007605 } else {
7606 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00007607 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7608 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007609 }
7610 break;
7611 }
Chris Lattnerc7471452008-06-30 02:43:01 +00007612 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007613 SDValue CondLHS = Node->getOperand(0);
7614 SDValue CondRHS = Node->getOperand(1);
7615 SDValue CondCode = Node->getOperand(4);
Chris Lattnerc7471452008-06-30 02:43:01 +00007616
Dan Gohman8181bd12008-07-27 21:46:04 +00007617 SDValue LL, LH, RL, RH;
Chris Lattnerc7471452008-06-30 02:43:01 +00007618 SplitVectorOp(Node->getOperand(2), LL, LH);
7619 SplitVectorOp(Node->getOperand(3), RL, RH);
7620
7621 // Handle a simple select with vector operands.
7622 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7623 LL, RL, CondCode);
7624 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7625 LH, RH, CondCode);
7626 break;
7627 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00007628 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007629 SDValue LL, LH, RL, RH;
Nate Begeman9a1ce152008-05-12 19:40:03 +00007630 SplitVectorOp(Node->getOperand(0), LL, LH);
7631 SplitVectorOp(Node->getOperand(1), RL, RH);
7632 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7633 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7634 break;
7635 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007636 case ISD::ADD:
7637 case ISD::SUB:
7638 case ISD::MUL:
7639 case ISD::FADD:
7640 case ISD::FSUB:
7641 case ISD::FMUL:
7642 case ISD::SDIV:
7643 case ISD::UDIV:
7644 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007645 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007646 case ISD::AND:
7647 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00007648 case ISD::XOR:
7649 case ISD::UREM:
7650 case ISD::SREM:
Mon P Wang26342922008-12-18 20:03:17 +00007651 case ISD::FREM:
7652 case ISD::SHL:
7653 case ISD::SRA:
7654 case ISD::SRL: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007655 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007656 SplitVectorOp(Node->getOperand(0), LL, LH);
7657 SplitVectorOp(Node->getOperand(1), RL, RH);
7658
Nate Begeman4a365ad2007-11-15 21:15:26 +00007659 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7660 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007661 break;
7662 }
Dan Gohman29c3cef2008-08-14 20:04:46 +00007663 case ISD::FP_ROUND:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007664 case ISD::FPOWI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007665 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007666 SplitVectorOp(Node->getOperand(0), L, H);
7667
Nate Begeman4a365ad2007-11-15 21:15:26 +00007668 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7669 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00007670 break;
7671 }
7672 case ISD::CTTZ:
7673 case ISD::CTLZ:
7674 case ISD::CTPOP:
7675 case ISD::FNEG:
7676 case ISD::FABS:
7677 case ISD::FSQRT:
7678 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00007679 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007680 case ISD::FLOG:
7681 case ISD::FLOG2:
7682 case ISD::FLOG10:
7683 case ISD::FEXP:
7684 case ISD::FEXP2:
Nate Begeman78246ca2007-11-17 03:58:34 +00007685 case ISD::FP_TO_SINT:
7686 case ISD::FP_TO_UINT:
7687 case ISD::SINT_TO_FP:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007688 case ISD::UINT_TO_FP:
7689 case ISD::TRUNCATE:
7690 case ISD::ANY_EXTEND:
7691 case ISD::SIGN_EXTEND:
7692 case ISD::ZERO_EXTEND:
7693 case ISD::FP_EXTEND: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007694 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007695 SplitVectorOp(Node->getOperand(0), L, H);
7696
Nate Begeman4a365ad2007-11-15 21:15:26 +00007697 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7698 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00007699 break;
7700 }
Mon P Wang73d31542008-11-10 20:54:11 +00007701 case ISD::CONVERT_RNDSAT: {
7702 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7703 SDValue L, H;
7704 SplitVectorOp(Node->getOperand(0), L, H);
7705 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7706 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7707 SDValue STyOpL = DAG.getValueType(L.getValueType());
7708 SDValue STyOpH = DAG.getValueType(H.getValueType());
7709
7710 SDValue RndOp = Node->getOperand(3);
7711 SDValue SatOp = Node->getOperand(4);
7712
7713 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7714 RndOp, SatOp, CvtCode);
7715 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7716 RndOp, SatOp, CvtCode);
7717 break;
7718 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007719 case ISD::LOAD: {
7720 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007721 SDValue Ch = LD->getChain();
7722 SDValue Ptr = LD->getBasePtr();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007723 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007724 const Value *SV = LD->getSrcValue();
7725 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007726 MVT MemoryVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007727 unsigned Alignment = LD->getAlignment();
7728 bool isVolatile = LD->isVolatile();
7729
Dan Gohman29c3cef2008-08-14 20:04:46 +00007730 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7731 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7732
7733 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7734 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7735 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7736
7737 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7738 NewVT_Lo, Ch, Ptr, Offset,
7739 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7740 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007741 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00007742 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007743 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00007744 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00007745 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7746 NewVT_Hi, Ch, Ptr, Offset,
7747 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007748
7749 // Build a factor node to remember that this load is independent of the
7750 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00007751 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007752 Hi.getValue(1));
7753
7754 // Remember that we legalized the chain.
7755 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
7756 break;
7757 }
7758 case ISD::BIT_CONVERT: {
7759 // We know the result is a vector. The input may be either a vector or a
7760 // scalar value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007761 SDValue InOp = Node->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007762 if (!InOp.getValueType().isVector() ||
7763 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007764 // The input is a scalar or single-element vector.
7765 // Lower to a store/load so that it can be split.
7766 // FIXME: this could be improved probably.
Mon P Wang36b59ac2008-07-15 05:28:34 +00007767 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7768 Op.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00007769 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greif1c80d112008-08-28 21:40:38 +00007770 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007771
Dan Gohman8181bd12008-07-27 21:46:04 +00007772 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00007773 InOp, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007774 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00007775 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007776 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007777 }
7778 // Split the vector and convert each of the pieces now.
7779 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007780 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7781 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007782 break;
7783 }
7784 }
7785
7786 // Remember in a map if the values will be reused later.
7787 bool isNew =
7788 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
7789 assert(isNew && "Value already split?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007790 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007791}
7792
7793
7794/// ScalarizeVectorOp - Given an operand of single-element vector type
7795/// (e.g. v1f32), convert it into the equivalent operation that returns a
7796/// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007797SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00007798 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007799 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007800 MVT NewVT = Op.getValueType().getVectorElementType();
7801 assert(Op.getValueType().getVectorNumElements() == 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007802
7803 // See if we already scalarized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007804 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007805 if (I != ScalarizedNodes.end()) return I->second;
7806
Dan Gohman8181bd12008-07-27 21:46:04 +00007807 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007808 switch (Node->getOpcode()) {
7809 default:
7810#ifndef NDEBUG
7811 Node->dump(&DAG); cerr << "\n";
7812#endif
7813 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7814 case ISD::ADD:
7815 case ISD::FADD:
7816 case ISD::SUB:
7817 case ISD::FSUB:
7818 case ISD::MUL:
7819 case ISD::FMUL:
7820 case ISD::SDIV:
7821 case ISD::UDIV:
7822 case ISD::FDIV:
7823 case ISD::SREM:
7824 case ISD::UREM:
7825 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007826 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007827 case ISD::AND:
7828 case ISD::OR:
7829 case ISD::XOR:
7830 Result = DAG.getNode(Node->getOpcode(),
7831 NewVT,
7832 ScalarizeVectorOp(Node->getOperand(0)),
7833 ScalarizeVectorOp(Node->getOperand(1)));
7834 break;
7835 case ISD::FNEG:
7836 case ISD::FABS:
7837 case ISD::FSQRT:
7838 case ISD::FSIN:
7839 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007840 case ISD::FLOG:
7841 case ISD::FLOG2:
7842 case ISD::FLOG10:
7843 case ISD::FEXP:
7844 case ISD::FEXP2:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007845 case ISD::FP_TO_SINT:
7846 case ISD::FP_TO_UINT:
7847 case ISD::SINT_TO_FP:
7848 case ISD::UINT_TO_FP:
7849 case ISD::SIGN_EXTEND:
7850 case ISD::ZERO_EXTEND:
7851 case ISD::ANY_EXTEND:
7852 case ISD::TRUNCATE:
7853 case ISD::FP_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007854 Result = DAG.getNode(Node->getOpcode(),
7855 NewVT,
7856 ScalarizeVectorOp(Node->getOperand(0)));
7857 break;
Mon P Wang73d31542008-11-10 20:54:11 +00007858 case ISD::CONVERT_RNDSAT: {
7859 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7860 Result = DAG.getConvertRndSat(NewVT, Op0,
7861 DAG.getValueType(NewVT),
7862 DAG.getValueType(Op0.getValueType()),
7863 Node->getOperand(3),
7864 Node->getOperand(4),
7865 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7866 break;
7867 }
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007868 case ISD::FPOWI:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007869 case ISD::FP_ROUND:
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007870 Result = DAG.getNode(Node->getOpcode(),
7871 NewVT,
7872 ScalarizeVectorOp(Node->getOperand(0)),
7873 Node->getOperand(1));
7874 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007875 case ISD::LOAD: {
7876 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007877 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7878 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman29c3cef2008-08-14 20:04:46 +00007879 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007880 const Value *SV = LD->getSrcValue();
7881 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007882 MVT MemoryVT = LD->getMemoryVT();
7883 unsigned Alignment = LD->getAlignment();
7884 bool isVolatile = LD->isVolatile();
7885
7886 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7887 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7888
7889 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7890 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7891 MemoryVT.getVectorElementType(),
7892 isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007893
7894 // Remember that we legalized the chain.
7895 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7896 break;
7897 }
7898 case ISD::BUILD_VECTOR:
7899 Result = Node->getOperand(0);
7900 break;
7901 case ISD::INSERT_VECTOR_ELT:
7902 // Returning the inserted scalar element.
7903 Result = Node->getOperand(1);
7904 break;
7905 case ISD::CONCAT_VECTORS:
7906 assert(Node->getOperand(0).getValueType() == NewVT &&
7907 "Concat of non-legal vectors not yet supported!");
7908 Result = Node->getOperand(0);
7909 break;
7910 case ISD::VECTOR_SHUFFLE: {
7911 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007912 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007913 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007914 Result = ScalarizeVectorOp(Node->getOperand(1));
7915 else
7916 Result = ScalarizeVectorOp(Node->getOperand(0));
7917 break;
7918 }
7919 case ISD::EXTRACT_SUBVECTOR:
Mon P Wang927daf52008-11-06 22:52:21 +00007920 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007921 Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007922 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007923 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007924 SDValue Op0 = Op.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007925 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng2cc16e72008-05-16 17:19:05 +00007926 Op0 = ScalarizeVectorOp(Op0);
7927 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007928 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007929 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007930 case ISD::SELECT:
7931 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
7932 ScalarizeVectorOp(Op.getOperand(1)),
7933 ScalarizeVectorOp(Op.getOperand(2)));
7934 break;
Chris Lattnerc7471452008-06-30 02:43:01 +00007935 case ISD::SELECT_CC:
7936 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7937 Node->getOperand(1),
7938 ScalarizeVectorOp(Op.getOperand(2)),
7939 ScalarizeVectorOp(Op.getOperand(3)),
7940 Node->getOperand(4));
7941 break;
Nate Begeman78ca4f92008-05-12 23:09:43 +00007942 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007943 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7944 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Nate Begeman78ca4f92008-05-12 23:09:43 +00007945 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7946 Op.getOperand(2));
7947 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7948 DAG.getConstant(-1ULL, NewVT),
7949 DAG.getConstant(0ULL, NewVT));
7950 break;
7951 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007952 }
7953
7954 if (TLI.isTypeLegal(NewVT))
7955 Result = LegalizeOp(Result);
7956 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7957 assert(isNew && "Value already scalarized?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007958 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007959 return Result;
7960}
7961
7962
Mon P Wang1448aad2008-10-30 08:01:45 +00007963SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7964 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7965 if (I != WidenNodes.end()) return I->second;
7966
7967 MVT VT = Op.getValueType();
7968 assert(VT.isVector() && "Cannot widen non-vector type!");
7969
7970 SDValue Result;
7971 SDNode *Node = Op.getNode();
7972 MVT EVT = VT.getVectorElementType();
7973
7974 unsigned NumElts = VT.getVectorNumElements();
7975 unsigned NewNumElts = WidenVT.getVectorNumElements();
7976 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7977 assert(NewNumElts < 17);
7978
7979 // When widen is called, it is assumed that it is more efficient to use a
7980 // wide type. The default action is to widen to operation to a wider legal
7981 // vector type and then do the operation if it is legal by calling LegalizeOp
7982 // again. If there is no vector equivalent, we will unroll the operation, do
7983 // it, and rebuild the vector. If most of the operations are vectorizible to
7984 // the legal type, the resulting code will be more efficient. If this is not
7985 // the case, the resulting code will preform badly as we end up generating
7986 // code to pack/unpack the results. It is the function that calls widen
Mon P Wanga5a239f2008-11-06 05:31:54 +00007987 // that is responsible for seeing this doesn't happen.
Mon P Wang1448aad2008-10-30 08:01:45 +00007988 switch (Node->getOpcode()) {
7989 default:
7990#ifndef NDEBUG
7991 Node->dump(&DAG);
7992#endif
7993 assert(0 && "Unexpected operation in WidenVectorOp!");
7994 break;
7995 case ISD::CopyFromReg:
Mon P Wang257e1c72008-11-15 06:05:52 +00007996 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang1448aad2008-10-30 08:01:45 +00007997 case ISD::Constant:
7998 case ISD::ConstantFP:
7999 // To build a vector of these elements, clients should call BuildVector
8000 // and with each element instead of creating a node with a vector type
8001 assert(0 && "Unexpected operation in WidenVectorOp!");
8002 case ISD::VAARG:
8003 // Variable Arguments with vector types doesn't make any sense to me
8004 assert(0 && "Unexpected operation in WidenVectorOp!");
8005 break;
Mon P Wang257e1c72008-11-15 06:05:52 +00008006 case ISD::UNDEF:
8007 Result = DAG.getNode(ISD::UNDEF, WidenVT);
8008 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00008009 case ISD::BUILD_VECTOR: {
8010 // Build a vector with undefined for the new nodes
8011 SDValueVector NewOps(Node->op_begin(), Node->op_end());
8012 for (unsigned i = NumElts; i < NewNumElts; ++i) {
8013 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
8014 }
8015 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
8016 break;
8017 }
8018 case ISD::INSERT_VECTOR_ELT: {
8019 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8020 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
8021 Node->getOperand(1), Node->getOperand(2));
8022 break;
8023 }
8024 case ISD::VECTOR_SHUFFLE: {
8025 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8026 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8027 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
8028 // used as permutation array. We build the vector here instead of widening
8029 // because we don't want to legalize and have it turned to something else.
8030 SDValue PermOp = Node->getOperand(2);
8031 SDValueVector NewOps;
8032 MVT PVT = PermOp.getValueType().getVectorElementType();
8033 for (unsigned i = 0; i < NumElts; ++i) {
8034 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
8035 NewOps.push_back(PermOp.getOperand(i));
8036 } else {
8037 unsigned Idx =
Mon P Wangec428ad2008-12-13 08:15:14 +00008038 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
Mon P Wang1448aad2008-10-30 08:01:45 +00008039 if (Idx < NumElts) {
8040 NewOps.push_back(PermOp.getOperand(i));
8041 }
8042 else {
8043 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
8044 PermOp.getOperand(i).getValueType()));
8045 }
8046 }
8047 }
8048 for (unsigned i = NumElts; i < NewNumElts; ++i) {
8049 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
8050 }
8051
8052 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
8053 MVT::getVectorVT(PVT, NewOps.size()),
8054 &NewOps[0], NewOps.size());
8055
8056 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
8057 break;
8058 }
8059 case ISD::LOAD: {
8060 // If the load widen returns true, we can use a single load for the
8061 // vector. Otherwise, it is returning a token factor for multiple
8062 // loads.
8063 SDValue TFOp;
8064 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
8065 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
8066 else
8067 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
8068 break;
8069 }
8070
8071 case ISD::BIT_CONVERT: {
8072 SDValue Tmp1 = Node->getOperand(0);
8073 // Converts between two different types so we need to determine
8074 // the correct widen type for the input operand.
Mon P Wang26342922008-12-18 20:03:17 +00008075 MVT InVT = Tmp1.getValueType();
8076 unsigned WidenSize = WidenVT.getSizeInBits();
8077 if (InVT.isVector()) {
8078 MVT InEltVT = InVT.getVectorElementType();
8079 unsigned InEltSize = InEltVT.getSizeInBits();
8080 assert(WidenSize % InEltSize == 0 &&
8081 "can not widen bit convert that are not multiple of element type");
8082 MVT NewInWidenVT = MVT::getVectorVT(InEltVT, WidenSize / InEltSize);
8083 Tmp1 = WidenVectorOp(Tmp1, NewInWidenVT);
8084 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
8085 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Tmp1);
8086 } else {
8087 // If the result size is a multiple of the input size, widen the input
8088 // and then convert.
8089 unsigned InSize = InVT.getSizeInBits();
8090 assert(WidenSize % InSize == 0 &&
8091 "can not widen bit convert that are not multiple of element type");
8092 unsigned NewNumElts = WidenSize / InSize;
8093 SmallVector<SDValue, 16> Ops(NewNumElts);
8094 SDValue UndefVal = DAG.getNode(ISD::UNDEF, InVT);
8095 Ops[0] = Tmp1;
8096 for (unsigned i = 1; i < NewNumElts; ++i)
8097 Ops[i] = UndefVal;
Mon P Wang1448aad2008-10-30 08:01:45 +00008098
Mon P Wang26342922008-12-18 20:03:17 +00008099 MVT NewInVT = MVT::getVectorVT(InVT, NewNumElts);
8100 Result = DAG.getNode(ISD::BUILD_VECTOR, NewInVT, &Ops[0], NewNumElts);
8101 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Result);
Mon P Wang1448aad2008-10-30 08:01:45 +00008102 }
8103 break;
8104 }
8105
8106 case ISD::SINT_TO_FP:
8107 case ISD::UINT_TO_FP:
8108 case ISD::FP_TO_SINT:
Mon P Wang26342922008-12-18 20:03:17 +00008109 case ISD::FP_TO_UINT:
8110 case ISD::FP_ROUND: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008111 SDValue Tmp1 = Node->getOperand(0);
8112 // Converts between two different types so we need to determine
8113 // the correct widen type for the input operand.
8114 MVT TVT = Tmp1.getValueType();
8115 assert(TVT.isVector() && "can not widen non vector type");
8116 MVT TEVT = TVT.getVectorElementType();
8117 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
8118 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
8119 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
8120 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang1448aad2008-10-30 08:01:45 +00008121 break;
8122 }
8123
8124 case ISD::FP_EXTEND:
8125 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
8126 case ISD::TRUNCATE:
8127 case ISD::SIGN_EXTEND:
8128 case ISD::ZERO_EXTEND:
8129 case ISD::ANY_EXTEND:
Mon P Wang1448aad2008-10-30 08:01:45 +00008130 case ISD::SIGN_EXTEND_INREG:
8131 case ISD::FABS:
8132 case ISD::FNEG:
8133 case ISD::FSQRT:
8134 case ISD::FSIN:
Mon P Wang257e1c72008-11-15 06:05:52 +00008135 case ISD::FCOS:
8136 case ISD::CTPOP:
8137 case ISD::CTTZ:
8138 case ISD::CTLZ: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008139 // Unary op widening
Mon P Wang26342922008-12-18 20:03:17 +00008140 SDValue Tmp1;
Mon P Wang1448aad2008-10-30 08:01:45 +00008141 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8142 assert(Tmp1.getValueType() == WidenVT);
8143 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang1448aad2008-10-30 08:01:45 +00008144 break;
8145 }
Mon P Wang73d31542008-11-10 20:54:11 +00008146 case ISD::CONVERT_RNDSAT: {
8147 SDValue RndOp = Node->getOperand(3);
8148 SDValue SatOp = Node->getOperand(4);
Mon P Wang73d31542008-11-10 20:54:11 +00008149 SDValue SrcOp = Node->getOperand(0);
8150
8151 // Converts between two different types so we need to determine
8152 // the correct widen type for the input operand.
8153 MVT SVT = SrcOp.getValueType();
8154 assert(SVT.isVector() && "can not widen non vector type");
8155 MVT SEVT = SVT.getVectorElementType();
8156 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8157
8158 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8159 assert(SrcOp.getValueType() == WidenVT);
8160 SDValue DTyOp = DAG.getValueType(WidenVT);
8161 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8162 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8163
8164 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
8165 RndOp, SatOp, CvtCode);
Mon P Wang73d31542008-11-10 20:54:11 +00008166 break;
8167 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008168 case ISD::FPOW:
8169 case ISD::FPOWI:
8170 case ISD::ADD:
8171 case ISD::SUB:
8172 case ISD::MUL:
8173 case ISD::MULHS:
8174 case ISD::MULHU:
8175 case ISD::AND:
8176 case ISD::OR:
8177 case ISD::XOR:
8178 case ISD::FADD:
8179 case ISD::FSUB:
8180 case ISD::FMUL:
8181 case ISD::SDIV:
8182 case ISD::SREM:
8183 case ISD::FDIV:
8184 case ISD::FREM:
8185 case ISD::FCOPYSIGN:
8186 case ISD::UDIV:
8187 case ISD::UREM:
8188 case ISD::BSWAP: {
8189 // Binary op widening
Mon P Wang1448aad2008-10-30 08:01:45 +00008190 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8191 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8192 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8193 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
Mon P Wang1448aad2008-10-30 08:01:45 +00008194 break;
8195 }
8196
8197 case ISD::SHL:
8198 case ISD::SRA:
8199 case ISD::SRL: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008200 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8201 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangd5638262008-12-02 07:35:08 +00008202 SDValue ShOp = Node->getOperand(1);
8203 MVT ShVT = ShOp.getValueType();
8204 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8205 WidenVT.getVectorNumElements());
8206 ShOp = WidenVectorOp(ShOp, NewShVT);
8207 assert(ShOp.getValueType() == NewShVT);
8208 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, ShOp);
Mon P Wang1448aad2008-10-30 08:01:45 +00008209 break;
8210 }
Mon P Wangd5638262008-12-02 07:35:08 +00008211
Mon P Wang1448aad2008-10-30 08:01:45 +00008212 case ISD::EXTRACT_VECTOR_ELT: {
8213 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8214 assert(Tmp1.getValueType() == WidenVT);
8215 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8216 break;
8217 }
8218 case ISD::CONCAT_VECTORS: {
8219 // We concurrently support only widen on a multiple of the incoming vector.
8220 // We could widen on a multiple of the incoming operand if necessary.
8221 unsigned NumConcat = NewNumElts / NumElts;
8222 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Mon P Wangd5638262008-12-02 07:35:08 +00008223 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
Mon P Wang1448aad2008-10-30 08:01:45 +00008224 SmallVector<SDValue, 8> MOps;
8225 MOps.push_back(Op);
8226 for (unsigned i = 1; i != NumConcat; ++i) {
8227 MOps.push_back(UndefVal);
8228 }
8229 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8230 &MOps[0], MOps.size()));
8231 break;
8232 }
8233 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang257e1c72008-11-15 06:05:52 +00008234 SDValue Tmp1 = Node->getOperand(0);
8235 SDValue Idx = Node->getOperand(1);
8236 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8237 if (CIdx && CIdx->getZExtValue() == 0) {
8238 // Since we are access the start of the vector, the incoming
8239 // vector type might be the proper.
8240 MVT Tmp1VT = Tmp1.getValueType();
8241 if (Tmp1VT == WidenVT)
8242 return Tmp1;
8243 else {
8244 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8245 if (Tmp1VTNumElts < NewNumElts)
8246 Result = WidenVectorOp(Tmp1, WidenVT);
8247 else
8248 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8249 }
8250 } else if (NewNumElts % NumElts == 0) {
8251 // Widen the extracted subvector.
8252 unsigned NumConcat = NewNumElts / NumElts;
8253 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8254 SmallVector<SDValue, 8> MOps;
8255 MOps.push_back(Op);
8256 for (unsigned i = 1; i != NumConcat; ++i) {
8257 MOps.push_back(UndefVal);
8258 }
8259 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8260 &MOps[0], MOps.size()));
8261 } else {
8262 assert(0 && "can not widen extract subvector");
8263 // This could be implemented using insert and build vector but I would
8264 // like to see when this happens.
8265 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008266 break;
8267 }
8268
8269 case ISD::SELECT: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008270 // Determine new condition widen type and widen
8271 SDValue Cond1 = Node->getOperand(0);
8272 MVT CondVT = Cond1.getValueType();
8273 assert(CondVT.isVector() && "can not widen non vector type");
8274 MVT CondEVT = CondVT.getVectorElementType();
8275 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8276 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8277 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8278
8279 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8280 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8281 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8282 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang1448aad2008-10-30 08:01:45 +00008283 break;
8284 }
8285
8286 case ISD::SELECT_CC: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008287 // Determine new condition widen type and widen
8288 SDValue Cond1 = Node->getOperand(0);
8289 SDValue Cond2 = Node->getOperand(1);
8290 MVT CondVT = Cond1.getValueType();
8291 assert(CondVT.isVector() && "can not widen non vector type");
8292 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8293 MVT CondEVT = CondVT.getVectorElementType();
8294 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8295 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8296 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8297 assert(Cond1.getValueType() == CondWidenVT &&
8298 Cond2.getValueType() == CondWidenVT && "condition not widen");
8299
8300 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8301 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8302 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8303 "operands not widen");
8304 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8305 Tmp2, Node->getOperand(4));
Mon P Wang1448aad2008-10-30 08:01:45 +00008306 break;
Mon P Wang42ac14e2008-10-30 18:21:52 +00008307 }
8308 case ISD::VSETCC: {
8309 // Determine widen for the operand
8310 SDValue Tmp1 = Node->getOperand(0);
8311 MVT TmpVT = Tmp1.getValueType();
8312 assert(TmpVT.isVector() && "can not widen non vector type");
8313 MVT TmpEVT = TmpVT.getVectorElementType();
8314 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8315 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8316 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
Mon P Wang26342922008-12-18 20:03:17 +00008317 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
Mon P Wang42ac14e2008-10-30 18:21:52 +00008318 Node->getOperand(2));
Mon P Wang1448aad2008-10-30 08:01:45 +00008319 break;
8320 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008321 case ISD::ATOMIC_CMP_SWAP_8:
8322 case ISD::ATOMIC_CMP_SWAP_16:
8323 case ISD::ATOMIC_CMP_SWAP_32:
8324 case ISD::ATOMIC_CMP_SWAP_64:
8325 case ISD::ATOMIC_LOAD_ADD_8:
8326 case ISD::ATOMIC_LOAD_SUB_8:
8327 case ISD::ATOMIC_LOAD_AND_8:
8328 case ISD::ATOMIC_LOAD_OR_8:
8329 case ISD::ATOMIC_LOAD_XOR_8:
8330 case ISD::ATOMIC_LOAD_NAND_8:
8331 case ISD::ATOMIC_LOAD_MIN_8:
8332 case ISD::ATOMIC_LOAD_MAX_8:
8333 case ISD::ATOMIC_LOAD_UMIN_8:
8334 case ISD::ATOMIC_LOAD_UMAX_8:
8335 case ISD::ATOMIC_SWAP_8:
8336 case ISD::ATOMIC_LOAD_ADD_16:
8337 case ISD::ATOMIC_LOAD_SUB_16:
8338 case ISD::ATOMIC_LOAD_AND_16:
8339 case ISD::ATOMIC_LOAD_OR_16:
8340 case ISD::ATOMIC_LOAD_XOR_16:
8341 case ISD::ATOMIC_LOAD_NAND_16:
8342 case ISD::ATOMIC_LOAD_MIN_16:
8343 case ISD::ATOMIC_LOAD_MAX_16:
8344 case ISD::ATOMIC_LOAD_UMIN_16:
8345 case ISD::ATOMIC_LOAD_UMAX_16:
8346 case ISD::ATOMIC_SWAP_16:
8347 case ISD::ATOMIC_LOAD_ADD_32:
8348 case ISD::ATOMIC_LOAD_SUB_32:
8349 case ISD::ATOMIC_LOAD_AND_32:
8350 case ISD::ATOMIC_LOAD_OR_32:
8351 case ISD::ATOMIC_LOAD_XOR_32:
8352 case ISD::ATOMIC_LOAD_NAND_32:
8353 case ISD::ATOMIC_LOAD_MIN_32:
8354 case ISD::ATOMIC_LOAD_MAX_32:
8355 case ISD::ATOMIC_LOAD_UMIN_32:
8356 case ISD::ATOMIC_LOAD_UMAX_32:
8357 case ISD::ATOMIC_SWAP_32:
8358 case ISD::ATOMIC_LOAD_ADD_64:
8359 case ISD::ATOMIC_LOAD_SUB_64:
8360 case ISD::ATOMIC_LOAD_AND_64:
8361 case ISD::ATOMIC_LOAD_OR_64:
8362 case ISD::ATOMIC_LOAD_XOR_64:
8363 case ISD::ATOMIC_LOAD_NAND_64:
8364 case ISD::ATOMIC_LOAD_MIN_64:
8365 case ISD::ATOMIC_LOAD_MAX_64:
8366 case ISD::ATOMIC_LOAD_UMIN_64:
8367 case ISD::ATOMIC_LOAD_UMAX_64:
8368 case ISD::ATOMIC_SWAP_64: {
8369 // For now, we assume that using vectors for these operations don't make
8370 // much sense so we just split it. We return an empty result
8371 SDValue X, Y;
8372 SplitVectorOp(Op, X, Y);
8373 return Result;
8374 break;
8375 }
8376
8377 } // end switch (Node->getOpcode())
8378
8379 assert(Result.getNode() && "Didn't set a result!");
8380 if (Result != Op)
8381 Result = LegalizeOp(Result);
8382
Mon P Wanga5a239f2008-11-06 05:31:54 +00008383 AddWidenedOperand(Op, Result);
Mon P Wang1448aad2008-10-30 08:01:45 +00008384 return Result;
8385}
8386
8387// Utility function to find a legal vector type and its associated element
8388// type from a preferred width and whose vector type must be the same size
8389// as the VVT.
8390// TLI: Target lowering used to determine legal types
8391// Width: Preferred width of element type
8392// VVT: Vector value type whose size we must match.
8393// Returns VecEVT and EVT - the vector type and its associated element type
8394static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8395 MVT& EVT, MVT& VecEVT) {
8396 // We start with the preferred width, make it a power of 2 and see if
8397 // we can find a vector type of that width. If not, we reduce it by
8398 // another power of 2. If we have widen the type, a vector of bytes should
8399 // always be legal.
8400 assert(TLI.isTypeLegal(VVT));
8401 unsigned EWidth = Width + 1;
8402 do {
8403 assert(EWidth > 0);
8404 EWidth = (1 << Log2_32(EWidth-1));
8405 EVT = MVT::getIntegerVT(EWidth);
8406 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8407 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8408 } while (!TLI.isTypeLegal(VecEVT) ||
8409 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8410}
8411
8412SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8413 SDValue Chain,
8414 SDValue BasePtr,
8415 const Value *SV,
8416 int SVOffset,
8417 unsigned Alignment,
8418 bool isVolatile,
8419 unsigned LdWidth,
8420 MVT ResType) {
8421 // We assume that we have good rules to handle loading power of two loads so
8422 // we break down the operations to power of 2 loads. The strategy is to
8423 // load the largest power of 2 that we can easily transform to a legal vector
8424 // and then insert into that vector, and the cast the result into the legal
8425 // vector that we want. This avoids unnecessary stack converts.
8426 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8427 // the load is nonvolatile, we an use a wider load for the value.
8428 // Find a vector length we can load a large chunk
8429 MVT EVT, VecEVT;
8430 unsigned EVTWidth;
8431 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8432 EVTWidth = EVT.getSizeInBits();
8433
8434 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8435 isVolatile, Alignment);
8436 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8437 LdChain.push_back(LdOp.getValue(1));
8438
8439 // Check if we can load the element with one instruction
8440 if (LdWidth == EVTWidth) {
8441 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8442 }
8443
8444 // The vector element order is endianness dependent.
8445 unsigned Idx = 1;
8446 LdWidth -= EVTWidth;
8447 unsigned Offset = 0;
8448
8449 while (LdWidth > 0) {
8450 unsigned Increment = EVTWidth / 8;
8451 Offset += Increment;
8452 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8453 DAG.getIntPtrConstant(Increment));
8454
8455 if (LdWidth < EVTWidth) {
8456 // Our current type we are using is too large, use a smaller size by
8457 // using a smaller power of 2
8458 unsigned oEVTWidth = EVTWidth;
8459 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8460 EVTWidth = EVT.getSizeInBits();
8461 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008462 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang1448aad2008-10-30 08:01:45 +00008463 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8464 }
8465
8466 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8467 SVOffset+Offset, isVolatile,
8468 MinAlign(Alignment, Offset));
8469 LdChain.push_back(LdOp.getValue(1));
8470 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8471 DAG.getIntPtrConstant(Idx++));
8472
8473 LdWidth -= EVTWidth;
8474 }
8475
8476 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8477}
8478
8479bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8480 SDValue& TFOp,
8481 SDValue Op,
8482 MVT NVT) {
8483 // TODO: Add support for ConcatVec and the ability to load many vector
8484 // types (e.g., v4i8). This will not work when a vector register
8485 // to memory mapping is strange (e.g., vector elements are not
8486 // stored in some sequential order).
8487
8488 // It must be true that the widen vector type is bigger than where
8489 // we need to load from.
8490 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8491 MVT LdVT = LD->getMemoryVT();
8492 assert(LdVT.isVector() && NVT.isVector());
8493 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8494
8495 // Load information
8496 SDValue Chain = LD->getChain();
8497 SDValue BasePtr = LD->getBasePtr();
8498 int SVOffset = LD->getSrcValueOffset();
8499 unsigned Alignment = LD->getAlignment();
8500 bool isVolatile = LD->isVolatile();
8501 const Value *SV = LD->getSrcValue();
8502 unsigned int LdWidth = LdVT.getSizeInBits();
8503
8504 // Load value as a large register
8505 SDValueVector LdChain;
8506 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8507 Alignment, isVolatile, LdWidth, NVT);
8508
8509 if (LdChain.size() == 1) {
8510 TFOp = LdChain[0];
8511 return true;
8512 }
8513 else {
8514 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8515 return false;
8516 }
8517}
8518
8519
8520void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8521 SDValue Chain,
8522 SDValue BasePtr,
8523 const Value *SV,
8524 int SVOffset,
8525 unsigned Alignment,
8526 bool isVolatile,
Mon P Wang257e1c72008-11-15 06:05:52 +00008527 SDValue ValOp,
Mon P Wang1448aad2008-10-30 08:01:45 +00008528 unsigned StWidth) {
8529 // Breaks the stores into a series of power of 2 width stores. For any
8530 // width, we convert the vector to the vector of element size that we
8531 // want to store. This avoids requiring a stack convert.
8532
8533 // Find a width of the element type we can store with
8534 MVT VVT = ValOp.getValueType();
8535 MVT EVT, VecEVT;
8536 unsigned EVTWidth;
8537 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8538 EVTWidth = EVT.getSizeInBits();
8539
8540 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8541 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang927daf52008-11-06 22:52:21 +00008542 DAG.getIntPtrConstant(0));
Mon P Wang1448aad2008-10-30 08:01:45 +00008543 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8544 isVolatile, Alignment);
8545 StChain.push_back(StOp);
8546
8547 // Check if we are done
8548 if (StWidth == EVTWidth) {
8549 return;
8550 }
8551
8552 unsigned Idx = 1;
8553 StWidth -= EVTWidth;
8554 unsigned Offset = 0;
8555
8556 while (StWidth > 0) {
8557 unsigned Increment = EVTWidth / 8;
8558 Offset += Increment;
8559 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8560 DAG.getIntPtrConstant(Increment));
8561
8562 if (StWidth < EVTWidth) {
8563 // Our current type we are using is too large, use a smaller size by
8564 // using a smaller power of 2
8565 unsigned oEVTWidth = EVTWidth;
8566 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8567 EVTWidth = EVT.getSizeInBits();
8568 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008569 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang1448aad2008-10-30 08:01:45 +00008570 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8571 }
8572
8573 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang257e1c72008-11-15 06:05:52 +00008574 DAG.getIntPtrConstant(Idx++));
Mon P Wang1448aad2008-10-30 08:01:45 +00008575 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8576 SVOffset + Offset, isVolatile,
8577 MinAlign(Alignment, Offset)));
8578 StWidth -= EVTWidth;
8579 }
8580}
8581
8582
8583SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8584 SDValue Chain,
8585 SDValue BasePtr) {
8586 // TODO: It might be cleaner if we can use SplitVector and have more legal
8587 // vector types that can be stored into memory (e.g., v4xi8 can
8588 // be stored as a word). This will not work when a vector register
8589 // to memory mapping is strange (e.g., vector elements are not
8590 // stored in some sequential order).
8591
8592 MVT StVT = ST->getMemoryVT();
8593 SDValue ValOp = ST->getValue();
8594
8595 // Check if we have widen this node with another value
8596 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8597 if (I != WidenNodes.end())
8598 ValOp = I->second;
8599
8600 MVT VVT = ValOp.getValueType();
8601
8602 // It must be true that we the widen vector type is bigger than where
8603 // we need to store.
8604 assert(StVT.isVector() && VVT.isVector());
8605 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8606 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8607
8608 // Store value
8609 SDValueVector StChain;
8610 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8611 ST->getSrcValueOffset(), ST->getAlignment(),
8612 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8613 if (StChain.size() == 1)
8614 return StChain[0];
8615 else
8616 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8617}
8618
8619
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008620// SelectionDAG::Legalize - This is the entry point for the file.
8621//
Duncan Sandse016a2e2008-12-14 09:43:15 +00008622void SelectionDAG::Legalize(bool TypesNeedLegalizing) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008623 /// run - This is the main entry point to this class.
8624 ///
Duncan Sandse016a2e2008-12-14 09:43:15 +00008625 SelectionDAGLegalize(*this, TypesNeedLegalizing).LegalizeDAG();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008626}
8627