blob: f84e22b10c9a4bf55b32ca21e7d3229c0055de8d [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;
54
55 // Libcall insertion helpers.
56
57 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
58 /// legalized. We use this to ensure that calls are properly serialized
59 /// against each other, including inserted libcalls.
Dan Gohman8181bd12008-07-27 21:46:04 +000060 SDValue LastCALLSEQ_END;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061
62 /// IsLegalizingCall - This member is used *only* for purposes of providing
63 /// helpful assertions that a libcall isn't created while another call is
64 /// being legalized (which could lead to non-serialized call sequences).
65 bool IsLegalizingCall;
66
67 enum LegalizeAction {
68 Legal, // The target natively supports this operation.
69 Promote, // This operation should be executed in a larger type.
70 Expand // Try to expand this to other ops, otherwise use a libcall.
71 };
72
73 /// ValueTypeActions - This is a bitvector that contains two bits for each
74 /// value type, where the two bits correspond to the LegalizeAction enum.
75 /// This can be queried with "getTypeAction(VT)".
76 TargetLowering::ValueTypeActionImpl ValueTypeActions;
77
78 /// LegalizedNodes - For nodes that are of legal width, and that have more
79 /// than one use, this map indicates what regularized operand to use. This
80 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000081 DenseMap<SDValue, SDValue> LegalizedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082
83 /// PromotedNodes - For nodes that are below legal width, and that have more
84 /// than one use, this map indicates what promoted value to use. This allows
85 /// us to avoid promoting the same thing more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000086 DenseMap<SDValue, SDValue> PromotedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
88 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang1448aad2008-10-30 08:01:45 +000089 /// which operands are the expanded version of the input. This allows
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 /// us to avoid expanding the same node more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000091 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
93 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang1448aad2008-10-30 08:01:45 +000094 /// which operands are the split version of the input. This allows us
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 /// to avoid splitting the same node more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000096 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097
98 /// ScalarizedNodes - For nodes that need to be converted from vector types to
99 /// scalar types, this contains the mapping of ones we have already
100 /// processed to the result.
Dan Gohman8181bd12008-07-27 21:46:04 +0000101 std::map<SDValue, SDValue> ScalarizedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102
Mon P Wanga5a239f2008-11-06 05:31:54 +0000103 /// WidenNodes - For nodes that need to be widened from one vector type to
104 /// another, this contains the mapping of those that we have already widen.
105 /// This allows us to avoid widening more than once.
Mon P Wang1448aad2008-10-30 08:01:45 +0000106 std::map<SDValue, SDValue> WidenNodes;
107
Dan Gohman8181bd12008-07-27 21:46:04 +0000108 void AddLegalizedOperand(SDValue From, SDValue To) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 LegalizedNodes.insert(std::make_pair(From, To));
110 // If someone requests legalization of the new node, return itself.
111 if (From != To)
112 LegalizedNodes.insert(std::make_pair(To, To));
113 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000114 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman55d19662008-07-07 17:46:23 +0000115 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 assert(isNew && "Got into the map somehow?");
117 // If someone requests legalization of the new node, return itself.
118 LegalizedNodes.insert(std::make_pair(To, To));
119 }
Mon P Wanga5a239f2008-11-06 05:31:54 +0000120 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang1448aad2008-10-30 08:01:45 +0000121 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
122 assert(isNew && "Got into the map somehow?");
123 // If someone requests legalization of the new node, return itself.
124 LegalizedNodes.insert(std::make_pair(To, To));
125 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126
127public:
Dan Gohmane887fdf2008-07-07 18:00:37 +0000128 explicit SelectionDAGLegalize(SelectionDAG &DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129
130 /// getTypeAction - Return how we should legalize values of this type, either
131 /// it is already legal or we need to expand it into multiple registers of
132 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands92c43912008-06-06 12:08:01 +0000133 LegalizeAction getTypeAction(MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
135 }
136
137 /// isTypeLegal - Return true if this type is legal on this target.
138 ///
Duncan Sands92c43912008-06-06 12:08:01 +0000139 bool isTypeLegal(MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 return getTypeAction(VT) == Legal;
141 }
142
143 void LegalizeDAG();
144
145private:
146 /// HandleOp - Legalize, Promote, or Expand the specified operand as
147 /// appropriate for its type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000148 void HandleOp(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149
150 /// LegalizeOp - We know that the specified value has a legal type.
151 /// Recursively ensure that the operands have legal types, then return the
152 /// result.
Dan Gohman8181bd12008-07-27 21:46:04 +0000153 SDValue LegalizeOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154
Dan Gohman6d05cac2007-10-11 23:57:53 +0000155 /// UnrollVectorOp - We know that the given vector has a legal type, however
156 /// the operation it performs is not legal and is an operation that we have
157 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
158 /// operating on each element individually.
Dan Gohman8181bd12008-07-27 21:46:04 +0000159 SDValue UnrollVectorOp(SDValue O);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000160
161 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
162 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
163 /// is necessary to spill the vector being inserted into to memory, perform
164 /// the insert there, and then read the result back.
Dan Gohman8181bd12008-07-27 21:46:04 +0000165 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
166 SDValue Idx);
Dan Gohman6d05cac2007-10-11 23:57:53 +0000167
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 /// PromoteOp - Given an operation that produces a value in an invalid type,
169 /// promote it to compute the value into a larger type. The produced value
170 /// will have the correct bits for the low portion of the register, but no
171 /// guarantee is made about the top bits: it may be zero, sign-extended, or
172 /// garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +0000173 SDValue PromoteOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174
Dan Gohman8181bd12008-07-27 21:46:04 +0000175 /// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman4fc03742008-10-01 15:07:49 +0000177 /// the LegalizedNodes map is filled in for any results that are not expanded,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 /// the ExpandedNodes map is filled in for any results that are expanded, and
179 /// the Lo/Hi values are returned. This applies to integer types and Vector
180 /// types.
Dan Gohman8181bd12008-07-27 21:46:04 +0000181 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182
Mon P Wanga5a239f2008-11-06 05:31:54 +0000183 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
184 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
185 /// for the existing elements but no guarantee is made about the new elements
186 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
187 /// when we have an instruction operating on an illegal vector type and we
188 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang1448aad2008-10-30 08:01:45 +0000189 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
190
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 /// SplitVectorOp - Given an operand of vector type, break it down into
192 /// two smaller values.
Dan Gohman8181bd12008-07-27 21:46:04 +0000193 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194
195 /// ScalarizeVectorOp - Given an operand of single-element vector type
196 /// (e.g. v1f32), convert it into the equivalent operation that returns a
197 /// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +0000198 SDValue ScalarizeVectorOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199
Mon P Wanga5a239f2008-11-06 05:31:54 +0000200 /// Useful 16 element vector type that is used to pass operands for widening.
Mon P Wang1448aad2008-10-30 08:01:45 +0000201 typedef SmallVector<SDValue, 16> SDValueVector;
202
203 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
204 /// the LdChain contains a single load and false if it contains a token
205 /// factor for multiple loads. It takes
206 /// Result: location to return the result
207 /// LdChain: location to return the load chain
208 /// Op: load operation to widen
209 /// NVT: widen vector result type we want for the load
210 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
211 SDValue Op, MVT NVT);
212
213 /// Helper genWidenVectorLoads - Helper function to generate a set of
214 /// loads to load a vector with a resulting wider type. It takes
215 /// LdChain: list of chains for the load we have generated
216 /// Chain: incoming chain for the ld vector
217 /// BasePtr: base pointer to load from
218 /// SV: memory disambiguation source value
219 /// SVOffset: memory disambiugation offset
220 /// Alignment: alignment of the memory
221 /// isVolatile: volatile load
222 /// LdWidth: width of memory that we want to load
223 /// ResType: the wider result result type for the resulting loaded vector
224 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
225 SDValue BasePtr, const Value *SV,
226 int SVOffset, unsigned Alignment,
227 bool isVolatile, unsigned LdWidth,
228 MVT ResType);
229
230 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
231 /// location. It takes
232 /// ST: store node that we want to replace
233 /// Chain: incoming store chain
234 /// BasePtr: base address of where we want to store into
235 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
236 SDValue BasePtr);
237
238 /// Helper genWidenVectorStores - Helper function to generate a set of
239 /// stores to store a widen vector into non widen memory
240 // It takes
241 // StChain: list of chains for the stores we have generated
242 // Chain: incoming chain for the ld vector
243 // BasePtr: base pointer to load from
244 // SV: memory disambiguation source value
245 // SVOffset: memory disambiugation offset
246 // Alignment: alignment of the memory
247 // isVolatile: volatile lod
248 // ValOp: value to store
249 // StWidth: width of memory that we want to store
250 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
251 SDValue BasePtr, const Value *SV,
252 int SVOffset, unsigned Alignment,
253 bool isVolatile, SDValue ValOp,
254 unsigned StWidth);
255
Duncan Sandsd3ace282008-07-21 10:20:31 +0000256 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 /// specified mask and type. Targets can specify exactly which masks they
258 /// support and the code generator is tasked with not creating illegal masks.
259 ///
260 /// Note that this will also return true for shuffles that are promoted to a
261 /// different type.
262 ///
263 /// If this is a legal shuffle, this method returns the (possibly promoted)
264 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman8181bd12008-07-27 21:46:04 +0000265 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266
267 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
268 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
269
Dan Gohman8181bd12008-07-27 21:46:04 +0000270 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Evan Cheng71343822008-10-15 02:05:31 +0000271 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC);
272 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC) {
273 LegalizeSetCCOperands(LHS, RHS, CC);
274 LegalizeSetCCCondCode(VT, LHS, RHS, CC);
275 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276
Dan Gohman8181bd12008-07-27 21:46:04 +0000277 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
278 SDValue &Hi);
279 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280
Dan Gohman8181bd12008-07-27 21:46:04 +0000281 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
282 SDValue ExpandBUILD_VECTOR(SDNode *Node);
283 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dan Gohman29c3cef2008-08-14 20:04:46 +0000284 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op);
Dan Gohman8181bd12008-07-27 21:46:04 +0000285 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
286 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
287 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288
Dan Gohman8181bd12008-07-27 21:46:04 +0000289 SDValue ExpandBSWAP(SDValue Op);
290 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
291 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
292 SDValue &Lo, SDValue &Hi);
293 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
294 SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295
Dan Gohman8181bd12008-07-27 21:46:04 +0000296 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
297 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298};
299}
300
301/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
302/// specified mask and type. Targets can specify exactly which masks they
303/// support and the code generator is tasked with not creating illegal masks.
304///
305/// Note that this will also return true for shuffles that are promoted to a
306/// different type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000307SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
309 default: return 0;
310 case TargetLowering::Legal:
311 case TargetLowering::Custom:
312 break;
313 case TargetLowering::Promote: {
314 // If this is promoted to a different type, convert the shuffle mask and
315 // ask if it is legal in the promoted type!
Duncan Sands92c43912008-06-06 12:08:01 +0000316 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd3ace282008-07-21 10:20:31 +0000317 MVT EltVT = NVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318
319 // If we changed # elements, change the shuffle mask.
320 unsigned NumEltsGrowth =
Duncan Sands92c43912008-06-06 12:08:01 +0000321 NVT.getVectorNumElements() / VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
323 if (NumEltsGrowth > 1) {
324 // Renumber the elements.
Dan Gohman8181bd12008-07-27 21:46:04 +0000325 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000327 SDValue InOp = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
329 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd3ace282008-07-21 10:20:31 +0000330 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000332 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd3ace282008-07-21 10:20:31 +0000333 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 }
335 }
336 }
337 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
338 }
339 VT = NVT;
340 break;
341 }
342 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000343 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344}
345
346SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
347 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
348 ValueTypeActions(TLI.getValueTypeActions()) {
349 assert(MVT::LAST_VALUETYPE <= 32 &&
350 "Too many value types for ValueTypeActions to hold!");
351}
352
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353void SelectionDAGLegalize::LegalizeDAG() {
354 LastCALLSEQ_END = DAG.getEntryNode();
355 IsLegalizingCall = false;
356
357 // The legalize process is inherently a bottom-up recursive process (users
358 // legalize their uses before themselves). Given infinite stack space, we
359 // could just start legalizing on the root and traverse the whole graph. In
360 // practice however, this causes us to run out of stack space on large basic
361 // blocks. To avoid this problem, compute an ordering of the nodes where each
362 // node is only legalized after all of its operands are legalized.
Dan Gohman2d2a7a32008-09-30 18:30:35 +0000363 DAG.AssignTopologicalOrder();
364 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
365 E = prior(DAG.allnodes_end()); I != next(E); ++I)
366 HandleOp(SDValue(I, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367
368 // Finally, it's possible the root changed. Get the new root.
Dan Gohman8181bd12008-07-27 21:46:04 +0000369 SDValue OldRoot = DAG.getRoot();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
371 DAG.setRoot(LegalizedNodes[OldRoot]);
372
373 ExpandedNodes.clear();
374 LegalizedNodes.clear();
375 PromotedNodes.clear();
376 SplitNodes.clear();
377 ScalarizedNodes.clear();
Mon P Wang1448aad2008-10-30 08:01:45 +0000378 WidenNodes.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379
380 // Remove dead nodes now.
381 DAG.RemoveDeadNodes();
382}
383
384
385/// FindCallEndFromCallStart - Given a chained node that is part of a call
386/// sequence, find the CALLSEQ_END node that terminates the call sequence.
387static SDNode *FindCallEndFromCallStart(SDNode *Node) {
388 if (Node->getOpcode() == ISD::CALLSEQ_END)
389 return Node;
390 if (Node->use_empty())
391 return 0; // No CallSeqEnd
392
393 // The chain is usually at the end.
Dan Gohman8181bd12008-07-27 21:46:04 +0000394 SDValue TheChain(Node, Node->getNumValues()-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 if (TheChain.getValueType() != MVT::Other) {
396 // Sometimes it's at the beginning.
Dan Gohman8181bd12008-07-27 21:46:04 +0000397 TheChain = SDValue(Node, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 if (TheChain.getValueType() != MVT::Other) {
399 // Otherwise, hunt for it.
400 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
401 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000402 TheChain = SDValue(Node, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 break;
404 }
405
406 // Otherwise, we walked into a node without a chain.
407 if (TheChain.getValueType() != MVT::Other)
408 return 0;
409 }
410 }
411
412 for (SDNode::use_iterator UI = Node->use_begin(),
413 E = Node->use_end(); UI != E; ++UI) {
414
415 // Make sure to only follow users of our token chain.
Dan Gohman0c97f1d2008-07-27 20:43:25 +0000416 SDNode *User = *UI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
418 if (User->getOperand(i) == TheChain)
419 if (SDNode *Result = FindCallEndFromCallStart(User))
420 return Result;
421 }
422 return 0;
423}
424
425/// FindCallStartFromCallEnd - Given a chained node that is part of a call
426/// sequence, find the CALLSEQ_START node that initiates the call sequence.
427static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
428 assert(Node && "Didn't find callseq_start for a call??");
429 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
430
431 assert(Node->getOperand(0).getValueType() == MVT::Other &&
432 "Node doesn't have a token chain argument!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000433 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434}
435
436/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
437/// see if any uses can reach Dest. If no dest operands can get to dest,
438/// legalize them, legalize ourself, and return false, otherwise, return true.
439///
440/// Keep track of the nodes we fine that actually do lead to Dest in
441/// NodesLeadingTo. This avoids retraversing them exponential number of times.
442///
443bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
444 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
445 if (N == Dest) return true; // N certainly leads to Dest :)
446
447 // If we've already processed this node and it does lead to Dest, there is no
448 // need to reprocess it.
449 if (NodesLeadingTo.count(N)) return true;
450
451 // If the first result of this node has been already legalized, then it cannot
452 // reach N.
453 switch (getTypeAction(N->getValueType(0))) {
454 case Legal:
Dan Gohman8181bd12008-07-27 21:46:04 +0000455 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 break;
457 case Promote:
Dan Gohman8181bd12008-07-27 21:46:04 +0000458 if (PromotedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 break;
460 case Expand:
Dan Gohman8181bd12008-07-27 21:46:04 +0000461 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462 break;
463 }
464
465 // Okay, this node has not already been legalized. Check and legalize all
466 // operands. If none lead to Dest, then we can legalize this node.
467 bool OperandsLeadToDest = false;
468 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
469 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greif1c80d112008-08-28 21:40:38 +0000470 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471
472 if (OperandsLeadToDest) {
473 NodesLeadingTo.insert(N);
474 return true;
475 }
476
477 // Okay, this node looks safe, legalize it and return false.
Dan Gohman8181bd12008-07-27 21:46:04 +0000478 HandleOp(SDValue(N, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 return false;
480}
481
Mon P Wang1448aad2008-10-30 08:01:45 +0000482/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483/// appropriate for its type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000484void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000485 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 switch (getTypeAction(VT)) {
487 default: assert(0 && "Bad type action!");
488 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang1448aad2008-10-30 08:01:45 +0000489 case Promote:
490 if (!VT.isVector()) {
491 (void)PromoteOp(Op);
492 break;
493 }
494 else {
495 // See if we can widen otherwise use Expand to either scalarize or split
496 MVT WidenVT = TLI.getWidenVectorType(VT);
497 if (WidenVT != MVT::Other) {
498 (void) WidenVectorOp(Op, WidenVT);
499 break;
500 }
501 // else fall thru to expand since we can't widen the vector
502 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +0000504 if (!VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 // If this is an illegal scalar, expand it into its two component
506 // pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +0000507 SDValue X, Y;
Chris Lattnerdad577b2007-08-25 01:00:22 +0000508 if (Op.getOpcode() == ISD::TargetConstant)
509 break; // Allow illegal target nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 ExpandOp(Op, X, Y);
Duncan Sands92c43912008-06-06 12:08:01 +0000511 } else if (VT.getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 // If this is an illegal single element vector, convert it to a
513 // scalar operation.
514 (void)ScalarizeVectorOp(Op);
515 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +0000516 // This is an illegal multiple element vector.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 // Split it in half and legalize both parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000518 SDValue X, Y;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 SplitVectorOp(Op, X, Y);
520 }
521 break;
522 }
523}
524
525/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
526/// a load from the constant pool.
Dan Gohman8181bd12008-07-27 21:46:04 +0000527static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 SelectionDAG &DAG, TargetLowering &TLI) {
529 bool Extend = false;
530
531 // If a FP immediate is precise when represented as a float and if the
532 // target can do an extending load from float to double, we put it into
533 // the constant pool as a float, even if it's is statically typed as a
Chris Lattnere718cc52008-03-05 06:46:58 +0000534 // double. This shrinks FP constants and canonicalizes them for targets where
535 // an FP extending load is the same cost as a normal load (such as on the x87
536 // fp stack or PPC FP unit).
Duncan Sands92c43912008-06-06 12:08:01 +0000537 MVT VT = CFP->getValueType(0);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000538 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 if (!UseCP) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000540 if (VT!=MVT::f64 && VT!=MVT::f32)
541 assert(0 && "Invalid type expansion");
Dale Johannesen49cc7ce2008-10-09 18:53:47 +0000542 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Cheng354be062008-03-04 08:05:30 +0000543 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544 }
545
Duncan Sands92c43912008-06-06 12:08:01 +0000546 MVT OrigVT = VT;
547 MVT SVT = VT;
Evan Cheng354be062008-03-04 08:05:30 +0000548 while (SVT != MVT::f32) {
Duncan Sands92c43912008-06-06 12:08:01 +0000549 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Cheng354be062008-03-04 08:05:30 +0000550 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
551 // Only do this if the target has a native EXTLOAD instruction from
552 // smaller type.
Evan Cheng08c171a2008-10-14 21:26:46 +0000553 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattnere718cc52008-03-05 06:46:58 +0000554 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000555 const Type *SType = SVT.getTypeForMVT();
Evan Cheng354be062008-03-04 08:05:30 +0000556 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
557 VT = SVT;
558 Extend = true;
559 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 }
561
Dan Gohman8181bd12008-07-27 21:46:04 +0000562 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +0000563 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Cheng354be062008-03-04 08:05:30 +0000564 if (Extend)
565 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohmanfb020b62008-02-07 18:41:25 +0000566 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman04637d12008-09-16 22:05:41 +0000567 0, VT, false, Alignment);
Evan Cheng354be062008-03-04 08:05:30 +0000568 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +0000569 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570}
571
572
573/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
574/// operations.
575static
Dan Gohman8181bd12008-07-27 21:46:04 +0000576SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
577 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +0000578 MVT VT = Node->getValueType(0);
579 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
581 "fcopysign expansion only supported for f32 and f64");
Duncan Sands92c43912008-06-06 12:08:01 +0000582 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583
584 // First get the sign bit of second operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000585 SDValue Mask1 = (SrcVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
587 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
588 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000589 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
591 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands92c43912008-06-06 12:08:01 +0000592 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 if (SizeDiff > 0) {
594 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
595 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
596 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnere6fa1452008-07-10 23:46:13 +0000597 } else if (SizeDiff < 0) {
598 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
599 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
600 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
601 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602
603 // Clear the sign bit of first operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000604 SDValue Mask2 = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
606 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
607 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman8181bd12008-07-27 21:46:04 +0000608 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
610
611 // Or the value with the sign bit.
612 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
613 return Result;
614}
615
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000616/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
617static
Dan Gohman8181bd12008-07-27 21:46:04 +0000618SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
619 TargetLowering &TLI) {
620 SDValue Chain = ST->getChain();
621 SDValue Ptr = ST->getBasePtr();
622 SDValue Val = ST->getValue();
Duncan Sands92c43912008-06-06 12:08:01 +0000623 MVT VT = Val.getValueType();
Dale Johannesen08275382007-09-08 19:29:23 +0000624 int Alignment = ST->getAlignment();
625 int SVOffset = ST->getSrcValueOffset();
Duncan Sands92c43912008-06-06 12:08:01 +0000626 if (ST->getMemoryVT().isFloatingPoint() ||
627 ST->getMemoryVT().isVector()) {
Dale Johannesen08275382007-09-08 19:29:23 +0000628 // Expand to a bitconvert of the value to the integer type of the
629 // same size, then a (misaligned) int store.
Duncan Sands92c43912008-06-06 12:08:01 +0000630 MVT intVT;
631 if (VT.is128BitVector() || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesendc0ee192008-02-27 22:36:00 +0000632 intVT = MVT::i128;
Duncan Sands92c43912008-06-06 12:08:01 +0000633 else if (VT.is64BitVector() || VT==MVT::f64)
Dale Johannesen08275382007-09-08 19:29:23 +0000634 intVT = MVT::i64;
635 else if (VT==MVT::f32)
636 intVT = MVT::i32;
637 else
Dale Johannesenb1d1ab92008-02-28 18:36:51 +0000638 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen08275382007-09-08 19:29:23 +0000639
Dan Gohman8181bd12008-07-27 21:46:04 +0000640 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
Dale Johannesen08275382007-09-08 19:29:23 +0000641 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
642 SVOffset, ST->isVolatile(), Alignment);
643 }
Duncan Sands92c43912008-06-06 12:08:01 +0000644 assert(ST->getMemoryVT().isInteger() &&
645 !ST->getMemoryVT().isVector() &&
Dale Johannesen08275382007-09-08 19:29:23 +0000646 "Unaligned store of unknown type.");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000647 // Get the half-size VT
Duncan Sands92c43912008-06-06 12:08:01 +0000648 MVT NewStoredVT =
649 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
650 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000651 int IncrementSize = NumBits / 8;
652
653 // Divide the stored value in two parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000654 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
655 SDValue Lo = Val;
656 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000657
658 // Store the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000659 SDValue Store1, Store2;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000660 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
661 ST->getSrcValue(), SVOffset, NewStoredVT,
662 ST->isVolatile(), Alignment);
663 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
664 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsa3691432007-10-28 12:59:45 +0000665 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000666 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
667 ST->getSrcValue(), SVOffset + IncrementSize,
668 NewStoredVT, ST->isVolatile(), Alignment);
669
670 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
671}
672
673/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
674static
Dan Gohman8181bd12008-07-27 21:46:04 +0000675SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
676 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000677 int SVOffset = LD->getSrcValueOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +0000678 SDValue Chain = LD->getChain();
679 SDValue Ptr = LD->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +0000680 MVT VT = LD->getValueType(0);
681 MVT LoadedVT = LD->getMemoryVT();
682 if (VT.isFloatingPoint() || VT.isVector()) {
Dale Johannesen08275382007-09-08 19:29:23 +0000683 // Expand to a (misaligned) integer load of the same size,
Dale Johannesendc0ee192008-02-27 22:36:00 +0000684 // then bitconvert to floating point or vector.
Duncan Sands92c43912008-06-06 12:08:01 +0000685 MVT intVT;
686 if (LoadedVT.is128BitVector() ||
Dale Johannesenf8c1e852008-03-01 03:40:57 +0000687 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesendc0ee192008-02-27 22:36:00 +0000688 intVT = MVT::i128;
Duncan Sands92c43912008-06-06 12:08:01 +0000689 else if (LoadedVT.is64BitVector() || LoadedVT == MVT::f64)
Dale Johannesen08275382007-09-08 19:29:23 +0000690 intVT = MVT::i64;
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000691 else if (LoadedVT == MVT::f32)
Dale Johannesen08275382007-09-08 19:29:23 +0000692 intVT = MVT::i32;
693 else
Dale Johannesendc0ee192008-02-27 22:36:00 +0000694 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen08275382007-09-08 19:29:23 +0000695
Dan Gohman8181bd12008-07-27 21:46:04 +0000696 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
Dale Johannesen08275382007-09-08 19:29:23 +0000697 SVOffset, LD->isVolatile(),
698 LD->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +0000699 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Duncan Sands92c43912008-06-06 12:08:01 +0000700 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesen08275382007-09-08 19:29:23 +0000701 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
702
Dan Gohman8181bd12008-07-27 21:46:04 +0000703 SDValue Ops[] = { Result, Chain };
Duncan Sands698842f2008-07-02 17:40:58 +0000704 return DAG.getMergeValues(Ops, 2);
Dale Johannesen08275382007-09-08 19:29:23 +0000705 }
Duncan Sands92c43912008-06-06 12:08:01 +0000706 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000707 "Unaligned load of unsupported type.");
708
Dale Johannesendc0ee192008-02-27 22:36:00 +0000709 // Compute the new VT that is half the size of the old one. This is an
710 // integer MVT.
Duncan Sands92c43912008-06-06 12:08:01 +0000711 unsigned NumBits = LoadedVT.getSizeInBits();
712 MVT NewLoadedVT;
713 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000714 NumBits >>= 1;
715
716 unsigned Alignment = LD->getAlignment();
717 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000718 ISD::LoadExtType HiExtType = LD->getExtensionType();
719
720 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
721 if (HiExtType == ISD::NON_EXTLOAD)
722 HiExtType = ISD::ZEXTLOAD;
723
724 // Load the value in two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000725 SDValue Lo, Hi;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000726 if (TLI.isLittleEndian()) {
727 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
728 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
729 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
730 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
731 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
732 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000733 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000734 } else {
735 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
736 NewLoadedVT,LD->isVolatile(), Alignment);
737 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
738 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
739 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
740 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000741 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000742 }
743
744 // aggregate the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000745 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
746 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000747 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
748
Dan Gohman8181bd12008-07-27 21:46:04 +0000749 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000750 Hi.getValue(1));
751
Dan Gohman8181bd12008-07-27 21:46:04 +0000752 SDValue Ops[] = { Result, TF };
Duncan Sands698842f2008-07-02 17:40:58 +0000753 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000754}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000755
Dan Gohman6d05cac2007-10-11 23:57:53 +0000756/// UnrollVectorOp - We know that the given vector has a legal type, however
757/// the operation it performs is not legal and is an operation that we have
758/// no way of lowering. "Unroll" the vector, splitting out the scalars and
759/// operating on each element individually.
Dan Gohman8181bd12008-07-27 21:46:04 +0000760SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000761 MVT VT = Op.getValueType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000762 assert(isTypeLegal(VT) &&
763 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000764 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman6d05cac2007-10-11 23:57:53 +0000765 "Can't unroll a vector with multiple results!");
Duncan Sands92c43912008-06-06 12:08:01 +0000766 unsigned NE = VT.getVectorNumElements();
767 MVT EltVT = VT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000768
Dan Gohman8181bd12008-07-27 21:46:04 +0000769 SmallVector<SDValue, 8> Scalars;
770 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman6d05cac2007-10-11 23:57:53 +0000771 for (unsigned i = 0; i != NE; ++i) {
772 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000773 SDValue Operand = Op.getOperand(j);
Duncan Sands92c43912008-06-06 12:08:01 +0000774 MVT OperandVT = Operand.getValueType();
775 if (OperandVT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +0000776 // A vector operand; extract a single element.
Duncan Sands92c43912008-06-06 12:08:01 +0000777 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000778 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
779 OperandEltVT,
780 Operand,
781 DAG.getConstant(i, MVT::i32));
782 } else {
783 // A scalar operand; just use it as is.
784 Operands[j] = Operand;
785 }
786 }
787 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
788 &Operands[0], Operands.size()));
789 }
790
791 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
792}
793
Duncan Sands37a3f472008-01-10 10:28:30 +0000794/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands92c43912008-06-06 12:08:01 +0000795static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands37a3f472008-01-10 10:28:30 +0000796 RTLIB::Libcall Call_F32,
797 RTLIB::Libcall Call_F64,
798 RTLIB::Libcall Call_F80,
799 RTLIB::Libcall Call_PPCF128) {
800 return
801 VT == MVT::f32 ? Call_F32 :
802 VT == MVT::f64 ? Call_F64 :
803 VT == MVT::f80 ? Call_F80 :
804 VT == MVT::ppcf128 ? Call_PPCF128 :
805 RTLIB::UNKNOWN_LIBCALL;
806}
807
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000808/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
809/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
810/// is necessary to spill the vector being inserted into to memory, perform
811/// the insert there, and then read the result back.
Dan Gohman8181bd12008-07-27 21:46:04 +0000812SDValue SelectionDAGLegalize::
813PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
814 SDValue Tmp1 = Vec;
815 SDValue Tmp2 = Val;
816 SDValue Tmp3 = Idx;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000817
818 // If the target doesn't support this, we have to spill the input vector
819 // to a temporary stack slot, update the element, then reload it. This is
820 // badness. We could also load the value into a vector register (either
821 // with a "move to register" or "extload into register" instruction, then
822 // permute it into place, if the idx is a constant and if the idx is
823 // supported by the target.
Duncan Sands92c43912008-06-06 12:08:01 +0000824 MVT VT = Tmp1.getValueType();
825 MVT EltVT = VT.getVectorElementType();
826 MVT IdxVT = Tmp3.getValueType();
827 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +0000828 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000829
Gabor Greif1c80d112008-08-28 21:40:38 +0000830 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000831
832 // Store the vector.
Dan Gohman8181bd12008-07-27 21:46:04 +0000833 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang1448aad2008-10-30 08:01:45 +0000834 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000835
836 // Truncate or zero extend offset to target pointer type.
Duncan Sandsec142ee2008-06-08 20:54:56 +0000837 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000838 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
839 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +0000840 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000841 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman8181bd12008-07-27 21:46:04 +0000842 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000843 // Store the scalar value.
844 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000845 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000846 // Load the updated vector.
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000847 return DAG.getLoad(VT, Ch, StackPtr,
848 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000849}
850
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851/// LegalizeOp - We know that the specified value has a legal type, and
852/// that its operands are legal. Now ensure that the operation itself
853/// is legal, recursively ensuring that the operands' operations remain
854/// legal.
Dan Gohman8181bd12008-07-27 21:46:04 +0000855SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattnerdad577b2007-08-25 01:00:22 +0000856 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
857 return Op;
858
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 assert(isTypeLegal(Op.getValueType()) &&
860 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000861 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862
863 // If this operation defines any values that cannot be represented in a
864 // register on this target, make sure to expand or promote them.
865 if (Node->getNumValues() > 1) {
866 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
867 if (getTypeAction(Node->getValueType(i)) != Legal) {
868 HandleOp(Op.getValue(i));
869 assert(LegalizedNodes.count(Op) &&
870 "Handling didn't add legal operands!");
871 return LegalizedNodes[Op];
872 }
873 }
874
875 // Note that LegalizeOp may be reentered even from single-use nodes, which
876 // means that we always must cache transformed nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +0000877 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 if (I != LegalizedNodes.end()) return I->second;
879
Dan Gohman8181bd12008-07-27 21:46:04 +0000880 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
881 SDValue Result = Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000882 bool isCustom = false;
883
884 switch (Node->getOpcode()) {
885 case ISD::FrameIndex:
886 case ISD::EntryToken:
887 case ISD::Register:
888 case ISD::BasicBlock:
889 case ISD::TargetFrameIndex:
890 case ISD::TargetJumpTable:
891 case ISD::TargetConstant:
892 case ISD::TargetConstantFP:
893 case ISD::TargetConstantPool:
894 case ISD::TargetGlobalAddress:
895 case ISD::TargetGlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +0000896 case ISD::TargetExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000897 case ISD::VALUETYPE:
898 case ISD::SRCVALUE:
Dan Gohman12a9c082008-02-06 22:27:42 +0000899 case ISD::MEMOPERAND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000900 case ISD::CONDCODE:
Duncan Sandsc93fae32008-03-21 09:14:45 +0000901 case ISD::ARG_FLAGS:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +0000903 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904 "This must be legal!");
905 break;
906 default:
907 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
908 // If this is a target node, legalize it by legalizing the operands then
909 // passing it through.
Dan Gohman8181bd12008-07-27 21:46:04 +0000910 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
912 Ops.push_back(LegalizeOp(Node->getOperand(i)));
913
914 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
915
916 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
917 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +0000918 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 }
920 // Otherwise this is an unhandled builtin node. splat.
921#ifndef NDEBUG
922 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
923#endif
924 assert(0 && "Do not know how to legalize this operator!");
925 abort();
926 case ISD::GLOBAL_OFFSET_TABLE:
927 case ISD::GlobalAddress:
928 case ISD::GlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +0000929 case ISD::ExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 case ISD::ConstantPool:
931 case ISD::JumpTable: // Nothing to do.
932 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
933 default: assert(0 && "This action is not supported yet!");
934 case TargetLowering::Custom:
935 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +0000936 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000937 // FALLTHROUGH if the target doesn't want to lower this op after all.
938 case TargetLowering::Legal:
939 break;
940 }
941 break;
942 case ISD::FRAMEADDR:
943 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000944 // The only option for these nodes is to custom lower them. If the target
945 // does not custom lower them, then return zero.
946 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +0000947 if (Tmp1.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948 Result = Tmp1;
949 else
950 Result = DAG.getConstant(0, TLI.getPointerTy());
951 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000952 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands92c43912008-06-06 12:08:01 +0000953 MVT VT = Node->getValueType(0);
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000954 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
955 default: assert(0 && "This action is not supported yet!");
956 case TargetLowering::Custom:
957 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +0000958 if (Result.getNode()) break;
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000959 // Fall Thru
960 case TargetLowering::Legal:
961 Result = DAG.getConstant(0, VT);
962 break;
963 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000964 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000965 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966 case ISD::EXCEPTIONADDR: {
967 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +0000968 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
970 default: assert(0 && "This action is not supported yet!");
971 case TargetLowering::Expand: {
972 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000973 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000974 }
975 break;
976 case TargetLowering::Custom:
977 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +0000978 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000979 // Fall Thru
980 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000981 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands698842f2008-07-02 17:40:58 +0000982 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983 break;
984 }
985 }
986 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000987 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000988
Gabor Greif1c80d112008-08-28 21:40:38 +0000989 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000990 "Cannot return more than two values!");
991
992 // Since we produced two values, make sure to remember that we
993 // legalized both of them.
994 Tmp1 = LegalizeOp(Result);
995 Tmp2 = LegalizeOp(Result.getValue(1));
996 AddLegalizedOperand(Op.getValue(0), Tmp1);
997 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +0000998 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999 case ISD::EHSELECTION: {
1000 Tmp1 = LegalizeOp(Node->getOperand(0));
1001 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00001002 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001003 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1004 default: assert(0 && "This action is not supported yet!");
1005 case TargetLowering::Expand: {
1006 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001007 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001008 }
1009 break;
1010 case TargetLowering::Custom:
1011 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001012 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013 // Fall Thru
1014 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001015 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands698842f2008-07-02 17:40:58 +00001016 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001017 break;
1018 }
1019 }
1020 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001021 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001022
Gabor Greif1c80d112008-08-28 21:40:38 +00001023 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001024 "Cannot return more than two values!");
1025
1026 // Since we produced two values, make sure to remember that we
1027 // legalized both of them.
1028 Tmp1 = LegalizeOp(Result);
1029 Tmp2 = LegalizeOp(Result.getValue(1));
1030 AddLegalizedOperand(Op.getValue(0), Tmp1);
1031 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001032 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001033 case ISD::EH_RETURN: {
Duncan Sands92c43912008-06-06 12:08:01 +00001034 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 // The only "good" option for this node is to custom lower it.
1036 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1037 default: assert(0 && "This action is not supported at all!");
1038 case TargetLowering::Custom:
1039 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001040 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001041 // Fall Thru
1042 case TargetLowering::Legal:
1043 // Target does not know, how to lower this, lower to noop
1044 Result = LegalizeOp(Node->getOperand(0));
1045 break;
1046 }
1047 }
1048 break;
1049 case ISD::AssertSext:
1050 case ISD::AssertZext:
1051 Tmp1 = LegalizeOp(Node->getOperand(0));
1052 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1053 break;
1054 case ISD::MERGE_VALUES:
1055 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif46bf5472008-08-26 22:36:50 +00001056 Result = Node->getOperand(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001057 break;
1058 case ISD::CopyFromReg:
1059 Tmp1 = LegalizeOp(Node->getOperand(0));
1060 Result = Op.getValue(0);
1061 if (Node->getNumValues() == 2) {
1062 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1063 } else {
1064 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
1065 if (Node->getNumOperands() == 3) {
1066 Tmp2 = LegalizeOp(Node->getOperand(2));
1067 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1068 } else {
1069 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1070 }
1071 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1072 }
1073 // Since CopyFromReg produces two values, make sure to remember that we
1074 // legalized both of them.
1075 AddLegalizedOperand(Op.getValue(0), Result);
1076 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001077 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078 case ISD::UNDEF: {
Duncan Sands92c43912008-06-06 12:08:01 +00001079 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001080 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
1081 default: assert(0 && "This action is not supported yet!");
1082 case TargetLowering::Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00001083 if (VT.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001084 Result = DAG.getConstant(0, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00001085 else if (VT.isFloatingPoint())
1086 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesen20b76352007-09-26 17:26:49 +00001087 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001088 else
1089 assert(0 && "Unknown value type!");
1090 break;
1091 case TargetLowering::Legal:
1092 break;
1093 }
1094 break;
1095 }
1096
1097 case ISD::INTRINSIC_W_CHAIN:
1098 case ISD::INTRINSIC_WO_CHAIN:
1099 case ISD::INTRINSIC_VOID: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001100 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001101 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1102 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1103 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1104
1105 // Allow the target to custom lower its intrinsics if it wants to.
1106 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1107 TargetLowering::Custom) {
1108 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001109 if (Tmp3.getNode()) Result = Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001110 }
1111
Gabor Greif1c80d112008-08-28 21:40:38 +00001112 if (Result.getNode()->getNumValues() == 1) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001113
1114 // Must have return value and chain result.
Gabor Greif1c80d112008-08-28 21:40:38 +00001115 assert(Result.getNode()->getNumValues() == 2 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001116 "Cannot return more than two values!");
1117
1118 // Since loads produce two values, make sure to remember that we
1119 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001120 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1121 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001122 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001123 }
1124
Dan Gohman472d12c2008-06-30 20:59:49 +00001125 case ISD::DBG_STOPPOINT:
1126 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001127 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1128
Dan Gohman472d12c2008-06-30 20:59:49 +00001129 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130 case TargetLowering::Promote:
1131 default: assert(0 && "This action is not supported yet!");
1132 case TargetLowering::Expand: {
1133 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1134 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001135 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001136
Dan Gohman472d12c2008-06-30 20:59:49 +00001137 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001138 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman472d12c2008-06-30 20:59:49 +00001139 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1140 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001141
Dan Gohman472d12c2008-06-30 20:59:49 +00001142 unsigned Line = DSP->getLine();
1143 unsigned Col = DSP->getColumn();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001144
1145 if (useDEBUG_LOC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001146 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Chengd6f57682008-07-08 20:06:39 +00001147 DAG.getConstant(Col, MVT::i32),
1148 DAG.getConstant(SrcFile, MVT::i32) };
1149 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001150 } else {
Evan Cheng69eda822008-02-01 02:05:57 +00001151 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001152 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001153 }
1154 } else {
1155 Result = Tmp1; // chain
1156 }
1157 break;
1158 }
Evan Chengd6f57682008-07-08 20:06:39 +00001159 case TargetLowering::Legal: {
1160 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1161 if (Action == Legal && Tmp1 == Node->getOperand(0))
1162 break;
1163
Dan Gohman8181bd12008-07-27 21:46:04 +00001164 SmallVector<SDValue, 8> Ops;
Evan Chengd6f57682008-07-08 20:06:39 +00001165 Ops.push_back(Tmp1);
1166 if (Action == Legal) {
1167 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1168 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1169 } else {
1170 // Otherwise promote them.
1171 Ops.push_back(PromoteOp(Node->getOperand(1)));
1172 Ops.push_back(PromoteOp(Node->getOperand(2)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001173 }
Evan Chengd6f57682008-07-08 20:06:39 +00001174 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1175 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1176 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001177 break;
1178 }
Evan Chengd6f57682008-07-08 20:06:39 +00001179 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001180 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001181
1182 case ISD::DECLARE:
1183 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1184 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1185 default: assert(0 && "This action is not supported yet!");
1186 case TargetLowering::Legal:
1187 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1188 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1189 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1190 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1191 break;
Chris Lattner203cd052008-02-28 05:53:40 +00001192 case TargetLowering::Expand:
1193 Result = LegalizeOp(Node->getOperand(0));
1194 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001195 }
1196 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001197
1198 case ISD::DEBUG_LOC:
1199 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1200 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1201 default: assert(0 && "This action is not supported yet!");
Evan Chengd6f57682008-07-08 20:06:39 +00001202 case TargetLowering::Legal: {
1203 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Chengd6f57682008-07-08 20:06:39 +00001205 if (Action == Legal && Tmp1 == Node->getOperand(0))
1206 break;
1207 if (Action == Legal) {
1208 Tmp2 = Node->getOperand(1);
1209 Tmp3 = Node->getOperand(2);
1210 Tmp4 = Node->getOperand(3);
1211 } else {
1212 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1213 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1214 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1215 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001216 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1217 break;
1218 }
Evan Chengd6f57682008-07-08 20:06:39 +00001219 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001220 break;
1221
Dan Gohmanfa607c92008-07-01 00:05:16 +00001222 case ISD::DBG_LABEL:
1223 case ISD::EH_LABEL:
1224 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1225 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001226 default: assert(0 && "This action is not supported yet!");
1227 case TargetLowering::Legal:
1228 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohmanfa607c92008-07-01 00:05:16 +00001229 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001230 break;
1231 case TargetLowering::Expand:
1232 Result = LegalizeOp(Node->getOperand(0));
1233 break;
1234 }
1235 break;
1236
Evan Chengd1d68072008-03-08 00:58:38 +00001237 case ISD::PREFETCH:
1238 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1239 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1240 default: assert(0 && "This action is not supported yet!");
1241 case TargetLowering::Legal:
1242 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1243 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1244 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1245 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1246 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1247 break;
1248 case TargetLowering::Expand:
1249 // It's a noop.
1250 Result = LegalizeOp(Node->getOperand(0));
1251 break;
1252 }
1253 break;
1254
Andrew Lenharth785610d2008-02-16 01:24:58 +00001255 case ISD::MEMBARRIER: {
1256 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001257 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1258 default: assert(0 && "This action is not supported yet!");
1259 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001260 SDValue Ops[6];
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001261 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands3ee041a2008-02-27 08:53:44 +00001262 for (int x = 1; x < 6; ++x) {
1263 Ops[x] = Node->getOperand(x);
1264 if (!isTypeLegal(Ops[x].getValueType()))
1265 Ops[x] = PromoteOp(Ops[x]);
1266 }
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001267 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1268 break;
1269 }
1270 case TargetLowering::Expand:
1271 //There is no libgcc call for this op
1272 Result = Node->getOperand(0); // Noop
1273 break;
1274 }
Andrew Lenharth785610d2008-02-16 01:24:58 +00001275 break;
1276 }
1277
Dale Johannesenbc187662008-08-28 02:44:49 +00001278 case ISD::ATOMIC_CMP_SWAP_8:
1279 case ISD::ATOMIC_CMP_SWAP_16:
1280 case ISD::ATOMIC_CMP_SWAP_32:
1281 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001282 unsigned int num_operands = 4;
1283 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001284 SDValue Ops[4];
Mon P Wang078a62d2008-05-05 19:05:59 +00001285 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001286 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang078a62d2008-05-05 19:05:59 +00001287 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1288
1289 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1290 default: assert(0 && "This action is not supported yet!");
1291 case TargetLowering::Custom:
1292 Result = TLI.LowerOperation(Result, DAG);
1293 break;
1294 case TargetLowering::Legal:
1295 break;
1296 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001297 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1298 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001299 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001300 }
Dale Johannesenbc187662008-08-28 02:44:49 +00001301 case ISD::ATOMIC_LOAD_ADD_8:
1302 case ISD::ATOMIC_LOAD_SUB_8:
1303 case ISD::ATOMIC_LOAD_AND_8:
1304 case ISD::ATOMIC_LOAD_OR_8:
1305 case ISD::ATOMIC_LOAD_XOR_8:
1306 case ISD::ATOMIC_LOAD_NAND_8:
1307 case ISD::ATOMIC_LOAD_MIN_8:
1308 case ISD::ATOMIC_LOAD_MAX_8:
1309 case ISD::ATOMIC_LOAD_UMIN_8:
1310 case ISD::ATOMIC_LOAD_UMAX_8:
1311 case ISD::ATOMIC_SWAP_8:
1312 case ISD::ATOMIC_LOAD_ADD_16:
1313 case ISD::ATOMIC_LOAD_SUB_16:
1314 case ISD::ATOMIC_LOAD_AND_16:
1315 case ISD::ATOMIC_LOAD_OR_16:
1316 case ISD::ATOMIC_LOAD_XOR_16:
1317 case ISD::ATOMIC_LOAD_NAND_16:
1318 case ISD::ATOMIC_LOAD_MIN_16:
1319 case ISD::ATOMIC_LOAD_MAX_16:
1320 case ISD::ATOMIC_LOAD_UMIN_16:
1321 case ISD::ATOMIC_LOAD_UMAX_16:
1322 case ISD::ATOMIC_SWAP_16:
1323 case ISD::ATOMIC_LOAD_ADD_32:
1324 case ISD::ATOMIC_LOAD_SUB_32:
1325 case ISD::ATOMIC_LOAD_AND_32:
1326 case ISD::ATOMIC_LOAD_OR_32:
1327 case ISD::ATOMIC_LOAD_XOR_32:
1328 case ISD::ATOMIC_LOAD_NAND_32:
1329 case ISD::ATOMIC_LOAD_MIN_32:
1330 case ISD::ATOMIC_LOAD_MAX_32:
1331 case ISD::ATOMIC_LOAD_UMIN_32:
1332 case ISD::ATOMIC_LOAD_UMAX_32:
1333 case ISD::ATOMIC_SWAP_32:
1334 case ISD::ATOMIC_LOAD_ADD_64:
1335 case ISD::ATOMIC_LOAD_SUB_64:
1336 case ISD::ATOMIC_LOAD_AND_64:
1337 case ISD::ATOMIC_LOAD_OR_64:
1338 case ISD::ATOMIC_LOAD_XOR_64:
1339 case ISD::ATOMIC_LOAD_NAND_64:
1340 case ISD::ATOMIC_LOAD_MIN_64:
1341 case ISD::ATOMIC_LOAD_MAX_64:
1342 case ISD::ATOMIC_LOAD_UMIN_64:
1343 case ISD::ATOMIC_LOAD_UMAX_64:
1344 case ISD::ATOMIC_SWAP_64: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001345 unsigned int num_operands = 3;
1346 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001347 SDValue Ops[3];
Mon P Wang078a62d2008-05-05 19:05:59 +00001348 for (unsigned int x = 0; x < num_operands; ++x)
1349 Ops[x] = LegalizeOp(Node->getOperand(x));
1350 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sandsac496a12008-07-04 11:47:58 +00001351
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001352 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001353 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001354 case TargetLowering::Custom:
1355 Result = TLI.LowerOperation(Result, DAG);
1356 break;
1357 case TargetLowering::Legal:
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001358 break;
1359 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001360 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1361 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001362 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001363 }
Scott Michelf2e2b702007-08-08 23:23:31 +00001364 case ISD::Constant: {
1365 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1366 unsigned opAction =
1367 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1368
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001369 // We know we don't need to expand constants here, constants only have one
1370 // value and we check that it is fine above.
1371
Scott Michelf2e2b702007-08-08 23:23:31 +00001372 if (opAction == TargetLowering::Custom) {
1373 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001374 if (Tmp1.getNode())
Scott Michelf2e2b702007-08-08 23:23:31 +00001375 Result = Tmp1;
1376 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001377 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001378 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379 case ISD::ConstantFP: {
1380 // Spill FP immediates to the constant pool if the target cannot directly
1381 // codegen them. Targets often have some immediate values that can be
1382 // efficiently generated into an FP register without a load. We explicitly
1383 // leave these constants as ConstantFP nodes for the target to deal with.
1384 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1385
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1387 default: assert(0 && "This action is not supported yet!");
Nate Begemane2ba64f2008-02-14 08:57:00 +00001388 case TargetLowering::Legal:
1389 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390 case TargetLowering::Custom:
1391 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001392 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393 Result = Tmp3;
1394 break;
1395 }
1396 // FALLTHROUGH
Nate Begemane2ba64f2008-02-14 08:57:00 +00001397 case TargetLowering::Expand: {
1398 // Check to see if this FP immediate is already legal.
1399 bool isLegal = false;
1400 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1401 E = TLI.legal_fpimm_end(); I != E; ++I) {
1402 if (CFP->isExactlyValue(*I)) {
1403 isLegal = true;
1404 break;
1405 }
1406 }
1407 // If this is a legal constant, turn it into a TargetConstantFP node.
1408 if (isLegal)
1409 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001410 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1411 }
Nate Begemane2ba64f2008-02-14 08:57:00 +00001412 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 break;
1414 }
1415 case ISD::TokenFactor:
1416 if (Node->getNumOperands() == 2) {
1417 Tmp1 = LegalizeOp(Node->getOperand(0));
1418 Tmp2 = LegalizeOp(Node->getOperand(1));
1419 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1420 } else if (Node->getNumOperands() == 3) {
1421 Tmp1 = LegalizeOp(Node->getOperand(0));
1422 Tmp2 = LegalizeOp(Node->getOperand(1));
1423 Tmp3 = LegalizeOp(Node->getOperand(2));
1424 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1425 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00001426 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001427 // Legalize the operands.
1428 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1429 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1430 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1431 }
1432 break;
1433
1434 case ISD::FORMAL_ARGUMENTS:
1435 case ISD::CALL:
1436 // The only option for this is to custom lower it.
1437 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001438 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesenac246272008-03-05 19:14:03 +00001439 // A call within a calling sequence must be legalized to something
1440 // other than the normal CALLSEQ_END. Violating this gets Legalize
1441 // into an infinite loop.
1442 assert ((!IsLegalizingCall ||
1443 Node->getOpcode() != ISD::CALL ||
Gabor Greif1c80d112008-08-28 21:40:38 +00001444 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesenac246272008-03-05 19:14:03 +00001445 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001446
1447 // The number of incoming and outgoing values should match; unless the final
1448 // outgoing value is a flag.
Gabor Greif1c80d112008-08-28 21:40:38 +00001449 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1450 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1451 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling22f8deb2007-11-13 00:44:25 +00001452 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001453 "Lowering call/formal_arguments produced unexpected # results!");
1454
1455 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1456 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greif1c80d112008-08-28 21:40:38 +00001457 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1458 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling22f8deb2007-11-13 00:44:25 +00001459 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001460 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00001461 if (Op.getResNo() == i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001462 Tmp2 = Tmp1;
Dan Gohman8181bd12008-07-27 21:46:04 +00001463 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001464 }
1465 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001466 case ISD::EXTRACT_SUBREG: {
1467 Tmp1 = LegalizeOp(Node->getOperand(0));
1468 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1469 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001470 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001471 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1472 }
1473 break;
1474 case ISD::INSERT_SUBREG: {
1475 Tmp1 = LegalizeOp(Node->getOperand(0));
1476 Tmp2 = LegalizeOp(Node->getOperand(1));
1477 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1478 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001479 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001480 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1481 }
1482 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001483 case ISD::BUILD_VECTOR:
1484 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1485 default: assert(0 && "This action is not supported yet!");
1486 case TargetLowering::Custom:
1487 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001488 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001489 Result = Tmp3;
1490 break;
1491 }
1492 // FALLTHROUGH
1493 case TargetLowering::Expand:
Gabor Greif1c80d112008-08-28 21:40:38 +00001494 Result = ExpandBUILD_VECTOR(Result.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001495 break;
1496 }
1497 break;
1498 case ISD::INSERT_VECTOR_ELT:
1499 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001500 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001501
1502 // The type of the value to insert may not be legal, even though the vector
1503 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1504 // here.
1505 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1506 default: assert(0 && "Cannot expand insert element operand");
1507 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1508 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang1448aad2008-10-30 08:01:45 +00001509 case Expand:
1510 // FIXME: An alternative would be to check to see if the target is not
1511 // going to custom lower this operation, we could bitcast to half elt
1512 // width and perform two inserts at that width, if that is legal.
1513 Tmp2 = Node->getOperand(1);
1514 break;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001515 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001516 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1517
1518 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1519 Node->getValueType(0))) {
1520 default: assert(0 && "This action is not supported yet!");
1521 case TargetLowering::Legal:
1522 break;
1523 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001524 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001525 if (Tmp4.getNode()) {
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001526 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001527 break;
1528 }
1529 // FALLTHROUGH
Mon P Wang1448aad2008-10-30 08:01:45 +00001530 case TargetLowering::Promote:
1531 // Fall thru for vector case
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001532 case TargetLowering::Expand: {
1533 // If the insert index is a constant, codegen this as a scalar_to_vector,
1534 // then a shuffle that inserts it into the right position in the vector.
1535 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001536 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1537 // match the element type of the vector being created.
1538 if (Tmp2.getValueType() ==
Duncan Sands92c43912008-06-06 12:08:01 +00001539 Op.getValueType().getVectorElementType()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001540 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001541 Tmp1.getValueType(), Tmp2);
1542
Duncan Sands92c43912008-06-06 12:08:01 +00001543 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1544 MVT ShufMaskVT =
1545 MVT::getIntVectorWithNumElements(NumElts);
1546 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001547
1548 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1549 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1550 // elt 0 of the RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00001551 SmallVector<SDValue, 8> ShufOps;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001552 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001553 if (i != InsertPos->getZExtValue())
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001554 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1555 else
1556 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1557 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001558 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001559 &ShufOps[0], ShufOps.size());
1560
1561 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1562 Tmp1, ScVec, ShufMask);
1563 Result = LegalizeOp(Result);
1564 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001565 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001566 }
Nate Begeman7c9e4b72008-04-25 18:07:40 +00001567 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001568 break;
1569 }
1570 }
1571 break;
1572 case ISD::SCALAR_TO_VECTOR:
1573 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1574 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1575 break;
1576 }
1577
1578 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1579 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1580 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1581 Node->getValueType(0))) {
1582 default: assert(0 && "This action is not supported yet!");
1583 case TargetLowering::Legal:
1584 break;
1585 case TargetLowering::Custom:
1586 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001587 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001588 Result = Tmp3;
1589 break;
1590 }
1591 // FALLTHROUGH
1592 case TargetLowering::Expand:
1593 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1594 break;
1595 }
1596 break;
1597 case ISD::VECTOR_SHUFFLE:
1598 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1599 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1600 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1601
1602 // Allow targets to custom lower the SHUFFLEs they support.
1603 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1604 default: assert(0 && "Unknown operation action!");
1605 case TargetLowering::Legal:
1606 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1607 "vector shuffle should not be created if not legal!");
1608 break;
1609 case TargetLowering::Custom:
1610 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001611 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001612 Result = Tmp3;
1613 break;
1614 }
1615 // FALLTHROUGH
1616 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00001617 MVT VT = Node->getValueType(0);
1618 MVT EltVT = VT.getVectorElementType();
1619 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00001620 SDValue Mask = Node->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001621 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00001622 SmallVector<SDValue,8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001623 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001624 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001625 if (Arg.getOpcode() == ISD::UNDEF) {
1626 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1627 } else {
1628 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001629 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001630 if (Idx < NumElems)
1631 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1632 DAG.getConstant(Idx, PtrVT)));
1633 else
1634 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1635 DAG.getConstant(Idx - NumElems, PtrVT)));
1636 }
1637 }
1638 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1639 break;
1640 }
1641 case TargetLowering::Promote: {
1642 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00001643 MVT OVT = Node->getValueType(0);
1644 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001645
1646 // Cast the two input vectors.
1647 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1648 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1649
1650 // Convert the shuffle mask to the right # elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00001651 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001652 assert(Tmp3.getNode() && "Shuffle not legal?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001653 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1654 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1655 break;
1656 }
1657 }
1658 break;
1659
1660 case ISD::EXTRACT_VECTOR_ELT:
1661 Tmp1 = Node->getOperand(0);
1662 Tmp2 = LegalizeOp(Node->getOperand(1));
1663 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1664 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1665 break;
1666
1667 case ISD::EXTRACT_SUBVECTOR:
1668 Tmp1 = Node->getOperand(0);
1669 Tmp2 = LegalizeOp(Node->getOperand(1));
1670 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1671 Result = ExpandEXTRACT_SUBVECTOR(Result);
1672 break;
1673
Mon P Wang1448aad2008-10-30 08:01:45 +00001674 case ISD::CONCAT_VECTORS: {
1675 // Use extract/insert/build vector for now. We might try to be
1676 // more clever later.
1677 MVT PtrVT = TLI.getPointerTy();
1678 SmallVector<SDValue, 8> Ops;
1679 unsigned NumOperands = Node->getNumOperands();
1680 for (unsigned i=0; i < NumOperands; ++i) {
1681 SDValue SubOp = Node->getOperand(i);
1682 MVT VVT = SubOp.getNode()->getValueType(0);
1683 MVT EltVT = VVT.getVectorElementType();
1684 unsigned NumSubElem = VVT.getVectorNumElements();
1685 for (unsigned j=0; j < NumSubElem; ++j) {
1686 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1687 DAG.getConstant(j, PtrVT)));
1688 }
1689 }
1690 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1691 &Ops[0], Ops.size()));
1692 }
1693
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001694 case ISD::CALLSEQ_START: {
1695 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1696
1697 // Recursively Legalize all of the inputs of the call end that do not lead
1698 // to this call start. This ensures that any libcalls that need be inserted
1699 // are inserted *before* the CALLSEQ_START.
1700 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1701 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greif1c80d112008-08-28 21:40:38 +00001702 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001703 NodesLeadingTo);
1704 }
1705
1706 // Now that we legalized all of the inputs (which may have inserted
1707 // libcalls) create the new CALLSEQ_START node.
1708 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1709
1710 // Merge in the last call, to ensure that this call start after the last
1711 // call ended.
1712 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1713 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1714 Tmp1 = LegalizeOp(Tmp1);
1715 }
1716
1717 // Do not try to legalize the target-specific arguments (#1+).
1718 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001719 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001720 Ops[0] = Tmp1;
1721 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1722 }
1723
1724 // Remember that the CALLSEQ_START is legalized.
1725 AddLegalizedOperand(Op.getValue(0), Result);
1726 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1727 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1728
1729 // Now that the callseq_start and all of the non-call nodes above this call
1730 // sequence have been legalized, legalize the call itself. During this
1731 // process, no libcalls can/will be inserted, guaranteeing that no calls
1732 // can overlap.
1733 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001734 // Note that we are selecting this call!
Dan Gohman8181bd12008-07-27 21:46:04 +00001735 LastCALLSEQ_END = SDValue(CallEnd, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001736 IsLegalizingCall = true;
1737
1738 // Legalize the call, starting from the CALLSEQ_END.
1739 LegalizeOp(LastCALLSEQ_END);
1740 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1741 return Result;
1742 }
1743 case ISD::CALLSEQ_END:
1744 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1745 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greif1c80d112008-08-28 21:40:38 +00001746 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001747 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1748 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001749 assert(I != LegalizedNodes.end() &&
1750 "Legalizing the call start should have legalized this node!");
1751 return I->second;
1752 }
1753
1754 // Otherwise, the call start has been legalized and everything is going
1755 // according to plan. Just legalize ourselves normally here.
1756 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1757 // Do not try to legalize the target-specific arguments (#1+), except for
1758 // an optional flag input.
1759 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1760 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001761 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001762 Ops[0] = Tmp1;
1763 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1764 }
1765 } else {
1766 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1767 if (Tmp1 != Node->getOperand(0) ||
1768 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001769 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001770 Ops[0] = Tmp1;
1771 Ops.back() = Tmp2;
1772 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1773 }
1774 }
1775 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1776 // This finishes up call legalization.
1777 IsLegalizingCall = false;
1778
1779 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001780 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001781 if (Node->getNumValues() == 2)
Dan Gohman8181bd12008-07-27 21:46:04 +00001782 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001783 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001784 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands92c43912008-06-06 12:08:01 +00001785 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001786 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1787 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1788 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1789 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1790
1791 Tmp1 = Result.getValue(0);
1792 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001793 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001794 default: assert(0 && "This action is not supported yet!");
1795 case TargetLowering::Expand: {
1796 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1797 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1798 " not tell us which reg is the stack pointer!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001799 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001800
1801 // Chain the dynamic stack allocation so that it doesn't modify the stack
1802 // pointer when other instructions are using the stack.
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001803 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling22f8deb2007-11-13 00:44:25 +00001804
Dan Gohman8181bd12008-07-27 21:46:04 +00001805 SDValue Size = Tmp2.getOperand(1);
1806 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Chenga448bc42007-08-16 23:50:06 +00001807 Chain = SP.getValue(1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001808 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Chenga448bc42007-08-16 23:50:06 +00001809 unsigned StackAlign =
1810 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1811 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001812 SP = DAG.getNode(ISD::AND, VT, SP,
1813 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001814 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling22f8deb2007-11-13 00:44:25 +00001815 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1816
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001817 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1818 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling22f8deb2007-11-13 00:44:25 +00001819
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001820 Tmp1 = LegalizeOp(Tmp1);
1821 Tmp2 = LegalizeOp(Tmp2);
1822 break;
1823 }
1824 case TargetLowering::Custom:
1825 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001826 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001827 Tmp1 = LegalizeOp(Tmp3);
1828 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1829 }
1830 break;
1831 case TargetLowering::Legal:
1832 break;
1833 }
1834 // Since this op produce two values, make sure to remember that we
1835 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001836 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1837 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001838 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001839 }
1840 case ISD::INLINEASM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001841 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001842 bool Changed = false;
1843 // Legalize all of the operands of the inline asm, in case they are nodes
1844 // that need to be expanded or something. Note we skip the asm string and
1845 // all of the TargetConstant flags.
Dan Gohman8181bd12008-07-27 21:46:04 +00001846 SDValue Op = LegalizeOp(Ops[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001847 Changed = Op != Ops[0];
1848 Ops[0] = Op;
1849
1850 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1851 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001852 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001853 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001854 SDValue Op = LegalizeOp(Ops[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001855 if (Op != Ops[i]) {
1856 Changed = true;
1857 Ops[i] = Op;
1858 }
1859 }
1860 }
1861
1862 if (HasInFlag) {
1863 Op = LegalizeOp(Ops.back());
1864 Changed |= Op != Ops.back();
1865 Ops.back() = Op;
1866 }
1867
1868 if (Changed)
1869 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1870
1871 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman8181bd12008-07-27 21:46:04 +00001872 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1873 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001874 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001875 }
1876 case ISD::BR:
1877 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1878 // Ensure that libcalls are emitted before a branch.
1879 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1880 Tmp1 = LegalizeOp(Tmp1);
1881 LastCALLSEQ_END = DAG.getEntryNode();
1882
1883 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1884 break;
1885 case ISD::BRIND:
1886 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1887 // Ensure that libcalls are emitted before a branch.
1888 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1889 Tmp1 = LegalizeOp(Tmp1);
1890 LastCALLSEQ_END = DAG.getEntryNode();
1891
1892 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1893 default: assert(0 && "Indirect target must be legal type (pointer)!");
1894 case Legal:
1895 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1896 break;
1897 }
1898 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1899 break;
1900 case ISD::BR_JT:
1901 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1902 // Ensure that libcalls are emitted before a branch.
1903 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1904 Tmp1 = LegalizeOp(Tmp1);
1905 LastCALLSEQ_END = DAG.getEntryNode();
1906
1907 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1908 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1909
1910 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1911 default: assert(0 && "This action is not supported yet!");
1912 case TargetLowering::Legal: break;
1913 case TargetLowering::Custom:
1914 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001915 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001916 break;
1917 case TargetLowering::Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001918 SDValue Chain = Result.getOperand(0);
1919 SDValue Table = Result.getOperand(1);
1920 SDValue Index = Result.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001921
Duncan Sands92c43912008-06-06 12:08:01 +00001922 MVT PTy = TLI.getPointerTy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001923 MachineFunction &MF = DAG.getMachineFunction();
1924 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
1925 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman8181bd12008-07-27 21:46:04 +00001926 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001927
Dan Gohman8181bd12008-07-27 21:46:04 +00001928 SDValue LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001929 switch (EntrySize) {
1930 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001931 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001932 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001933 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001934 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001935 }
1936
Evan Cheng6fb06762007-11-09 01:32:10 +00001937 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001938 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1939 // For PIC, the sequence is:
1940 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00001941 // RelocBase can be JumpTable, GOT or some sort of global base.
1942 if (PTy != MVT::i32)
1943 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1944 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1945 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001946 }
Evan Cheng6fb06762007-11-09 01:32:10 +00001947 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001948 }
1949 }
1950 break;
1951 case ISD::BRCOND:
1952 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1953 // Ensure that libcalls are emitted before a return.
1954 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1955 Tmp1 = LegalizeOp(Tmp1);
1956 LastCALLSEQ_END = DAG.getEntryNode();
1957
1958 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1959 case Expand: assert(0 && "It's impossible to expand bools");
1960 case Legal:
1961 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1962 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00001963 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001964 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1965
1966 // The top bits of the promoted condition are not necessarily zero, ensure
1967 // that the value is properly zero extended.
Dan Gohman07961cd2008-02-25 21:11:39 +00001968 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001969 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman07961cd2008-02-25 21:11:39 +00001970 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001971 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1972 break;
1973 }
Dan Gohman07961cd2008-02-25 21:11:39 +00001974 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001975
1976 // Basic block destination (Op#2) is always legal.
1977 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1978
1979 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1980 default: assert(0 && "This action is not supported yet!");
1981 case TargetLowering::Legal: break;
1982 case TargetLowering::Custom:
1983 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001984 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001985 break;
1986 case TargetLowering::Expand:
1987 // Expand brcond's setcc into its constituent parts and create a BR_CC
1988 // Node.
1989 if (Tmp2.getOpcode() == ISD::SETCC) {
1990 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1991 Tmp2.getOperand(0), Tmp2.getOperand(1),
1992 Node->getOperand(2));
1993 } else {
1994 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1995 DAG.getCondCode(ISD::SETNE), Tmp2,
1996 DAG.getConstant(0, Tmp2.getValueType()),
1997 Node->getOperand(2));
1998 }
1999 break;
2000 }
2001 break;
2002 case ISD::BR_CC:
2003 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2004 // Ensure that libcalls are emitted before a branch.
2005 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2006 Tmp1 = LegalizeOp(Tmp1);
2007 Tmp2 = Node->getOperand(2); // LHS
2008 Tmp3 = Node->getOperand(3); // RHS
2009 Tmp4 = Node->getOperand(1); // CC
2010
Dale Johannesen32100b22008-11-07 22:54:33 +00002011 LegalizeSetCC(TLI.getSetCCResultType(Tmp2), Tmp2, Tmp3, Tmp4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002012 LastCALLSEQ_END = DAG.getEntryNode();
2013
Evan Cheng71343822008-10-15 02:05:31 +00002014 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002015 // the LHS is a legal SETCC itself. In this case, we need to compare
2016 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00002017 if (Tmp3.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002018 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2019 Tmp4 = DAG.getCondCode(ISD::SETNE);
2020 }
2021
2022 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2023 Node->getOperand(4));
2024
2025 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2026 default: assert(0 && "Unexpected action for BR_CC!");
2027 case TargetLowering::Legal: break;
2028 case TargetLowering::Custom:
2029 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002030 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002031 break;
2032 }
2033 break;
2034 case ISD::LOAD: {
2035 LoadSDNode *LD = cast<LoadSDNode>(Node);
2036 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2037 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
2038
2039 ISD::LoadExtType ExtType = LD->getExtensionType();
2040 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands92c43912008-06-06 12:08:01 +00002041 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002042 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2043 Tmp3 = Result.getValue(0);
2044 Tmp4 = Result.getValue(1);
2045
2046 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2047 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002048 case TargetLowering::Legal:
2049 // If this is an unaligned load and the target doesn't support it,
2050 // expand it.
2051 if (!TLI.allowsUnalignedMemoryAccesses()) {
2052 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002053 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002054 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002055 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002056 TLI);
2057 Tmp3 = Result.getOperand(0);
2058 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00002059 Tmp3 = LegalizeOp(Tmp3);
2060 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002061 }
2062 }
2063 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002064 case TargetLowering::Custom:
2065 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002066 if (Tmp1.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002067 Tmp3 = LegalizeOp(Tmp1);
2068 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2069 }
2070 break;
2071 case TargetLowering::Promote: {
2072 // Only promote a load of vector type to another.
Duncan Sands92c43912008-06-06 12:08:01 +00002073 assert(VT.isVector() && "Cannot promote this load!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002074 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002075 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002076
2077 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
2078 LD->getSrcValueOffset(),
2079 LD->isVolatile(), LD->getAlignment());
2080 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2081 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2082 break;
2083 }
2084 }
2085 // Since loads produce two values, make sure to remember that we
2086 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002087 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2088 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif46bf5472008-08-26 22:36:50 +00002089 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002090 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00002091 MVT SrcVT = LD->getMemoryVT();
2092 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sands082524c2008-01-23 20:39:46 +00002093 int SVOffset = LD->getSrcValueOffset();
2094 unsigned Alignment = LD->getAlignment();
2095 bool isVolatile = LD->isVolatile();
2096
Duncan Sands92c43912008-06-06 12:08:01 +00002097 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002098 // Some targets pretend to have an i1 loading operation, and actually
2099 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2100 // bits are guaranteed to be zero; it helps the optimizers understand
2101 // that these bits are zero. It is also useful for EXTLOAD, since it
2102 // tells the optimizers that those bits are undefined. It would be
2103 // nice to have an effective generic way of getting these benefits...
2104 // Until such a way is found, don't insist on promoting i1 here.
2105 (SrcVT != MVT::i1 ||
Evan Cheng08c171a2008-10-14 21:26:46 +00002106 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002107 // Promote to a byte-sized load if not loading an integral number of
2108 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands92c43912008-06-06 12:08:01 +00002109 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2110 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002111 SDValue Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002112
2113 // The extra bits are guaranteed to be zero, since we stored them that
2114 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2115
2116 ISD::LoadExtType NewExtType =
2117 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2118
2119 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2120 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2121 NVT, isVolatile, Alignment);
2122
2123 Ch = Result.getValue(1); // The chain.
2124
2125 if (ExtType == ISD::SEXTLOAD)
2126 // Having the top bits zero doesn't help when sign extending.
2127 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2128 Result, DAG.getValueType(SrcVT));
2129 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2130 // All the top bits are guaranteed to be zero - inform the optimizers.
2131 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2132 DAG.getValueType(SrcVT));
2133
2134 Tmp1 = LegalizeOp(Result);
2135 Tmp2 = LegalizeOp(Ch);
2136 } else if (SrcWidth & (SrcWidth - 1)) {
2137 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands92c43912008-06-06 12:08:01 +00002138 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002139 "Unsupported extload!");
2140 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2141 assert(RoundWidth < SrcWidth);
2142 unsigned ExtraWidth = SrcWidth - RoundWidth;
2143 assert(ExtraWidth < RoundWidth);
2144 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2145 "Load size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002146 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2147 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002148 SDValue Lo, Hi, Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002149 unsigned IncrementSize;
2150
2151 if (TLI.isLittleEndian()) {
2152 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2153 // Load the bottom RoundWidth bits.
2154 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2155 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2156 Alignment);
2157
2158 // Load the remaining ExtraWidth bits.
2159 IncrementSize = RoundWidth / 8;
2160 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2161 DAG.getIntPtrConstant(IncrementSize));
2162 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2163 LD->getSrcValue(), SVOffset + IncrementSize,
2164 ExtraVT, isVolatile,
2165 MinAlign(Alignment, IncrementSize));
2166
2167 // Build a factor node to remember that this load is independent of the
2168 // other one.
2169 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2170 Hi.getValue(1));
2171
2172 // Move the top bits to the right place.
2173 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2174 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2175
2176 // Join the hi and lo parts.
2177 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002178 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00002179 // Big endian - avoid unaligned loads.
2180 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2181 // Load the top RoundWidth bits.
2182 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2183 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2184 Alignment);
2185
2186 // Load the remaining ExtraWidth bits.
2187 IncrementSize = RoundWidth / 8;
2188 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2189 DAG.getIntPtrConstant(IncrementSize));
2190 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2191 LD->getSrcValue(), SVOffset + IncrementSize,
2192 ExtraVT, isVolatile,
2193 MinAlign(Alignment, IncrementSize));
2194
2195 // Build a factor node to remember that this load is independent of the
2196 // other one.
2197 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2198 Hi.getValue(1));
2199
2200 // Move the top bits to the right place.
2201 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2202 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2203
2204 // Join the hi and lo parts.
2205 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2206 }
2207
2208 Tmp1 = LegalizeOp(Result);
2209 Tmp2 = LegalizeOp(Ch);
2210 } else {
Evan Cheng08c171a2008-10-14 21:26:46 +00002211 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002212 default: assert(0 && "This action is not supported yet!");
2213 case TargetLowering::Custom:
2214 isCustom = true;
2215 // FALLTHROUGH
2216 case TargetLowering::Legal:
2217 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2218 Tmp1 = Result.getValue(0);
2219 Tmp2 = Result.getValue(1);
2220
2221 if (isCustom) {
2222 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002223 if (Tmp3.getNode()) {
Duncan Sands082524c2008-01-23 20:39:46 +00002224 Tmp1 = LegalizeOp(Tmp3);
2225 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2226 }
2227 } else {
2228 // If this is an unaligned load and the target doesn't support it,
2229 // expand it.
2230 if (!TLI.allowsUnalignedMemoryAccesses()) {
2231 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002232 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sands082524c2008-01-23 20:39:46 +00002233 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002234 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sands082524c2008-01-23 20:39:46 +00002235 TLI);
2236 Tmp1 = Result.getOperand(0);
2237 Tmp2 = Result.getOperand(1);
2238 Tmp1 = LegalizeOp(Tmp1);
2239 Tmp2 = LegalizeOp(Tmp2);
2240 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002241 }
2242 }
Duncan Sands082524c2008-01-23 20:39:46 +00002243 break;
2244 case TargetLowering::Expand:
2245 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2246 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002247 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sands082524c2008-01-23 20:39:46 +00002248 LD->getSrcValueOffset(),
2249 LD->isVolatile(), LD->getAlignment());
2250 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2251 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2252 Tmp2 = LegalizeOp(Load.getValue(1));
2253 break;
2254 }
2255 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2256 // Turn the unsupported load into an EXTLOAD followed by an explicit
2257 // zero/sign extend inreg.
2258 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2259 Tmp1, Tmp2, LD->getSrcValue(),
2260 LD->getSrcValueOffset(), SrcVT,
2261 LD->isVolatile(), LD->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00002262 SDValue ValRes;
Duncan Sands082524c2008-01-23 20:39:46 +00002263 if (ExtType == ISD::SEXTLOAD)
2264 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2265 Result, DAG.getValueType(SrcVT));
2266 else
2267 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2268 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2269 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002270 break;
2271 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002272 }
Duncan Sands082524c2008-01-23 20:39:46 +00002273
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002274 // Since loads produce two values, make sure to remember that we legalized
2275 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002276 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2277 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002278 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002279 }
2280 }
2281 case ISD::EXTRACT_ELEMENT: {
Duncan Sands92c43912008-06-06 12:08:01 +00002282 MVT OpTy = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002283 switch (getTypeAction(OpTy)) {
2284 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2285 case Legal:
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002286 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002287 // 1 -> Hi
2288 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands92c43912008-06-06 12:08:01 +00002289 DAG.getConstant(OpTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002290 TLI.getShiftAmountTy()));
2291 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2292 } else {
2293 // 0 -> Lo
2294 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2295 Node->getOperand(0));
2296 }
2297 break;
2298 case Expand:
2299 // Get both the low and high parts.
2300 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002301 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002302 Result = Tmp2; // 1 -> Hi
2303 else
2304 Result = Tmp1; // 0 -> Lo
2305 break;
2306 }
2307 break;
2308 }
2309
2310 case ISD::CopyToReg:
2311 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2312
2313 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2314 "Register type must be legal!");
2315 // Legalize the incoming value (must be a legal type).
2316 Tmp2 = LegalizeOp(Node->getOperand(2));
2317 if (Node->getNumValues() == 1) {
2318 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2319 } else {
2320 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2321 if (Node->getNumOperands() == 4) {
2322 Tmp3 = LegalizeOp(Node->getOperand(3));
2323 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2324 Tmp3);
2325 } else {
2326 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2327 }
2328
2329 // Since this produces two values, make sure to remember that we legalized
2330 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002331 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2332 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002333 return Result;
2334 }
2335 break;
2336
2337 case ISD::RET:
2338 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2339
2340 // Ensure that libcalls are emitted before a return.
2341 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2342 Tmp1 = LegalizeOp(Tmp1);
2343 LastCALLSEQ_END = DAG.getEntryNode();
2344
2345 switch (Node->getNumOperands()) {
2346 case 3: // ret val
2347 Tmp2 = Node->getOperand(1);
2348 Tmp3 = Node->getOperand(2); // Signness
2349 switch (getTypeAction(Tmp2.getValueType())) {
2350 case Legal:
2351 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2352 break;
2353 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00002354 if (!Tmp2.getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002355 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002356 ExpandOp(Tmp2, Lo, Hi);
2357
2358 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002359 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002360 std::swap(Lo, Hi);
2361
Gabor Greif1c80d112008-08-28 21:40:38 +00002362 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002363 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2364 else
2365 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2366 Result = LegalizeOp(Result);
2367 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +00002368 SDNode *InVal = Tmp2.getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002369 int InIx = Tmp2.getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002370 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2371 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002372
2373 // Figure out if there is a simple type corresponding to this Vector
2374 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002375 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002376 if (TLI.isTypeLegal(TVT)) {
2377 // Turn this into a return of the vector type.
2378 Tmp2 = LegalizeOp(Tmp2);
2379 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2380 } else if (NumElems == 1) {
2381 // Turn this into a return of the scalar type.
2382 Tmp2 = ScalarizeVectorOp(Tmp2);
2383 Tmp2 = LegalizeOp(Tmp2);
2384 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2385
2386 // FIXME: Returns of gcc generic vectors smaller than a legal type
2387 // should be returned in integer registers!
2388
2389 // The scalarized value type may not be legal, e.g. it might require
2390 // promotion or expansion. Relegalize the return.
2391 Result = LegalizeOp(Result);
2392 } else {
2393 // FIXME: Returns of gcc generic vectors larger than a legal vector
2394 // type should be returned by reference!
Dan Gohman8181bd12008-07-27 21:46:04 +00002395 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002396 SplitVectorOp(Tmp2, Lo, Hi);
2397 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2398 Result = LegalizeOp(Result);
2399 }
2400 }
2401 break;
2402 case Promote:
2403 Tmp2 = PromoteOp(Node->getOperand(1));
2404 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2405 Result = LegalizeOp(Result);
2406 break;
2407 }
2408 break;
2409 case 1: // ret void
2410 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2411 break;
2412 default: { // ret <values>
Dan Gohman8181bd12008-07-27 21:46:04 +00002413 SmallVector<SDValue, 8> NewValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002414 NewValues.push_back(Tmp1);
2415 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2416 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2417 case Legal:
2418 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2419 NewValues.push_back(Node->getOperand(i+1));
2420 break;
2421 case Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002422 SDValue Lo, Hi;
Duncan Sands92c43912008-06-06 12:08:01 +00002423 assert(!Node->getOperand(i).getValueType().isExtended() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002424 "FIXME: TODO: implement returning non-legal vector types!");
2425 ExpandOp(Node->getOperand(i), Lo, Hi);
2426 NewValues.push_back(Lo);
2427 NewValues.push_back(Node->getOperand(i+1));
Gabor Greif1c80d112008-08-28 21:40:38 +00002428 if (Hi.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002429 NewValues.push_back(Hi);
2430 NewValues.push_back(Node->getOperand(i+1));
2431 }
2432 break;
2433 }
2434 case Promote:
2435 assert(0 && "Can't promote multiple return value yet!");
2436 }
2437
2438 if (NewValues.size() == Node->getNumOperands())
2439 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2440 else
2441 Result = DAG.getNode(ISD::RET, MVT::Other,
2442 &NewValues[0], NewValues.size());
2443 break;
2444 }
2445 }
2446
2447 if (Result.getOpcode() == ISD::RET) {
2448 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2449 default: assert(0 && "This action is not supported yet!");
2450 case TargetLowering::Legal: break;
2451 case TargetLowering::Custom:
2452 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002453 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002454 break;
2455 }
2456 }
2457 break;
2458 case ISD::STORE: {
2459 StoreSDNode *ST = cast<StoreSDNode>(Node);
2460 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2461 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2462 int SVOffset = ST->getSrcValueOffset();
2463 unsigned Alignment = ST->getAlignment();
2464 bool isVolatile = ST->isVolatile();
2465
2466 if (!ST->isTruncatingStore()) {
2467 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2468 // FIXME: We shouldn't do this for TargetConstantFP's.
2469 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2470 // to phase ordering between legalized code and the dag combiner. This
2471 // probably means that we need to integrate dag combiner and legalizer
2472 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002473 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002474 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002475 if (CFP->getValueType(0) == MVT::f32 &&
2476 getTypeAction(MVT::i32) == Legal) {
Dan Gohman39509762008-03-11 00:11:06 +00002477 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002478 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen1616e902007-09-11 18:32:33 +00002479 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002480 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2481 SVOffset, isVolatile, Alignment);
2482 break;
2483 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002484 // If this target supports 64-bit registers, do a single 64-bit store.
2485 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002486 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman39509762008-03-11 00:11:06 +00002487 zextOrTrunc(64), MVT::i64);
Chris Lattner19f229a2007-10-15 05:46:06 +00002488 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2489 SVOffset, isVolatile, Alignment);
2490 break;
Duncan Sands2418bec2008-06-13 19:07:40 +00002491 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002492 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2493 // stores. If the target supports neither 32- nor 64-bits, this
2494 // xform is certainly not worth it.
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002495 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman8181bd12008-07-27 21:46:04 +00002496 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2497 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002498 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002499
2500 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2501 SVOffset, isVolatile, Alignment);
2502 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002503 DAG.getIntPtrConstant(4));
Chris Lattner19f229a2007-10-15 05:46:06 +00002504 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002505 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002506
2507 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2508 break;
2509 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002510 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002511 }
2512
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002513 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002514 case Legal: {
2515 Tmp3 = LegalizeOp(ST->getValue());
2516 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2517 ST->getOffset());
2518
Duncan Sands92c43912008-06-06 12:08:01 +00002519 MVT VT = Tmp3.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002520 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2521 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002522 case TargetLowering::Legal:
2523 // If this is an unaligned store and the target doesn't support it,
2524 // expand it.
2525 if (!TLI.allowsUnalignedMemoryAccesses()) {
2526 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002527 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002528 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002529 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002530 TLI);
2531 }
2532 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002533 case TargetLowering::Custom:
2534 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002535 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002536 break;
2537 case TargetLowering::Promote:
Duncan Sands92c43912008-06-06 12:08:01 +00002538 assert(VT.isVector() && "Unknown legal promote case!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002539 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2540 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2541 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2542 ST->getSrcValue(), SVOffset, isVolatile,
2543 Alignment);
2544 break;
2545 }
2546 break;
2547 }
2548 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002549 if (!ST->getMemoryVT().isVector()) {
2550 // Truncate the value and store the result.
2551 Tmp3 = PromoteOp(ST->getValue());
2552 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2553 SVOffset, ST->getMemoryVT(),
2554 isVolatile, Alignment);
2555 break;
2556 }
2557 // Fall thru to expand for vector
2558 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002559 unsigned IncrementSize = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002560 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002561
2562 // If this is a vector type, then we have to calculate the increment as
2563 // the product of the element size in bytes, and the number of elements
2564 // in the high half of the vector.
Duncan Sands92c43912008-06-06 12:08:01 +00002565 if (ST->getValue().getValueType().isVector()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002566 SDNode *InVal = ST->getValue().getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002567 int InIx = ST->getValue().getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002568 MVT InVT = InVal->getValueType(InIx);
2569 unsigned NumElems = InVT.getVectorNumElements();
2570 MVT EVT = InVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002571
2572 // Figure out if there is a simple type corresponding to this Vector
2573 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002574 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002575 if (TLI.isTypeLegal(TVT)) {
2576 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002577 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002578 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2579 SVOffset, isVolatile, Alignment);
2580 Result = LegalizeOp(Result);
2581 break;
2582 } else if (NumElems == 1) {
2583 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002584 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002585 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2586 SVOffset, isVolatile, Alignment);
2587 // The scalarized value type may not be legal, e.g. it might require
2588 // promotion or expansion. Relegalize the scalar store.
2589 Result = LegalizeOp(Result);
2590 break;
2591 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +00002592 // Check if we have widen this node with another value
2593 std::map<SDValue, SDValue>::iterator I =
2594 WidenNodes.find(ST->getValue());
2595 if (I != WidenNodes.end()) {
2596 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2597 break;
2598 }
2599 else {
2600 SplitVectorOp(ST->getValue(), Lo, Hi);
2601 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2602 EVT.getSizeInBits()/8;
2603 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002604 }
2605 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002606 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greif1c80d112008-08-28 21:40:38 +00002607 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002608
Richard Pennington73ae9e42008-09-25 16:15:10 +00002609 if (Hi.getNode() && TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002610 std::swap(Lo, Hi);
2611 }
2612
2613 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2614 SVOffset, isVolatile, Alignment);
2615
Gabor Greif1c80d112008-08-28 21:40:38 +00002616 if (Hi.getNode() == NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002617 // Must be int <-> float one-to-one expansion.
2618 Result = Lo;
2619 break;
2620 }
2621
2622 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002623 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002624 assert(isTypeLegal(Tmp2.getValueType()) &&
2625 "Pointers must be legal!");
2626 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002627 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002628 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2629 SVOffset, isVolatile, Alignment);
2630 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2631 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00002632 } // case Expand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002633 }
2634 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002635 switch (getTypeAction(ST->getValue().getValueType())) {
2636 case Legal:
2637 Tmp3 = LegalizeOp(ST->getValue());
2638 break;
2639 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002640 if (!ST->getValue().getValueType().isVector()) {
2641 // We can promote the value, the truncstore will still take care of it.
2642 Tmp3 = PromoteOp(ST->getValue());
2643 break;
2644 }
2645 // Vector case falls through to expand
Chris Lattner3bc08502008-01-17 19:59:44 +00002646 case Expand:
2647 // Just store the low part. This may become a non-trunc store, so make
2648 // sure to use getTruncStore, not UpdateNodeOperands below.
2649 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2650 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2651 SVOffset, MVT::i8, isVolatile, Alignment);
2652 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002653
Duncan Sands92c43912008-06-06 12:08:01 +00002654 MVT StVT = ST->getMemoryVT();
2655 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands40676662008-01-22 07:17:34 +00002656
Duncan Sands92c43912008-06-06 12:08:01 +00002657 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands40676662008-01-22 07:17:34 +00002658 // Promote to a byte-sized store with upper bits zero if not
2659 // storing an integral number of bytes. For example, promote
2660 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands92c43912008-06-06 12:08:01 +00002661 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands40676662008-01-22 07:17:34 +00002662 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2663 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2664 SVOffset, NVT, isVolatile, Alignment);
2665 } else if (StWidth & (StWidth - 1)) {
2666 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands92c43912008-06-06 12:08:01 +00002667 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands40676662008-01-22 07:17:34 +00002668 "Unsupported truncstore!");
2669 unsigned RoundWidth = 1 << Log2_32(StWidth);
2670 assert(RoundWidth < StWidth);
2671 unsigned ExtraWidth = StWidth - RoundWidth;
2672 assert(ExtraWidth < RoundWidth);
2673 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2674 "Store size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002675 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2676 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002677 SDValue Lo, Hi;
Duncan Sands40676662008-01-22 07:17:34 +00002678 unsigned IncrementSize;
2679
2680 if (TLI.isLittleEndian()) {
2681 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2682 // Store the bottom RoundWidth bits.
2683 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2684 SVOffset, RoundVT,
2685 isVolatile, Alignment);
2686
2687 // Store the remaining ExtraWidth bits.
2688 IncrementSize = RoundWidth / 8;
2689 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2690 DAG.getIntPtrConstant(IncrementSize));
2691 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2692 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2693 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2694 SVOffset + IncrementSize, ExtraVT, isVolatile,
2695 MinAlign(Alignment, IncrementSize));
2696 } else {
2697 // Big endian - avoid unaligned stores.
2698 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2699 // Store the top RoundWidth bits.
2700 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2701 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2702 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2703 RoundVT, isVolatile, Alignment);
2704
2705 // Store the remaining ExtraWidth bits.
2706 IncrementSize = RoundWidth / 8;
2707 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2708 DAG.getIntPtrConstant(IncrementSize));
2709 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2710 SVOffset + IncrementSize, ExtraVT, isVolatile,
2711 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002712 }
Duncan Sands40676662008-01-22 07:17:34 +00002713
2714 // The order of the stores doesn't matter.
2715 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2716 } else {
2717 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2718 Tmp2 != ST->getBasePtr())
2719 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2720 ST->getOffset());
2721
2722 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2723 default: assert(0 && "This action is not supported yet!");
2724 case TargetLowering::Legal:
2725 // If this is an unaligned store and the target doesn't support it,
2726 // expand it.
2727 if (!TLI.allowsUnalignedMemoryAccesses()) {
2728 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002729 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands40676662008-01-22 07:17:34 +00002730 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002731 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands40676662008-01-22 07:17:34 +00002732 TLI);
2733 }
2734 break;
2735 case TargetLowering::Custom:
2736 Result = TLI.LowerOperation(Result, DAG);
2737 break;
2738 case Expand:
2739 // TRUNCSTORE:i16 i32 -> STORE i16
2740 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2741 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2742 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2743 isVolatile, Alignment);
2744 break;
2745 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002746 }
2747 }
2748 break;
2749 }
2750 case ISD::PCMARKER:
2751 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2752 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2753 break;
2754 case ISD::STACKSAVE:
2755 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2756 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2757 Tmp1 = Result.getValue(0);
2758 Tmp2 = Result.getValue(1);
2759
2760 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2761 default: assert(0 && "This action is not supported yet!");
2762 case TargetLowering::Legal: break;
2763 case TargetLowering::Custom:
2764 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002765 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002766 Tmp1 = LegalizeOp(Tmp3);
2767 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2768 }
2769 break;
2770 case TargetLowering::Expand:
2771 // Expand to CopyFromReg if the target set
2772 // StackPointerRegisterToSaveRestore.
2773 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2774 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2775 Node->getValueType(0));
2776 Tmp2 = Tmp1.getValue(1);
2777 } else {
2778 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2779 Tmp2 = Node->getOperand(0);
2780 }
2781 break;
2782 }
2783
2784 // Since stacksave produce two values, make sure to remember that we
2785 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002786 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2787 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002788 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002789
2790 case ISD::STACKRESTORE:
2791 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2792 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2793 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2794
2795 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2796 default: assert(0 && "This action is not supported yet!");
2797 case TargetLowering::Legal: break;
2798 case TargetLowering::Custom:
2799 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002800 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002801 break;
2802 case TargetLowering::Expand:
2803 // Expand to CopyToReg if the target set
2804 // StackPointerRegisterToSaveRestore.
2805 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2806 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2807 } else {
2808 Result = Tmp1;
2809 }
2810 break;
2811 }
2812 break;
2813
2814 case ISD::READCYCLECOUNTER:
2815 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2816 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2817 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2818 Node->getValueType(0))) {
2819 default: assert(0 && "This action is not supported yet!");
2820 case TargetLowering::Legal:
2821 Tmp1 = Result.getValue(0);
2822 Tmp2 = Result.getValue(1);
2823 break;
2824 case TargetLowering::Custom:
2825 Result = TLI.LowerOperation(Result, DAG);
2826 Tmp1 = LegalizeOp(Result.getValue(0));
2827 Tmp2 = LegalizeOp(Result.getValue(1));
2828 break;
2829 }
2830
2831 // Since rdcc produce two values, make sure to remember that we legalized
2832 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002833 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2834 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835 return Result;
2836
2837 case ISD::SELECT:
2838 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2839 case Expand: assert(0 && "It's impossible to expand bools");
2840 case Legal:
2841 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2842 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002843 case Promote: {
Mon P Wang1448aad2008-10-30 08:01:45 +00002844 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002845 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2846 // Make sure the condition is either zero or one.
Dan Gohman07961cd2008-02-25 21:11:39 +00002847 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002848 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman07961cd2008-02-25 21:11:39 +00002849 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002850 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2851 break;
2852 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002853 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002854 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2855 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2856
2857 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2858
2859 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2860 default: assert(0 && "This action is not supported yet!");
2861 case TargetLowering::Legal: break;
2862 case TargetLowering::Custom: {
2863 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002864 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002865 break;
2866 }
2867 case TargetLowering::Expand:
2868 if (Tmp1.getOpcode() == ISD::SETCC) {
2869 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2870 Tmp2, Tmp3,
2871 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2872 } else {
2873 Result = DAG.getSelectCC(Tmp1,
2874 DAG.getConstant(0, Tmp1.getValueType()),
2875 Tmp2, Tmp3, ISD::SETNE);
2876 }
2877 break;
2878 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00002879 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002880 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2881 unsigned ExtOp, TruncOp;
Duncan Sands92c43912008-06-06 12:08:01 +00002882 if (Tmp2.getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002883 ExtOp = ISD::BIT_CONVERT;
2884 TruncOp = ISD::BIT_CONVERT;
Duncan Sands92c43912008-06-06 12:08:01 +00002885 } else if (Tmp2.getValueType().isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002886 ExtOp = ISD::ANY_EXTEND;
2887 TruncOp = ISD::TRUNCATE;
2888 } else {
2889 ExtOp = ISD::FP_EXTEND;
2890 TruncOp = ISD::FP_ROUND;
2891 }
2892 // Promote each of the values to the new type.
2893 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2894 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2895 // Perform the larger operation, then round down.
2896 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00002897 if (TruncOp != ISD::FP_ROUND)
2898 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2899 else
2900 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2901 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002902 break;
2903 }
2904 }
2905 break;
2906 case ISD::SELECT_CC: {
2907 Tmp1 = Node->getOperand(0); // LHS
2908 Tmp2 = Node->getOperand(1); // RHS
2909 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2910 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman8181bd12008-07-27 21:46:04 +00002911 SDValue CC = Node->getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002912
Dale Johannesen32100b22008-11-07 22:54:33 +00002913 LegalizeSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2, CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002914
Evan Cheng71343822008-10-15 02:05:31 +00002915 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002916 // the LHS is a legal SETCC itself. In this case, we need to compare
2917 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00002918 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002919 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2920 CC = DAG.getCondCode(ISD::SETNE);
2921 }
2922 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2923
2924 // Everything is legal, see if we should expand this op or something.
2925 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2926 default: assert(0 && "This action is not supported yet!");
2927 case TargetLowering::Legal: break;
2928 case TargetLowering::Custom:
2929 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002930 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002931 break;
2932 }
2933 break;
2934 }
2935 case ISD::SETCC:
2936 Tmp1 = Node->getOperand(0);
2937 Tmp2 = Node->getOperand(1);
2938 Tmp3 = Node->getOperand(2);
Evan Cheng71343822008-10-15 02:05:31 +00002939 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002940
2941 // If we had to Expand the SetCC operands into a SELECT node, then it may
2942 // not always be possible to return a true LHS & RHS. In this case, just
2943 // return the value we legalized, returned in the LHS
Gabor Greif1c80d112008-08-28 21:40:38 +00002944 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002945 Result = Tmp1;
2946 break;
2947 }
2948
2949 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
2950 default: assert(0 && "Cannot handle this action for SETCC yet!");
2951 case TargetLowering::Custom:
2952 isCustom = true;
2953 // FALLTHROUGH.
2954 case TargetLowering::Legal:
2955 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2956 if (isCustom) {
2957 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002958 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002959 }
2960 break;
2961 case TargetLowering::Promote: {
2962 // First step, figure out the appropriate operation to use.
2963 // Allow SETCC to not be supported for all legal data types
2964 // Mostly this targets FP
Duncan Sands92c43912008-06-06 12:08:01 +00002965 MVT NewInTy = Node->getOperand(0).getValueType();
2966 MVT OldVT = NewInTy; OldVT = OldVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002967
2968 // Scan for the appropriate larger type to use.
2969 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00002970 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002971
Duncan Sands92c43912008-06-06 12:08:01 +00002972 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002973 "Fell off of the edge of the integer world");
Duncan Sands92c43912008-06-06 12:08:01 +00002974 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002975 "Fell off of the edge of the floating point world");
2976
2977 // If the target supports SETCC of this type, use it.
2978 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
2979 break;
2980 }
Duncan Sands92c43912008-06-06 12:08:01 +00002981 if (NewInTy.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002982 assert(0 && "Cannot promote Legal Integer SETCC yet");
2983 else {
2984 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2985 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2986 }
2987 Tmp1 = LegalizeOp(Tmp1);
2988 Tmp2 = LegalizeOp(Tmp2);
2989 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2990 Result = LegalizeOp(Result);
2991 break;
2992 }
2993 case TargetLowering::Expand:
2994 // Expand a setcc node into a select_cc of the same condition, lhs, and
2995 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands92c43912008-06-06 12:08:01 +00002996 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002997 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2998 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
2999 Tmp3);
3000 break;
3001 }
3002 break;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003003 case ISD::VSETCC: {
3004 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3005 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman8181bd12008-07-27 21:46:04 +00003006 SDValue CC = Node->getOperand(2);
Nate Begeman9a1ce152008-05-12 19:40:03 +00003007
3008 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3009
3010 // Everything is legal, see if we should expand this op or something.
3011 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3012 default: assert(0 && "This action is not supported yet!");
3013 case TargetLowering::Legal: break;
3014 case TargetLowering::Custom:
3015 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003016 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003017 break;
3018 }
3019 break;
3020 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003021
3022 case ISD::SHL_PARTS:
3023 case ISD::SRA_PARTS:
3024 case ISD::SRL_PARTS: {
Dan Gohman8181bd12008-07-27 21:46:04 +00003025 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003026 bool Changed = false;
3027 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3028 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3029 Changed |= Ops.back() != Node->getOperand(i);
3030 }
3031 if (Changed)
3032 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
3033
3034 switch (TLI.getOperationAction(Node->getOpcode(),
3035 Node->getValueType(0))) {
3036 default: assert(0 && "This action is not supported yet!");
3037 case TargetLowering::Legal: break;
3038 case TargetLowering::Custom:
3039 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003040 if (Tmp1.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003041 SDValue Tmp2, RetVal(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003042 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
3043 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman8181bd12008-07-27 21:46:04 +00003044 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00003045 if (i == Op.getResNo())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003046 RetVal = Tmp2;
3047 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003048 assert(RetVal.getNode() && "Illegal result number");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003049 return RetVal;
3050 }
3051 break;
3052 }
3053
3054 // Since these produce multiple values, make sure to remember that we
3055 // legalized all of them.
3056 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman8181bd12008-07-27 21:46:04 +00003057 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00003058 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003059 }
3060
3061 // Binary operators
3062 case ISD::ADD:
3063 case ISD::SUB:
3064 case ISD::MUL:
3065 case ISD::MULHS:
3066 case ISD::MULHU:
3067 case ISD::UDIV:
3068 case ISD::SDIV:
3069 case ISD::AND:
3070 case ISD::OR:
3071 case ISD::XOR:
3072 case ISD::SHL:
3073 case ISD::SRL:
3074 case ISD::SRA:
3075 case ISD::FADD:
3076 case ISD::FSUB:
3077 case ISD::FMUL:
3078 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00003079 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003080 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3081 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3082 case Expand: assert(0 && "Not possible");
3083 case Legal:
3084 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3085 break;
3086 case Promote:
3087 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3088 break;
3089 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003090
3091 if ((Node->getOpcode() == ISD::SHL ||
3092 Node->getOpcode() == ISD::SRL ||
3093 Node->getOpcode() == ISD::SRA) &&
3094 !Node->getValueType(0).isVector()) {
3095 if (TLI.getShiftAmountTy().bitsLT(Tmp2.getValueType()))
3096 Tmp2 = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), Tmp2);
3097 else if (TLI.getShiftAmountTy().bitsGT(Tmp2.getValueType()))
3098 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Tmp2);
3099 }
3100
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003101 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangbff5d9c2008-11-10 04:46:22 +00003102
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003103 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3104 default: assert(0 && "BinOp legalize operation not supported");
3105 case TargetLowering::Legal: break;
3106 case TargetLowering::Custom:
3107 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003108 if (Tmp1.getNode()) {
Nate Begemanbb1ce942008-07-29 15:49:41 +00003109 Result = Tmp1;
3110 break;
Nate Begeman7569e762008-07-29 19:07:27 +00003111 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003112 // Fall through if the custom lower can't deal with the operation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003113 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00003114 MVT VT = Op.getValueType();
Mon P Wang1448aad2008-10-30 08:01:45 +00003115
Dan Gohman5a199552007-10-08 18:33:35 +00003116 // See if multiply or divide can be lowered using two-result operations.
3117 SDVTList VTs = DAG.getVTList(VT, VT);
3118 if (Node->getOpcode() == ISD::MUL) {
3119 // We just need the low half of the multiply; try both the signed
3120 // and unsigned forms. If the target supports both SMUL_LOHI and
3121 // UMUL_LOHI, form a preference by checking which forms of plain
3122 // MULH it supports.
3123 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3124 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3125 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3126 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3127 unsigned OpToUse = 0;
3128 if (HasSMUL_LOHI && !HasMULHS) {
3129 OpToUse = ISD::SMUL_LOHI;
3130 } else if (HasUMUL_LOHI && !HasMULHU) {
3131 OpToUse = ISD::UMUL_LOHI;
3132 } else if (HasSMUL_LOHI) {
3133 OpToUse = ISD::SMUL_LOHI;
3134 } else if (HasUMUL_LOHI) {
3135 OpToUse = ISD::UMUL_LOHI;
3136 }
3137 if (OpToUse) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003138 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003139 break;
3140 }
3141 }
3142 if (Node->getOpcode() == ISD::MULHS &&
3143 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003144 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3145 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003146 break;
3147 }
3148 if (Node->getOpcode() == ISD::MULHU &&
3149 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003150 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3151 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003152 break;
3153 }
3154 if (Node->getOpcode() == ISD::SDIV &&
3155 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003156 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3157 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003158 break;
3159 }
3160 if (Node->getOpcode() == ISD::UDIV &&
3161 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003162 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3163 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003164 break;
3165 }
Mon P Wang1448aad2008-10-30 08:01:45 +00003166
Dan Gohman6d05cac2007-10-11 23:57:53 +00003167 // Check to see if we have a libcall for this operator.
3168 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3169 bool isSigned = false;
3170 switch (Node->getOpcode()) {
3171 case ISD::UDIV:
3172 case ISD::SDIV:
3173 if (VT == MVT::i32) {
3174 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang1448aad2008-10-30 08:01:45 +00003175 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003176 isSigned = Node->getOpcode() == ISD::SDIV;
3177 }
3178 break;
Chris Lattner48188652008-10-04 21:27:46 +00003179 case ISD::MUL:
3180 if (VT == MVT::i32)
3181 LC = RTLIB::MUL_I32;
3182 break;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003183 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00003184 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3185 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003186 break;
3187 default: break;
3188 }
3189 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003190 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003191 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003192 break;
3193 }
Mon P Wang1448aad2008-10-30 08:01:45 +00003194
Duncan Sands92c43912008-06-06 12:08:01 +00003195 assert(Node->getValueType(0).isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003196 "Cannot expand this binary operator!");
3197 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003198 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003199 break;
3200 }
3201 case TargetLowering::Promote: {
3202 switch (Node->getOpcode()) {
3203 default: assert(0 && "Do not know how to promote this BinOp!");
3204 case ISD::AND:
3205 case ISD::OR:
3206 case ISD::XOR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003207 MVT OVT = Node->getValueType(0);
3208 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3209 assert(OVT.isVector() && "Cannot promote this BinOp!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003210 // Bit convert each of the values to the new type.
3211 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3212 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3213 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3214 // Bit convert the result back the original type.
3215 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3216 break;
3217 }
3218 }
3219 }
3220 }
3221 break;
3222
Dan Gohman475cd732007-10-05 14:17:22 +00003223 case ISD::SMUL_LOHI:
3224 case ISD::UMUL_LOHI:
3225 case ISD::SDIVREM:
3226 case ISD::UDIVREM:
3227 // These nodes will only be produced by target-specific lowering, so
3228 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003229 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003230 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003231
3232 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3233 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3234 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003235 break;
3236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003237 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3238 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3239 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3240 case Expand: assert(0 && "Not possible");
3241 case Legal:
3242 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3243 break;
3244 case Promote:
3245 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3246 break;
3247 }
3248
3249 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3250
3251 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3252 default: assert(0 && "Operation not supported");
3253 case TargetLowering::Custom:
3254 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003255 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003256 break;
3257 case TargetLowering::Legal: break;
3258 case TargetLowering::Expand: {
3259 // If this target supports fabs/fneg natively and select is cheap,
3260 // do this efficiently.
3261 if (!TLI.isSelectExpensive() &&
3262 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3263 TargetLowering::Legal &&
3264 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3265 TargetLowering::Legal) {
3266 // Get the sign bit of the RHS.
Duncan Sands92c43912008-06-06 12:08:01 +00003267 MVT IVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003268 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman8181bd12008-07-27 21:46:04 +00003269 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel502151f2008-03-10 15:42:14 +00003270 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003271 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3272 // Get the absolute value of the result.
Dan Gohman8181bd12008-07-27 21:46:04 +00003273 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003274 // Select between the nabs and abs value based on the sign bit of
3275 // the input.
3276 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3277 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3278 AbsVal),
3279 AbsVal);
3280 Result = LegalizeOp(Result);
3281 break;
3282 }
3283
3284 // Otherwise, do bitwise ops!
Duncan Sands92c43912008-06-06 12:08:01 +00003285 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3287 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3288 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3289 Result = LegalizeOp(Result);
3290 break;
3291 }
3292 }
3293 break;
3294
3295 case ISD::ADDC:
3296 case ISD::SUBC:
3297 Tmp1 = LegalizeOp(Node->getOperand(0));
3298 Tmp2 = LegalizeOp(Node->getOperand(1));
3299 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3300 // Since this produces two values, make sure to remember that we legalized
3301 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003302 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3303 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003304 return Result;
3305
3306 case ISD::ADDE:
3307 case ISD::SUBE:
3308 Tmp1 = LegalizeOp(Node->getOperand(0));
3309 Tmp2 = LegalizeOp(Node->getOperand(1));
3310 Tmp3 = LegalizeOp(Node->getOperand(2));
3311 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3312 // Since this produces two values, make sure to remember that we legalized
3313 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003314 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3315 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003316 return Result;
3317
3318 case ISD::BUILD_PAIR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003319 MVT PairTy = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003320 // TODO: handle the case where the Lo and Hi operands are not of legal type
3321 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3322 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3323 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3324 case TargetLowering::Promote:
3325 case TargetLowering::Custom:
3326 assert(0 && "Cannot promote/custom this yet!");
3327 case TargetLowering::Legal:
3328 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3329 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3330 break;
3331 case TargetLowering::Expand:
3332 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3333 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3334 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003335 DAG.getConstant(PairTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003336 TLI.getShiftAmountTy()));
3337 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3338 break;
3339 }
3340 break;
3341 }
3342
3343 case ISD::UREM:
3344 case ISD::SREM:
3345 case ISD::FREM:
3346 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3347 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3348
3349 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3350 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3351 case TargetLowering::Custom:
3352 isCustom = true;
3353 // FALLTHROUGH
3354 case TargetLowering::Legal:
3355 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3356 if (isCustom) {
3357 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003358 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003359 }
3360 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003361 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003362 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3363 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands92c43912008-06-06 12:08:01 +00003364 MVT VT = Node->getValueType(0);
Dan Gohman5a199552007-10-08 18:33:35 +00003365
3366 // See if remainder can be lowered using two-result operations.
3367 SDVTList VTs = DAG.getVTList(VT, VT);
3368 if (Node->getOpcode() == ISD::SREM &&
3369 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003370 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003371 break;
3372 }
3373 if (Node->getOpcode() == ISD::UREM &&
3374 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003375 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003376 break;
3377 }
3378
Duncan Sands92c43912008-06-06 12:08:01 +00003379 if (VT.isInteger()) {
Dan Gohman5a199552007-10-08 18:33:35 +00003380 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003381 TargetLowering::Legal) {
3382 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003383 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3384 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3385 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands92c43912008-06-06 12:08:01 +00003386 } else if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003387 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003388 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003389 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003390 "Cannot expand this binary operator!");
3391 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3392 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman8181bd12008-07-27 21:46:04 +00003393 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003394 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003395 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003396 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00003397 assert(VT.isFloatingPoint() &&
Dan Gohman59b4b102007-11-06 22:11:54 +00003398 "remainder op must have integer or floating-point type");
Duncan Sands92c43912008-06-06 12:08:01 +00003399 if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003400 Result = LegalizeOp(UnrollVectorOp(Op));
3401 } else {
3402 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003403 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3404 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003405 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003406 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003407 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003408 }
3409 break;
3410 }
Dan Gohman5a199552007-10-08 18:33:35 +00003411 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003412 break;
3413 case ISD::VAARG: {
3414 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3415 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3416
Duncan Sands92c43912008-06-06 12:08:01 +00003417 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003418 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3419 default: assert(0 && "This action is not supported yet!");
3420 case TargetLowering::Custom:
3421 isCustom = true;
3422 // FALLTHROUGH
3423 case TargetLowering::Legal:
3424 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3425 Result = Result.getValue(0);
3426 Tmp1 = Result.getValue(1);
3427
3428 if (isCustom) {
3429 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003430 if (Tmp2.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003431 Result = LegalizeOp(Tmp2);
3432 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3433 }
3434 }
3435 break;
3436 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003437 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003438 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003439 // Increment the pointer, VAList, to the next vaarg
Duncan Sands55a4c232008-11-03 11:51:11 +00003440 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3441 DAG.getConstant(TLI.getTargetData()->getABITypeSize(VT.getTypeForMVT()),
3442 TLI.getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003443 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00003444 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003445 // Load the actual argument out of the pointer VAList
3446 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3447 Tmp1 = LegalizeOp(Result.getValue(1));
3448 Result = LegalizeOp(Result);
3449 break;
3450 }
3451 }
3452 // Since VAARG produces two values, make sure to remember that we
3453 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003454 AddLegalizedOperand(SDValue(Node, 0), Result);
3455 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00003456 return Op.getResNo() ? Tmp1 : Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003457 }
3458
3459 case ISD::VACOPY:
3460 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3461 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3462 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3463
3464 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3465 default: assert(0 && "This action is not supported yet!");
3466 case TargetLowering::Custom:
3467 isCustom = true;
3468 // FALLTHROUGH
3469 case TargetLowering::Legal:
3470 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3471 Node->getOperand(3), Node->getOperand(4));
3472 if (isCustom) {
3473 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003474 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003475 }
3476 break;
3477 case TargetLowering::Expand:
3478 // This defaults to loading a pointer from the input and storing it to the
3479 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003480 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3481 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman6b9a08e2008-04-17 02:09:26 +00003482 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3483 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003484 break;
3485 }
3486 break;
3487
3488 case ISD::VAEND:
3489 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3490 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3491
3492 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3493 default: assert(0 && "This action is not supported yet!");
3494 case TargetLowering::Custom:
3495 isCustom = true;
3496 // FALLTHROUGH
3497 case TargetLowering::Legal:
3498 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3499 if (isCustom) {
3500 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003501 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003502 }
3503 break;
3504 case TargetLowering::Expand:
3505 Result = Tmp1; // Default to a no-op, return the chain
3506 break;
3507 }
3508 break;
3509
3510 case ISD::VASTART:
3511 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3512 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3513
3514 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3515
3516 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3517 default: assert(0 && "This action is not supported yet!");
3518 case TargetLowering::Legal: break;
3519 case TargetLowering::Custom:
3520 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003521 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003522 break;
3523 }
3524 break;
3525
3526 case ISD::ROTL:
3527 case ISD::ROTR:
3528 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3529 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3530 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3531 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3532 default:
3533 assert(0 && "ROTL/ROTR legalize operation not supported");
3534 break;
3535 case TargetLowering::Legal:
3536 break;
3537 case TargetLowering::Custom:
3538 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003539 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003540 break;
3541 case TargetLowering::Promote:
3542 assert(0 && "Do not know how to promote ROTL/ROTR");
3543 break;
3544 case TargetLowering::Expand:
3545 assert(0 && "Do not know how to expand ROTL/ROTR");
3546 break;
3547 }
3548 break;
3549
3550 case ISD::BSWAP:
3551 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3552 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3553 case TargetLowering::Custom:
3554 assert(0 && "Cannot custom legalize this yet!");
3555 case TargetLowering::Legal:
3556 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3557 break;
3558 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003559 MVT OVT = Tmp1.getValueType();
3560 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3561 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003562
3563 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3564 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3565 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3566 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3567 break;
3568 }
3569 case TargetLowering::Expand:
3570 Result = ExpandBSWAP(Tmp1);
3571 break;
3572 }
3573 break;
3574
3575 case ISD::CTPOP:
3576 case ISD::CTTZ:
3577 case ISD::CTLZ:
3578 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3579 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003580 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003581 case TargetLowering::Legal:
3582 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003583 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003584 TargetLowering::Custom) {
3585 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003586 if (Tmp1.getNode()) {
Scott Michelbc62b412007-08-02 02:22:46 +00003587 Result = Tmp1;
3588 }
Scott Michel48b63e62007-07-30 21:00:31 +00003589 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003590 break;
3591 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003592 MVT OVT = Tmp1.getValueType();
3593 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003594
3595 // Zero extend the argument.
3596 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3597 // Perform the larger operation, then subtract if needed.
3598 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3599 switch (Node->getOpcode()) {
3600 case ISD::CTPOP:
3601 Result = Tmp1;
3602 break;
3603 case ISD::CTTZ:
3604 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00003605 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003606 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003607 ISD::SETEQ);
3608 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003609 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003610 break;
3611 case ISD::CTLZ:
3612 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3613 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003614 DAG.getConstant(NVT.getSizeInBits() -
3615 OVT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003616 break;
3617 }
3618 break;
3619 }
3620 case TargetLowering::Expand:
3621 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3622 break;
3623 }
3624 break;
3625
3626 // Unary operators
3627 case ISD::FABS:
3628 case ISD::FNEG:
3629 case ISD::FSQRT:
3630 case ISD::FSIN:
3631 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003632 case ISD::FLOG:
3633 case ISD::FLOG2:
3634 case ISD::FLOG10:
3635 case ISD::FEXP:
3636 case ISD::FEXP2:
Dan Gohmanc8b20e22008-08-21 17:55:02 +00003637 case ISD::FTRUNC:
3638 case ISD::FFLOOR:
3639 case ISD::FCEIL:
3640 case ISD::FRINT:
3641 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003642 Tmp1 = LegalizeOp(Node->getOperand(0));
3643 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3644 case TargetLowering::Promote:
3645 case TargetLowering::Custom:
3646 isCustom = true;
3647 // FALLTHROUGH
3648 case TargetLowering::Legal:
3649 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3650 if (isCustom) {
3651 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003652 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003653 }
3654 break;
3655 case TargetLowering::Expand:
3656 switch (Node->getOpcode()) {
3657 default: assert(0 && "Unreachable!");
3658 case ISD::FNEG:
3659 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3660 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3661 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3662 break;
3663 case ISD::FABS: {
3664 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands92c43912008-06-06 12:08:01 +00003665 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003666 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel502151f2008-03-10 15:42:14 +00003667 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00003668 ISD::SETUGT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003669 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3670 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3671 break;
3672 }
Evan Cheng1fac6952008-09-09 23:35:53 +00003673 case ISD::FSQRT:
3674 case ISD::FSIN:
3675 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003676 case ISD::FLOG:
3677 case ISD::FLOG2:
3678 case ISD::FLOG10:
3679 case ISD::FEXP:
3680 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00003681 case ISD::FTRUNC:
3682 case ISD::FFLOOR:
3683 case ISD::FCEIL:
3684 case ISD::FRINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00003685 case ISD::FNEARBYINT: {
Duncan Sands92c43912008-06-06 12:08:01 +00003686 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003687
3688 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003689 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003690 Result = LegalizeOp(UnrollVectorOp(Op));
3691 break;
3692 }
3693
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003694 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3695 switch(Node->getOpcode()) {
3696 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003697 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3698 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003699 break;
3700 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003701 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3702 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003703 break;
3704 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003705 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3706 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003707 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00003708 case ISD::FLOG:
3709 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3710 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3711 break;
3712 case ISD::FLOG2:
3713 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3714 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3715 break;
3716 case ISD::FLOG10:
3717 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3718 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3719 break;
3720 case ISD::FEXP:
3721 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3722 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3723 break;
3724 case ISD::FEXP2:
3725 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3726 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3727 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00003728 case ISD::FTRUNC:
3729 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3730 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3731 break;
3732 case ISD::FFLOOR:
3733 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3734 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3735 break;
3736 case ISD::FCEIL:
3737 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3738 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3739 break;
3740 case ISD::FRINT:
3741 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3742 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3743 break;
3744 case ISD::FNEARBYINT:
3745 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3746 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3747 break;
Evan Cheng1fac6952008-09-09 23:35:53 +00003748 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003749 default: assert(0 && "Unreachable!");
3750 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003751 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003752 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003753 break;
3754 }
3755 }
3756 break;
3757 }
3758 break;
3759 case ISD::FPOWI: {
Duncan Sands92c43912008-06-06 12:08:01 +00003760 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003761
3762 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003763 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003764 Result = LegalizeOp(UnrollVectorOp(Op));
3765 break;
3766 }
3767
3768 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003769 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3770 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003771 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003772 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003773 break;
3774 }
3775 case ISD::BIT_CONVERT:
3776 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003777 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3778 Node->getValueType(0));
Duncan Sands92c43912008-06-06 12:08:01 +00003779 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003780 // The input has to be a vector type, we have to either scalarize it, pack
3781 // it, or convert it based on whether the input vector type is legal.
Gabor Greif1c80d112008-08-28 21:40:38 +00003782 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00003783 int InIx = Node->getOperand(0).getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00003784 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3785 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003786
3787 // Figure out if there is a simple type corresponding to this Vector
3788 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00003789 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003790 if (TLI.isTypeLegal(TVT)) {
3791 // Turn this into a bit convert of the vector input.
3792 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3793 LegalizeOp(Node->getOperand(0)));
3794 break;
3795 } else if (NumElems == 1) {
3796 // Turn this into a bit convert of the scalar input.
3797 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3798 ScalarizeVectorOp(Node->getOperand(0)));
3799 break;
3800 } else {
3801 // FIXME: UNIMP! Store then reload
3802 assert(0 && "Cast from unsupported vector type not implemented yet!");
3803 }
3804 } else {
3805 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3806 Node->getOperand(0).getValueType())) {
3807 default: assert(0 && "Unknown operation action!");
3808 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003809 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3810 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003811 break;
3812 case TargetLowering::Legal:
3813 Tmp1 = LegalizeOp(Node->getOperand(0));
3814 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3815 break;
3816 }
3817 }
3818 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003819 case ISD::CONVERT_RNDSAT: {
3820 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3821 switch (CvtCode) {
3822 default: assert(0 && "Unknown cvt code!");
3823 case ISD::CVT_SF:
3824 case ISD::CVT_UF:
3825 break;
3826 case ISD::CVT_FF:
3827 case ISD::CVT_FS:
3828 case ISD::CVT_FU:
3829 case ISD::CVT_SS:
3830 case ISD::CVT_SU:
3831 case ISD::CVT_US:
3832 case ISD::CVT_UU: {
3833 SDValue DTyOp = Node->getOperand(1);
3834 SDValue STyOp = Node->getOperand(2);
3835 SDValue RndOp = Node->getOperand(3);
3836 SDValue SatOp = Node->getOperand(4);
3837 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3838 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3839 case Legal:
3840 Tmp1 = LegalizeOp(Node->getOperand(0));
3841 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3842 RndOp, SatOp);
3843 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3844 TargetLowering::Custom) {
3845 Tmp1 = TLI.LowerOperation(Result, DAG);
3846 if (Tmp1.getNode()) Result = Tmp1;
3847 }
3848 break;
3849 case Promote:
3850 Result = PromoteOp(Node->getOperand(0));
3851 // For FP, make Op1 a i32
3852
3853 Result = DAG.getConvertRndSat(Result.getValueType(), Result,
3854 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3855 break;
3856 }
3857 break;
3858 }
3859 } // end switch CvtCode
3860 break;
3861 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003862 // Conversion operators. The source and destination have different types.
3863 case ISD::SINT_TO_FP:
3864 case ISD::UINT_TO_FP: {
3865 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman29c3cef2008-08-14 20:04:46 +00003866 Result = LegalizeINT_TO_FP(Result, isSigned,
3867 Node->getValueType(0), Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003868 break;
3869 }
3870 case ISD::TRUNCATE:
3871 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3872 case Legal:
3873 Tmp1 = LegalizeOp(Node->getOperand(0));
3874 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3875 break;
3876 case Expand:
3877 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3878
3879 // Since the result is legal, we should just be able to truncate the low
3880 // part of the source.
3881 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3882 break;
3883 case Promote:
3884 Result = PromoteOp(Node->getOperand(0));
3885 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3886 break;
3887 }
3888 break;
3889
3890 case ISD::FP_TO_SINT:
3891 case ISD::FP_TO_UINT:
3892 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3893 case Legal:
3894 Tmp1 = LegalizeOp(Node->getOperand(0));
3895
3896 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3897 default: assert(0 && "Unknown operation action!");
3898 case TargetLowering::Custom:
3899 isCustom = true;
3900 // FALLTHROUGH
3901 case TargetLowering::Legal:
3902 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3903 if (isCustom) {
3904 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003905 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003906 }
3907 break;
3908 case TargetLowering::Promote:
3909 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3910 Node->getOpcode() == ISD::FP_TO_SINT);
3911 break;
3912 case TargetLowering::Expand:
3913 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003914 SDValue True, False;
Duncan Sands92c43912008-06-06 12:08:01 +00003915 MVT VT = Node->getOperand(0).getValueType();
3916 MVT NVT = Node->getValueType(0);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003917 const uint64_t zero[] = {0, 0};
Duncan Sands92c43912008-06-06 12:08:01 +00003918 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3919 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohman88ae8c52008-02-29 01:44:25 +00003920 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003921 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel502151f2008-03-10 15:42:14 +00003922 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003923 Node->getOperand(0), Tmp2, ISD::SETLT);
3924 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3925 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
3926 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
3927 Tmp2));
3928 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman88ae8c52008-02-29 01:44:25 +00003929 DAG.getConstant(x, NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003930 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3931 break;
3932 } else {
3933 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3934 }
3935 break;
3936 }
3937 break;
3938 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00003939 MVT VT = Op.getValueType();
3940 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00003941 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003942 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00003943 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3944 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3945 Node->getOperand(0), DAG.getValueType(MVT::f64));
3946 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3947 DAG.getIntPtrConstant(1));
3948 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3949 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00003950 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3951 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3952 Tmp2 = DAG.getConstantFP(apf, OVT);
3953 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3954 // FIXME: generated code sucks.
3955 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3956 DAG.getNode(ISD::ADD, MVT::i32,
3957 DAG.getNode(ISD::FP_TO_SINT, VT,
3958 DAG.getNode(ISD::FSUB, OVT,
3959 Node->getOperand(0), Tmp2)),
3960 DAG.getConstant(0x80000000, MVT::i32)),
3961 DAG.getNode(ISD::FP_TO_SINT, VT,
3962 Node->getOperand(0)),
3963 DAG.getCondCode(ISD::SETGE));
3964 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003965 break;
3966 }
Dan Gohmanec51f642008-03-10 23:03:31 +00003967 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsf68dffb2008-07-17 02:36:29 +00003968 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
3969 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
3970 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman8181bd12008-07-27 21:46:04 +00003971 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003972 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003973 break;
3974 }
3975 case Promote:
3976 Tmp1 = PromoteOp(Node->getOperand(0));
3977 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3978 Result = LegalizeOp(Result);
3979 break;
3980 }
3981 break;
3982
Chris Lattner56ecde32008-01-16 06:57:07 +00003983 case ISD::FP_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00003984 MVT DstVT = Op.getValueType();
3985 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00003986 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3987 // The only other way we can lower this is to turn it into a STORE,
3988 // LOAD pair, targetting a temporary location (a stack slot).
3989 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3990 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00003991 }
3992 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3993 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3994 case Legal:
3995 Tmp1 = LegalizeOp(Node->getOperand(0));
3996 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3997 break;
3998 case Promote:
3999 Tmp1 = PromoteOp(Node->getOperand(0));
4000 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4001 break;
4002 }
4003 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004004 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004005 case ISD::FP_ROUND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004006 MVT DstVT = Op.getValueType();
4007 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004008 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4009 if (SrcVT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004010 SDValue Lo;
Dale Johannesena0d36082008-01-20 01:18:38 +00004011 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00004012 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00004013 if (DstVT!=MVT::f64)
4014 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00004015 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004016 }
Chris Lattner5872a362008-01-17 07:00:52 +00004017 // The only other way we can lower this is to turn it into a STORE,
4018 // LOAD pair, targetting a temporary location (a stack slot).
4019 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4020 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004021 }
Chris Lattner56ecde32008-01-16 06:57:07 +00004022 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4023 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4024 case Legal:
4025 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004026 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004027 break;
4028 case Promote:
4029 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004030 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4031 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004032 break;
4033 }
4034 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004035 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004036 case ISD::ANY_EXTEND:
4037 case ISD::ZERO_EXTEND:
4038 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004039 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4040 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4041 case Legal:
4042 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac54d002008-04-30 00:26:38 +00004043 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michelac7091c2008-02-15 23:05:48 +00004044 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4045 TargetLowering::Custom) {
Scott Michelac54d002008-04-30 00:26:38 +00004046 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004047 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelac7091c2008-02-15 23:05:48 +00004048 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004049 break;
4050 case Promote:
4051 switch (Node->getOpcode()) {
4052 case ISD::ANY_EXTEND:
4053 Tmp1 = PromoteOp(Node->getOperand(0));
4054 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
4055 break;
4056 case ISD::ZERO_EXTEND:
4057 Result = PromoteOp(Node->getOperand(0));
4058 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4059 Result = DAG.getZeroExtendInReg(Result,
4060 Node->getOperand(0).getValueType());
4061 break;
4062 case ISD::SIGN_EXTEND:
4063 Result = PromoteOp(Node->getOperand(0));
4064 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4065 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4066 Result,
4067 DAG.getValueType(Node->getOperand(0).getValueType()));
4068 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004069 }
4070 }
4071 break;
4072 case ISD::FP_ROUND_INREG:
4073 case ISD::SIGN_EXTEND_INREG: {
4074 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004075 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004076
4077 // If this operation is not supported, convert it to a shl/shr or load/store
4078 // pair.
4079 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4080 default: assert(0 && "This action not supported for this op yet!");
4081 case TargetLowering::Legal:
4082 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
4083 break;
4084 case TargetLowering::Expand:
4085 // If this is an integer extend and shifts are supported, do that.
4086 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
4087 // NOTE: we could fall back on load/store here too for targets without
4088 // SAR. However, it is doubtful that any exist.
Duncan Sands92c43912008-06-06 12:08:01 +00004089 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4090 ExtraVT.getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00004091 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004092 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4093 Node->getOperand(0), ShiftCst);
4094 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4095 Result, ShiftCst);
4096 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
4097 // The only way we can lower this is to turn it into a TRUNCSTORE,
4098 // EXTLOAD pair, targetting a temporary location (a stack slot).
4099
4100 // NOTE: there is a choice here between constantly creating new stack
4101 // slots and always reusing the same one. We currently always create
4102 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00004103 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4104 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004105 } else {
4106 assert(0 && "Unknown op");
4107 }
4108 break;
4109 }
4110 break;
4111 }
Duncan Sands38947cd2007-07-27 12:58:54 +00004112 case ISD::TRAMPOLINE: {
Dan Gohman8181bd12008-07-27 21:46:04 +00004113 SDValue Ops[6];
Duncan Sands38947cd2007-07-27 12:58:54 +00004114 for (unsigned i = 0; i != 6; ++i)
4115 Ops[i] = LegalizeOp(Node->getOperand(i));
4116 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4117 // The only option for this node is to custom lower it.
4118 Result = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004119 assert(Result.getNode() && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00004120
4121 // Since trampoline produces two values, make sure to remember that we
4122 // legalized both of them.
4123 Tmp1 = LegalizeOp(Result.getValue(1));
4124 Result = LegalizeOp(Result);
Dan Gohman8181bd12008-07-27 21:46:04 +00004125 AddLegalizedOperand(SDValue(Node, 0), Result);
4126 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00004127 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00004128 }
Dan Gohmane8e4a412008-05-14 00:43:10 +00004129 case ISD::FLT_ROUNDS_: {
Duncan Sands92c43912008-06-06 12:08:01 +00004130 MVT VT = Node->getValueType(0);
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004131 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4132 default: assert(0 && "This action not supported for this op yet!");
4133 case TargetLowering::Custom:
4134 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004135 if (Result.getNode()) break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004136 // Fall Thru
4137 case TargetLowering::Legal:
4138 // If this operation is not supported, lower it to constant 1
4139 Result = DAG.getConstant(1, VT);
4140 break;
4141 }
Dan Gohmane09dc8c2008-05-12 16:07:15 +00004142 break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004143 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004144 case ISD::TRAP: {
Duncan Sands92c43912008-06-06 12:08:01 +00004145 MVT VT = Node->getValueType(0);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004146 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4147 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00004148 case TargetLowering::Legal:
4149 Tmp1 = LegalizeOp(Node->getOperand(0));
4150 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4151 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004152 case TargetLowering::Custom:
4153 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004154 if (Result.getNode()) break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004155 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00004156 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004157 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00004158 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004159 TargetLowering::ArgListTy Args;
Dan Gohman8181bd12008-07-27 21:46:04 +00004160 std::pair<SDValue,SDValue> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004161 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00004162 false, false, false, false, CallingConv::C, false,
Bill Wendlingfef06052008-09-16 21:48:12 +00004163 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner88e03932008-01-15 22:09:33 +00004164 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004165 Result = CallResult.second;
4166 break;
4167 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004168 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004169 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004170 }
4171
4172 assert(Result.getValueType() == Op.getValueType() &&
4173 "Bad legalization!");
4174
4175 // Make sure that the generated code is itself legal.
4176 if (Result != Op)
4177 Result = LegalizeOp(Result);
4178
4179 // Note that LegalizeOp may be reentered even from single-use nodes, which
4180 // means that we always must cache transformed nodes.
4181 AddLegalizedOperand(Op, Result);
4182 return Result;
4183}
4184
4185/// PromoteOp - Given an operation that produces a value in an invalid type,
4186/// promote it to compute the value into a larger type. The produced value will
4187/// have the correct bits for the low portion of the register, but no guarantee
4188/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +00004189SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00004190 MVT VT = Op.getValueType();
4191 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004192 assert(getTypeAction(VT) == Promote &&
4193 "Caller should expand or legalize operands that are not promotable!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004194 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004195 "Cannot promote to smaller type!");
4196
Dan Gohman8181bd12008-07-27 21:46:04 +00004197 SDValue Tmp1, Tmp2, Tmp3;
4198 SDValue Result;
Gabor Greif1c80d112008-08-28 21:40:38 +00004199 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004200
Dan Gohman8181bd12008-07-27 21:46:04 +00004201 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004202 if (I != PromotedNodes.end()) return I->second;
4203
4204 switch (Node->getOpcode()) {
4205 case ISD::CopyFromReg:
4206 assert(0 && "CopyFromReg must be legal!");
4207 default:
4208#ifndef NDEBUG
4209 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4210#endif
4211 assert(0 && "Do not know how to promote this operator!");
4212 abort();
4213 case ISD::UNDEF:
4214 Result = DAG.getNode(ISD::UNDEF, NVT);
4215 break;
4216 case ISD::Constant:
4217 if (VT != MVT::i1)
4218 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4219 else
4220 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
4221 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4222 break;
4223 case ISD::ConstantFP:
4224 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4225 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4226 break;
4227
4228 case ISD::SETCC:
Scott Michel502151f2008-03-10 15:42:14 +00004229 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004230 && "SetCC type is not legal??");
Scott Michel502151f2008-03-10 15:42:14 +00004231 Result = DAG.getNode(ISD::SETCC,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004232 TLI.getSetCCResultType(Node->getOperand(0)),
4233 Node->getOperand(0), Node->getOperand(1),
4234 Node->getOperand(2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004235 break;
4236
4237 case ISD::TRUNCATE:
4238 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4239 case Legal:
4240 Result = LegalizeOp(Node->getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00004241 assert(Result.getValueType().bitsGE(NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004242 "This truncation doesn't make sense!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004243 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004244 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4245 break;
4246 case Promote:
4247 // The truncation is not required, because we don't guarantee anything
4248 // about high bits anyway.
4249 Result = PromoteOp(Node->getOperand(0));
4250 break;
4251 case Expand:
4252 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4253 // Truncate the low part of the expanded value to the result type
4254 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4255 }
4256 break;
4257 case ISD::SIGN_EXTEND:
4258 case ISD::ZERO_EXTEND:
4259 case ISD::ANY_EXTEND:
4260 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4261 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4262 case Legal:
4263 // Input is legal? Just do extend all the way to the larger type.
4264 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4265 break;
4266 case Promote:
4267 // Promote the reg if it's smaller.
4268 Result = PromoteOp(Node->getOperand(0));
4269 // The high bits are not guaranteed to be anything. Insert an extend.
4270 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4271 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4272 DAG.getValueType(Node->getOperand(0).getValueType()));
4273 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4274 Result = DAG.getZeroExtendInReg(Result,
4275 Node->getOperand(0).getValueType());
4276 break;
4277 }
4278 break;
Mon P Wang73d31542008-11-10 20:54:11 +00004279 case ISD::CONVERT_RNDSAT: {
4280 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4281 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4282 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4283 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4284 "can only promote integers");
4285 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4286 Node->getOperand(1), Node->getOperand(2),
4287 Node->getOperand(3), Node->getOperand(4),
4288 CvtCode);
4289 break;
4290
4291 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004292 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004293 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4294 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004295 Result = PromoteOp(Result);
4296 break;
4297
4298 case ISD::FP_EXTEND:
4299 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4300 case ISD::FP_ROUND:
4301 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4302 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4303 case Promote: assert(0 && "Unreachable with 2 FP types!");
4304 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004305 if (Node->getConstantOperandVal(1) == 0) {
4306 // Input is legal? Do an FP_ROUND_INREG.
4307 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4308 DAG.getValueType(VT));
4309 } else {
4310 // Just remove the truncate, it isn't affecting the value.
4311 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4312 Node->getOperand(1));
4313 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004314 break;
4315 }
4316 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004317 case ISD::SINT_TO_FP:
4318 case ISD::UINT_TO_FP:
4319 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4320 case Legal:
4321 // No extra round required here.
4322 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4323 break;
4324
4325 case Promote:
4326 Result = PromoteOp(Node->getOperand(0));
4327 if (Node->getOpcode() == ISD::SINT_TO_FP)
4328 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4329 Result,
4330 DAG.getValueType(Node->getOperand(0).getValueType()));
4331 else
4332 Result = DAG.getZeroExtendInReg(Result,
4333 Node->getOperand(0).getValueType());
4334 // No extra round required here.
4335 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4336 break;
4337 case Expand:
4338 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4339 Node->getOperand(0));
4340 // Round if we cannot tolerate excess precision.
4341 if (NoExcessFPPrecision)
4342 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4343 DAG.getValueType(VT));
4344 break;
4345 }
4346 break;
4347
4348 case ISD::SIGN_EXTEND_INREG:
4349 Result = PromoteOp(Node->getOperand(0));
4350 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4351 Node->getOperand(1));
4352 break;
4353 case ISD::FP_TO_SINT:
4354 case ISD::FP_TO_UINT:
4355 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4356 case Legal:
4357 case Expand:
4358 Tmp1 = Node->getOperand(0);
4359 break;
4360 case Promote:
4361 // The input result is prerounded, so we don't have to do anything
4362 // special.
4363 Tmp1 = PromoteOp(Node->getOperand(0));
4364 break;
4365 }
4366 // If we're promoting a UINT to a larger size, check to see if the new node
4367 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4368 // we can use that instead. This allows us to generate better code for
4369 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4370 // legal, such as PowerPC.
4371 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4372 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4373 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4374 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4375 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4376 } else {
4377 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4378 }
4379 break;
4380
4381 case ISD::FABS:
4382 case ISD::FNEG:
4383 Tmp1 = PromoteOp(Node->getOperand(0));
4384 assert(Tmp1.getValueType() == NVT);
4385 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4386 // NOTE: we do not have to do any extra rounding here for
4387 // NoExcessFPPrecision, because we know the input will have the appropriate
4388 // precision, and these operations don't modify precision at all.
4389 break;
4390
Dale Johannesen92b33082008-09-04 00:47:13 +00004391 case ISD::FLOG:
4392 case ISD::FLOG2:
4393 case ISD::FLOG10:
4394 case ISD::FEXP:
4395 case ISD::FEXP2:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004396 case ISD::FSQRT:
4397 case ISD::FSIN:
4398 case ISD::FCOS:
Dan Gohmanb2158232008-08-21 18:38:14 +00004399 case ISD::FTRUNC:
4400 case ISD::FFLOOR:
4401 case ISD::FCEIL:
4402 case ISD::FRINT:
4403 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004404 Tmp1 = PromoteOp(Node->getOperand(0));
4405 assert(Tmp1.getValueType() == NVT);
4406 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4407 if (NoExcessFPPrecision)
4408 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4409 DAG.getValueType(VT));
4410 break;
4411
Evan Cheng1fac6952008-09-09 23:35:53 +00004412 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004413 case ISD::FPOWI: {
Evan Cheng1fac6952008-09-09 23:35:53 +00004414 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004415 // directly as well, which may be better.
4416 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng1fac6952008-09-09 23:35:53 +00004417 Tmp2 = Node->getOperand(1);
4418 if (Node->getOpcode() == ISD::FPOW)
4419 Tmp2 = PromoteOp(Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004420 assert(Tmp1.getValueType() == NVT);
Evan Cheng1fac6952008-09-09 23:35:53 +00004421 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004422 if (NoExcessFPPrecision)
4423 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4424 DAG.getValueType(VT));
4425 break;
4426 }
4427
Dale Johannesenbc187662008-08-28 02:44:49 +00004428 case ISD::ATOMIC_CMP_SWAP_8:
4429 case ISD::ATOMIC_CMP_SWAP_16:
4430 case ISD::ATOMIC_CMP_SWAP_32:
4431 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004432 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004433 Tmp2 = PromoteOp(Node->getOperand(2));
4434 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004435 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4436 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004437 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004438 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004439 // Remember that we legalized the chain.
4440 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4441 break;
4442 }
Dale Johannesenbc187662008-08-28 02:44:49 +00004443 case ISD::ATOMIC_LOAD_ADD_8:
4444 case ISD::ATOMIC_LOAD_SUB_8:
4445 case ISD::ATOMIC_LOAD_AND_8:
4446 case ISD::ATOMIC_LOAD_OR_8:
4447 case ISD::ATOMIC_LOAD_XOR_8:
4448 case ISD::ATOMIC_LOAD_NAND_8:
4449 case ISD::ATOMIC_LOAD_MIN_8:
4450 case ISD::ATOMIC_LOAD_MAX_8:
4451 case ISD::ATOMIC_LOAD_UMIN_8:
4452 case ISD::ATOMIC_LOAD_UMAX_8:
4453 case ISD::ATOMIC_SWAP_8:
4454 case ISD::ATOMIC_LOAD_ADD_16:
4455 case ISD::ATOMIC_LOAD_SUB_16:
4456 case ISD::ATOMIC_LOAD_AND_16:
4457 case ISD::ATOMIC_LOAD_OR_16:
4458 case ISD::ATOMIC_LOAD_XOR_16:
4459 case ISD::ATOMIC_LOAD_NAND_16:
4460 case ISD::ATOMIC_LOAD_MIN_16:
4461 case ISD::ATOMIC_LOAD_MAX_16:
4462 case ISD::ATOMIC_LOAD_UMIN_16:
4463 case ISD::ATOMIC_LOAD_UMAX_16:
4464 case ISD::ATOMIC_SWAP_16:
4465 case ISD::ATOMIC_LOAD_ADD_32:
4466 case ISD::ATOMIC_LOAD_SUB_32:
4467 case ISD::ATOMIC_LOAD_AND_32:
4468 case ISD::ATOMIC_LOAD_OR_32:
4469 case ISD::ATOMIC_LOAD_XOR_32:
4470 case ISD::ATOMIC_LOAD_NAND_32:
4471 case ISD::ATOMIC_LOAD_MIN_32:
4472 case ISD::ATOMIC_LOAD_MAX_32:
4473 case ISD::ATOMIC_LOAD_UMIN_32:
4474 case ISD::ATOMIC_LOAD_UMAX_32:
4475 case ISD::ATOMIC_SWAP_32:
4476 case ISD::ATOMIC_LOAD_ADD_64:
4477 case ISD::ATOMIC_LOAD_SUB_64:
4478 case ISD::ATOMIC_LOAD_AND_64:
4479 case ISD::ATOMIC_LOAD_OR_64:
4480 case ISD::ATOMIC_LOAD_XOR_64:
4481 case ISD::ATOMIC_LOAD_NAND_64:
4482 case ISD::ATOMIC_LOAD_MIN_64:
4483 case ISD::ATOMIC_LOAD_MAX_64:
4484 case ISD::ATOMIC_LOAD_UMIN_64:
4485 case ISD::ATOMIC_LOAD_UMAX_64:
4486 case ISD::ATOMIC_SWAP_64: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004487 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004488 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004489 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4490 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004491 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004492 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004493 // Remember that we legalized the chain.
4494 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4495 break;
4496 }
4497
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004498 case ISD::AND:
4499 case ISD::OR:
4500 case ISD::XOR:
4501 case ISD::ADD:
4502 case ISD::SUB:
4503 case ISD::MUL:
4504 // The input may have strange things in the top bits of the registers, but
4505 // these operations don't care. They may have weird bits going out, but
4506 // that too is okay if they are integer operations.
4507 Tmp1 = PromoteOp(Node->getOperand(0));
4508 Tmp2 = PromoteOp(Node->getOperand(1));
4509 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4510 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4511 break;
4512 case ISD::FADD:
4513 case ISD::FSUB:
4514 case ISD::FMUL:
4515 Tmp1 = PromoteOp(Node->getOperand(0));
4516 Tmp2 = PromoteOp(Node->getOperand(1));
4517 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4518 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4519
4520 // Floating point operations will give excess precision that we may not be
4521 // able to tolerate. If we DO allow excess precision, just leave it,
4522 // otherwise excise it.
4523 // FIXME: Why would we need to round FP ops more than integer ones?
4524 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4525 if (NoExcessFPPrecision)
4526 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4527 DAG.getValueType(VT));
4528 break;
4529
4530 case ISD::SDIV:
4531 case ISD::SREM:
4532 // These operators require that their input be sign extended.
4533 Tmp1 = PromoteOp(Node->getOperand(0));
4534 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004535 if (NVT.isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004536 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4537 DAG.getValueType(VT));
4538 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4539 DAG.getValueType(VT));
4540 }
4541 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4542
4543 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands92c43912008-06-06 12:08:01 +00004544 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004545 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4546 DAG.getValueType(VT));
4547 break;
4548 case ISD::FDIV:
4549 case ISD::FREM:
4550 case ISD::FCOPYSIGN:
4551 // These operators require that their input be fp extended.
4552 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004553 case Expand: assert(0 && "not implemented");
4554 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4555 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004556 }
4557 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004558 case Expand: assert(0 && "not implemented");
4559 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4560 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004561 }
4562 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4563
4564 // Perform FP_ROUND: this is probably overly pessimistic.
4565 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4566 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4567 DAG.getValueType(VT));
4568 break;
4569
4570 case ISD::UDIV:
4571 case ISD::UREM:
4572 // These operators require that their input be zero extended.
4573 Tmp1 = PromoteOp(Node->getOperand(0));
4574 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004575 assert(NVT.isInteger() && "Operators don't apply to FP!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004576 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4577 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4578 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4579 break;
4580
4581 case ISD::SHL:
4582 Tmp1 = PromoteOp(Node->getOperand(0));
4583 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4584 break;
4585 case ISD::SRA:
4586 // The input value must be properly sign extended.
4587 Tmp1 = PromoteOp(Node->getOperand(0));
4588 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4589 DAG.getValueType(VT));
4590 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4591 break;
4592 case ISD::SRL:
4593 // The input value must be properly zero extended.
4594 Tmp1 = PromoteOp(Node->getOperand(0));
4595 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4596 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4597 break;
4598
4599 case ISD::VAARG:
4600 Tmp1 = Node->getOperand(0); // Get the chain.
4601 Tmp2 = Node->getOperand(1); // Get the pointer.
4602 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4603 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sandsac496a12008-07-04 11:47:58 +00004604 Result = TLI.LowerOperation(Tmp3, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004605 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004606 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00004607 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004608 // Increment the pointer, VAList, to the next vaarg
4609 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands92c43912008-06-06 12:08:01 +00004610 DAG.getConstant(VT.getSizeInBits()/8,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004611 TLI.getPointerTy()));
4612 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00004613 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004614 // Load the actual argument out of the pointer VAList
4615 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4616 }
4617 // Remember that we legalized the chain.
4618 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4619 break;
4620
4621 case ISD::LOAD: {
4622 LoadSDNode *LD = cast<LoadSDNode>(Node);
4623 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4624 ? ISD::EXTLOAD : LD->getExtensionType();
4625 Result = DAG.getExtLoad(ExtType, NVT,
4626 LD->getChain(), LD->getBasePtr(),
4627 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004628 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004629 LD->isVolatile(),
4630 LD->getAlignment());
4631 // Remember that we legalized the chain.
4632 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4633 break;
4634 }
Scott Michel67224b22008-06-02 22:18:03 +00004635 case ISD::SELECT: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004636 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4637 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel67224b22008-06-02 22:18:03 +00004638
Duncan Sands92c43912008-06-06 12:08:01 +00004639 MVT VT2 = Tmp2.getValueType();
Scott Michel67224b22008-06-02 22:18:03 +00004640 assert(VT2 == Tmp3.getValueType()
Scott Michel7b54de02008-06-03 19:13:20 +00004641 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4642 // Ensure that the resulting node is at least the same size as the operands'
4643 // value types, because we cannot assume that TLI.getSetCCValueType() is
4644 // constant.
4645 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004646 break;
Scott Michel67224b22008-06-02 22:18:03 +00004647 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004648 case ISD::SELECT_CC:
4649 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4650 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4651 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4652 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4653 break;
4654 case ISD::BSWAP:
4655 Tmp1 = Node->getOperand(0);
4656 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4657 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4658 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004659 DAG.getConstant(NVT.getSizeInBits() -
4660 VT.getSizeInBits(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004661 TLI.getShiftAmountTy()));
4662 break;
4663 case ISD::CTPOP:
4664 case ISD::CTTZ:
4665 case ISD::CTLZ:
4666 // Zero extend the argument
4667 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4668 // Perform the larger operation, then subtract if needed.
4669 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4670 switch(Node->getOpcode()) {
4671 case ISD::CTPOP:
4672 Result = Tmp1;
4673 break;
4674 case ISD::CTTZ:
4675 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00004676 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004677 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004678 ISD::SETEQ);
4679 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00004680 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004681 break;
4682 case ISD::CTLZ:
4683 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4684 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004685 DAG.getConstant(NVT.getSizeInBits() -
4686 VT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004687 break;
4688 }
4689 break;
4690 case ISD::EXTRACT_SUBVECTOR:
4691 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4692 break;
4693 case ISD::EXTRACT_VECTOR_ELT:
4694 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4695 break;
4696 }
4697
Gabor Greif1c80d112008-08-28 21:40:38 +00004698 assert(Result.getNode() && "Didn't set a result!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004699
4700 // Make sure the result is itself legal.
4701 Result = LegalizeOp(Result);
4702
4703 // Remember that we promoted this!
4704 AddPromotedOperand(Op, Result);
4705 return Result;
4706}
4707
4708/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4709/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4710/// based on the vector type. The return type of this matches the element type
4711/// of the vector, which may not be legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00004712SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004713 // We know that operand #0 is the Vec vector. If the index is a constant
4714 // or if the invec is a supported hardware type, we can use it. Otherwise,
4715 // lower to a store then an indexed load.
Dan Gohman8181bd12008-07-27 21:46:04 +00004716 SDValue Vec = Op.getOperand(0);
4717 SDValue Idx = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004718
Duncan Sands92c43912008-06-06 12:08:01 +00004719 MVT TVT = Vec.getValueType();
4720 unsigned NumElems = TVT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004721
4722 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4723 default: assert(0 && "This action is not supported yet!");
4724 case TargetLowering::Custom: {
4725 Vec = LegalizeOp(Vec);
4726 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004727 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004728 if (Tmp3.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004729 return Tmp3;
4730 break;
4731 }
4732 case TargetLowering::Legal:
4733 if (isTypeLegal(TVT)) {
4734 Vec = LegalizeOp(Vec);
4735 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004736 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004737 }
4738 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00004739 case TargetLowering::Promote:
4740 assert(TVT.isVector() && "not vector type");
4741 // fall thru to expand since vectors are by default are promote
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004742 case TargetLowering::Expand:
4743 break;
4744 }
4745
4746 if (NumElems == 1) {
4747 // This must be an access of the only element. Return it.
4748 Op = ScalarizeVectorOp(Vec);
4749 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00004750 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004751 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004752 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004753 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004754 if (CIdx->getZExtValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004755 Vec = Lo;
4756 } else {
4757 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004758 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004759 Idx.getValueType());
4760 }
4761
4762 // It's now an extract from the appropriate high or low part. Recurse.
4763 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4764 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4765 } else {
4766 // Store the value to a temporary stack slot, then LOAD the scalar
4767 // element back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00004768 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4769 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004770
4771 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +00004772 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004773 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4774 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004775
Duncan Sandsec142ee2008-06-08 20:54:56 +00004776 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00004777 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004778 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00004779 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004780
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004781 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4782
4783 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
4784 }
4785 return Op;
4786}
4787
4788/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
4789/// we assume the operation can be split if it is not already legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00004790SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004791 // We know that operand #0 is the Vec vector. For now we assume the index
4792 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman8181bd12008-07-27 21:46:04 +00004793 SDValue Vec = Op.getOperand(0);
4794 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004795
Duncan Sands92c43912008-06-06 12:08:01 +00004796 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004797
Duncan Sands92c43912008-06-06 12:08:01 +00004798 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004799 // This must be an access of the desired vector length. Return it.
4800 return Vec;
4801 }
4802
4803 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004804 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004805 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004806 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004807 Vec = Lo;
4808 } else {
4809 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004810 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
4811 Idx.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004812 }
4813
4814 // It's now an extract from the appropriate high or low part. Recurse.
4815 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4816 return ExpandEXTRACT_SUBVECTOR(Op);
4817}
4818
4819/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4820/// with condition CC on the current target. This usually involves legalizing
4821/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4822/// there may be no choice but to create a new SetCC node to represent the
4823/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman8181bd12008-07-27 21:46:04 +00004824/// LHS, and the SDValue returned in RHS has a nil SDNode value.
4825void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
4826 SDValue &RHS,
4827 SDValue &CC) {
4828 SDValue Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004829
4830 switch (getTypeAction(LHS.getValueType())) {
4831 case Legal:
4832 Tmp1 = LegalizeOp(LHS); // LHS
4833 Tmp2 = LegalizeOp(RHS); // RHS
4834 break;
4835 case Promote:
4836 Tmp1 = PromoteOp(LHS); // LHS
4837 Tmp2 = PromoteOp(RHS); // RHS
4838
4839 // If this is an FP compare, the operands have already been extended.
Duncan Sands92c43912008-06-06 12:08:01 +00004840 if (LHS.getValueType().isInteger()) {
4841 MVT VT = LHS.getValueType();
4842 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004843
4844 // Otherwise, we have to insert explicit sign or zero extends. Note
4845 // that we could insert sign extends for ALL conditions, but zero extend
4846 // is cheaper on many machines (an AND instead of two shifts), so prefer
4847 // it.
4848 switch (cast<CondCodeSDNode>(CC)->get()) {
4849 default: assert(0 && "Unknown integer comparison!");
4850 case ISD::SETEQ:
4851 case ISD::SETNE:
4852 case ISD::SETUGE:
4853 case ISD::SETUGT:
4854 case ISD::SETULE:
4855 case ISD::SETULT:
4856 // ALL of these operations will work if we either sign or zero extend
4857 // the operands (including the unsigned comparisons!). Zero extend is
4858 // usually a simpler/cheaper operation, so prefer it.
4859 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4860 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4861 break;
4862 case ISD::SETGE:
4863 case ISD::SETGT:
4864 case ISD::SETLT:
4865 case ISD::SETLE:
4866 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4867 DAG.getValueType(VT));
4868 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4869 DAG.getValueType(VT));
Evan Chengd901b662008-10-13 18:46:18 +00004870 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
4871 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004872 break;
4873 }
4874 }
4875 break;
4876 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00004877 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004878 if (VT == MVT::f32 || VT == MVT::f64) {
4879 // Expand into one or more soft-fp libcall(s).
Evan Cheng24108632008-07-01 21:35:46 +00004880 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004881 switch (cast<CondCodeSDNode>(CC)->get()) {
4882 case ISD::SETEQ:
4883 case ISD::SETOEQ:
4884 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4885 break;
4886 case ISD::SETNE:
4887 case ISD::SETUNE:
4888 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
4889 break;
4890 case ISD::SETGE:
4891 case ISD::SETOGE:
4892 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4893 break;
4894 case ISD::SETLT:
4895 case ISD::SETOLT:
4896 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4897 break;
4898 case ISD::SETLE:
4899 case ISD::SETOLE:
4900 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4901 break;
4902 case ISD::SETGT:
4903 case ISD::SETOGT:
4904 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4905 break;
4906 case ISD::SETUO:
4907 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4908 break;
4909 case ISD::SETO:
4910 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
4911 break;
4912 default:
4913 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4914 switch (cast<CondCodeSDNode>(CC)->get()) {
4915 case ISD::SETONE:
4916 // SETONE = SETOLT | SETOGT
4917 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4918 // Fallthrough
4919 case ISD::SETUGT:
4920 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4921 break;
4922 case ISD::SETUGE:
4923 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4924 break;
4925 case ISD::SETULT:
4926 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4927 break;
4928 case ISD::SETULE:
4929 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4930 break;
4931 case ISD::SETUEQ:
4932 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4933 break;
4934 default: assert(0 && "Unsupported FP setcc!");
4935 }
4936 }
Duncan Sandsf19591c2008-06-30 10:19:09 +00004937
Dan Gohman8181bd12008-07-27 21:46:04 +00004938 SDValue Dummy;
4939 SDValue Ops[2] = { LHS, RHS };
Gabor Greif1c80d112008-08-28 21:40:38 +00004940 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004941 false /*sign irrelevant*/, Dummy);
4942 Tmp2 = DAG.getConstant(0, MVT::i32);
4943 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
4944 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel502151f2008-03-10 15:42:14 +00004945 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004946 CC);
Gabor Greif1c80d112008-08-28 21:40:38 +00004947 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004948 false /*sign irrelevant*/, Dummy);
Scott Michel502151f2008-03-10 15:42:14 +00004949 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004950 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
4951 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004952 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004953 }
Evan Cheng18a1ab12008-07-07 07:18:09 +00004954 LHS = LegalizeOp(Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004955 RHS = Tmp2;
4956 return;
4957 }
4958
Dan Gohman8181bd12008-07-27 21:46:04 +00004959 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004960 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004961 ExpandOp(RHS, RHSLo, RHSHi);
4962 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4963
4964 if (VT==MVT::ppcf128) {
4965 // FIXME: This generated code sucks. We want to generate
Dale Johannesen26317b62008-09-12 00:30:56 +00004966 // FCMPU crN, hi1, hi2
Dale Johannesen472d15d2007-10-06 01:24:11 +00004967 // BNE crN, L:
Dale Johannesen26317b62008-09-12 00:30:56 +00004968 // FCMPU crN, lo1, lo2
Dale Johannesen472d15d2007-10-06 01:24:11 +00004969 // The following can be improved, but not that much.
Dale Johannesen26317b62008-09-12 00:30:56 +00004970 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
4971 ISD::SETOEQ);
Scott Michel502151f2008-03-10 15:42:14 +00004972 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004973 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Dale Johannesen26317b62008-09-12 00:30:56 +00004974 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
4975 ISD::SETUNE);
Scott Michel502151f2008-03-10 15:42:14 +00004976 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004977 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4978 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00004979 Tmp2 = SDValue();
Dale Johannesen472d15d2007-10-06 01:24:11 +00004980 break;
4981 }
4982
4983 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004984 case ISD::SETEQ:
4985 case ISD::SETNE:
4986 if (RHSLo == RHSHi)
4987 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4988 if (RHSCST->isAllOnesValue()) {
4989 // Comparison to -1.
4990 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4991 Tmp2 = RHSLo;
4992 break;
4993 }
4994
4995 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4996 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4997 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4998 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4999 break;
5000 default:
5001 // If this is a comparison of the sign bit, just look at the top part.
5002 // X > -1, x < 0
5003 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5004 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00005005 CST->isNullValue()) || // X < 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005006 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5007 CST->isAllOnesValue())) { // X > -1
5008 Tmp1 = LHSHi;
5009 Tmp2 = RHSHi;
5010 break;
5011 }
5012
5013 // FIXME: This generated code sucks.
5014 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005015 switch (CCCode) {
5016 default: assert(0 && "Unknown integer setcc!");
5017 case ISD::SETLT:
5018 case ISD::SETULT: LowCC = ISD::SETULT; break;
5019 case ISD::SETGT:
5020 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5021 case ISD::SETLE:
5022 case ISD::SETULE: LowCC = ISD::SETULE; break;
5023 case ISD::SETGE:
5024 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5025 }
5026
5027 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5028 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5029 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5030
5031 // NOTE: on targets without efficient SELECT of bools, we can always use
5032 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
5033 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel502151f2008-03-10 15:42:14 +00005034 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005035 LowCC, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005036 if (!Tmp1.getNode())
Scott Michel502151f2008-03-10 15:42:14 +00005037 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
5038 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005039 CCCode, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005040 if (!Tmp2.getNode())
Scott Michel502151f2008-03-10 15:42:14 +00005041 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005042 RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005043
Gabor Greif1c80d112008-08-28 21:40:38 +00005044 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5045 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman9d24dc72008-03-13 22:13:53 +00005046 if ((Tmp1C && Tmp1C->isNullValue()) ||
5047 (Tmp2C && Tmp2C->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005048 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5049 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman9d24dc72008-03-13 22:13:53 +00005050 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005051 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5052 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5053 // low part is known false, returns high part.
5054 // For LE / GE, if high part is known false, ignore the low part.
5055 // For LT / GT, if high part is known true, ignore the low part.
5056 Tmp1 = Tmp2;
Dan Gohman8181bd12008-07-27 21:46:04 +00005057 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005058 } else {
Scott Michel502151f2008-03-10 15:42:14 +00005059 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005060 ISD::SETEQ, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005061 if (!Result.getNode())
Scott Michel502151f2008-03-10 15:42:14 +00005062 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005063 ISD::SETEQ);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005064 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5065 Result, Tmp1, Tmp2));
5066 Tmp1 = Result;
Dan Gohman8181bd12008-07-27 21:46:04 +00005067 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005068 }
5069 }
5070 }
5071 }
5072 LHS = Tmp1;
5073 RHS = Tmp2;
5074}
5075
Evan Cheng71343822008-10-15 02:05:31 +00005076/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5077/// condition code CC on the current target. This routine assumes LHS and rHS
5078/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5079/// illegal condition code into AND / OR of multiple SETCC values.
5080void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5081 SDValue &LHS, SDValue &RHS,
5082 SDValue &CC) {
5083 MVT OpVT = LHS.getValueType();
5084 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5085 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5086 default: assert(0 && "Unknown condition code action!");
5087 case TargetLowering::Legal:
5088 // Nothing to do.
5089 break;
5090 case TargetLowering::Expand: {
5091 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5092 unsigned Opc = 0;
5093 switch (CCCode) {
5094 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohman2b5b9ca2008-10-21 03:12:54 +00005095 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5096 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5097 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5098 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5099 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5100 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5101 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5102 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5103 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5104 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5105 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5106 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng71343822008-10-15 02:05:31 +00005107 // FIXME: Implement more expansions.
5108 }
5109
5110 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5111 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5112 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5113 RHS = SDValue();
5114 CC = SDValue();
5115 break;
5116 }
5117 }
5118}
5119
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005120/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5121/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5122/// a load from the stack slot to DestVT, extending it if needed.
5123/// The resultant code need not be legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00005124SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5125 MVT SlotVT,
5126 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005127 // Create the stack frame object.
Mon P Wang55854cc2008-07-05 20:40:31 +00005128 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5129 SrcOp.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00005130 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang55854cc2008-07-05 20:40:31 +00005131
Dan Gohman20e37962008-02-11 18:58:42 +00005132 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005133 int SPFI = StackPtrFI->getIndex();
Mon P Wang55854cc2008-07-05 20:40:31 +00005134
Duncan Sands92c43912008-06-06 12:08:01 +00005135 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5136 unsigned SlotSize = SlotVT.getSizeInBits();
5137 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang55854cc2008-07-05 20:40:31 +00005138 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5139 DestVT.getTypeForMVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005140
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005141 // Emit a store to the stack slot. Use a truncstore if the input value is
5142 // later than DestVT.
Dan Gohman8181bd12008-07-27 21:46:04 +00005143 SDValue Store;
Mon P Wang55854cc2008-07-05 20:40:31 +00005144
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005145 if (SrcSize > SlotSize)
Dan Gohman12a9c082008-02-06 22:27:42 +00005146 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005147 PseudoSourceValue::getFixedStack(SPFI), 0,
5148 SlotVT, false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005149 else {
5150 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman12a9c082008-02-06 22:27:42 +00005151 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005152 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang55854cc2008-07-05 20:40:31 +00005153 false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005154 }
5155
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005156 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005157 if (SlotSize == DestSize)
Mon P Wang55854cc2008-07-05 20:40:31 +00005158 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005159
5160 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang55854cc2008-07-05 20:40:31 +00005161 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5162 false, DestAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005163}
5164
Dan Gohman8181bd12008-07-27 21:46:04 +00005165SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005166 // Create a vector sized/aligned stack slot, store the value to element #0,
5167 // then load the whole vector back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00005168 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman12a9c082008-02-06 22:27:42 +00005169
Dan Gohman20e37962008-02-11 18:58:42 +00005170 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005171 int SPFI = StackPtrFI->getIndex();
5172
Dan Gohman8181bd12008-07-27 21:46:04 +00005173 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005174 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00005175 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005176 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005177}
5178
5179
5180/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
5181/// support the operation, but do support the resultant vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00005182SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005183
5184 // If the only non-undef value is the low element, turn this into a
5185 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
5186 unsigned NumElems = Node->getNumOperands();
5187 bool isOnlyLowElement = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005188 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005189
Dan Gohman8181bd12008-07-27 21:46:04 +00005190 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerd8cee732008-03-09 00:29:42 +00005191 // and use a bitmask instead of a list of elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00005192 std::map<SDValue, std::vector<unsigned> > Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005193 Values[SplatValue].push_back(0);
5194 bool isConstant = true;
5195 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5196 SplatValue.getOpcode() != ISD::UNDEF)
5197 isConstant = false;
5198
5199 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005200 SDValue V = Node->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005201 Values[V].push_back(i);
5202 if (V.getOpcode() != ISD::UNDEF)
5203 isOnlyLowElement = false;
5204 if (SplatValue != V)
Dan Gohman8181bd12008-07-27 21:46:04 +00005205 SplatValue = SDValue(0,0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005206
5207 // If this isn't a constant element or an undef, we can't use a constant
5208 // pool load.
5209 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5210 V.getOpcode() != ISD::UNDEF)
5211 isConstant = false;
5212 }
5213
5214 if (isOnlyLowElement) {
5215 // If the low element is an undef too, then this whole things is an undef.
5216 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5217 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5218 // Otherwise, turn this into a scalar_to_vector node.
5219 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5220 Node->getOperand(0));
5221 }
5222
5223 // If all elements are constants, create a load from the constant pool.
5224 if (isConstant) {
Duncan Sands92c43912008-06-06 12:08:01 +00005225 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005226 std::vector<Constant*> CV;
5227 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5228 if (ConstantFPSDNode *V =
5229 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005230 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005231 } else if (ConstantSDNode *V =
Chris Lattner5e0610f2008-04-20 00:41:09 +00005232 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005233 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005234 } else {
5235 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner5e0610f2008-04-20 00:41:09 +00005236 const Type *OpNTy =
Duncan Sands92c43912008-06-06 12:08:01 +00005237 Node->getOperand(0).getValueType().getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005238 CV.push_back(UndefValue::get(OpNTy));
5239 }
5240 }
5241 Constant *CP = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005242 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005243 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman12a9c082008-02-06 22:27:42 +00005244 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005245 PseudoSourceValue::getConstantPool(), 0,
5246 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005247 }
5248
Gabor Greif1c80d112008-08-28 21:40:38 +00005249 if (SplatValue.getNode()) { // Splat of one value?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005250 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands92c43912008-06-06 12:08:01 +00005251 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman8181bd12008-07-27 21:46:04 +00005252 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5253 std::vector<SDValue> ZeroVec(NumElems, Zero);
5254 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005255 &ZeroVec[0], ZeroVec.size());
5256
5257 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
5258 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
5259 // Get the splatted value into the low element of a vector register.
Dan Gohman8181bd12008-07-27 21:46:04 +00005260 SDValue LowValVec =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005261 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5262
5263 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5264 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5265 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5266 SplatMask);
5267 }
5268 }
5269
5270 // If there are only two unique elements, we may be able to turn this into a
5271 // vector shuffle.
5272 if (Values.size() == 2) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005273 // Get the two values in deterministic order.
Dan Gohman8181bd12008-07-27 21:46:04 +00005274 SDValue Val1 = Node->getOperand(1);
5275 SDValue Val2;
5276 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerd8cee732008-03-09 00:29:42 +00005277 if (MI->first != Val1)
5278 Val2 = MI->first;
5279 else
5280 Val2 = (++MI)->first;
5281
5282 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5283 // vector shuffle has the undef vector on the RHS.
5284 if (Val1.getOpcode() == ISD::UNDEF)
5285 std::swap(Val1, Val2);
5286
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005287 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands92c43912008-06-06 12:08:01 +00005288 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5289 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005290 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005291
5292 // Set elements of the shuffle mask for Val1.
5293 std::vector<unsigned> &Val1Elts = Values[Val1];
5294 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5295 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5296
5297 // Set elements of the shuffle mask for Val2.
5298 std::vector<unsigned> &Val2Elts = Values[Val2];
5299 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5300 if (Val2.getOpcode() != ISD::UNDEF)
5301 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5302 else
5303 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5304
Dan Gohman8181bd12008-07-27 21:46:04 +00005305 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005306 &MaskVec[0], MaskVec.size());
5307
Chris Lattnerd8cee732008-03-09 00:29:42 +00005308 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005309 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5310 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005311 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5312 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005313 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005314
5315 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerd8cee732008-03-09 00:29:42 +00005316 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005317 }
5318 }
5319
5320 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5321 // aligned object on the stack, store each element into it, then load
5322 // the result as a vector.
Duncan Sands92c43912008-06-06 12:08:01 +00005323 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005324 // Create the stack frame object.
Dan Gohman8181bd12008-07-27 21:46:04 +00005325 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005326
5327 // Emit a store of each element to the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00005328 SmallVector<SDValue, 8> Stores;
Duncan Sands92c43912008-06-06 12:08:01 +00005329 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005330 // Store (in the right endianness) the elements to memory.
5331 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5332 // Ignore undef elements.
5333 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5334
5335 unsigned Offset = TypeByteSize*i;
5336
Dan Gohman8181bd12008-07-27 21:46:04 +00005337 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005338 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5339
5340 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
5341 NULL, 0));
5342 }
5343
Dan Gohman8181bd12008-07-27 21:46:04 +00005344 SDValue StoreChain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005345 if (!Stores.empty()) // Not all undef elements?
5346 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5347 &Stores[0], Stores.size());
5348 else
5349 StoreChain = DAG.getEntryNode();
5350
5351 // Result is a load from the stack slot.
5352 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
5353}
5354
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005355void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman8181bd12008-07-27 21:46:04 +00005356 SDValue Op, SDValue Amt,
5357 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005358 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00005359 SDValue LHSL, LHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005360 ExpandOp(Op, LHSL, LHSH);
5361
Dan Gohman8181bd12008-07-27 21:46:04 +00005362 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands92c43912008-06-06 12:08:01 +00005363 MVT VT = LHSL.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005364 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5365 Hi = Lo.getValue(1);
5366}
5367
5368
5369/// ExpandShift - Try to find a clever way to expand this shift operation out to
5370/// smaller elements. If we can't find a way that is more efficient than a
5371/// libcall on this target, return false. Otherwise, return true with the
5372/// low-parts expanded into Lo and Hi.
Dan Gohman8181bd12008-07-27 21:46:04 +00005373bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5374 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005375 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5376 "This is not a shift!");
5377
Duncan Sands92c43912008-06-06 12:08:01 +00005378 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman8181bd12008-07-27 21:46:04 +00005379 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands92c43912008-06-06 12:08:01 +00005380 MVT ShTy = ShAmt.getValueType();
5381 unsigned ShBits = ShTy.getSizeInBits();
5382 unsigned VTBits = Op.getValueType().getSizeInBits();
5383 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005384
Chris Lattner8c931452007-10-14 20:35:12 +00005385 // Handle the case when Amt is an immediate.
Gabor Greif1c80d112008-08-28 21:40:38 +00005386 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005387 unsigned Cst = CN->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005388 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005389 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005390 ExpandOp(Op, InL, InH);
5391 switch(Opc) {
5392 case ISD::SHL:
5393 if (Cst > VTBits) {
5394 Lo = DAG.getConstant(0, NVT);
5395 Hi = DAG.getConstant(0, NVT);
5396 } else if (Cst > NVTBits) {
5397 Lo = DAG.getConstant(0, NVT);
5398 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5399 } else if (Cst == NVTBits) {
5400 Lo = DAG.getConstant(0, NVT);
5401 Hi = InL;
5402 } else {
5403 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5404 Hi = DAG.getNode(ISD::OR, NVT,
5405 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5406 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5407 }
5408 return true;
5409 case ISD::SRL:
5410 if (Cst > VTBits) {
5411 Lo = DAG.getConstant(0, NVT);
5412 Hi = DAG.getConstant(0, NVT);
5413 } else if (Cst > NVTBits) {
5414 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5415 Hi = DAG.getConstant(0, NVT);
5416 } else if (Cst == NVTBits) {
5417 Lo = InH;
5418 Hi = DAG.getConstant(0, NVT);
5419 } else {
5420 Lo = DAG.getNode(ISD::OR, NVT,
5421 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5422 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5423 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5424 }
5425 return true;
5426 case ISD::SRA:
5427 if (Cst > VTBits) {
5428 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5429 DAG.getConstant(NVTBits-1, ShTy));
5430 } else if (Cst > NVTBits) {
5431 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5432 DAG.getConstant(Cst-NVTBits, ShTy));
5433 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5434 DAG.getConstant(NVTBits-1, ShTy));
5435 } else if (Cst == NVTBits) {
5436 Lo = InH;
5437 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5438 DAG.getConstant(NVTBits-1, ShTy));
5439 } else {
5440 Lo = DAG.getNode(ISD::OR, NVT,
5441 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5442 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5443 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5444 }
5445 return true;
5446 }
5447 }
5448
5449 // Okay, the shift amount isn't constant. However, if we can tell that it is
5450 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005451 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5452 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005453 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5454
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005455 // If we know that if any of the high bits of the shift amount are one, then
5456 // we can do this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005457 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005458 // Mask out the high bit, which we know is set.
5459 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005460 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005461
5462 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005463 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005464 ExpandOp(Op, InL, InH);
5465 switch(Opc) {
5466 case ISD::SHL:
5467 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5468 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5469 return true;
5470 case ISD::SRL:
5471 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5472 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5473 return true;
5474 case ISD::SRA:
5475 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5476 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5477 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5478 return true;
5479 }
5480 }
5481
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005482 // If we know that the high bits of the shift amount are all zero, then we can
5483 // do this as a couple of simple shifts.
5484 if ((KnownZero & Mask) == Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005485 // Compute 32-amt.
Dan Gohman8181bd12008-07-27 21:46:04 +00005486 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005487 DAG.getConstant(NVTBits, Amt.getValueType()),
5488 Amt);
5489
5490 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005491 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005492 ExpandOp(Op, InL, InH);
5493 switch(Opc) {
5494 case ISD::SHL:
5495 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5496 Hi = DAG.getNode(ISD::OR, NVT,
5497 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5498 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5499 return true;
5500 case ISD::SRL:
5501 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5502 Lo = DAG.getNode(ISD::OR, NVT,
5503 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5504 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5505 return true;
5506 case ISD::SRA:
5507 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5508 Lo = DAG.getNode(ISD::OR, NVT,
5509 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5510 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5511 return true;
5512 }
5513 }
5514
5515 return false;
5516}
5517
5518
5519// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5520// does not fit into a register, return the lo part and set the hi part to the
5521// by-reg argument. If it does fit into a single register, return the result
5522// and leave the Hi part unset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005523SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5524 bool isSigned, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005525 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5526 // The input chain to this libcall is the entry node of the function.
5527 // Legalizing the call will automatically add the previous call to the
5528 // dependence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005529 SDValue InChain = DAG.getEntryNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005530
5531 TargetLowering::ArgListTy Args;
5532 TargetLowering::ArgListEntry Entry;
5533 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00005534 MVT ArgVT = Node->getOperand(i).getValueType();
5535 const Type *ArgTy = ArgVT.getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005536 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5537 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005538 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005539 Args.push_back(Entry);
5540 }
Bill Wendlingfef06052008-09-16 21:48:12 +00005541 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang1448aad2008-10-30 08:01:45 +00005542 TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005543
5544 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands92c43912008-06-06 12:08:01 +00005545 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman8181bd12008-07-27 21:46:04 +00005546 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen67cc9b62008-09-26 19:31:26 +00005547 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5548 CallingConv::C, false, Callee, Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005549
5550 // Legalize the call sequence, starting with the chain. This will advance
5551 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5552 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5553 LegalizeOp(CallInfo.second);
Dan Gohman8181bd12008-07-27 21:46:04 +00005554 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005555 switch (getTypeAction(CallInfo.first.getValueType())) {
5556 default: assert(0 && "Unknown thing");
5557 case Legal:
5558 Result = CallInfo.first;
5559 break;
5560 case Expand:
5561 ExpandOp(CallInfo.first, Result, Hi);
5562 break;
5563 }
5564 return Result;
5565}
5566
Dan Gohman29c3cef2008-08-14 20:04:46 +00005567/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5568///
5569SDValue SelectionDAGLegalize::
5570LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5571 bool isCustom = false;
5572 SDValue Tmp1;
5573 switch (getTypeAction(Op.getValueType())) {
5574 case Legal:
5575 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5576 Op.getValueType())) {
5577 default: assert(0 && "Unknown operation action!");
5578 case TargetLowering::Custom:
5579 isCustom = true;
5580 // FALLTHROUGH
5581 case TargetLowering::Legal:
5582 Tmp1 = LegalizeOp(Op);
Gabor Greif1c80d112008-08-28 21:40:38 +00005583 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005584 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5585 else
5586 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5587 DestTy, Tmp1);
5588 if (isCustom) {
5589 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005590 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman29c3cef2008-08-14 20:04:46 +00005591 }
5592 break;
5593 case TargetLowering::Expand:
5594 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5595 break;
5596 case TargetLowering::Promote:
5597 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5598 break;
5599 }
5600 break;
5601 case Expand:
5602 Result = ExpandIntToFP(isSigned, DestTy, Op);
5603 break;
5604 case Promote:
5605 Tmp1 = PromoteOp(Op);
5606 if (isSigned) {
5607 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5608 Tmp1, DAG.getValueType(Op.getValueType()));
5609 } else {
5610 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5611 Op.getValueType());
5612 }
Gabor Greif1c80d112008-08-28 21:40:38 +00005613 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005614 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5615 else
5616 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5617 DestTy, Tmp1);
5618 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5619 break;
5620 }
5621 return Result;
5622}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005623
5624/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5625///
Dan Gohman8181bd12008-07-27 21:46:04 +00005626SDValue SelectionDAGLegalize::
5627ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands92c43912008-06-06 12:08:01 +00005628 MVT SourceVT = Source.getValueType();
Dan Gohman8b232ff2008-03-11 01:59:03 +00005629 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005630
Dan Gohman29c3cef2008-08-14 20:04:46 +00005631 // Expand unsupported int-to-fp vector casts by unrolling them.
5632 if (DestTy.isVector()) {
5633 if (!ExpandSource)
5634 return LegalizeOp(UnrollVectorOp(Source));
5635 MVT DestEltTy = DestTy.getVectorElementType();
5636 if (DestTy.getVectorNumElements() == 1) {
5637 SDValue Scalar = ScalarizeVectorOp(Source);
5638 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5639 DestEltTy, Scalar);
5640 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5641 }
5642 SDValue Lo, Hi;
5643 SplitVectorOp(Source, Lo, Hi);
5644 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5645 DestTy.getVectorNumElements() / 2);
5646 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5647 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengd901b662008-10-13 18:46:18 +00005648 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5649 HiResult));
Dan Gohman29c3cef2008-08-14 20:04:46 +00005650 }
5651
Evan Chengf99a7752008-04-01 02:18:22 +00005652 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5653 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmana193dba2008-03-05 02:07:31 +00005654 // The integer value loaded will be incorrectly if the 'sign bit' of the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005655 // incoming integer is set. To handle this, we dynamically test to see if
5656 // it is set, and, if so, add a fudge factor.
Dan Gohman8181bd12008-07-27 21:46:04 +00005657 SDValue Hi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005658 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005659 SDValue Lo;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005660 ExpandOp(Source, Lo, Hi);
5661 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5662 } else {
5663 // The comparison for the sign bit will use the entire operand.
5664 Hi = Source;
5665 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005666
Dale Johannesen96db7962008-11-04 20:52:49 +00005667 // Check to see if the target has a custom way to lower this. If so, use
5668 // it. (Note we've already expanded the operand in this case.)
Dale Johannesena359b8b2008-10-21 20:50:01 +00005669 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5670 default: assert(0 && "This action not implemented for this operation!");
5671 case TargetLowering::Legal:
5672 case TargetLowering::Expand:
5673 break; // This case is handled below.
5674 case TargetLowering::Custom: {
5675 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5676 Source), DAG);
5677 if (NV.getNode())
5678 return LegalizeOp(NV);
5679 break; // The target decided this was legal after all
5680 }
5681 }
5682
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005683 // If this is unsigned, and not supported, first perform the conversion to
5684 // signed, then adjust the result if the sign bit is set.
Dan Gohman8181bd12008-07-27 21:46:04 +00005685 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005686
Dan Gohman8181bd12008-07-27 21:46:04 +00005687 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005688 DAG.getConstant(0, Hi.getValueType()),
5689 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005690 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5691 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005692 SignSet, Four, Zero);
5693 uint64_t FF = 0x5f800000ULL;
5694 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmana193dba2008-03-05 02:07:31 +00005695 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005696
Dan Gohman8181bd12008-07-27 21:46:04 +00005697 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005698 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005699 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00005700 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00005701 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005702 if (DestTy == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005703 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005704 PseudoSourceValue::getConstantPool(), 0,
5705 false, Alignment);
Duncan Sandsec142ee2008-06-08 20:54:56 +00005706 else if (DestTy.bitsGT(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005707 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005708 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00005709 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005710 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00005711 MVT::f32, false, Alignment);
Dale Johannesen2fc20782007-09-14 22:26:36 +00005712 else
5713 assert(0 && "Unexpected conversion");
5714
Duncan Sands92c43912008-06-06 12:08:01 +00005715 MVT SCVT = SignedConv.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005716 if (SCVT != DestTy) {
5717 // Destination type needs to be expanded as well. The FADD now we are
5718 // constructing will be expanded into a libcall.
Duncan Sands92c43912008-06-06 12:08:01 +00005719 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5720 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmanc98645c2008-03-05 01:08:17 +00005721 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005722 SignedConv, SignedConv.getValue(1));
5723 }
5724 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5725 }
5726 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5727 }
5728
5729 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmanc98645c2008-03-05 01:08:17 +00005730 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005731 default: assert(0 && "This action not implemented for this operation!");
5732 case TargetLowering::Legal:
5733 case TargetLowering::Expand:
5734 break; // This case is handled below.
5735 case TargetLowering::Custom: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005736 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005737 Source), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005738 if (NV.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005739 return LegalizeOp(NV);
5740 break; // The target decided this was legal after all
5741 }
5742 }
5743
5744 // Expand the source, then glue it back together for the call. We must expand
5745 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman8b232ff2008-03-11 01:59:03 +00005746 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005747 SDValue SrcLo, SrcHi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005748 ExpandOp(Source, SrcLo, SrcHi);
5749 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5750 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005751
Duncan Sandsf68dffb2008-07-17 02:36:29 +00005752 RTLIB::Libcall LC = isSigned ?
5753 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5754 RTLIB::getUINTTOFP(SourceVT, DestTy);
5755 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5756
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005757 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman8181bd12008-07-27 21:46:04 +00005758 SDValue HiPart;
Gabor Greif1c80d112008-08-28 21:40:38 +00005759 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5760 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmanec51f642008-03-10 23:03:31 +00005761 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5762 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005763}
5764
5765/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5766/// INT_TO_FP operation of the specified operand when the target requests that
5767/// we expand it. At this point, we know that the result and operand types are
5768/// legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00005769SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5770 SDValue Op0,
5771 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005772 if (Op0.getValueType() == MVT::i32) {
5773 // simple 32-bit [signed|unsigned] integer to float/double expansion
5774
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005775 // Get the stack frame index of a 8 byte buffer.
Dan Gohman8181bd12008-07-27 21:46:04 +00005776 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005777
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005778 // word offset constant for Hi/Lo address computation
Dan Gohman8181bd12008-07-27 21:46:04 +00005779 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005780 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman8181bd12008-07-27 21:46:04 +00005781 SDValue Hi = StackSlot;
5782 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005783 if (TLI.isLittleEndian())
5784 std::swap(Hi, Lo);
5785
5786 // if signed map to unsigned space
Dan Gohman8181bd12008-07-27 21:46:04 +00005787 SDValue Op0Mapped;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005788 if (isSigned) {
5789 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman8181bd12008-07-27 21:46:04 +00005790 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005791 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5792 } else {
5793 Op0Mapped = Op0;
5794 }
5795 // store the lo of the constructed double - based on integer input
Dan Gohman8181bd12008-07-27 21:46:04 +00005796 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005797 Op0Mapped, Lo, NULL, 0);
5798 // initial hi portion of constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00005799 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005800 // store the hi of the constructed double - biased exponent
Dan Gohman8181bd12008-07-27 21:46:04 +00005801 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005802 // load the constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00005803 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005804 // FP constant to bias correct the final result
Dan Gohman8181bd12008-07-27 21:46:04 +00005805 SDValue Bias = DAG.getConstantFP(isSigned ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005806 BitsToDouble(0x4330000080000000ULL)
5807 : BitsToDouble(0x4330000000000000ULL),
5808 MVT::f64);
5809 // subtract the bias
Dan Gohman8181bd12008-07-27 21:46:04 +00005810 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005811 // final result
Dan Gohman8181bd12008-07-27 21:46:04 +00005812 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005813 // handle final rounding
5814 if (DestVT == MVT::f64) {
5815 // do nothing
5816 Result = Sub;
Duncan Sandsec142ee2008-06-08 20:54:56 +00005817 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner5872a362008-01-17 07:00:52 +00005818 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5819 DAG.getIntPtrConstant(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00005820 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005821 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005822 }
5823 return Result;
5824 }
5825 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman8181bd12008-07-27 21:46:04 +00005826 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005827
Dan Gohman8181bd12008-07-27 21:46:04 +00005828 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005829 DAG.getConstant(0, Op0.getValueType()),
5830 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005831 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5832 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005833 SignSet, Four, Zero);
5834
5835 // If the sign bit of the integer is set, the large number will be treated
5836 // as a negative number. To counteract this, the dynamic code adds an
5837 // offset depending on the data type.
5838 uint64_t FF;
Duncan Sands92c43912008-06-06 12:08:01 +00005839 switch (Op0.getValueType().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005840 default: assert(0 && "Unsupported integer type!");
5841 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5842 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5843 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5844 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5845 }
5846 if (TLI.isLittleEndian()) FF <<= 32;
5847 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5848
Dan Gohman8181bd12008-07-27 21:46:04 +00005849 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005850 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005851 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00005852 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00005853 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005854 if (DestVT == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005855 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005856 PseudoSourceValue::getConstantPool(), 0,
5857 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005858 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00005859 FudgeInReg =
5860 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5861 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005862 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00005863 MVT::f32, false, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005864 }
5865
5866 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5867}
5868
5869/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5870/// *INT_TO_FP operation of the specified operand when the target requests that
5871/// we promote it. At this point, we know that the result and operand types are
5872/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5873/// operation that takes a larger input.
Dan Gohman8181bd12008-07-27 21:46:04 +00005874SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
5875 MVT DestVT,
5876 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005877 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00005878 MVT NewInTy = LegalOp.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005879
5880 unsigned OpToUse = 0;
5881
5882 // Scan for the appropriate larger type to use.
5883 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00005884 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
5885 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005886
5887 // If the target supports SINT_TO_FP of this type, use it.
5888 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5889 default: break;
5890 case TargetLowering::Legal:
5891 if (!TLI.isTypeLegal(NewInTy))
5892 break; // Can't use this datatype.
5893 // FALL THROUGH.
5894 case TargetLowering::Custom:
5895 OpToUse = ISD::SINT_TO_FP;
5896 break;
5897 }
5898 if (OpToUse) break;
5899 if (isSigned) continue;
5900
5901 // If the target supports UINT_TO_FP of this type, use it.
5902 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5903 default: break;
5904 case TargetLowering::Legal:
5905 if (!TLI.isTypeLegal(NewInTy))
5906 break; // Can't use this datatype.
5907 // FALL THROUGH.
5908 case TargetLowering::Custom:
5909 OpToUse = ISD::UINT_TO_FP;
5910 break;
5911 }
5912 if (OpToUse) break;
5913
5914 // Otherwise, try a larger type.
5915 }
5916
5917 // Okay, we found the operation and type to use. Zero extend our input to the
5918 // desired type then run the operation on it.
5919 return DAG.getNode(OpToUse, DestVT,
5920 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5921 NewInTy, LegalOp));
5922}
5923
5924/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5925/// FP_TO_*INT operation of the specified operand when the target requests that
5926/// we promote it. At this point, we know that the result and operand types are
5927/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5928/// operation that returns a larger result.
Dan Gohman8181bd12008-07-27 21:46:04 +00005929SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
5930 MVT DestVT,
5931 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005932 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00005933 MVT NewOutTy = DestVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005934
5935 unsigned OpToUse = 0;
5936
5937 // Scan for the appropriate larger type to use.
5938 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00005939 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
5940 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005941
5942 // If the target supports FP_TO_SINT returning this type, use it.
5943 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5944 default: break;
5945 case TargetLowering::Legal:
5946 if (!TLI.isTypeLegal(NewOutTy))
5947 break; // Can't use this datatype.
5948 // FALL THROUGH.
5949 case TargetLowering::Custom:
5950 OpToUse = ISD::FP_TO_SINT;
5951 break;
5952 }
5953 if (OpToUse) break;
5954
5955 // If the target supports FP_TO_UINT of this type, use it.
5956 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5957 default: break;
5958 case TargetLowering::Legal:
5959 if (!TLI.isTypeLegal(NewOutTy))
5960 break; // Can't use this datatype.
5961 // FALL THROUGH.
5962 case TargetLowering::Custom:
5963 OpToUse = ISD::FP_TO_UINT;
5964 break;
5965 }
5966 if (OpToUse) break;
5967
5968 // Otherwise, try a larger type.
5969 }
5970
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005971
5972 // Okay, we found the operation and type to use.
Dan Gohman8181bd12008-07-27 21:46:04 +00005973 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sandsac496a12008-07-04 11:47:58 +00005974
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005975 // If the operation produces an invalid type, it must be custom lowered. Use
5976 // the target lowering hooks to expand it. Just keep the low part of the
5977 // expanded operation, we know that we're truncating anyway.
5978 if (getTypeAction(NewOutTy) == Expand) {
Gabor Greif1c80d112008-08-28 21:40:38 +00005979 Operation = SDValue(TLI.ReplaceNodeResults(Operation.getNode(), DAG), 0);
5980 assert(Operation.getNode() && "Didn't return anything");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005981 }
Duncan Sandsac496a12008-07-04 11:47:58 +00005982
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005983 // Truncate the result of the extended FP_TO_*INT operation to the desired
5984 // size.
5985 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005986}
5987
5988/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5989///
Dan Gohman8181bd12008-07-27 21:46:04 +00005990SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00005991 MVT VT = Op.getValueType();
5992 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00005993 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands92c43912008-06-06 12:08:01 +00005994 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005995 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5996 case MVT::i16:
5997 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5998 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5999 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6000 case MVT::i32:
6001 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6002 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6003 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6004 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6005 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6006 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6007 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6008 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6009 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6010 case MVT::i64:
6011 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6012 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6013 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6014 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6015 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6016 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6017 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6018 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6019 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6020 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6021 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6022 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6023 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6024 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6025 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6026 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6027 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6028 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6029 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6030 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6031 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6032 }
6033}
6034
6035/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6036///
Dan Gohman8181bd12008-07-27 21:46:04 +00006037SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006038 switch (Opc) {
6039 default: assert(0 && "Cannot expand this yet!");
6040 case ISD::CTPOP: {
6041 static const uint64_t mask[6] = {
6042 0x5555555555555555ULL, 0x3333333333333333ULL,
6043 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6044 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6045 };
Duncan Sands92c43912008-06-06 12:08:01 +00006046 MVT VT = Op.getValueType();
6047 MVT ShVT = TLI.getShiftAmountTy();
6048 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006049 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6050 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman8181bd12008-07-27 21:46:04 +00006051 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6052 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006053 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6054 DAG.getNode(ISD::AND, VT,
6055 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6056 }
6057 return Op;
6058 }
6059 case ISD::CTLZ: {
6060 // for now, we do this:
6061 // x = x | (x >> 1);
6062 // x = x | (x >> 2);
6063 // ...
6064 // x = x | (x >>16);
6065 // x = x | (x >>32); // for 64-bit input
6066 // return popcount(~x);
6067 //
6068 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006069 MVT VT = Op.getValueType();
6070 MVT ShVT = TLI.getShiftAmountTy();
6071 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006072 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006073 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006074 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6075 }
6076 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6077 return DAG.getNode(ISD::CTPOP, VT, Op);
6078 }
6079 case ISD::CTTZ: {
6080 // for now, we use: { return popcount(~x & (x - 1)); }
6081 // unless the target has ctlz but not ctpop, in which case we use:
6082 // { return 32 - nlz(~x & (x-1)); }
6083 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006084 MVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006085 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6086 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006087 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6088 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6089 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6090 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6091 TLI.isOperationLegal(ISD::CTLZ, VT))
6092 return DAG.getNode(ISD::SUB, VT,
Duncan Sands92c43912008-06-06 12:08:01 +00006093 DAG.getConstant(VT.getSizeInBits(), VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006094 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6095 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6096 }
6097 }
6098}
6099
Dan Gohman8181bd12008-07-27 21:46:04 +00006100/// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006101/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman4fc03742008-10-01 15:07:49 +00006102/// LegalizedNodes map is filled in for any results that are not expanded, the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006103/// ExpandedNodes map is filled in for any results that are expanded, and the
6104/// Lo/Hi values are returned.
Dan Gohman8181bd12008-07-27 21:46:04 +00006105void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands92c43912008-06-06 12:08:01 +00006106 MVT VT = Op.getValueType();
6107 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greif1c80d112008-08-28 21:40:38 +00006108 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006109 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00006110 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands92c43912008-06-06 12:08:01 +00006111 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006112
6113 // See if we already expanded it.
Dan Gohman8181bd12008-07-27 21:46:04 +00006114 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006115 = ExpandedNodes.find(Op);
6116 if (I != ExpandedNodes.end()) {
6117 Lo = I->second.first;
6118 Hi = I->second.second;
6119 return;
6120 }
6121
6122 switch (Node->getOpcode()) {
6123 case ISD::CopyFromReg:
6124 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006125 case ISD::FP_ROUND_INREG:
6126 if (VT == MVT::ppcf128 &&
6127 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6128 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006129 SDValue SrcLo, SrcHi, Src;
Dale Johannesend3b6af32007-10-11 23:32:15 +00006130 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6131 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006132 SDValue Result = TLI.LowerOperation(
Dale Johannesend3b6af32007-10-11 23:32:15 +00006133 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006134 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6135 Lo = Result.getNode()->getOperand(0);
6136 Hi = Result.getNode()->getOperand(1);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006137 break;
6138 }
6139 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006140 default:
6141#ifndef NDEBUG
6142 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
6143#endif
6144 assert(0 && "Do not know how to expand this operator!");
6145 abort();
Dan Gohman550c8462008-02-27 01:52:30 +00006146 case ISD::EXTRACT_ELEMENT:
6147 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006148 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman550c8462008-02-27 01:52:30 +00006149 return ExpandOp(Hi, Lo, Hi);
Dan Gohman7e7aa2c2008-02-27 19:44:57 +00006150 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006151 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006152 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6153 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6154 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006155 case ISD::UNDEF:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006156 Lo = DAG.getNode(ISD::UNDEF, NVT);
6157 Hi = DAG.getNode(ISD::UNDEF, NVT);
6158 break;
6159 case ISD::Constant: {
Duncan Sands92c43912008-06-06 12:08:01 +00006160 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman97f1f8e2008-03-03 22:20:46 +00006161 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6162 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6163 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006164 break;
6165 }
6166 case ISD::ConstantFP: {
6167 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00006168 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00006169 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen2aef5692007-10-11 18:07:22 +00006170 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6171 MVT::f64);
6172 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6173 MVT::f64);
6174 break;
6175 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006176 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
6177 if (getTypeAction(Lo.getValueType()) == Expand)
6178 ExpandOp(Lo, Lo, Hi);
6179 break;
6180 }
6181 case ISD::BUILD_PAIR:
6182 // Return the operands.
6183 Lo = Node->getOperand(0);
6184 Hi = Node->getOperand(1);
6185 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006186
6187 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00006188 if (Node->getNumValues() == 1) {
6189 ExpandOp(Op.getOperand(0), Lo, Hi);
6190 break;
6191 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006192 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif46bf5472008-08-26 22:36:50 +00006193 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006194 Op.getValue(1).getValueType() == MVT::Other &&
6195 "unhandled MERGE_VALUES");
6196 ExpandOp(Op.getOperand(0), Lo, Hi);
6197 // Remember that we legalized the chain.
6198 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6199 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006200
6201 case ISD::SIGN_EXTEND_INREG:
6202 ExpandOp(Node->getOperand(0), Lo, Hi);
6203 // sext_inreg the low part if needed.
6204 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6205
6206 // The high part gets the sign extension from the lo-part. This handles
6207 // things like sextinreg V:i64 from i8.
6208 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands92c43912008-06-06 12:08:01 +00006209 DAG.getConstant(NVT.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006210 TLI.getShiftAmountTy()));
6211 break;
6212
6213 case ISD::BSWAP: {
6214 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006215 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006216 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6217 Lo = TempLo;
6218 break;
6219 }
6220
6221 case ISD::CTPOP:
6222 ExpandOp(Node->getOperand(0), Lo, Hi);
6223 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6224 DAG.getNode(ISD::CTPOP, NVT, Lo),
6225 DAG.getNode(ISD::CTPOP, NVT, Hi));
6226 Hi = DAG.getConstant(0, NVT);
6227 break;
6228
6229 case ISD::CTLZ: {
6230 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
6231 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006232 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6233 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
6234 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006235 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006236 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006237 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6238
6239 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6240 Hi = DAG.getConstant(0, NVT);
6241 break;
6242 }
6243
6244 case ISD::CTTZ: {
6245 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
6246 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006247 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6248 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
6249 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006250 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006251 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006252 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6253
6254 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6255 Hi = DAG.getConstant(0, NVT);
6256 break;
6257 }
6258
6259 case ISD::VAARG: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006260 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6261 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006262 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6263 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6264
6265 // Remember that we legalized the chain.
6266 Hi = LegalizeOp(Hi);
6267 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006268 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006269 std::swap(Lo, Hi);
6270 break;
6271 }
6272
6273 case ISD::LOAD: {
6274 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00006275 SDValue Ch = LD->getChain(); // Legalize the chain.
6276 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006277 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman29c3cef2008-08-14 20:04:46 +00006278 const Value *SV = LD->getSrcValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006279 int SVOffset = LD->getSrcValueOffset();
6280 unsigned Alignment = LD->getAlignment();
6281 bool isVolatile = LD->isVolatile();
6282
6283 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman29c3cef2008-08-14 20:04:46 +00006284 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006285 isVolatile, Alignment);
6286 if (VT == MVT::f32 || VT == MVT::f64) {
6287 // f32->i32 or f64->i64 one to one expansion.
6288 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006289 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006290 // Recursively expand the new load.
6291 if (getTypeAction(NVT) == Expand)
6292 ExpandOp(Lo, Lo, Hi);
6293 break;
6294 }
6295
6296 // Increment the pointer to the other half.
Duncan Sands92c43912008-06-06 12:08:01 +00006297 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006298 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006299 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006300 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006301 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00006302 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006303 isVolatile, Alignment);
6304
6305 // Build a factor node to remember that this load is independent of the
6306 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00006307 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006308 Hi.getValue(1));
6309
6310 // Remember that we legalized the chain.
6311 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006312 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006313 std::swap(Lo, Hi);
6314 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00006315 MVT EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006316
Dale Johannesen2550e3a2007-10-19 20:29:00 +00006317 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6318 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006319 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman29c3cef2008-08-14 20:04:46 +00006320 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006321 SVOffset, isVolatile, Alignment);
6322 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006323 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006324 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6325 break;
6326 }
6327
6328 if (EVT == NVT)
Dan Gohman29c3cef2008-08-14 20:04:46 +00006329 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006330 SVOffset, isVolatile, Alignment);
6331 else
Dan Gohman29c3cef2008-08-14 20:04:46 +00006332 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006333 SVOffset, EVT, isVolatile,
6334 Alignment);
6335
6336 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006337 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006338
6339 if (ExtType == ISD::SEXTLOAD) {
6340 // The high part is obtained by SRA'ing all but one of the bits of the
6341 // lo part.
Duncan Sands92c43912008-06-06 12:08:01 +00006342 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006343 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6344 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6345 } else if (ExtType == ISD::ZEXTLOAD) {
6346 // The high part is just a zero.
6347 Hi = DAG.getConstant(0, NVT);
6348 } else /* if (ExtType == ISD::EXTLOAD) */ {
6349 // The high part is undefined.
6350 Hi = DAG.getNode(ISD::UNDEF, NVT);
6351 }
6352 }
6353 break;
6354 }
6355 case ISD::AND:
6356 case ISD::OR:
6357 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +00006358 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006359 ExpandOp(Node->getOperand(0), LL, LH);
6360 ExpandOp(Node->getOperand(1), RL, RH);
6361 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6362 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6363 break;
6364 }
6365 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006366 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006367 ExpandOp(Node->getOperand(1), LL, LH);
6368 ExpandOp(Node->getOperand(2), RL, RH);
6369 if (getTypeAction(NVT) == Expand)
6370 NVT = TLI.getTypeToExpandTo(NVT);
6371 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
6372 if (VT != MVT::f32)
6373 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
6374 break;
6375 }
6376 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006377 SDValue TL, TH, FL, FH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006378 ExpandOp(Node->getOperand(2), TL, TH);
6379 ExpandOp(Node->getOperand(3), FL, FH);
6380 if (getTypeAction(NVT) == Expand)
6381 NVT = TLI.getTypeToExpandTo(NVT);
6382 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6383 Node->getOperand(1), TL, FL, Node->getOperand(4));
6384 if (VT != MVT::f32)
6385 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6386 Node->getOperand(1), TH, FH, Node->getOperand(4));
6387 break;
6388 }
6389 case ISD::ANY_EXTEND:
6390 // The low part is any extension of the input (which degenerates to a copy).
6391 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6392 // The high part is undefined.
6393 Hi = DAG.getNode(ISD::UNDEF, NVT);
6394 break;
6395 case ISD::SIGN_EXTEND: {
6396 // The low part is just a sign extension of the input (which degenerates to
6397 // a copy).
6398 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
6399
6400 // The high part is obtained by SRA'ing all but one of the bits of the lo
6401 // part.
Duncan Sands92c43912008-06-06 12:08:01 +00006402 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006403 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6404 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6405 break;
6406 }
6407 case ISD::ZERO_EXTEND:
6408 // The low part is just a zero extension of the input (which degenerates to
6409 // a copy).
6410 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
6411
6412 // The high part is just a zero.
6413 Hi = DAG.getConstant(0, NVT);
6414 break;
6415
6416 case ISD::TRUNCATE: {
6417 // The input value must be larger than this value. Expand *it*.
Dan Gohman8181bd12008-07-27 21:46:04 +00006418 SDValue NewLo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006419 ExpandOp(Node->getOperand(0), NewLo, Hi);
6420
6421 // The low part is now either the right size, or it is closer. If not the
6422 // right size, make an illegal truncate so we recursively expand it.
6423 if (NewLo.getValueType() != Node->getValueType(0))
6424 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6425 ExpandOp(NewLo, Lo, Hi);
6426 break;
6427 }
6428
6429 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006430 SDValue Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006431 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6432 // If the target wants to, allow it to lower this itself.
6433 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6434 case Expand: assert(0 && "cannot expand FP!");
6435 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6436 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6437 }
6438 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6439 }
6440
6441 // f32 / f64 must be expanded to i32 / i64.
6442 if (VT == MVT::f32 || VT == MVT::f64) {
6443 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6444 if (getTypeAction(NVT) == Expand)
6445 ExpandOp(Lo, Lo, Hi);
6446 break;
6447 }
6448
6449 // If source operand will be expanded to the same type as VT, i.e.
6450 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands92c43912008-06-06 12:08:01 +00006451 MVT VT0 = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006452 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6453 ExpandOp(Node->getOperand(0), Lo, Hi);
6454 break;
6455 }
6456
6457 // Turn this into a load/store pair by default.
Gabor Greif1c80d112008-08-28 21:40:38 +00006458 if (Tmp.getNode() == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00006459 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006460
6461 ExpandOp(Tmp, Lo, Hi);
6462 break;
6463 }
6464
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006465 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006466 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6467 TargetLowering::Custom &&
6468 "Must custom expand ReadCycleCounter");
Dan Gohman8181bd12008-07-27 21:46:04 +00006469 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006470 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006471 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006472 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006473 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006474 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006475 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006476
Dale Johannesen44eb5372008-10-03 19:41:08 +00006477 case ISD::ATOMIC_CMP_SWAP_64: {
6478 // This operation does not need a loop.
6479 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6480 assert(Tmp.getNode() && "Node must be custom expanded!");
6481 ExpandOp(Tmp.getValue(0), Lo, Hi);
6482 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6483 LegalizeOp(Tmp.getValue(1)));
6484 break;
6485 }
6486
Dale Johannesenf160d802008-10-02 18:53:47 +00006487 case ISD::ATOMIC_LOAD_ADD_64:
6488 case ISD::ATOMIC_LOAD_SUB_64:
6489 case ISD::ATOMIC_LOAD_AND_64:
6490 case ISD::ATOMIC_LOAD_OR_64:
6491 case ISD::ATOMIC_LOAD_XOR_64:
6492 case ISD::ATOMIC_LOAD_NAND_64:
Dale Johannesen44eb5372008-10-03 19:41:08 +00006493 case ISD::ATOMIC_SWAP_64: {
6494 // These operations require a loop to be generated. We can't do that yet,
6495 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesenf160d802008-10-02 18:53:47 +00006496 SDValue In2Lo, In2Hi, In2;
6497 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6498 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006499 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6500 SDValue Replace =
6501 DAG.getAtomic(Op.getOpcode(), Op.getOperand(0), Op.getOperand(1), In2,
6502 Anode->getSrcValue(), Anode->getAlignment());
6503 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006504 ExpandOp(Result.getValue(0), Lo, Hi);
6505 // Remember that we legalized the chain.
6506 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharth81580822008-03-05 01:15:49 +00006507 break;
6508 }
6509
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006510 // These operators cannot be expanded directly, emit them as calls to
6511 // library functions.
6512 case ISD::FP_TO_SINT: {
6513 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006514 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006515 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6516 case Expand: assert(0 && "cannot expand FP!");
6517 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6518 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6519 }
6520
6521 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6522
6523 // Now that the custom expander is done, expand the result, which is still
6524 // VT.
Gabor Greif1c80d112008-08-28 21:40:38 +00006525 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006526 ExpandOp(Op, Lo, Hi);
6527 break;
6528 }
6529 }
6530
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006531 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6532 VT);
6533 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6534 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006535 break;
6536 }
6537
6538 case ISD::FP_TO_UINT: {
6539 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006540 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006541 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6542 case Expand: assert(0 && "cannot expand FP!");
6543 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6544 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6545 }
6546
6547 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6548
6549 // Now that the custom expander is done, expand the result.
Gabor Greif1c80d112008-08-28 21:40:38 +00006550 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006551 ExpandOp(Op, Lo, Hi);
6552 break;
6553 }
6554 }
6555
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006556 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6557 VT);
6558 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6559 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006560 break;
6561 }
6562
6563 case ISD::SHL: {
6564 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006565 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006566 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006567 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006568 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006569 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006570 // Now that the custom expander is done, expand the result, which is
6571 // still VT.
6572 ExpandOp(Op, Lo, Hi);
6573 break;
6574 }
6575 }
6576
6577 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6578 // this X << 1 as X+X.
6579 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00006580 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006581 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006582 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006583 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6584 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6585 LoOps[1] = LoOps[0];
6586 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6587
6588 HiOps[1] = HiOps[0];
6589 HiOps[2] = Lo.getValue(1);
6590 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6591 break;
6592 }
6593 }
6594
6595 // If we can emit an efficient shift operation, do so now.
6596 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6597 break;
6598
6599 // If this target supports SHL_PARTS, use it.
6600 TargetLowering::LegalizeAction Action =
6601 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6602 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6603 Action == TargetLowering::Custom) {
6604 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6605 break;
6606 }
6607
6608 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006609 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006610 break;
6611 }
6612
6613 case ISD::SRA: {
6614 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006615 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006616 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006617 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006618 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006619 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006620 // Now that the custom expander is done, expand the result, which is
6621 // still VT.
6622 ExpandOp(Op, Lo, Hi);
6623 break;
6624 }
6625 }
6626
6627 // If we can emit an efficient shift operation, do so now.
6628 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6629 break;
6630
6631 // If this target supports SRA_PARTS, use it.
6632 TargetLowering::LegalizeAction Action =
6633 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6634 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6635 Action == TargetLowering::Custom) {
6636 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6637 break;
6638 }
6639
6640 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006641 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006642 break;
6643 }
6644
6645 case ISD::SRL: {
6646 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006647 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006648 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006649 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006650 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006651 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006652 // Now that the custom expander is done, expand the result, which is
6653 // still VT.
6654 ExpandOp(Op, Lo, Hi);
6655 break;
6656 }
6657 }
6658
6659 // If we can emit an efficient shift operation, do so now.
6660 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6661 break;
6662
6663 // If this target supports SRL_PARTS, use it.
6664 TargetLowering::LegalizeAction Action =
6665 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6666 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6667 Action == TargetLowering::Custom) {
6668 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6669 break;
6670 }
6671
6672 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006673 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006674 break;
6675 }
6676
6677 case ISD::ADD:
6678 case ISD::SUB: {
6679 // If the target wants to custom expand this, let them.
6680 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6681 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006682 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006683 if (Result.getNode()) {
Duncan Sands4c3885b2008-06-22 09:42:16 +00006684 ExpandOp(Result, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006685 break;
6686 }
6687 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006688 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006689 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006690 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6691 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6692 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006693 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006694 LoOps[0] = LHSL;
6695 LoOps[1] = RHSL;
6696 HiOps[0] = LHSH;
6697 HiOps[1] = RHSH;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006698
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006699 //cascaded check to see if any smaller size has a a carry flag.
6700 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6701 bool hasCarry = false;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006702 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6703 MVT AVT = MVT::getIntegerVT(BitSize);
6704 if (TLI.isOperationLegal(OpV, AVT)) {
6705 hasCarry = true;
6706 break;
6707 }
6708 }
6709
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006710 if(hasCarry) {
Andrew Lenharth5e814462008-10-07 14:15:42 +00006711 if (Node->getOpcode() == ISD::ADD) {
6712 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6713 HiOps[2] = Lo.getValue(1);
6714 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6715 } else {
6716 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6717 HiOps[2] = Lo.getValue(1);
6718 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6719 }
6720 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006721 } else {
Andrew Lenharth5e814462008-10-07 14:15:42 +00006722 if (Node->getOpcode() == ISD::ADD) {
6723 Lo = DAG.getNode(ISD::ADD, VTList, LoOps, 2);
6724 Hi = DAG.getNode(ISD::ADD, VTList, HiOps, 2);
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006725 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6726 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006727 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6728 DAG.getConstant(1, NVT),
6729 DAG.getConstant(0, NVT));
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006730 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6731 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006732 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6733 DAG.getConstant(1, NVT),
6734 Carry1);
6735 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6736 } else {
6737 Lo = DAG.getNode(ISD::SUB, VTList, LoOps, 2);
6738 Hi = DAG.getNode(ISD::SUB, VTList, HiOps, 2);
6739 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6740 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6741 DAG.getConstant(1, NVT),
6742 DAG.getConstant(0, NVT));
6743 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6744 }
6745 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006746 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006747 }
6748
6749 case ISD::ADDC:
6750 case ISD::SUBC: {
6751 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006752 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006753 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6754 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6755 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006756 SDValue LoOps[2] = { LHSL, RHSL };
6757 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006758
6759 if (Node->getOpcode() == ISD::ADDC) {
6760 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6761 HiOps[2] = Lo.getValue(1);
6762 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6763 } else {
6764 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6765 HiOps[2] = Lo.getValue(1);
6766 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6767 }
6768 // Remember that we legalized the flag.
6769 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6770 break;
6771 }
6772 case ISD::ADDE:
6773 case ISD::SUBE: {
6774 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006775 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006776 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6777 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6778 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006779 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6780 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006781
6782 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6783 HiOps[2] = Lo.getValue(1);
6784 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6785
6786 // Remember that we legalized the flag.
6787 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6788 break;
6789 }
6790 case ISD::MUL: {
6791 // If the target wants to custom expand this, let them.
6792 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006793 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006794 if (New.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006795 ExpandOp(New, Lo, Hi);
6796 break;
6797 }
6798 }
6799
6800 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6801 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00006802 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6803 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6804 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006805 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006806 ExpandOp(Node->getOperand(0), LL, LH);
6807 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman07961cd2008-02-25 21:11:39 +00006808 unsigned OuterBitSize = Op.getValueSizeInBits();
6809 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman5a199552007-10-08 18:33:35 +00006810 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6811 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman2594d942008-03-10 20:42:19 +00006812 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6813 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6814 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman5a199552007-10-08 18:33:35 +00006815 // The inputs are both zero-extended.
6816 if (HasUMUL_LOHI) {
6817 // We can emit a umul_lohi.
6818 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00006819 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00006820 break;
6821 }
6822 if (HasMULHU) {
6823 // We can emit a mulhu+mul.
6824 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6825 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6826 break;
6827 }
Dan Gohman5a199552007-10-08 18:33:35 +00006828 }
Dan Gohman07961cd2008-02-25 21:11:39 +00006829 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman5a199552007-10-08 18:33:35 +00006830 // The input values are both sign-extended.
6831 if (HasSMUL_LOHI) {
6832 // We can emit a smul_lohi.
6833 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00006834 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00006835 break;
6836 }
6837 if (HasMULHS) {
6838 // We can emit a mulhs+mul.
6839 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6840 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6841 break;
6842 }
6843 }
6844 if (HasUMUL_LOHI) {
6845 // Lo,Hi = umul LHS, RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00006846 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman5a199552007-10-08 18:33:35 +00006847 DAG.getVTList(NVT, NVT), LL, RL);
6848 Lo = UMulLOHI;
6849 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006850 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6851 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6852 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6853 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6854 break;
6855 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00006856 if (HasMULHU) {
6857 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6858 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6859 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6860 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6861 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6862 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6863 break;
6864 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006865 }
6866
Dan Gohman5a199552007-10-08 18:33:35 +00006867 // If nothing else, we can make a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006868 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006869 break;
6870 }
6871 case ISD::SDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006872 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006873 break;
6874 case ISD::UDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006875 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006876 break;
6877 case ISD::SREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006878 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006879 break;
6880 case ISD::UREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006881 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006882 break;
6883
6884 case ISD::FADD:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006885 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
6886 RTLIB::ADD_F64,
6887 RTLIB::ADD_F80,
6888 RTLIB::ADD_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006889 Node, false, Hi);
6890 break;
6891 case ISD::FSUB:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006892 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
6893 RTLIB::SUB_F64,
6894 RTLIB::SUB_F80,
6895 RTLIB::SUB_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006896 Node, false, Hi);
6897 break;
6898 case ISD::FMUL:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006899 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
6900 RTLIB::MUL_F64,
6901 RTLIB::MUL_F80,
6902 RTLIB::MUL_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006903 Node, false, Hi);
6904 break;
6905 case ISD::FDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006906 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
6907 RTLIB::DIV_F64,
6908 RTLIB::DIV_F80,
6909 RTLIB::DIV_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006910 Node, false, Hi);
6911 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006912 case ISD::FP_EXTEND: {
Dale Johannesen4c14d512007-10-12 01:37:08 +00006913 if (VT == MVT::ppcf128) {
6914 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6915 Node->getOperand(0).getValueType()==MVT::f64);
6916 const uint64_t zero = 0;
6917 if (Node->getOperand(0).getValueType()==MVT::f32)
6918 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6919 else
6920 Hi = Node->getOperand(0);
6921 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6922 break;
6923 }
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006924 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
6925 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
6926 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006927 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006928 }
6929 case ISD::FP_ROUND: {
6930 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
6931 VT);
6932 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
6933 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006934 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006935 }
Evan Cheng5316b392008-09-09 23:02:14 +00006936 case ISD::FSQRT:
6937 case ISD::FSIN:
6938 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00006939 case ISD::FLOG:
6940 case ISD::FLOG2:
6941 case ISD::FLOG10:
6942 case ISD::FEXP:
6943 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00006944 case ISD::FTRUNC:
6945 case ISD::FFLOOR:
6946 case ISD::FCEIL:
6947 case ISD::FRINT:
6948 case ISD::FNEARBYINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00006949 case ISD::FPOW:
6950 case ISD::FPOWI: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006951 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
6952 switch(Node->getOpcode()) {
6953 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00006954 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6955 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006956 break;
6957 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00006958 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6959 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006960 break;
6961 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00006962 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6963 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006964 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00006965 case ISD::FLOG:
6966 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
6967 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
6968 break;
6969 case ISD::FLOG2:
6970 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
6971 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
6972 break;
6973 case ISD::FLOG10:
6974 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
6975 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
6976 break;
6977 case ISD::FEXP:
6978 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
6979 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
6980 break;
6981 case ISD::FEXP2:
6982 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
6983 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
6984 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00006985 case ISD::FTRUNC:
6986 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
6987 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
6988 break;
6989 case ISD::FFLOOR:
6990 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
6991 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
6992 break;
6993 case ISD::FCEIL:
6994 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
6995 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
6996 break;
6997 case ISD::FRINT:
6998 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
6999 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7000 break;
7001 case ISD::FNEARBYINT:
7002 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7003 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7004 break;
Evan Cheng5316b392008-09-09 23:02:14 +00007005 case ISD::FPOW:
7006 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7007 RTLIB::POW_PPCF128);
7008 break;
7009 case ISD::FPOWI:
7010 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7011 RTLIB::POWI_PPCF128);
7012 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007013 default: assert(0 && "Unreachable!");
7014 }
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007015 Lo = ExpandLibCall(LC, Node, false, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007016 break;
7017 }
7018 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007019 if (VT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007020 SDValue Tmp;
Dale Johannesen5707ef82007-10-12 19:02:17 +00007021 ExpandOp(Node->getOperand(0), Lo, Tmp);
7022 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7023 // lo = hi==fabs(hi) ? lo : -lo;
7024 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7025 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7026 DAG.getCondCode(ISD::SETEQ));
7027 break;
7028 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007029 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007030 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7031 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7032 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7033 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7034 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7035 if (getTypeAction(NVT) == Expand)
7036 ExpandOp(Lo, Lo, Hi);
7037 break;
7038 }
7039 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007040 if (VT == MVT::ppcf128) {
7041 ExpandOp(Node->getOperand(0), Lo, Hi);
7042 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7043 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7044 break;
7045 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007046 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007047 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7048 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7049 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7050 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7051 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7052 if (getTypeAction(NVT) == Expand)
7053 ExpandOp(Lo, Lo, Hi);
7054 break;
7055 }
7056 case ISD::FCOPYSIGN: {
7057 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7058 if (getTypeAction(NVT) == Expand)
7059 ExpandOp(Lo, Lo, Hi);
7060 break;
7061 }
7062 case ISD::SINT_TO_FP:
7063 case ISD::UINT_TO_FP: {
7064 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands92c43912008-06-06 12:08:01 +00007065 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007066
7067 // Promote the operand if needed. Do this before checking for
7068 // ppcf128 so conversions of i16 and i8 work.
7069 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007070 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesen6a779c82008-03-18 17:28:38 +00007071 Tmp = isSigned
7072 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7073 DAG.getValueType(SrcVT))
7074 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greif1c80d112008-08-28 21:40:38 +00007075 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007076 SrcVT = Node->getOperand(0).getValueType();
7077 }
7078
Dan Gohmanec51f642008-03-10 23:03:31 +00007079 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman84d00962008-02-25 21:39:34 +00007080 static const uint64_t zero = 0;
Dale Johannesen4c14d512007-10-12 01:37:08 +00007081 if (isSigned) {
7082 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7083 Node->getOperand(0)));
7084 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7085 } else {
Dan Gohman84d00962008-02-25 21:39:34 +00007086 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen4c14d512007-10-12 01:37:08 +00007087 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7088 Node->getOperand(0)));
7089 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7090 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007091 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00007092 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7093 DAG.getConstant(0, MVT::i32),
7094 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7095 DAG.getConstantFP(
7096 APFloat(APInt(128, 2, TwoE32)),
7097 MVT::ppcf128)),
7098 Hi,
7099 DAG.getCondCode(ISD::SETLT)),
7100 Lo, Hi);
7101 }
7102 break;
7103 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007104 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7105 // si64->ppcf128 done by libcall, below
Dan Gohman84d00962008-02-25 21:39:34 +00007106 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007107 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7108 Lo, Hi);
7109 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7110 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7111 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7112 DAG.getConstant(0, MVT::i64),
7113 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7114 DAG.getConstantFP(
7115 APFloat(APInt(128, 2, TwoE64)),
7116 MVT::ppcf128)),
7117 Hi,
7118 DAG.getCondCode(ISD::SETLT)),
7119 Lo, Hi);
7120 break;
7121 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007122
Dan Gohmanec51f642008-03-10 23:03:31 +00007123 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7124 Node->getOperand(0));
Evan Chenga8740032008-04-01 01:50:16 +00007125 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng4a2f6df2008-04-01 01:51:26 +00007126 // float to i32 etc. can be 'expanded' to a single node.
Evan Chenga8740032008-04-01 01:50:16 +00007127 ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007128 break;
7129 }
7130 }
7131
7132 // Make sure the resultant values have been legalized themselves, unless this
7133 // is a type that requires multi-step expansion.
7134 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7135 Lo = LegalizeOp(Lo);
Gabor Greif1c80d112008-08-28 21:40:38 +00007136 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007137 // Don't legalize the high part if it is expanded to a single node.
7138 Hi = LegalizeOp(Hi);
7139 }
7140
7141 // Remember in a map if the values will be reused later.
Dan Gohman55d19662008-07-07 17:46:23 +00007142 bool isNew =
7143 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007144 assert(isNew && "Value already expanded?!?");
7145}
7146
7147/// SplitVectorOp - Given an operand of vector type, break it down into
7148/// two smaller values, still of vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00007149void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7150 SDValue &Hi) {
Duncan Sands92c43912008-06-06 12:08:01 +00007151 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007152 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007153 unsigned NumElements = Op.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007154 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00007155
Duncan Sands92c43912008-06-06 12:08:01 +00007156 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman4a365ad2007-11-15 21:15:26 +00007157
7158 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7159 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7160
Duncan Sands92c43912008-06-06 12:08:01 +00007161 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7162 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007163
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007164 // See if we already split it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007165 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007166 = SplitNodes.find(Op);
7167 if (I != SplitNodes.end()) {
7168 Lo = I->second.first;
7169 Hi = I->second.second;
7170 return;
7171 }
7172
7173 switch (Node->getOpcode()) {
7174 default:
7175#ifndef NDEBUG
7176 Node->dump(&DAG);
7177#endif
7178 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00007179 case ISD::UNDEF:
7180 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7181 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7182 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007183 case ISD::BUILD_PAIR:
7184 Lo = Node->getOperand(0);
7185 Hi = Node->getOperand(1);
7186 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007187 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007188 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7189 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007190 unsigned Index = Idx->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007191 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007192 if (Index < NewNumElts_Lo)
7193 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7194 DAG.getIntPtrConstant(Index));
7195 else
7196 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7197 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7198 break;
7199 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007200 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007201 Node->getOperand(1),
7202 Node->getOperand(2));
7203 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007204 break;
7205 }
Chris Lattner587c46d2007-11-19 21:16:54 +00007206 case ISD::VECTOR_SHUFFLE: {
7207 // Build the low part.
Dan Gohman8181bd12008-07-27 21:46:04 +00007208 SDValue Mask = Node->getOperand(2);
7209 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +00007210 MVT PtrVT = TLI.getPointerTy();
Chris Lattner587c46d2007-11-19 21:16:54 +00007211
7212 // Insert all of the elements from the input that are needed. We use
7213 // buildvector of extractelement here because the input vectors will have
7214 // to be legalized, so this makes the code simpler.
7215 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007216 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007217 if (IdxNode.getOpcode() == ISD::UNDEF) {
7218 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7219 continue;
7220 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007221 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007222 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007223 if (Idx >= NumElements) {
7224 InVec = Node->getOperand(1);
7225 Idx -= NumElements;
7226 }
7227 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7228 DAG.getConstant(Idx, PtrVT)));
7229 }
7230 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7231 Ops.clear();
7232
7233 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007234 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007235 if (IdxNode.getOpcode() == ISD::UNDEF) {
7236 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7237 continue;
7238 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007239 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007240 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007241 if (Idx >= NumElements) {
7242 InVec = Node->getOperand(1);
7243 Idx -= NumElements;
7244 }
7245 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7246 DAG.getConstant(Idx, PtrVT)));
7247 }
Mon P Wang2e89b112008-07-25 01:30:26 +00007248 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner587c46d2007-11-19 21:16:54 +00007249 break;
7250 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007251 case ISD::BUILD_VECTOR: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007252 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00007253 Node->op_begin()+NewNumElts_Lo);
7254 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007255
Dan Gohman8181bd12008-07-27 21:46:04 +00007256 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007257 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007258 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007259 break;
7260 }
7261 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00007262 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007263 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7264 if (NewNumSubvectors == 1) {
7265 Lo = Node->getOperand(0);
7266 Hi = Node->getOperand(1);
7267 } else {
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007268 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7269 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007270 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007271
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007272 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007273 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007274 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007275 }
7276 break;
7277 }
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007278 case ISD::EXTRACT_SUBVECTOR: {
7279 SDValue Vec = Op.getOperand(0);
7280 SDValue Idx = Op.getOperand(1);
7281 MVT IdxVT = Idx.getValueType();
7282
7283 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7284 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7285 if (CIdx) {
7286 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7287 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7288 IdxVT));
7289 } else {
7290 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7291 DAG.getConstant(NewNumElts_Lo, IdxVT));
7292 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7293 }
7294 break;
7295 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00007296 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007297 SDValue Cond = Node->getOperand(0);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007298
Dan Gohman8181bd12008-07-27 21:46:04 +00007299 SDValue LL, LH, RL, RH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007300 SplitVectorOp(Node->getOperand(1), LL, LH);
7301 SplitVectorOp(Node->getOperand(2), RL, RH);
7302
Duncan Sands92c43912008-06-06 12:08:01 +00007303 if (Cond.getValueType().isVector()) {
Dan Gohmand5d4c872007-10-17 14:48:28 +00007304 // Handle a vector merge.
Dan Gohman8181bd12008-07-27 21:46:04 +00007305 SDValue CL, CH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007306 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007307 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7308 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007309 } else {
7310 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00007311 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7312 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007313 }
7314 break;
7315 }
Chris Lattnerc7471452008-06-30 02:43:01 +00007316 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007317 SDValue CondLHS = Node->getOperand(0);
7318 SDValue CondRHS = Node->getOperand(1);
7319 SDValue CondCode = Node->getOperand(4);
Chris Lattnerc7471452008-06-30 02:43:01 +00007320
Dan Gohman8181bd12008-07-27 21:46:04 +00007321 SDValue LL, LH, RL, RH;
Chris Lattnerc7471452008-06-30 02:43:01 +00007322 SplitVectorOp(Node->getOperand(2), LL, LH);
7323 SplitVectorOp(Node->getOperand(3), RL, RH);
7324
7325 // Handle a simple select with vector operands.
7326 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7327 LL, RL, CondCode);
7328 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7329 LH, RH, CondCode);
7330 break;
7331 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00007332 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007333 SDValue LL, LH, RL, RH;
Nate Begeman9a1ce152008-05-12 19:40:03 +00007334 SplitVectorOp(Node->getOperand(0), LL, LH);
7335 SplitVectorOp(Node->getOperand(1), RL, RH);
7336 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7337 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7338 break;
7339 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007340 case ISD::ADD:
7341 case ISD::SUB:
7342 case ISD::MUL:
7343 case ISD::FADD:
7344 case ISD::FSUB:
7345 case ISD::FMUL:
7346 case ISD::SDIV:
7347 case ISD::UDIV:
7348 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007349 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007350 case ISD::AND:
7351 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00007352 case ISD::XOR:
7353 case ISD::UREM:
7354 case ISD::SREM:
7355 case ISD::FREM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007356 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007357 SplitVectorOp(Node->getOperand(0), LL, LH);
7358 SplitVectorOp(Node->getOperand(1), RL, RH);
7359
Nate Begeman4a365ad2007-11-15 21:15:26 +00007360 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7361 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007362 break;
7363 }
Dan Gohman29c3cef2008-08-14 20:04:46 +00007364 case ISD::FP_ROUND:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007365 case ISD::FPOWI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007366 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007367 SplitVectorOp(Node->getOperand(0), L, H);
7368
Nate Begeman4a365ad2007-11-15 21:15:26 +00007369 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7370 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00007371 break;
7372 }
7373 case ISD::CTTZ:
7374 case ISD::CTLZ:
7375 case ISD::CTPOP:
7376 case ISD::FNEG:
7377 case ISD::FABS:
7378 case ISD::FSQRT:
7379 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00007380 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007381 case ISD::FLOG:
7382 case ISD::FLOG2:
7383 case ISD::FLOG10:
7384 case ISD::FEXP:
7385 case ISD::FEXP2:
Nate Begeman78246ca2007-11-17 03:58:34 +00007386 case ISD::FP_TO_SINT:
7387 case ISD::FP_TO_UINT:
7388 case ISD::SINT_TO_FP:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007389 case ISD::UINT_TO_FP:
7390 case ISD::TRUNCATE:
7391 case ISD::ANY_EXTEND:
7392 case ISD::SIGN_EXTEND:
7393 case ISD::ZERO_EXTEND:
7394 case ISD::FP_EXTEND: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007395 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007396 SplitVectorOp(Node->getOperand(0), L, H);
7397
Nate Begeman4a365ad2007-11-15 21:15:26 +00007398 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7399 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00007400 break;
7401 }
Mon P Wang73d31542008-11-10 20:54:11 +00007402 case ISD::CONVERT_RNDSAT: {
7403 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7404 SDValue L, H;
7405 SplitVectorOp(Node->getOperand(0), L, H);
7406 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7407 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7408 SDValue STyOpL = DAG.getValueType(L.getValueType());
7409 SDValue STyOpH = DAG.getValueType(H.getValueType());
7410
7411 SDValue RndOp = Node->getOperand(3);
7412 SDValue SatOp = Node->getOperand(4);
7413
7414 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7415 RndOp, SatOp, CvtCode);
7416 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7417 RndOp, SatOp, CvtCode);
7418 break;
7419 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007420 case ISD::LOAD: {
7421 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007422 SDValue Ch = LD->getChain();
7423 SDValue Ptr = LD->getBasePtr();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007424 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007425 const Value *SV = LD->getSrcValue();
7426 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007427 MVT MemoryVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007428 unsigned Alignment = LD->getAlignment();
7429 bool isVolatile = LD->isVolatile();
7430
Dan Gohman29c3cef2008-08-14 20:04:46 +00007431 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7432 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7433
7434 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7435 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7436 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7437
7438 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7439 NewVT_Lo, Ch, Ptr, Offset,
7440 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7441 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007442 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00007443 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007444 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00007445 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00007446 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7447 NewVT_Hi, Ch, Ptr, Offset,
7448 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007449
7450 // Build a factor node to remember that this load is independent of the
7451 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00007452 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007453 Hi.getValue(1));
7454
7455 // Remember that we legalized the chain.
7456 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
7457 break;
7458 }
7459 case ISD::BIT_CONVERT: {
7460 // We know the result is a vector. The input may be either a vector or a
7461 // scalar value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007462 SDValue InOp = Node->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007463 if (!InOp.getValueType().isVector() ||
7464 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007465 // The input is a scalar or single-element vector.
7466 // Lower to a store/load so that it can be split.
7467 // FIXME: this could be improved probably.
Mon P Wang36b59ac2008-07-15 05:28:34 +00007468 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7469 Op.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00007470 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greif1c80d112008-08-28 21:40:38 +00007471 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007472
Dan Gohman8181bd12008-07-27 21:46:04 +00007473 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00007474 InOp, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007475 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00007476 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007477 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007478 }
7479 // Split the vector and convert each of the pieces now.
7480 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007481 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7482 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007483 break;
7484 }
7485 }
7486
7487 // Remember in a map if the values will be reused later.
7488 bool isNew =
7489 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
7490 assert(isNew && "Value already split?!?");
7491}
7492
7493
7494/// ScalarizeVectorOp - Given an operand of single-element vector type
7495/// (e.g. v1f32), convert it into the equivalent operation that returns a
7496/// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007497SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00007498 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007499 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007500 MVT NewVT = Op.getValueType().getVectorElementType();
7501 assert(Op.getValueType().getVectorNumElements() == 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007502
7503 // See if we already scalarized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007504 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007505 if (I != ScalarizedNodes.end()) return I->second;
7506
Dan Gohman8181bd12008-07-27 21:46:04 +00007507 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007508 switch (Node->getOpcode()) {
7509 default:
7510#ifndef NDEBUG
7511 Node->dump(&DAG); cerr << "\n";
7512#endif
7513 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7514 case ISD::ADD:
7515 case ISD::FADD:
7516 case ISD::SUB:
7517 case ISD::FSUB:
7518 case ISD::MUL:
7519 case ISD::FMUL:
7520 case ISD::SDIV:
7521 case ISD::UDIV:
7522 case ISD::FDIV:
7523 case ISD::SREM:
7524 case ISD::UREM:
7525 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007526 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007527 case ISD::AND:
7528 case ISD::OR:
7529 case ISD::XOR:
7530 Result = DAG.getNode(Node->getOpcode(),
7531 NewVT,
7532 ScalarizeVectorOp(Node->getOperand(0)),
7533 ScalarizeVectorOp(Node->getOperand(1)));
7534 break;
7535 case ISD::FNEG:
7536 case ISD::FABS:
7537 case ISD::FSQRT:
7538 case ISD::FSIN:
7539 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007540 case ISD::FLOG:
7541 case ISD::FLOG2:
7542 case ISD::FLOG10:
7543 case ISD::FEXP:
7544 case ISD::FEXP2:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007545 case ISD::FP_TO_SINT:
7546 case ISD::FP_TO_UINT:
7547 case ISD::SINT_TO_FP:
7548 case ISD::UINT_TO_FP:
7549 case ISD::SIGN_EXTEND:
7550 case ISD::ZERO_EXTEND:
7551 case ISD::ANY_EXTEND:
7552 case ISD::TRUNCATE:
7553 case ISD::FP_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007554 Result = DAG.getNode(Node->getOpcode(),
7555 NewVT,
7556 ScalarizeVectorOp(Node->getOperand(0)));
7557 break;
Mon P Wang73d31542008-11-10 20:54:11 +00007558 case ISD::CONVERT_RNDSAT: {
7559 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7560 Result = DAG.getConvertRndSat(NewVT, Op0,
7561 DAG.getValueType(NewVT),
7562 DAG.getValueType(Op0.getValueType()),
7563 Node->getOperand(3),
7564 Node->getOperand(4),
7565 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7566 break;
7567 }
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007568 case ISD::FPOWI:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007569 case ISD::FP_ROUND:
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007570 Result = DAG.getNode(Node->getOpcode(),
7571 NewVT,
7572 ScalarizeVectorOp(Node->getOperand(0)),
7573 Node->getOperand(1));
7574 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007575 case ISD::LOAD: {
7576 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007577 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7578 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman29c3cef2008-08-14 20:04:46 +00007579 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007580 const Value *SV = LD->getSrcValue();
7581 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007582 MVT MemoryVT = LD->getMemoryVT();
7583 unsigned Alignment = LD->getAlignment();
7584 bool isVolatile = LD->isVolatile();
7585
7586 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7587 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7588
7589 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7590 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7591 MemoryVT.getVectorElementType(),
7592 isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007593
7594 // Remember that we legalized the chain.
7595 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7596 break;
7597 }
7598 case ISD::BUILD_VECTOR:
7599 Result = Node->getOperand(0);
7600 break;
7601 case ISD::INSERT_VECTOR_ELT:
7602 // Returning the inserted scalar element.
7603 Result = Node->getOperand(1);
7604 break;
7605 case ISD::CONCAT_VECTORS:
7606 assert(Node->getOperand(0).getValueType() == NewVT &&
7607 "Concat of non-legal vectors not yet supported!");
7608 Result = Node->getOperand(0);
7609 break;
7610 case ISD::VECTOR_SHUFFLE: {
7611 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007612 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007613 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007614 Result = ScalarizeVectorOp(Node->getOperand(1));
7615 else
7616 Result = ScalarizeVectorOp(Node->getOperand(0));
7617 break;
7618 }
7619 case ISD::EXTRACT_SUBVECTOR:
Mon P Wang927daf52008-11-06 22:52:21 +00007620 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007621 Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007622 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007623 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007624 SDValue Op0 = Op.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007625 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng2cc16e72008-05-16 17:19:05 +00007626 Op0 = ScalarizeVectorOp(Op0);
7627 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007628 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007629 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007630 case ISD::SELECT:
7631 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
7632 ScalarizeVectorOp(Op.getOperand(1)),
7633 ScalarizeVectorOp(Op.getOperand(2)));
7634 break;
Chris Lattnerc7471452008-06-30 02:43:01 +00007635 case ISD::SELECT_CC:
7636 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7637 Node->getOperand(1),
7638 ScalarizeVectorOp(Op.getOperand(2)),
7639 ScalarizeVectorOp(Op.getOperand(3)),
7640 Node->getOperand(4));
7641 break;
Nate Begeman78ca4f92008-05-12 23:09:43 +00007642 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007643 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7644 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Nate Begeman78ca4f92008-05-12 23:09:43 +00007645 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7646 Op.getOperand(2));
7647 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7648 DAG.getConstant(-1ULL, NewVT),
7649 DAG.getConstant(0ULL, NewVT));
7650 break;
7651 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007652 }
7653
7654 if (TLI.isTypeLegal(NewVT))
7655 Result = LegalizeOp(Result);
7656 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7657 assert(isNew && "Value already scalarized?");
7658 return Result;
7659}
7660
7661
Mon P Wang1448aad2008-10-30 08:01:45 +00007662SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7663 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7664 if (I != WidenNodes.end()) return I->second;
7665
7666 MVT VT = Op.getValueType();
7667 assert(VT.isVector() && "Cannot widen non-vector type!");
7668
7669 SDValue Result;
7670 SDNode *Node = Op.getNode();
7671 MVT EVT = VT.getVectorElementType();
7672
7673 unsigned NumElts = VT.getVectorNumElements();
7674 unsigned NewNumElts = WidenVT.getVectorNumElements();
7675 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7676 assert(NewNumElts < 17);
7677
7678 // When widen is called, it is assumed that it is more efficient to use a
7679 // wide type. The default action is to widen to operation to a wider legal
7680 // vector type and then do the operation if it is legal by calling LegalizeOp
7681 // again. If there is no vector equivalent, we will unroll the operation, do
7682 // it, and rebuild the vector. If most of the operations are vectorizible to
7683 // the legal type, the resulting code will be more efficient. If this is not
7684 // the case, the resulting code will preform badly as we end up generating
7685 // code to pack/unpack the results. It is the function that calls widen
Mon P Wanga5a239f2008-11-06 05:31:54 +00007686 // that is responsible for seeing this doesn't happen.
Mon P Wang1448aad2008-10-30 08:01:45 +00007687 switch (Node->getOpcode()) {
7688 default:
7689#ifndef NDEBUG
7690 Node->dump(&DAG);
7691#endif
7692 assert(0 && "Unexpected operation in WidenVectorOp!");
7693 break;
7694 case ISD::CopyFromReg:
7695 assert(0 && "CopyFromReg must be legal!");
7696 case ISD::UNDEF:
7697 case ISD::Constant:
7698 case ISD::ConstantFP:
7699 // To build a vector of these elements, clients should call BuildVector
7700 // and with each element instead of creating a node with a vector type
7701 assert(0 && "Unexpected operation in WidenVectorOp!");
7702 case ISD::VAARG:
7703 // Variable Arguments with vector types doesn't make any sense to me
7704 assert(0 && "Unexpected operation in WidenVectorOp!");
7705 break;
7706 case ISD::BUILD_VECTOR: {
7707 // Build a vector with undefined for the new nodes
7708 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7709 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7710 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7711 }
7712 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7713 break;
7714 }
7715 case ISD::INSERT_VECTOR_ELT: {
7716 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7717 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7718 Node->getOperand(1), Node->getOperand(2));
7719 break;
7720 }
7721 case ISD::VECTOR_SHUFFLE: {
7722 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7723 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7724 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7725 // used as permutation array. We build the vector here instead of widening
7726 // because we don't want to legalize and have it turned to something else.
7727 SDValue PermOp = Node->getOperand(2);
7728 SDValueVector NewOps;
7729 MVT PVT = PermOp.getValueType().getVectorElementType();
7730 for (unsigned i = 0; i < NumElts; ++i) {
7731 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7732 NewOps.push_back(PermOp.getOperand(i));
7733 } else {
7734 unsigned Idx =
7735 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
7736 if (Idx < NumElts) {
7737 NewOps.push_back(PermOp.getOperand(i));
7738 }
7739 else {
7740 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7741 PermOp.getOperand(i).getValueType()));
7742 }
7743 }
7744 }
7745 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7746 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
7747 }
7748
7749 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
7750 MVT::getVectorVT(PVT, NewOps.size()),
7751 &NewOps[0], NewOps.size());
7752
7753 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
7754 break;
7755 }
7756 case ISD::LOAD: {
7757 // If the load widen returns true, we can use a single load for the
7758 // vector. Otherwise, it is returning a token factor for multiple
7759 // loads.
7760 SDValue TFOp;
7761 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
7762 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
7763 else
7764 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
7765 break;
7766 }
7767
7768 case ISD::BIT_CONVERT: {
7769 SDValue Tmp1 = Node->getOperand(0);
7770 // Converts between two different types so we need to determine
7771 // the correct widen type for the input operand.
7772 MVT TVT = Tmp1.getValueType();
7773 assert(TVT.isVector() && "can not widen non vector type");
7774 MVT TEVT = TVT.getVectorElementType();
7775 assert(WidenVT.getSizeInBits() % EVT.getSizeInBits() == 0 &&
7776 "can not widen bit bit convert that are not multiple of element type");
7777 MVT TWidenVT = MVT::getVectorVT(TEVT,
7778 WidenVT.getSizeInBits()/EVT.getSizeInBits());
7779 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7780 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
7781 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7782
7783 TargetLowering::LegalizeAction action =
7784 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7785 switch (action) {
7786 default: assert(0 && "action not supported");
7787 case TargetLowering::Legal:
7788 break;
7789 case TargetLowering::Promote:
7790 // We defer the promotion to when we legalize the op
7791 break;
7792 case TargetLowering::Expand:
7793 // Expand the operation into a bunch of nasty scalar code.
7794 Result = LegalizeOp(UnrollVectorOp(Result));
7795 break;
7796 }
7797 break;
7798 }
7799
7800 case ISD::SINT_TO_FP:
7801 case ISD::UINT_TO_FP:
7802 case ISD::FP_TO_SINT:
7803 case ISD::FP_TO_UINT: {
7804 SDValue Tmp1 = Node->getOperand(0);
7805 // Converts between two different types so we need to determine
7806 // the correct widen type for the input operand.
7807 MVT TVT = Tmp1.getValueType();
7808 assert(TVT.isVector() && "can not widen non vector type");
7809 MVT TEVT = TVT.getVectorElementType();
7810 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
7811 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7812 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
7813 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7814
7815 TargetLowering::LegalizeAction action =
7816 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7817 switch (action) {
7818 default: assert(0 && "action not supported");
7819 case TargetLowering::Legal:
7820 break;
7821 case TargetLowering::Promote:
7822 // We defer the promotion to when we legalize the op
7823 break;
7824 case TargetLowering::Expand:
7825 // Expand the operation into a bunch of nasty scalar code.
7826 Result = LegalizeOp(UnrollVectorOp(Result));
7827 break;
7828 }
7829 break;
7830 }
7831
7832 case ISD::FP_EXTEND:
7833 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
7834 case ISD::TRUNCATE:
7835 case ISD::SIGN_EXTEND:
7836 case ISD::ZERO_EXTEND:
7837 case ISD::ANY_EXTEND:
7838 case ISD::FP_ROUND:
7839 case ISD::SIGN_EXTEND_INREG:
7840 case ISD::FABS:
7841 case ISD::FNEG:
7842 case ISD::FSQRT:
7843 case ISD::FSIN:
7844 case ISD::FCOS: {
7845 // Unary op widening
7846 SDValue Tmp1;
7847 TargetLowering::LegalizeAction action =
7848 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7849
7850 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7851 assert(Tmp1.getValueType() == WidenVT);
7852 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7853 switch (action) {
7854 default: assert(0 && "action not supported");
7855 case TargetLowering::Legal:
7856 break;
7857 case TargetLowering::Promote:
7858 // We defer the promotion to when we legalize the op
7859 break;
7860 case TargetLowering::Expand:
7861 // Expand the operation into a bunch of nasty scalar code.
7862 Result = LegalizeOp(UnrollVectorOp(Result));
7863 break;
7864 }
7865 break;
7866 }
Mon P Wang73d31542008-11-10 20:54:11 +00007867 case ISD::CONVERT_RNDSAT: {
7868 SDValue RndOp = Node->getOperand(3);
7869 SDValue SatOp = Node->getOperand(4);
7870
7871 TargetLowering::LegalizeAction action =
7872 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7873
7874 SDValue SrcOp = Node->getOperand(0);
7875
7876 // Converts between two different types so we need to determine
7877 // the correct widen type for the input operand.
7878 MVT SVT = SrcOp.getValueType();
7879 assert(SVT.isVector() && "can not widen non vector type");
7880 MVT SEVT = SVT.getVectorElementType();
7881 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
7882
7883 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
7884 assert(SrcOp.getValueType() == WidenVT);
7885 SDValue DTyOp = DAG.getValueType(WidenVT);
7886 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
7887 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7888
7889 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
7890 RndOp, SatOp, CvtCode);
7891 switch (action) {
7892 default: assert(0 && "action not supported");
7893 case TargetLowering::Legal:
7894 break;
7895 case TargetLowering::Promote:
7896 // We defer the promotion to when we legalize the op
7897 break;
7898 case TargetLowering::Expand:
7899 // Expand the operation into a bunch of nasty scalar code.
7900 Result = LegalizeOp(UnrollVectorOp(Result));
7901 break;
7902 }
7903 break;
7904 }
Mon P Wang1448aad2008-10-30 08:01:45 +00007905 case ISD::FPOW:
7906 case ISD::FPOWI:
7907 case ISD::ADD:
7908 case ISD::SUB:
7909 case ISD::MUL:
7910 case ISD::MULHS:
7911 case ISD::MULHU:
7912 case ISD::AND:
7913 case ISD::OR:
7914 case ISD::XOR:
7915 case ISD::FADD:
7916 case ISD::FSUB:
7917 case ISD::FMUL:
7918 case ISD::SDIV:
7919 case ISD::SREM:
7920 case ISD::FDIV:
7921 case ISD::FREM:
7922 case ISD::FCOPYSIGN:
7923 case ISD::UDIV:
7924 case ISD::UREM:
7925 case ISD::BSWAP: {
7926 // Binary op widening
7927 TargetLowering::LegalizeAction action =
7928 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7929
7930 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7931 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7932 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
7933 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
7934 switch (action) {
7935 default: assert(0 && "action not supported");
7936 case TargetLowering::Legal:
7937 break;
7938 case TargetLowering::Promote:
7939 // We defer the promotion to when we legalize the op
7940 break;
7941 case TargetLowering::Expand:
7942 // Expand the operation into a bunch of nasty scalar code by first
7943 // Widening to the right type and then unroll the beast.
7944 Result = LegalizeOp(UnrollVectorOp(Result));
7945 break;
7946 }
7947 break;
7948 }
7949
7950 case ISD::SHL:
7951 case ISD::SRA:
7952 case ISD::SRL: {
7953 // Binary op with one non vector operand
7954 TargetLowering::LegalizeAction action =
7955 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7956
7957 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7958 assert(Tmp1.getValueType() == WidenVT);
7959 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Node->getOperand(1));
7960 switch (action) {
7961 default: assert(0 && "action not supported");
7962 case TargetLowering::Legal:
7963 break;
7964 case TargetLowering::Promote:
7965 // We defer the promotion to when we legalize the op
7966 break;
7967 case TargetLowering::Expand:
7968 // Expand the operation into a bunch of nasty scalar code.
7969 Result = LegalizeOp(UnrollVectorOp(Result));
7970 break;
7971 }
7972 break;
7973 }
7974 case ISD::EXTRACT_VECTOR_ELT: {
7975 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7976 assert(Tmp1.getValueType() == WidenVT);
7977 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
7978 break;
7979 }
7980 case ISD::CONCAT_VECTORS: {
7981 // We concurrently support only widen on a multiple of the incoming vector.
7982 // We could widen on a multiple of the incoming operand if necessary.
7983 unsigned NumConcat = NewNumElts / NumElts;
7984 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
7985 std::vector<SDValue> UnOps(NumElts, DAG.getNode(ISD::UNDEF,
7986 VT.getVectorElementType()));
7987 SDValue UndefVal = DAG.getNode(ISD::BUILD_VECTOR, VT,
7988 &UnOps[0], UnOps.size());
7989 SmallVector<SDValue, 8> MOps;
7990 MOps.push_back(Op);
7991 for (unsigned i = 1; i != NumConcat; ++i) {
7992 MOps.push_back(UndefVal);
7993 }
7994 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
7995 &MOps[0], MOps.size()));
7996 break;
7997 }
7998 case ISD::EXTRACT_SUBVECTOR: {
7999 SDValue Tmp1;
8000
8001 // The incoming vector might already be the proper type
8002 if (Node->getOperand(0).getValueType() != WidenVT)
8003 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8004 else
8005 Tmp1 = Node->getOperand(0);
8006 assert(Tmp1.getValueType() == WidenVT);
8007 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Node->getOperand(1));
8008 break;
8009 }
8010
8011 case ISD::SELECT: {
8012 TargetLowering::LegalizeAction action =
8013 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8014
8015 // Determine new condition widen type and widen
8016 SDValue Cond1 = Node->getOperand(0);
8017 MVT CondVT = Cond1.getValueType();
8018 assert(CondVT.isVector() && "can not widen non vector type");
8019 MVT CondEVT = CondVT.getVectorElementType();
8020 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8021 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8022 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8023
8024 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8025 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8026 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8027 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
8028 switch (action) {
8029 default: assert(0 && "action not supported");
8030 case TargetLowering::Legal:
8031 break;
8032 case TargetLowering::Promote:
8033 // We defer the promotion to when we legalize the op
8034 break;
8035 case TargetLowering::Expand:
8036 // Expand the operation into a bunch of nasty scalar code by first
8037 // Widening to the right type and then unroll the beast.
8038 Result = LegalizeOp(UnrollVectorOp(Result));
8039 break;
8040 }
8041 break;
8042 }
8043
8044 case ISD::SELECT_CC: {
8045 TargetLowering::LegalizeAction action =
8046 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8047
8048 // Determine new condition widen type and widen
8049 SDValue Cond1 = Node->getOperand(0);
8050 SDValue Cond2 = Node->getOperand(1);
8051 MVT CondVT = Cond1.getValueType();
8052 assert(CondVT.isVector() && "can not widen non vector type");
8053 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8054 MVT CondEVT = CondVT.getVectorElementType();
8055 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8056 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8057 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8058 assert(Cond1.getValueType() == CondWidenVT &&
8059 Cond2.getValueType() == CondWidenVT && "condition not widen");
8060
8061 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8062 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8063 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8064 "operands not widen");
8065 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8066 Tmp2, Node->getOperand(4));
8067 switch (action) {
8068 default: assert(0 && "action not supported");
8069 case TargetLowering::Legal:
8070 break;
8071 case TargetLowering::Promote:
8072 // We defer the promotion to when we legalize the op
8073 break;
8074 case TargetLowering::Expand:
8075 // Expand the operation into a bunch of nasty scalar code by first
8076 // Widening to the right type and then unroll the beast.
8077 Result = LegalizeOp(UnrollVectorOp(Result));
8078 break;
8079 }
8080 break;
Mon P Wang42ac14e2008-10-30 18:21:52 +00008081 }
8082 case ISD::VSETCC: {
8083 // Determine widen for the operand
8084 SDValue Tmp1 = Node->getOperand(0);
8085 MVT TmpVT = Tmp1.getValueType();
8086 assert(TmpVT.isVector() && "can not widen non vector type");
8087 MVT TmpEVT = TmpVT.getVectorElementType();
8088 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8089 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8090 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
8091 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
8092 Node->getOperand(2));
Mon P Wang1448aad2008-10-30 08:01:45 +00008093 break;
8094 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008095 case ISD::ATOMIC_CMP_SWAP_8:
8096 case ISD::ATOMIC_CMP_SWAP_16:
8097 case ISD::ATOMIC_CMP_SWAP_32:
8098 case ISD::ATOMIC_CMP_SWAP_64:
8099 case ISD::ATOMIC_LOAD_ADD_8:
8100 case ISD::ATOMIC_LOAD_SUB_8:
8101 case ISD::ATOMIC_LOAD_AND_8:
8102 case ISD::ATOMIC_LOAD_OR_8:
8103 case ISD::ATOMIC_LOAD_XOR_8:
8104 case ISD::ATOMIC_LOAD_NAND_8:
8105 case ISD::ATOMIC_LOAD_MIN_8:
8106 case ISD::ATOMIC_LOAD_MAX_8:
8107 case ISD::ATOMIC_LOAD_UMIN_8:
8108 case ISD::ATOMIC_LOAD_UMAX_8:
8109 case ISD::ATOMIC_SWAP_8:
8110 case ISD::ATOMIC_LOAD_ADD_16:
8111 case ISD::ATOMIC_LOAD_SUB_16:
8112 case ISD::ATOMIC_LOAD_AND_16:
8113 case ISD::ATOMIC_LOAD_OR_16:
8114 case ISD::ATOMIC_LOAD_XOR_16:
8115 case ISD::ATOMIC_LOAD_NAND_16:
8116 case ISD::ATOMIC_LOAD_MIN_16:
8117 case ISD::ATOMIC_LOAD_MAX_16:
8118 case ISD::ATOMIC_LOAD_UMIN_16:
8119 case ISD::ATOMIC_LOAD_UMAX_16:
8120 case ISD::ATOMIC_SWAP_16:
8121 case ISD::ATOMIC_LOAD_ADD_32:
8122 case ISD::ATOMIC_LOAD_SUB_32:
8123 case ISD::ATOMIC_LOAD_AND_32:
8124 case ISD::ATOMIC_LOAD_OR_32:
8125 case ISD::ATOMIC_LOAD_XOR_32:
8126 case ISD::ATOMIC_LOAD_NAND_32:
8127 case ISD::ATOMIC_LOAD_MIN_32:
8128 case ISD::ATOMIC_LOAD_MAX_32:
8129 case ISD::ATOMIC_LOAD_UMIN_32:
8130 case ISD::ATOMIC_LOAD_UMAX_32:
8131 case ISD::ATOMIC_SWAP_32:
8132 case ISD::ATOMIC_LOAD_ADD_64:
8133 case ISD::ATOMIC_LOAD_SUB_64:
8134 case ISD::ATOMIC_LOAD_AND_64:
8135 case ISD::ATOMIC_LOAD_OR_64:
8136 case ISD::ATOMIC_LOAD_XOR_64:
8137 case ISD::ATOMIC_LOAD_NAND_64:
8138 case ISD::ATOMIC_LOAD_MIN_64:
8139 case ISD::ATOMIC_LOAD_MAX_64:
8140 case ISD::ATOMIC_LOAD_UMIN_64:
8141 case ISD::ATOMIC_LOAD_UMAX_64:
8142 case ISD::ATOMIC_SWAP_64: {
8143 // For now, we assume that using vectors for these operations don't make
8144 // much sense so we just split it. We return an empty result
8145 SDValue X, Y;
8146 SplitVectorOp(Op, X, Y);
8147 return Result;
8148 break;
8149 }
8150
8151 } // end switch (Node->getOpcode())
8152
8153 assert(Result.getNode() && "Didn't set a result!");
8154 if (Result != Op)
8155 Result = LegalizeOp(Result);
8156
Mon P Wanga5a239f2008-11-06 05:31:54 +00008157 AddWidenedOperand(Op, Result);
Mon P Wang1448aad2008-10-30 08:01:45 +00008158 return Result;
8159}
8160
8161// Utility function to find a legal vector type and its associated element
8162// type from a preferred width and whose vector type must be the same size
8163// as the VVT.
8164// TLI: Target lowering used to determine legal types
8165// Width: Preferred width of element type
8166// VVT: Vector value type whose size we must match.
8167// Returns VecEVT and EVT - the vector type and its associated element type
8168static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8169 MVT& EVT, MVT& VecEVT) {
8170 // We start with the preferred width, make it a power of 2 and see if
8171 // we can find a vector type of that width. If not, we reduce it by
8172 // another power of 2. If we have widen the type, a vector of bytes should
8173 // always be legal.
8174 assert(TLI.isTypeLegal(VVT));
8175 unsigned EWidth = Width + 1;
8176 do {
8177 assert(EWidth > 0);
8178 EWidth = (1 << Log2_32(EWidth-1));
8179 EVT = MVT::getIntegerVT(EWidth);
8180 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8181 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8182 } while (!TLI.isTypeLegal(VecEVT) ||
8183 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8184}
8185
8186SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8187 SDValue Chain,
8188 SDValue BasePtr,
8189 const Value *SV,
8190 int SVOffset,
8191 unsigned Alignment,
8192 bool isVolatile,
8193 unsigned LdWidth,
8194 MVT ResType) {
8195 // We assume that we have good rules to handle loading power of two loads so
8196 // we break down the operations to power of 2 loads. The strategy is to
8197 // load the largest power of 2 that we can easily transform to a legal vector
8198 // and then insert into that vector, and the cast the result into the legal
8199 // vector that we want. This avoids unnecessary stack converts.
8200 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8201 // the load is nonvolatile, we an use a wider load for the value.
8202 // Find a vector length we can load a large chunk
8203 MVT EVT, VecEVT;
8204 unsigned EVTWidth;
8205 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8206 EVTWidth = EVT.getSizeInBits();
8207
8208 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8209 isVolatile, Alignment);
8210 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8211 LdChain.push_back(LdOp.getValue(1));
8212
8213 // Check if we can load the element with one instruction
8214 if (LdWidth == EVTWidth) {
8215 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8216 }
8217
8218 // The vector element order is endianness dependent.
8219 unsigned Idx = 1;
8220 LdWidth -= EVTWidth;
8221 unsigned Offset = 0;
8222
8223 while (LdWidth > 0) {
8224 unsigned Increment = EVTWidth / 8;
8225 Offset += Increment;
8226 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8227 DAG.getIntPtrConstant(Increment));
8228
8229 if (LdWidth < EVTWidth) {
8230 // Our current type we are using is too large, use a smaller size by
8231 // using a smaller power of 2
8232 unsigned oEVTWidth = EVTWidth;
8233 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8234 EVTWidth = EVT.getSizeInBits();
8235 // Readjust position and vector position based on new load type
8236 Idx = Idx * (oEVTWidth/EVTWidth)+1;
8237 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8238 }
8239
8240 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8241 SVOffset+Offset, isVolatile,
8242 MinAlign(Alignment, Offset));
8243 LdChain.push_back(LdOp.getValue(1));
8244 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8245 DAG.getIntPtrConstant(Idx++));
8246
8247 LdWidth -= EVTWidth;
8248 }
8249
8250 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8251}
8252
8253bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8254 SDValue& TFOp,
8255 SDValue Op,
8256 MVT NVT) {
8257 // TODO: Add support for ConcatVec and the ability to load many vector
8258 // types (e.g., v4i8). This will not work when a vector register
8259 // to memory mapping is strange (e.g., vector elements are not
8260 // stored in some sequential order).
8261
8262 // It must be true that the widen vector type is bigger than where
8263 // we need to load from.
8264 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8265 MVT LdVT = LD->getMemoryVT();
8266 assert(LdVT.isVector() && NVT.isVector());
8267 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8268
8269 // Load information
8270 SDValue Chain = LD->getChain();
8271 SDValue BasePtr = LD->getBasePtr();
8272 int SVOffset = LD->getSrcValueOffset();
8273 unsigned Alignment = LD->getAlignment();
8274 bool isVolatile = LD->isVolatile();
8275 const Value *SV = LD->getSrcValue();
8276 unsigned int LdWidth = LdVT.getSizeInBits();
8277
8278 // Load value as a large register
8279 SDValueVector LdChain;
8280 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8281 Alignment, isVolatile, LdWidth, NVT);
8282
8283 if (LdChain.size() == 1) {
8284 TFOp = LdChain[0];
8285 return true;
8286 }
8287 else {
8288 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8289 return false;
8290 }
8291}
8292
8293
8294void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8295 SDValue Chain,
8296 SDValue BasePtr,
8297 const Value *SV,
8298 int SVOffset,
8299 unsigned Alignment,
8300 bool isVolatile,
8301 SDValue ValOp,
8302 unsigned StWidth) {
8303 // Breaks the stores into a series of power of 2 width stores. For any
8304 // width, we convert the vector to the vector of element size that we
8305 // want to store. This avoids requiring a stack convert.
8306
8307 // Find a width of the element type we can store with
8308 MVT VVT = ValOp.getValueType();
8309 MVT EVT, VecEVT;
8310 unsigned EVTWidth;
8311 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8312 EVTWidth = EVT.getSizeInBits();
8313
8314 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8315 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang927daf52008-11-06 22:52:21 +00008316 DAG.getIntPtrConstant(0));
Mon P Wang1448aad2008-10-30 08:01:45 +00008317 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8318 isVolatile, Alignment);
8319 StChain.push_back(StOp);
8320
8321 // Check if we are done
8322 if (StWidth == EVTWidth) {
8323 return;
8324 }
8325
8326 unsigned Idx = 1;
8327 StWidth -= EVTWidth;
8328 unsigned Offset = 0;
8329
8330 while (StWidth > 0) {
8331 unsigned Increment = EVTWidth / 8;
8332 Offset += Increment;
8333 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8334 DAG.getIntPtrConstant(Increment));
8335
8336 if (StWidth < EVTWidth) {
8337 // Our current type we are using is too large, use a smaller size by
8338 // using a smaller power of 2
8339 unsigned oEVTWidth = EVTWidth;
8340 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8341 EVTWidth = EVT.getSizeInBits();
8342 // Readjust position and vector position based on new load type
8343 Idx = Idx * (oEVTWidth/EVTWidth)+1;
8344 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8345 }
8346
8347 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
8348 DAG.getIntPtrConstant(Idx));
8349 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8350 SVOffset + Offset, isVolatile,
8351 MinAlign(Alignment, Offset)));
8352 StWidth -= EVTWidth;
8353 }
8354}
8355
8356
8357SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8358 SDValue Chain,
8359 SDValue BasePtr) {
8360 // TODO: It might be cleaner if we can use SplitVector and have more legal
8361 // vector types that can be stored into memory (e.g., v4xi8 can
8362 // be stored as a word). This will not work when a vector register
8363 // to memory mapping is strange (e.g., vector elements are not
8364 // stored in some sequential order).
8365
8366 MVT StVT = ST->getMemoryVT();
8367 SDValue ValOp = ST->getValue();
8368
8369 // Check if we have widen this node with another value
8370 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8371 if (I != WidenNodes.end())
8372 ValOp = I->second;
8373
8374 MVT VVT = ValOp.getValueType();
8375
8376 // It must be true that we the widen vector type is bigger than where
8377 // we need to store.
8378 assert(StVT.isVector() && VVT.isVector());
8379 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8380 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8381
8382 // Store value
8383 SDValueVector StChain;
8384 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8385 ST->getSrcValueOffset(), ST->getAlignment(),
8386 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8387 if (StChain.size() == 1)
8388 return StChain[0];
8389 else
8390 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8391}
8392
8393
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008394// SelectionDAG::Legalize - This is the entry point for the file.
8395//
8396void SelectionDAG::Legalize() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008397 /// run - This is the main entry point to this class.
8398 ///
8399 SelectionDAGLegalize(*this).LegalizeDAG();
8400}
8401