blob: c535f3a082b5407c9db68bd4d1f7d74f5a51cfdb [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000019#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga448bc42007-08-16 23:50:06 +000020#include "llvm/Target/TargetFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Target/TargetLowering.h"
22#include "llvm/Target/TargetData.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetOptions.h"
Dan Gohmane8b391e2008-04-12 04:36:06 +000025#include "llvm/Target/TargetSubtarget.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/CallingConv.h"
27#include "llvm/Constants.h"
28#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/Compiler.h"
Duncan Sandsa3691432007-10-28 12:59:45 +000031#include "llvm/Support/MathExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/ADT/DenseMap.h"
33#include "llvm/ADT/SmallVector.h"
34#include "llvm/ADT/SmallPtrSet.h"
35#include <map>
36using namespace llvm;
37
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038//===----------------------------------------------------------------------===//
39/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
40/// hacks on it until the target machine can handle it. This involves
41/// eliminating value sizes the machine cannot handle (promoting small sizes to
42/// large sizes or splitting up large values into small values) as well as
43/// eliminating operations the machine cannot handle.
44///
45/// This code also does a small amount of optimization and recognition of idioms
46/// as part of its processing. For example, if a target does not support a
47/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
48/// will attempt merge setcc and brc instructions into brcc's.
49///
50namespace {
51class VISIBILITY_HIDDEN SelectionDAGLegalize {
52 TargetLowering &TLI;
53 SelectionDAG &DAG;
Duncan Sandse016a2e2008-12-14 09:43:15 +000054 bool TypesNeedLegalizing;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055
56 // Libcall insertion helpers.
57
58 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
59 /// legalized. We use this to ensure that calls are properly serialized
60 /// against each other, including inserted libcalls.
Dan Gohman8181bd12008-07-27 21:46:04 +000061 SDValue LastCALLSEQ_END;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062
63 /// IsLegalizingCall - This member is used *only* for purposes of providing
64 /// helpful assertions that a libcall isn't created while another call is
65 /// being legalized (which could lead to non-serialized call sequences).
66 bool IsLegalizingCall;
67
68 enum LegalizeAction {
69 Legal, // The target natively supports this operation.
70 Promote, // This operation should be executed in a larger type.
71 Expand // Try to expand this to other ops, otherwise use a libcall.
72 };
73
74 /// ValueTypeActions - This is a bitvector that contains two bits for each
75 /// value type, where the two bits correspond to the LegalizeAction enum.
76 /// This can be queried with "getTypeAction(VT)".
77 TargetLowering::ValueTypeActionImpl ValueTypeActions;
78
79 /// LegalizedNodes - For nodes that are of legal width, and that have more
80 /// than one use, this map indicates what regularized operand to use. This
81 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000082 DenseMap<SDValue, SDValue> LegalizedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083
84 /// PromotedNodes - For nodes that are below legal width, and that have more
85 /// than one use, this map indicates what promoted value to use. This allows
86 /// us to avoid promoting the same thing more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000087 DenseMap<SDValue, SDValue> PromotedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088
89 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang1448aad2008-10-30 08:01:45 +000090 /// which operands are the expanded version of the input. This allows
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 /// us to avoid expanding the same node more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000092 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093
94 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang1448aad2008-10-30 08:01:45 +000095 /// which operands are the split version of the input. This allows us
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 /// to avoid splitting the same node more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000097 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098
99 /// ScalarizedNodes - For nodes that need to be converted from vector types to
100 /// scalar types, this contains the mapping of ones we have already
101 /// processed to the result.
Dan Gohman8181bd12008-07-27 21:46:04 +0000102 std::map<SDValue, SDValue> ScalarizedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103
Mon P Wanga5a239f2008-11-06 05:31:54 +0000104 /// WidenNodes - For nodes that need to be widened from one vector type to
105 /// another, this contains the mapping of those that we have already widen.
106 /// This allows us to avoid widening more than once.
Mon P Wang1448aad2008-10-30 08:01:45 +0000107 std::map<SDValue, SDValue> WidenNodes;
108
Dan Gohman8181bd12008-07-27 21:46:04 +0000109 void AddLegalizedOperand(SDValue From, SDValue To) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 LegalizedNodes.insert(std::make_pair(From, To));
111 // If someone requests legalization of the new node, return itself.
112 if (From != To)
113 LegalizedNodes.insert(std::make_pair(To, To));
114 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000115 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman55d19662008-07-07 17:46:23 +0000116 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 assert(isNew && "Got into the map somehow?");
Evan Chengcf576fd2008-11-24 07:09:49 +0000118 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 // If someone requests legalization of the new node, return itself.
120 LegalizedNodes.insert(std::make_pair(To, To));
121 }
Mon P Wanga5a239f2008-11-06 05:31:54 +0000122 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang1448aad2008-10-30 08:01:45 +0000123 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
124 assert(isNew && "Got into the map somehow?");
Evan Chengcf576fd2008-11-24 07:09:49 +0000125 isNew = isNew;
Mon P Wang1448aad2008-10-30 08:01:45 +0000126 // If someone requests legalization of the new node, return itself.
127 LegalizedNodes.insert(std::make_pair(To, To));
128 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129
130public:
Duncan Sandse016a2e2008-12-14 09:43:15 +0000131 explicit SelectionDAGLegalize(SelectionDAG &DAG, bool TypesNeedLegalizing);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132
133 /// getTypeAction - Return how we should legalize values of this type, either
134 /// it is already legal or we need to expand it into multiple registers of
135 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands92c43912008-06-06 12:08:01 +0000136 LegalizeAction getTypeAction(MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
138 }
139
140 /// isTypeLegal - Return true if this type is legal on this target.
141 ///
Duncan Sands92c43912008-06-06 12:08:01 +0000142 bool isTypeLegal(MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 return getTypeAction(VT) == Legal;
144 }
145
146 void LegalizeDAG();
147
148private:
149 /// HandleOp - Legalize, Promote, or Expand the specified operand as
150 /// appropriate for its type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000151 void HandleOp(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152
153 /// LegalizeOp - We know that the specified value has a legal type.
154 /// Recursively ensure that the operands have legal types, then return the
155 /// result.
Dan Gohman8181bd12008-07-27 21:46:04 +0000156 SDValue LegalizeOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157
Dan Gohman6d05cac2007-10-11 23:57:53 +0000158 /// UnrollVectorOp - We know that the given vector has a legal type, however
159 /// the operation it performs is not legal and is an operation that we have
160 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
161 /// operating on each element individually.
Dan Gohman8181bd12008-07-27 21:46:04 +0000162 SDValue UnrollVectorOp(SDValue O);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000163
164 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
165 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
166 /// is necessary to spill the vector being inserted into to memory, perform
167 /// the insert there, and then read the result back.
Dan Gohman8181bd12008-07-27 21:46:04 +0000168 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
169 SDValue Idx);
Dan Gohman6d05cac2007-10-11 23:57:53 +0000170
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 /// PromoteOp - Given an operation that produces a value in an invalid type,
172 /// promote it to compute the value into a larger type. The produced value
173 /// will have the correct bits for the low portion of the register, but no
174 /// guarantee is made about the top bits: it may be zero, sign-extended, or
175 /// garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +0000176 SDValue PromoteOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177
Dan Gohman8181bd12008-07-27 21:46:04 +0000178 /// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman4fc03742008-10-01 15:07:49 +0000180 /// the LegalizedNodes map is filled in for any results that are not expanded,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 /// the ExpandedNodes map is filled in for any results that are expanded, and
182 /// the Lo/Hi values are returned. This applies to integer types and Vector
183 /// types.
Dan Gohman8181bd12008-07-27 21:46:04 +0000184 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185
Mon P Wanga5a239f2008-11-06 05:31:54 +0000186 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
187 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
188 /// for the existing elements but no guarantee is made about the new elements
189 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
190 /// when we have an instruction operating on an illegal vector type and we
191 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang1448aad2008-10-30 08:01:45 +0000192 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
193
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 /// SplitVectorOp - Given an operand of vector type, break it down into
195 /// two smaller values.
Dan Gohman8181bd12008-07-27 21:46:04 +0000196 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197
198 /// ScalarizeVectorOp - Given an operand of single-element vector type
199 /// (e.g. v1f32), convert it into the equivalent operation that returns a
200 /// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +0000201 SDValue ScalarizeVectorOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202
Mon P Wanga5a239f2008-11-06 05:31:54 +0000203 /// Useful 16 element vector type that is used to pass operands for widening.
Mon P Wang1448aad2008-10-30 08:01:45 +0000204 typedef SmallVector<SDValue, 16> SDValueVector;
205
206 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
207 /// the LdChain contains a single load and false if it contains a token
208 /// factor for multiple loads. It takes
209 /// Result: location to return the result
210 /// LdChain: location to return the load chain
211 /// Op: load operation to widen
212 /// NVT: widen vector result type we want for the load
213 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
214 SDValue Op, MVT NVT);
215
216 /// Helper genWidenVectorLoads - Helper function to generate a set of
217 /// loads to load a vector with a resulting wider type. It takes
218 /// LdChain: list of chains for the load we have generated
219 /// Chain: incoming chain for the ld vector
220 /// BasePtr: base pointer to load from
221 /// SV: memory disambiguation source value
222 /// SVOffset: memory disambiugation offset
223 /// Alignment: alignment of the memory
224 /// isVolatile: volatile load
225 /// LdWidth: width of memory that we want to load
226 /// ResType: the wider result result type for the resulting loaded vector
227 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
228 SDValue BasePtr, const Value *SV,
229 int SVOffset, unsigned Alignment,
230 bool isVolatile, unsigned LdWidth,
231 MVT ResType);
232
233 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
234 /// location. It takes
235 /// ST: store node that we want to replace
236 /// Chain: incoming store chain
237 /// BasePtr: base address of where we want to store into
238 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
239 SDValue BasePtr);
240
241 /// Helper genWidenVectorStores - Helper function to generate a set of
242 /// stores to store a widen vector into non widen memory
243 // It takes
244 // StChain: list of chains for the stores we have generated
245 // Chain: incoming chain for the ld vector
246 // BasePtr: base pointer to load from
247 // SV: memory disambiguation source value
248 // SVOffset: memory disambiugation offset
249 // Alignment: alignment of the memory
250 // isVolatile: volatile lod
251 // ValOp: value to store
252 // StWidth: width of memory that we want to store
253 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
254 SDValue BasePtr, const Value *SV,
255 int SVOffset, unsigned Alignment,
256 bool isVolatile, SDValue ValOp,
257 unsigned StWidth);
258
Duncan Sandsd3ace282008-07-21 10:20:31 +0000259 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 /// specified mask and type. Targets can specify exactly which masks they
261 /// support and the code generator is tasked with not creating illegal masks.
262 ///
263 /// Note that this will also return true for shuffles that are promoted to a
264 /// different type.
265 ///
266 /// If this is a legal shuffle, this method returns the (possibly promoted)
267 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman8181bd12008-07-27 21:46:04 +0000268 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269
270 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
271 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
272
Dan Gohman8181bd12008-07-27 21:46:04 +0000273 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Evan Cheng71343822008-10-15 02:05:31 +0000274 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC);
275 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC) {
276 LegalizeSetCCOperands(LHS, RHS, CC);
277 LegalizeSetCCCondCode(VT, LHS, RHS, CC);
278 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279
Dan Gohman8181bd12008-07-27 21:46:04 +0000280 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
281 SDValue &Hi);
282 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283
Dan Gohman8181bd12008-07-27 21:46:04 +0000284 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
285 SDValue ExpandBUILD_VECTOR(SDNode *Node);
286 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dan Gohman29c3cef2008-08-14 20:04:46 +0000287 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op);
Dan Gohman8181bd12008-07-27 21:46:04 +0000288 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
289 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
290 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291
Dan Gohman8181bd12008-07-27 21:46:04 +0000292 SDValue ExpandBSWAP(SDValue Op);
293 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
294 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
295 SDValue &Lo, SDValue &Hi);
296 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
297 SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298
Dan Gohman8181bd12008-07-27 21:46:04 +0000299 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
300 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Mon P Wang9901e732008-12-09 05:46:39 +0000301
302 // Returns the legalized (truncated or extended) shift amount.
303 SDValue LegalizeShiftAmount(SDValue ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304};
305}
306
307/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
308/// specified mask and type. Targets can specify exactly which masks they
309/// support and the code generator is tasked with not creating illegal masks.
310///
311/// Note that this will also return true for shuffles that are promoted to a
312/// different type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000313SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
315 default: return 0;
316 case TargetLowering::Legal:
317 case TargetLowering::Custom:
318 break;
319 case TargetLowering::Promote: {
320 // If this is promoted to a different type, convert the shuffle mask and
321 // ask if it is legal in the promoted type!
Duncan Sands92c43912008-06-06 12:08:01 +0000322 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd3ace282008-07-21 10:20:31 +0000323 MVT EltVT = NVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324
325 // If we changed # elements, change the shuffle mask.
326 unsigned NumEltsGrowth =
Duncan Sands92c43912008-06-06 12:08:01 +0000327 NVT.getVectorNumElements() / VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
329 if (NumEltsGrowth > 1) {
330 // Renumber the elements.
Dan Gohman8181bd12008-07-27 21:46:04 +0000331 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000333 SDValue InOp = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
335 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd3ace282008-07-21 10:20:31 +0000336 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000338 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd3ace282008-07-21 10:20:31 +0000339 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 }
341 }
342 }
343 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
344 }
345 VT = NVT;
346 break;
347 }
348 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000349 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350}
351
Duncan Sandse016a2e2008-12-14 09:43:15 +0000352SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag, bool types)
353 : TLI(dag.getTargetLoweringInfo()), DAG(dag), TypesNeedLegalizing(types),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 ValueTypeActions(TLI.getValueTypeActions()) {
355 assert(MVT::LAST_VALUETYPE <= 32 &&
356 "Too many value types for ValueTypeActions to hold!");
357}
358
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359void SelectionDAGLegalize::LegalizeDAG() {
360 LastCALLSEQ_END = DAG.getEntryNode();
361 IsLegalizingCall = false;
362
363 // The legalize process is inherently a bottom-up recursive process (users
364 // legalize their uses before themselves). Given infinite stack space, we
365 // could just start legalizing on the root and traverse the whole graph. In
366 // practice however, this causes us to run out of stack space on large basic
367 // blocks. To avoid this problem, compute an ordering of the nodes where each
368 // node is only legalized after all of its operands are legalized.
Dan Gohman2d2a7a32008-09-30 18:30:35 +0000369 DAG.AssignTopologicalOrder();
370 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
371 E = prior(DAG.allnodes_end()); I != next(E); ++I)
372 HandleOp(SDValue(I, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373
374 // Finally, it's possible the root changed. Get the new root.
Dan Gohman8181bd12008-07-27 21:46:04 +0000375 SDValue OldRoot = DAG.getRoot();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
377 DAG.setRoot(LegalizedNodes[OldRoot]);
378
379 ExpandedNodes.clear();
380 LegalizedNodes.clear();
381 PromotedNodes.clear();
382 SplitNodes.clear();
383 ScalarizedNodes.clear();
Mon P Wang1448aad2008-10-30 08:01:45 +0000384 WidenNodes.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385
386 // Remove dead nodes now.
387 DAG.RemoveDeadNodes();
388}
389
390
391/// FindCallEndFromCallStart - Given a chained node that is part of a call
392/// sequence, find the CALLSEQ_END node that terminates the call sequence.
393static SDNode *FindCallEndFromCallStart(SDNode *Node) {
394 if (Node->getOpcode() == ISD::CALLSEQ_END)
395 return Node;
396 if (Node->use_empty())
397 return 0; // No CallSeqEnd
398
399 // The chain is usually at the end.
Dan Gohman8181bd12008-07-27 21:46:04 +0000400 SDValue TheChain(Node, Node->getNumValues()-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 if (TheChain.getValueType() != MVT::Other) {
402 // Sometimes it's at the beginning.
Dan Gohman8181bd12008-07-27 21:46:04 +0000403 TheChain = SDValue(Node, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 if (TheChain.getValueType() != MVT::Other) {
405 // Otherwise, hunt for it.
406 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
407 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000408 TheChain = SDValue(Node, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 break;
410 }
411
412 // Otherwise, we walked into a node without a chain.
413 if (TheChain.getValueType() != MVT::Other)
414 return 0;
415 }
416 }
417
418 for (SDNode::use_iterator UI = Node->use_begin(),
419 E = Node->use_end(); UI != E; ++UI) {
420
421 // Make sure to only follow users of our token chain.
Dan Gohman0c97f1d2008-07-27 20:43:25 +0000422 SDNode *User = *UI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
424 if (User->getOperand(i) == TheChain)
425 if (SDNode *Result = FindCallEndFromCallStart(User))
426 return Result;
427 }
428 return 0;
429}
430
431/// FindCallStartFromCallEnd - Given a chained node that is part of a call
432/// sequence, find the CALLSEQ_START node that initiates the call sequence.
433static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
434 assert(Node && "Didn't find callseq_start for a call??");
435 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
436
437 assert(Node->getOperand(0).getValueType() == MVT::Other &&
438 "Node doesn't have a token chain argument!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000439 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440}
441
442/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
443/// see if any uses can reach Dest. If no dest operands can get to dest,
444/// legalize them, legalize ourself, and return false, otherwise, return true.
445///
446/// Keep track of the nodes we fine that actually do lead to Dest in
447/// NodesLeadingTo. This avoids retraversing them exponential number of times.
448///
449bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
450 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
451 if (N == Dest) return true; // N certainly leads to Dest :)
452
453 // If we've already processed this node and it does lead to Dest, there is no
454 // need to reprocess it.
455 if (NodesLeadingTo.count(N)) return true;
456
457 // If the first result of this node has been already legalized, then it cannot
458 // reach N.
459 switch (getTypeAction(N->getValueType(0))) {
460 case Legal:
Dan Gohman8181bd12008-07-27 21:46:04 +0000461 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462 break;
463 case Promote:
Dan Gohman8181bd12008-07-27 21:46:04 +0000464 if (PromotedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 break;
466 case Expand:
Dan Gohman8181bd12008-07-27 21:46:04 +0000467 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 break;
469 }
470
471 // Okay, this node has not already been legalized. Check and legalize all
472 // operands. If none lead to Dest, then we can legalize this node.
473 bool OperandsLeadToDest = false;
474 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
475 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greif1c80d112008-08-28 21:40:38 +0000476 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477
478 if (OperandsLeadToDest) {
479 NodesLeadingTo.insert(N);
480 return true;
481 }
482
483 // Okay, this node looks safe, legalize it and return false.
Dan Gohman8181bd12008-07-27 21:46:04 +0000484 HandleOp(SDValue(N, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 return false;
486}
487
Mon P Wang1448aad2008-10-30 08:01:45 +0000488/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489/// appropriate for its type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000490void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000491 MVT VT = Op.getValueType();
Duncan Sandse016a2e2008-12-14 09:43:15 +0000492 // If the type legalizer was run then we should never see any illegal result
493 // types here except for target constants (the type legalizer does not touch
Mon P Wang26342922008-12-18 20:03:17 +0000494 // those) or for build vector used as a mask for a vector shuffle.
495 // FIXME: We can removed the BUILD_VECTOR case when we fix PR2957.
Duncan Sandse016a2e2008-12-14 09:43:15 +0000496 assert((TypesNeedLegalizing || getTypeAction(VT) == Legal ||
Mon P Wang26342922008-12-18 20:03:17 +0000497 Op.getOpcode() == ISD::TargetConstant ||
498 Op.getOpcode() == ISD::BUILD_VECTOR) &&
Duncan Sandse016a2e2008-12-14 09:43:15 +0000499 "Illegal type introduced after type legalization?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 switch (getTypeAction(VT)) {
501 default: assert(0 && "Bad type action!");
502 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang1448aad2008-10-30 08:01:45 +0000503 case Promote:
504 if (!VT.isVector()) {
505 (void)PromoteOp(Op);
506 break;
507 }
508 else {
509 // See if we can widen otherwise use Expand to either scalarize or split
510 MVT WidenVT = TLI.getWidenVectorType(VT);
511 if (WidenVT != MVT::Other) {
512 (void) WidenVectorOp(Op, WidenVT);
513 break;
514 }
515 // else fall thru to expand since we can't widen the vector
516 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +0000518 if (!VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 // If this is an illegal scalar, expand it into its two component
520 // pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +0000521 SDValue X, Y;
Chris Lattnerdad577b2007-08-25 01:00:22 +0000522 if (Op.getOpcode() == ISD::TargetConstant)
523 break; // Allow illegal target nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 ExpandOp(Op, X, Y);
Duncan Sands92c43912008-06-06 12:08:01 +0000525 } else if (VT.getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 // If this is an illegal single element vector, convert it to a
527 // scalar operation.
528 (void)ScalarizeVectorOp(Op);
529 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +0000530 // This is an illegal multiple element vector.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 // Split it in half and legalize both parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000532 SDValue X, Y;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 SplitVectorOp(Op, X, Y);
534 }
535 break;
536 }
537}
538
539/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
540/// a load from the constant pool.
Dan Gohman8181bd12008-07-27 21:46:04 +0000541static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542 SelectionDAG &DAG, TargetLowering &TLI) {
543 bool Extend = false;
544
545 // If a FP immediate is precise when represented as a float and if the
546 // target can do an extending load from float to double, we put it into
547 // the constant pool as a float, even if it's is statically typed as a
Chris Lattnere718cc52008-03-05 06:46:58 +0000548 // double. This shrinks FP constants and canonicalizes them for targets where
549 // an FP extending load is the same cost as a normal load (such as on the x87
550 // fp stack or PPC FP unit).
Duncan Sands92c43912008-06-06 12:08:01 +0000551 MVT VT = CFP->getValueType(0);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000552 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 if (!UseCP) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000554 if (VT!=MVT::f64 && VT!=MVT::f32)
555 assert(0 && "Invalid type expansion");
Dale Johannesen49cc7ce2008-10-09 18:53:47 +0000556 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Cheng354be062008-03-04 08:05:30 +0000557 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558 }
559
Duncan Sands92c43912008-06-06 12:08:01 +0000560 MVT OrigVT = VT;
561 MVT SVT = VT;
Evan Cheng354be062008-03-04 08:05:30 +0000562 while (SVT != MVT::f32) {
Duncan Sands92c43912008-06-06 12:08:01 +0000563 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Cheng354be062008-03-04 08:05:30 +0000564 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
565 // Only do this if the target has a native EXTLOAD instruction from
566 // smaller type.
Evan Cheng08c171a2008-10-14 21:26:46 +0000567 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattnere718cc52008-03-05 06:46:58 +0000568 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000569 const Type *SType = SVT.getTypeForMVT();
Evan Cheng354be062008-03-04 08:05:30 +0000570 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
571 VT = SVT;
572 Extend = true;
573 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 }
575
Dan Gohman8181bd12008-07-27 21:46:04 +0000576 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +0000577 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Cheng354be062008-03-04 08:05:30 +0000578 if (Extend)
579 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohmanfb020b62008-02-07 18:41:25 +0000580 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman04637d12008-09-16 22:05:41 +0000581 0, VT, false, Alignment);
Evan Cheng354be062008-03-04 08:05:30 +0000582 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +0000583 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584}
585
586
587/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
588/// operations.
589static
Dan Gohman8181bd12008-07-27 21:46:04 +0000590SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
591 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +0000592 MVT VT = Node->getValueType(0);
593 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
595 "fcopysign expansion only supported for f32 and f64");
Duncan Sands92c43912008-06-06 12:08:01 +0000596 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597
598 // First get the sign bit of second operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000599 SDValue Mask1 = (SrcVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
601 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
602 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000603 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
605 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands92c43912008-06-06 12:08:01 +0000606 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 if (SizeDiff > 0) {
608 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
609 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
610 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnere6fa1452008-07-10 23:46:13 +0000611 } else if (SizeDiff < 0) {
612 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
613 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
614 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
615 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616
617 // Clear the sign bit of first operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000618 SDValue Mask2 = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
620 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
621 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman8181bd12008-07-27 21:46:04 +0000622 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
624
625 // Or the value with the sign bit.
626 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
627 return Result;
628}
629
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000630/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
631static
Dan Gohman8181bd12008-07-27 21:46:04 +0000632SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
633 TargetLowering &TLI) {
634 SDValue Chain = ST->getChain();
635 SDValue Ptr = ST->getBasePtr();
636 SDValue Val = ST->getValue();
Duncan Sands92c43912008-06-06 12:08:01 +0000637 MVT VT = Val.getValueType();
Dale Johannesen08275382007-09-08 19:29:23 +0000638 int Alignment = ST->getAlignment();
639 int SVOffset = ST->getSrcValueOffset();
Duncan Sands92c43912008-06-06 12:08:01 +0000640 if (ST->getMemoryVT().isFloatingPoint() ||
641 ST->getMemoryVT().isVector()) {
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000642 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
643 if (TLI.isTypeLegal(intVT)) {
644 // Expand to a bitconvert of the value to the integer type of the
645 // same size, then a (misaligned) int store.
646 // FIXME: Does not handle truncating floating point stores!
647 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
648 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
649 SVOffset, ST->isVolatile(), Alignment);
650 } else {
651 // Do a (aligned) store to a stack slot, then copy from the stack slot
652 // to the final destination using (unaligned) integer loads and stores.
653 MVT StoredVT = ST->getMemoryVT();
654 MVT RegVT =
655 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
656 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
657 unsigned RegBytes = RegVT.getSizeInBits() / 8;
658 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen08275382007-09-08 19:29:23 +0000659
Duncan Sands734f49b2008-12-13 07:18:38 +0000660 // Make sure the stack slot is also aligned for the register type.
661 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
662
663 // Perform the original store, only redirected to the stack slot.
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000664 SDValue Store = DAG.getTruncStore(Chain, Val, StackPtr, NULL, 0,StoredVT);
665 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
666 SmallVector<SDValue, 8> Stores;
667 unsigned Offset = 0;
668
669 // Do all but one copies using the full register width.
670 for (unsigned i = 1; i < NumRegs; i++) {
671 // Load one integer register's worth from the stack slot.
672 SDValue Load = DAG.getLoad(RegVT, Store, StackPtr, NULL, 0);
673 // Store it to the final location. Remember the store.
674 Stores.push_back(DAG.getStore(Load.getValue(1), Load, Ptr,
675 ST->getSrcValue(), SVOffset + Offset,
676 ST->isVolatile(),
677 MinAlign(ST->getAlignment(), Offset)));
678 // Increment the pointers.
679 Offset += RegBytes;
680 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
681 Increment);
682 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
683 }
684
Duncan Sands734f49b2008-12-13 07:18:38 +0000685 // The last store may be partial. Do a truncating store. On big-endian
686 // machines this requires an extending load from the stack slot to ensure
687 // that the bits are in the right place.
688 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000689
Duncan Sands734f49b2008-12-13 07:18:38 +0000690 // Load from the stack slot.
691 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Store, StackPtr,
692 NULL, 0, MemVT);
693
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000694 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, Ptr,
695 ST->getSrcValue(), SVOffset + Offset,
Duncan Sands734f49b2008-12-13 07:18:38 +0000696 MemVT, ST->isVolatile(),
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000697 MinAlign(ST->getAlignment(), Offset)));
698 // The order of the stores doesn't matter - say it with a TokenFactor.
699 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
700 Stores.size());
701 }
Dale Johannesen08275382007-09-08 19:29:23 +0000702 }
Duncan Sands92c43912008-06-06 12:08:01 +0000703 assert(ST->getMemoryVT().isInteger() &&
704 !ST->getMemoryVT().isVector() &&
Dale Johannesen08275382007-09-08 19:29:23 +0000705 "Unaligned store of unknown type.");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000706 // Get the half-size VT
Duncan Sands92c43912008-06-06 12:08:01 +0000707 MVT NewStoredVT =
708 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
709 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000710 int IncrementSize = NumBits / 8;
711
712 // Divide the stored value in two parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000713 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
714 SDValue Lo = Val;
715 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000716
717 // Store the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000718 SDValue Store1, Store2;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000719 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
720 ST->getSrcValue(), SVOffset, NewStoredVT,
721 ST->isVolatile(), Alignment);
722 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
723 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsa3691432007-10-28 12:59:45 +0000724 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000725 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
726 ST->getSrcValue(), SVOffset + IncrementSize,
727 NewStoredVT, ST->isVolatile(), Alignment);
728
729 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
730}
731
732/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
733static
Dan Gohman8181bd12008-07-27 21:46:04 +0000734SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
735 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000736 int SVOffset = LD->getSrcValueOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +0000737 SDValue Chain = LD->getChain();
738 SDValue Ptr = LD->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +0000739 MVT VT = LD->getValueType(0);
740 MVT LoadedVT = LD->getMemoryVT();
741 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000742 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
743 if (TLI.isTypeLegal(intVT)) {
744 // Expand to a (misaligned) integer load of the same size,
745 // then bitconvert to floating point or vector.
746 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
747 SVOffset, LD->isVolatile(),
Dale Johannesen08275382007-09-08 19:29:23 +0000748 LD->getAlignment());
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000749 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
750 if (VT.isFloatingPoint() && LoadedVT != VT)
751 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
Dale Johannesen08275382007-09-08 19:29:23 +0000752
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000753 SDValue Ops[] = { Result, Chain };
754 return DAG.getMergeValues(Ops, 2);
755 } else {
756 // Copy the value to a (aligned) stack slot using (unaligned) integer
757 // loads and stores, then do a (aligned) load from the stack slot.
758 MVT RegVT = TLI.getRegisterType(intVT);
759 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
760 unsigned RegBytes = RegVT.getSizeInBits() / 8;
761 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
762
Duncan Sands734f49b2008-12-13 07:18:38 +0000763 // Make sure the stack slot is also aligned for the register type.
764 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
765
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000766 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
767 SmallVector<SDValue, 8> Stores;
768 SDValue StackPtr = StackBase;
769 unsigned Offset = 0;
770
771 // Do all but one copies using the full register width.
772 for (unsigned i = 1; i < NumRegs; i++) {
773 // Load one integer register's worth from the original location.
774 SDValue Load = DAG.getLoad(RegVT, Chain, Ptr, LD->getSrcValue(),
775 SVOffset + Offset, LD->isVolatile(),
776 MinAlign(LD->getAlignment(), Offset));
777 // Follow the load with a store to the stack slot. Remember the store.
778 Stores.push_back(DAG.getStore(Load.getValue(1), Load, StackPtr,
779 NULL, 0));
780 // Increment the pointers.
781 Offset += RegBytes;
782 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
783 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
784 Increment);
785 }
786
787 // The last copy may be partial. Do an extending load.
Duncan Sands734f49b2008-12-13 07:18:38 +0000788 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000789 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Chain, Ptr,
790 LD->getSrcValue(), SVOffset + Offset,
Duncan Sands734f49b2008-12-13 07:18:38 +0000791 MemVT, LD->isVolatile(),
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000792 MinAlign(LD->getAlignment(), Offset));
793 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sands734f49b2008-12-13 07:18:38 +0000794 // On big-endian machines this requires a truncating store to ensure
795 // that the bits end up in the right place.
796 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, StackPtr,
797 NULL, 0, MemVT));
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000798
799 // The order of the stores doesn't matter - say it with a TokenFactor.
800 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
801 Stores.size());
802
803 // Finally, perform the original load only redirected to the stack slot.
804 Load = DAG.getExtLoad(LD->getExtensionType(), VT, TF, StackBase,
805 NULL, 0, LoadedVT);
806
807 // Callers expect a MERGE_VALUES node.
808 SDValue Ops[] = { Load, TF };
809 return DAG.getMergeValues(Ops, 2);
810 }
Dale Johannesen08275382007-09-08 19:29:23 +0000811 }
Duncan Sands92c43912008-06-06 12:08:01 +0000812 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000813 "Unaligned load of unsupported type.");
814
Dale Johannesendc0ee192008-02-27 22:36:00 +0000815 // Compute the new VT that is half the size of the old one. This is an
816 // integer MVT.
Duncan Sands92c43912008-06-06 12:08:01 +0000817 unsigned NumBits = LoadedVT.getSizeInBits();
818 MVT NewLoadedVT;
819 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000820 NumBits >>= 1;
821
822 unsigned Alignment = LD->getAlignment();
823 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000824 ISD::LoadExtType HiExtType = LD->getExtensionType();
825
826 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
827 if (HiExtType == ISD::NON_EXTLOAD)
828 HiExtType = ISD::ZEXTLOAD;
829
830 // Load the value in two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000831 SDValue Lo, Hi;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000832 if (TLI.isLittleEndian()) {
833 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
834 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
835 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
836 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
837 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
838 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000839 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000840 } else {
841 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
842 NewLoadedVT,LD->isVolatile(), Alignment);
843 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
844 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
845 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
846 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000847 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000848 }
849
850 // aggregate the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000851 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
852 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000853 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
854
Dan Gohman8181bd12008-07-27 21:46:04 +0000855 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000856 Hi.getValue(1));
857
Dan Gohman8181bd12008-07-27 21:46:04 +0000858 SDValue Ops[] = { Result, TF };
Duncan Sands698842f2008-07-02 17:40:58 +0000859 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000860}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861
Dan Gohman6d05cac2007-10-11 23:57:53 +0000862/// UnrollVectorOp - We know that the given vector has a legal type, however
863/// the operation it performs is not legal and is an operation that we have
864/// no way of lowering. "Unroll" the vector, splitting out the scalars and
865/// operating on each element individually.
Dan Gohman8181bd12008-07-27 21:46:04 +0000866SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000867 MVT VT = Op.getValueType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000868 assert(isTypeLegal(VT) &&
869 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000870 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman6d05cac2007-10-11 23:57:53 +0000871 "Can't unroll a vector with multiple results!");
Duncan Sands92c43912008-06-06 12:08:01 +0000872 unsigned NE = VT.getVectorNumElements();
873 MVT EltVT = VT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000874
Dan Gohman8181bd12008-07-27 21:46:04 +0000875 SmallVector<SDValue, 8> Scalars;
876 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman6d05cac2007-10-11 23:57:53 +0000877 for (unsigned i = 0; i != NE; ++i) {
878 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000879 SDValue Operand = Op.getOperand(j);
Duncan Sands92c43912008-06-06 12:08:01 +0000880 MVT OperandVT = Operand.getValueType();
881 if (OperandVT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +0000882 // A vector operand; extract a single element.
Duncan Sands92c43912008-06-06 12:08:01 +0000883 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000884 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
885 OperandEltVT,
886 Operand,
887 DAG.getConstant(i, MVT::i32));
888 } else {
889 // A scalar operand; just use it as is.
890 Operands[j] = Operand;
891 }
892 }
Mon P Wang9901e732008-12-09 05:46:39 +0000893
894 switch (Op.getOpcode()) {
895 default:
896 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
897 &Operands[0], Operands.size()));
898 break;
899 case ISD::SHL:
900 case ISD::SRA:
901 case ISD::SRL:
902 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT, Operands[0],
903 LegalizeShiftAmount(Operands[1])));
904 break;
905 }
Dan Gohman6d05cac2007-10-11 23:57:53 +0000906 }
907
908 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
909}
910
Duncan Sands37a3f472008-01-10 10:28:30 +0000911/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands92c43912008-06-06 12:08:01 +0000912static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands37a3f472008-01-10 10:28:30 +0000913 RTLIB::Libcall Call_F32,
914 RTLIB::Libcall Call_F64,
915 RTLIB::Libcall Call_F80,
916 RTLIB::Libcall Call_PPCF128) {
917 return
918 VT == MVT::f32 ? Call_F32 :
919 VT == MVT::f64 ? Call_F64 :
920 VT == MVT::f80 ? Call_F80 :
921 VT == MVT::ppcf128 ? Call_PPCF128 :
922 RTLIB::UNKNOWN_LIBCALL;
923}
924
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000925/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
926/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
927/// is necessary to spill the vector being inserted into to memory, perform
928/// the insert there, and then read the result back.
Dan Gohman8181bd12008-07-27 21:46:04 +0000929SDValue SelectionDAGLegalize::
930PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
931 SDValue Tmp1 = Vec;
932 SDValue Tmp2 = Val;
933 SDValue Tmp3 = Idx;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000934
935 // If the target doesn't support this, we have to spill the input vector
936 // to a temporary stack slot, update the element, then reload it. This is
937 // badness. We could also load the value into a vector register (either
938 // with a "move to register" or "extload into register" instruction, then
939 // permute it into place, if the idx is a constant and if the idx is
940 // supported by the target.
Duncan Sands92c43912008-06-06 12:08:01 +0000941 MVT VT = Tmp1.getValueType();
942 MVT EltVT = VT.getVectorElementType();
943 MVT IdxVT = Tmp3.getValueType();
944 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +0000945 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000946
Gabor Greif1c80d112008-08-28 21:40:38 +0000947 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000948
949 // Store the vector.
Dan Gohman8181bd12008-07-27 21:46:04 +0000950 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang1448aad2008-10-30 08:01:45 +0000951 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000952
953 // Truncate or zero extend offset to target pointer type.
Duncan Sandsec142ee2008-06-08 20:54:56 +0000954 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000955 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
956 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +0000957 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000958 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman8181bd12008-07-27 21:46:04 +0000959 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000960 // Store the scalar value.
961 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000962 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000963 // Load the updated vector.
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000964 return DAG.getLoad(VT, Ch, StackPtr,
965 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000966}
967
Mon P Wang9901e732008-12-09 05:46:39 +0000968SDValue SelectionDAGLegalize::LegalizeShiftAmount(SDValue ShiftAmt) {
969 if (TLI.getShiftAmountTy().bitsLT(ShiftAmt.getValueType()))
970 return DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), ShiftAmt);
971
972 if (TLI.getShiftAmountTy().bitsGT(ShiftAmt.getValueType()))
973 return DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), ShiftAmt);
974
975 return ShiftAmt;
976}
977
978
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000979/// LegalizeOp - We know that the specified value has a legal type, and
980/// that its operands are legal. Now ensure that the operation itself
981/// is legal, recursively ensuring that the operands' operations remain
982/// legal.
Dan Gohman8181bd12008-07-27 21:46:04 +0000983SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattnerdad577b2007-08-25 01:00:22 +0000984 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
985 return Op;
986
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000987 assert(isTypeLegal(Op.getValueType()) &&
988 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000989 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000990
991 // If this operation defines any values that cannot be represented in a
992 // register on this target, make sure to expand or promote them.
993 if (Node->getNumValues() > 1) {
994 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
995 if (getTypeAction(Node->getValueType(i)) != Legal) {
996 HandleOp(Op.getValue(i));
997 assert(LegalizedNodes.count(Op) &&
998 "Handling didn't add legal operands!");
999 return LegalizedNodes[Op];
1000 }
1001 }
1002
1003 // Note that LegalizeOp may be reentered even from single-use nodes, which
1004 // means that we always must cache transformed nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00001005 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006 if (I != LegalizedNodes.end()) return I->second;
1007
Dan Gohman8181bd12008-07-27 21:46:04 +00001008 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
1009 SDValue Result = Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 bool isCustom = false;
1011
1012 switch (Node->getOpcode()) {
1013 case ISD::FrameIndex:
1014 case ISD::EntryToken:
1015 case ISD::Register:
1016 case ISD::BasicBlock:
1017 case ISD::TargetFrameIndex:
1018 case ISD::TargetJumpTable:
1019 case ISD::TargetConstant:
1020 case ISD::TargetConstantFP:
1021 case ISD::TargetConstantPool:
1022 case ISD::TargetGlobalAddress:
1023 case ISD::TargetGlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +00001024 case ISD::TargetExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 case ISD::VALUETYPE:
1026 case ISD::SRCVALUE:
Dan Gohman12a9c082008-02-06 22:27:42 +00001027 case ISD::MEMOPERAND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001028 case ISD::CONDCODE:
Duncan Sandsc93fae32008-03-21 09:14:45 +00001029 case ISD::ARG_FLAGS:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00001031 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032 "This must be legal!");
1033 break;
1034 default:
1035 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1036 // If this is a target node, legalize it by legalizing the operands then
1037 // passing it through.
Dan Gohman8181bd12008-07-27 21:46:04 +00001038 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1040 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1041
1042 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
1043
1044 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1045 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00001046 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047 }
1048 // Otherwise this is an unhandled builtin node. splat.
1049#ifndef NDEBUG
1050 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
1051#endif
1052 assert(0 && "Do not know how to legalize this operator!");
1053 abort();
1054 case ISD::GLOBAL_OFFSET_TABLE:
1055 case ISD::GlobalAddress:
1056 case ISD::GlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +00001057 case ISD::ExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 case ISD::ConstantPool:
1059 case ISD::JumpTable: // Nothing to do.
1060 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1061 default: assert(0 && "This action is not supported yet!");
1062 case TargetLowering::Custom:
1063 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001064 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001065 // FALLTHROUGH if the target doesn't want to lower this op after all.
1066 case TargetLowering::Legal:
1067 break;
1068 }
1069 break;
1070 case ISD::FRAMEADDR:
1071 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001072 // The only option for these nodes is to custom lower them. If the target
1073 // does not custom lower them, then return zero.
1074 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001075 if (Tmp1.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001076 Result = Tmp1;
1077 else
1078 Result = DAG.getConstant(0, TLI.getPointerTy());
1079 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +00001080 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands92c43912008-06-06 12:08:01 +00001081 MVT VT = Node->getValueType(0);
Anton Korobeynikov09386bd2007-08-29 19:28:29 +00001082 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1083 default: assert(0 && "This action is not supported yet!");
1084 case TargetLowering::Custom:
1085 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001086 if (Result.getNode()) break;
Anton Korobeynikov09386bd2007-08-29 19:28:29 +00001087 // Fall Thru
1088 case TargetLowering::Legal:
1089 Result = DAG.getConstant(0, VT);
1090 break;
1091 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +00001092 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +00001093 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001094 case ISD::EXCEPTIONADDR: {
1095 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00001096 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001097 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1098 default: assert(0 && "This action is not supported yet!");
1099 case TargetLowering::Expand: {
1100 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001101 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001102 }
1103 break;
1104 case TargetLowering::Custom:
1105 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001106 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001107 // Fall Thru
1108 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001109 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands698842f2008-07-02 17:40:58 +00001110 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001111 break;
1112 }
1113 }
1114 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001115 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001116
Gabor Greif1c80d112008-08-28 21:40:38 +00001117 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001118 "Cannot return more than two values!");
1119
1120 // Since we produced two values, make sure to remember that we
1121 // legalized both of them.
1122 Tmp1 = LegalizeOp(Result);
1123 Tmp2 = LegalizeOp(Result.getValue(1));
1124 AddLegalizedOperand(Op.getValue(0), Tmp1);
1125 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001126 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001127 case ISD::EHSELECTION: {
1128 Tmp1 = LegalizeOp(Node->getOperand(0));
1129 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00001130 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001131 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1132 default: assert(0 && "This action is not supported yet!");
1133 case TargetLowering::Expand: {
1134 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001135 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001136 }
1137 break;
1138 case TargetLowering::Custom:
1139 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001140 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001141 // Fall Thru
1142 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001143 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands698842f2008-07-02 17:40:58 +00001144 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145 break;
1146 }
1147 }
1148 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001149 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001150
Gabor Greif1c80d112008-08-28 21:40:38 +00001151 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001152 "Cannot return more than two values!");
1153
1154 // Since we produced two values, make sure to remember that we
1155 // legalized both of them.
1156 Tmp1 = LegalizeOp(Result);
1157 Tmp2 = LegalizeOp(Result.getValue(1));
1158 AddLegalizedOperand(Op.getValue(0), Tmp1);
1159 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001160 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001161 case ISD::EH_RETURN: {
Duncan Sands92c43912008-06-06 12:08:01 +00001162 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001163 // The only "good" option for this node is to custom lower it.
1164 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1165 default: assert(0 && "This action is not supported at all!");
1166 case TargetLowering::Custom:
1167 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001168 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001169 // Fall Thru
1170 case TargetLowering::Legal:
1171 // Target does not know, how to lower this, lower to noop
1172 Result = LegalizeOp(Node->getOperand(0));
1173 break;
1174 }
1175 }
1176 break;
1177 case ISD::AssertSext:
1178 case ISD::AssertZext:
1179 Tmp1 = LegalizeOp(Node->getOperand(0));
1180 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1181 break;
1182 case ISD::MERGE_VALUES:
1183 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif46bf5472008-08-26 22:36:50 +00001184 Result = Node->getOperand(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001185 break;
1186 case ISD::CopyFromReg:
1187 Tmp1 = LegalizeOp(Node->getOperand(0));
1188 Result = Op.getValue(0);
1189 if (Node->getNumValues() == 2) {
1190 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1191 } else {
1192 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
1193 if (Node->getNumOperands() == 3) {
1194 Tmp2 = LegalizeOp(Node->getOperand(2));
1195 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1196 } else {
1197 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1198 }
1199 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1200 }
1201 // Since CopyFromReg produces two values, make sure to remember that we
1202 // legalized both of them.
1203 AddLegalizedOperand(Op.getValue(0), Result);
1204 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001205 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001206 case ISD::UNDEF: {
Duncan Sands92c43912008-06-06 12:08:01 +00001207 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001208 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
1209 default: assert(0 && "This action is not supported yet!");
1210 case TargetLowering::Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00001211 if (VT.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001212 Result = DAG.getConstant(0, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00001213 else if (VT.isFloatingPoint())
1214 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesen20b76352007-09-26 17:26:49 +00001215 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001216 else
1217 assert(0 && "Unknown value type!");
1218 break;
1219 case TargetLowering::Legal:
1220 break;
1221 }
1222 break;
1223 }
1224
1225 case ISD::INTRINSIC_W_CHAIN:
1226 case ISD::INTRINSIC_WO_CHAIN:
1227 case ISD::INTRINSIC_VOID: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001228 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001229 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1230 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1231 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1232
1233 // Allow the target to custom lower its intrinsics if it wants to.
1234 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1235 TargetLowering::Custom) {
1236 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001237 if (Tmp3.getNode()) Result = Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001238 }
1239
Gabor Greif1c80d112008-08-28 21:40:38 +00001240 if (Result.getNode()->getNumValues() == 1) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001241
1242 // Must have return value and chain result.
Gabor Greif1c80d112008-08-28 21:40:38 +00001243 assert(Result.getNode()->getNumValues() == 2 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 "Cannot return more than two values!");
1245
1246 // Since loads produce two values, make sure to remember that we
1247 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001248 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1249 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001250 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001251 }
1252
Dan Gohman472d12c2008-06-30 20:59:49 +00001253 case ISD::DBG_STOPPOINT:
1254 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001255 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1256
Dan Gohman472d12c2008-06-30 20:59:49 +00001257 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001258 case TargetLowering::Promote:
1259 default: assert(0 && "This action is not supported yet!");
1260 case TargetLowering::Expand: {
1261 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1262 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001263 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264
Dan Gohman472d12c2008-06-30 20:59:49 +00001265 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001266 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman472d12c2008-06-30 20:59:49 +00001267 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1268 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269
Dan Gohman472d12c2008-06-30 20:59:49 +00001270 unsigned Line = DSP->getLine();
1271 unsigned Col = DSP->getColumn();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001272
1273 if (useDEBUG_LOC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001274 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Chengd6f57682008-07-08 20:06:39 +00001275 DAG.getConstant(Col, MVT::i32),
1276 DAG.getConstant(SrcFile, MVT::i32) };
1277 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001278 } else {
Evan Cheng69eda822008-02-01 02:05:57 +00001279 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001280 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001281 }
1282 } else {
1283 Result = Tmp1; // chain
1284 }
1285 break;
1286 }
Evan Chengd6f57682008-07-08 20:06:39 +00001287 case TargetLowering::Legal: {
1288 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1289 if (Action == Legal && Tmp1 == Node->getOperand(0))
1290 break;
1291
Dan Gohman8181bd12008-07-27 21:46:04 +00001292 SmallVector<SDValue, 8> Ops;
Evan Chengd6f57682008-07-08 20:06:39 +00001293 Ops.push_back(Tmp1);
1294 if (Action == Legal) {
1295 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1296 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1297 } else {
1298 // Otherwise promote them.
1299 Ops.push_back(PromoteOp(Node->getOperand(1)));
1300 Ops.push_back(PromoteOp(Node->getOperand(2)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001301 }
Evan Chengd6f57682008-07-08 20:06:39 +00001302 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1303 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1304 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001305 break;
1306 }
Evan Chengd6f57682008-07-08 20:06:39 +00001307 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001308 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001309
1310 case ISD::DECLARE:
1311 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1312 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1313 default: assert(0 && "This action is not supported yet!");
1314 case TargetLowering::Legal:
1315 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1316 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1317 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1318 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1319 break;
Chris Lattner203cd052008-02-28 05:53:40 +00001320 case TargetLowering::Expand:
1321 Result = LegalizeOp(Node->getOperand(0));
1322 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001323 }
1324 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001325
1326 case ISD::DEBUG_LOC:
1327 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1328 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1329 default: assert(0 && "This action is not supported yet!");
Evan Chengd6f57682008-07-08 20:06:39 +00001330 case TargetLowering::Legal: {
1331 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001332 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Chengd6f57682008-07-08 20:06:39 +00001333 if (Action == Legal && Tmp1 == Node->getOperand(0))
1334 break;
1335 if (Action == Legal) {
1336 Tmp2 = Node->getOperand(1);
1337 Tmp3 = Node->getOperand(2);
1338 Tmp4 = Node->getOperand(3);
1339 } else {
1340 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1341 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1342 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1343 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001344 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1345 break;
1346 }
Evan Chengd6f57682008-07-08 20:06:39 +00001347 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001348 break;
1349
Dan Gohmanfa607c92008-07-01 00:05:16 +00001350 case ISD::DBG_LABEL:
1351 case ISD::EH_LABEL:
1352 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1353 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001354 default: assert(0 && "This action is not supported yet!");
1355 case TargetLowering::Legal:
1356 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohmanfa607c92008-07-01 00:05:16 +00001357 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001358 break;
1359 case TargetLowering::Expand:
1360 Result = LegalizeOp(Node->getOperand(0));
1361 break;
1362 }
1363 break;
1364
Evan Chengd1d68072008-03-08 00:58:38 +00001365 case ISD::PREFETCH:
1366 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1367 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1368 default: assert(0 && "This action is not supported yet!");
1369 case TargetLowering::Legal:
1370 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1371 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1372 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1373 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1374 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1375 break;
1376 case TargetLowering::Expand:
1377 // It's a noop.
1378 Result = LegalizeOp(Node->getOperand(0));
1379 break;
1380 }
1381 break;
1382
Andrew Lenharth785610d2008-02-16 01:24:58 +00001383 case ISD::MEMBARRIER: {
1384 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001385 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1386 default: assert(0 && "This action is not supported yet!");
1387 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001388 SDValue Ops[6];
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001389 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands3ee041a2008-02-27 08:53:44 +00001390 for (int x = 1; x < 6; ++x) {
1391 Ops[x] = Node->getOperand(x);
1392 if (!isTypeLegal(Ops[x].getValueType()))
1393 Ops[x] = PromoteOp(Ops[x]);
1394 }
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001395 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1396 break;
1397 }
1398 case TargetLowering::Expand:
1399 //There is no libgcc call for this op
1400 Result = Node->getOperand(0); // Noop
1401 break;
1402 }
Andrew Lenharth785610d2008-02-16 01:24:58 +00001403 break;
1404 }
1405
Dan Gohmanbebba8d2008-12-23 21:37:04 +00001406 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001407 unsigned int num_operands = 4;
1408 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001409 SDValue Ops[4];
Mon P Wang078a62d2008-05-05 19:05:59 +00001410 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001411 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang078a62d2008-05-05 19:05:59 +00001412 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1413
1414 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1415 default: assert(0 && "This action is not supported yet!");
1416 case TargetLowering::Custom:
1417 Result = TLI.LowerOperation(Result, DAG);
1418 break;
1419 case TargetLowering::Legal:
1420 break;
1421 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001422 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1423 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001424 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001425 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00001426 case ISD::ATOMIC_LOAD_ADD:
1427 case ISD::ATOMIC_LOAD_SUB:
1428 case ISD::ATOMIC_LOAD_AND:
1429 case ISD::ATOMIC_LOAD_OR:
1430 case ISD::ATOMIC_LOAD_XOR:
1431 case ISD::ATOMIC_LOAD_NAND:
1432 case ISD::ATOMIC_LOAD_MIN:
1433 case ISD::ATOMIC_LOAD_MAX:
1434 case ISD::ATOMIC_LOAD_UMIN:
1435 case ISD::ATOMIC_LOAD_UMAX:
1436 case ISD::ATOMIC_SWAP: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001437 unsigned int num_operands = 3;
1438 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001439 SDValue Ops[3];
Mon P Wang078a62d2008-05-05 19:05:59 +00001440 for (unsigned int x = 0; x < num_operands; ++x)
1441 Ops[x] = LegalizeOp(Node->getOperand(x));
1442 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sandsac496a12008-07-04 11:47:58 +00001443
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001444 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001445 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001446 case TargetLowering::Custom:
1447 Result = TLI.LowerOperation(Result, DAG);
1448 break;
1449 case TargetLowering::Legal:
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001450 break;
1451 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001452 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1453 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001454 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001455 }
Scott Michelf2e2b702007-08-08 23:23:31 +00001456 case ISD::Constant: {
1457 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1458 unsigned opAction =
1459 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1460
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001461 // We know we don't need to expand constants here, constants only have one
1462 // value and we check that it is fine above.
1463
Scott Michelf2e2b702007-08-08 23:23:31 +00001464 if (opAction == TargetLowering::Custom) {
1465 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001466 if (Tmp1.getNode())
Scott Michelf2e2b702007-08-08 23:23:31 +00001467 Result = Tmp1;
1468 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001469 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001470 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001471 case ISD::ConstantFP: {
1472 // Spill FP immediates to the constant pool if the target cannot directly
1473 // codegen them. Targets often have some immediate values that can be
1474 // efficiently generated into an FP register without a load. We explicitly
1475 // leave these constants as ConstantFP nodes for the target to deal with.
1476 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1477
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001478 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1479 default: assert(0 && "This action is not supported yet!");
Nate Begemane2ba64f2008-02-14 08:57:00 +00001480 case TargetLowering::Legal:
1481 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001482 case TargetLowering::Custom:
1483 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001484 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001485 Result = Tmp3;
1486 break;
1487 }
1488 // FALLTHROUGH
Nate Begemane2ba64f2008-02-14 08:57:00 +00001489 case TargetLowering::Expand: {
1490 // Check to see if this FP immediate is already legal.
1491 bool isLegal = false;
1492 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1493 E = TLI.legal_fpimm_end(); I != E; ++I) {
1494 if (CFP->isExactlyValue(*I)) {
1495 isLegal = true;
1496 break;
1497 }
1498 }
1499 // If this is a legal constant, turn it into a TargetConstantFP node.
1500 if (isLegal)
1501 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1503 }
Nate Begemane2ba64f2008-02-14 08:57:00 +00001504 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001505 break;
1506 }
1507 case ISD::TokenFactor:
1508 if (Node->getNumOperands() == 2) {
1509 Tmp1 = LegalizeOp(Node->getOperand(0));
1510 Tmp2 = LegalizeOp(Node->getOperand(1));
1511 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1512 } else if (Node->getNumOperands() == 3) {
1513 Tmp1 = LegalizeOp(Node->getOperand(0));
1514 Tmp2 = LegalizeOp(Node->getOperand(1));
1515 Tmp3 = LegalizeOp(Node->getOperand(2));
1516 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1517 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00001518 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001519 // Legalize the operands.
1520 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1521 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1522 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1523 }
1524 break;
1525
1526 case ISD::FORMAL_ARGUMENTS:
1527 case ISD::CALL:
1528 // The only option for this is to custom lower it.
1529 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001530 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesenac246272008-03-05 19:14:03 +00001531 // A call within a calling sequence must be legalized to something
1532 // other than the normal CALLSEQ_END. Violating this gets Legalize
1533 // into an infinite loop.
1534 assert ((!IsLegalizingCall ||
1535 Node->getOpcode() != ISD::CALL ||
Gabor Greif1c80d112008-08-28 21:40:38 +00001536 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesenac246272008-03-05 19:14:03 +00001537 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001538
1539 // The number of incoming and outgoing values should match; unless the final
1540 // outgoing value is a flag.
Gabor Greif1c80d112008-08-28 21:40:38 +00001541 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1542 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1543 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling22f8deb2007-11-13 00:44:25 +00001544 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545 "Lowering call/formal_arguments produced unexpected # results!");
1546
1547 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1548 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greif1c80d112008-08-28 21:40:38 +00001549 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1550 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling22f8deb2007-11-13 00:44:25 +00001551 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001552 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00001553 if (Op.getResNo() == i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001554 Tmp2 = Tmp1;
Dan Gohman8181bd12008-07-27 21:46:04 +00001555 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001556 }
1557 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001558 case ISD::EXTRACT_SUBREG: {
1559 Tmp1 = LegalizeOp(Node->getOperand(0));
1560 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1561 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001562 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001563 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1564 }
1565 break;
1566 case ISD::INSERT_SUBREG: {
1567 Tmp1 = LegalizeOp(Node->getOperand(0));
1568 Tmp2 = LegalizeOp(Node->getOperand(1));
1569 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1570 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001571 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001572 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1573 }
1574 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001575 case ISD::BUILD_VECTOR:
1576 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1577 default: assert(0 && "This action is not supported yet!");
1578 case TargetLowering::Custom:
1579 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001580 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001581 Result = Tmp3;
1582 break;
1583 }
1584 // FALLTHROUGH
1585 case TargetLowering::Expand:
Gabor Greif1c80d112008-08-28 21:40:38 +00001586 Result = ExpandBUILD_VECTOR(Result.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001587 break;
1588 }
1589 break;
1590 case ISD::INSERT_VECTOR_ELT:
1591 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001592 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001593
1594 // The type of the value to insert may not be legal, even though the vector
1595 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1596 // here.
1597 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1598 default: assert(0 && "Cannot expand insert element operand");
1599 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1600 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang1448aad2008-10-30 08:01:45 +00001601 case Expand:
1602 // FIXME: An alternative would be to check to see if the target is not
1603 // going to custom lower this operation, we could bitcast to half elt
1604 // width and perform two inserts at that width, if that is legal.
1605 Tmp2 = Node->getOperand(1);
1606 break;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001607 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001608 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1609
1610 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1611 Node->getValueType(0))) {
1612 default: assert(0 && "This action is not supported yet!");
1613 case TargetLowering::Legal:
1614 break;
1615 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001616 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001617 if (Tmp4.getNode()) {
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001618 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001619 break;
1620 }
1621 // FALLTHROUGH
Mon P Wang1448aad2008-10-30 08:01:45 +00001622 case TargetLowering::Promote:
1623 // Fall thru for vector case
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001624 case TargetLowering::Expand: {
1625 // If the insert index is a constant, codegen this as a scalar_to_vector,
1626 // then a shuffle that inserts it into the right position in the vector.
1627 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001628 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1629 // match the element type of the vector being created.
1630 if (Tmp2.getValueType() ==
Duncan Sands92c43912008-06-06 12:08:01 +00001631 Op.getValueType().getVectorElementType()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001632 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001633 Tmp1.getValueType(), Tmp2);
1634
Duncan Sands92c43912008-06-06 12:08:01 +00001635 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1636 MVT ShufMaskVT =
1637 MVT::getIntVectorWithNumElements(NumElts);
1638 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001639
1640 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1641 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1642 // elt 0 of the RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00001643 SmallVector<SDValue, 8> ShufOps;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001644 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001645 if (i != InsertPos->getZExtValue())
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001646 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1647 else
1648 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1649 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001650 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001651 &ShufOps[0], ShufOps.size());
1652
1653 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1654 Tmp1, ScVec, ShufMask);
1655 Result = LegalizeOp(Result);
1656 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001657 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001658 }
Nate Begeman7c9e4b72008-04-25 18:07:40 +00001659 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001660 break;
1661 }
1662 }
1663 break;
1664 case ISD::SCALAR_TO_VECTOR:
1665 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1666 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1667 break;
1668 }
1669
1670 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1671 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1672 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1673 Node->getValueType(0))) {
1674 default: assert(0 && "This action is not supported yet!");
1675 case TargetLowering::Legal:
1676 break;
1677 case TargetLowering::Custom:
1678 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001679 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001680 Result = Tmp3;
1681 break;
1682 }
1683 // FALLTHROUGH
1684 case TargetLowering::Expand:
1685 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1686 break;
1687 }
1688 break;
1689 case ISD::VECTOR_SHUFFLE:
1690 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1691 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1692 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1693
1694 // Allow targets to custom lower the SHUFFLEs they support.
1695 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1696 default: assert(0 && "Unknown operation action!");
1697 case TargetLowering::Legal:
1698 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1699 "vector shuffle should not be created if not legal!");
1700 break;
1701 case TargetLowering::Custom:
1702 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001703 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001704 Result = Tmp3;
1705 break;
1706 }
1707 // FALLTHROUGH
1708 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00001709 MVT VT = Node->getValueType(0);
1710 MVT EltVT = VT.getVectorElementType();
1711 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00001712 SDValue Mask = Node->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001713 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00001714 SmallVector<SDValue,8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001715 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001716 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001717 if (Arg.getOpcode() == ISD::UNDEF) {
1718 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1719 } else {
1720 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001721 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001722 if (Idx < NumElems)
1723 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1724 DAG.getConstant(Idx, PtrVT)));
1725 else
1726 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1727 DAG.getConstant(Idx - NumElems, PtrVT)));
1728 }
1729 }
1730 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1731 break;
1732 }
1733 case TargetLowering::Promote: {
1734 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00001735 MVT OVT = Node->getValueType(0);
1736 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001737
1738 // Cast the two input vectors.
1739 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1740 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1741
1742 // Convert the shuffle mask to the right # elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00001743 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001744 assert(Tmp3.getNode() && "Shuffle not legal?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001745 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1746 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1747 break;
1748 }
1749 }
1750 break;
1751
1752 case ISD::EXTRACT_VECTOR_ELT:
1753 Tmp1 = Node->getOperand(0);
1754 Tmp2 = LegalizeOp(Node->getOperand(1));
1755 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1756 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1757 break;
1758
1759 case ISD::EXTRACT_SUBVECTOR:
1760 Tmp1 = Node->getOperand(0);
1761 Tmp2 = LegalizeOp(Node->getOperand(1));
1762 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1763 Result = ExpandEXTRACT_SUBVECTOR(Result);
1764 break;
1765
Mon P Wang1448aad2008-10-30 08:01:45 +00001766 case ISD::CONCAT_VECTORS: {
1767 // Use extract/insert/build vector for now. We might try to be
1768 // more clever later.
1769 MVT PtrVT = TLI.getPointerTy();
1770 SmallVector<SDValue, 8> Ops;
1771 unsigned NumOperands = Node->getNumOperands();
1772 for (unsigned i=0; i < NumOperands; ++i) {
1773 SDValue SubOp = Node->getOperand(i);
1774 MVT VVT = SubOp.getNode()->getValueType(0);
1775 MVT EltVT = VVT.getVectorElementType();
1776 unsigned NumSubElem = VVT.getVectorNumElements();
1777 for (unsigned j=0; j < NumSubElem; ++j) {
1778 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1779 DAG.getConstant(j, PtrVT)));
1780 }
1781 }
1782 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1783 &Ops[0], Ops.size()));
1784 }
1785
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001786 case ISD::CALLSEQ_START: {
1787 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1788
1789 // Recursively Legalize all of the inputs of the call end that do not lead
1790 // to this call start. This ensures that any libcalls that need be inserted
1791 // are inserted *before* the CALLSEQ_START.
1792 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1793 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greif1c80d112008-08-28 21:40:38 +00001794 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001795 NodesLeadingTo);
1796 }
1797
1798 // Now that we legalized all of the inputs (which may have inserted
1799 // libcalls) create the new CALLSEQ_START node.
1800 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1801
1802 // Merge in the last call, to ensure that this call start after the last
1803 // call ended.
1804 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1805 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1806 Tmp1 = LegalizeOp(Tmp1);
1807 }
1808
1809 // Do not try to legalize the target-specific arguments (#1+).
1810 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001811 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001812 Ops[0] = Tmp1;
1813 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1814 }
1815
1816 // Remember that the CALLSEQ_START is legalized.
1817 AddLegalizedOperand(Op.getValue(0), Result);
1818 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1819 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1820
1821 // Now that the callseq_start and all of the non-call nodes above this call
1822 // sequence have been legalized, legalize the call itself. During this
1823 // process, no libcalls can/will be inserted, guaranteeing that no calls
1824 // can overlap.
1825 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001826 // Note that we are selecting this call!
Dan Gohman8181bd12008-07-27 21:46:04 +00001827 LastCALLSEQ_END = SDValue(CallEnd, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001828 IsLegalizingCall = true;
1829
1830 // Legalize the call, starting from the CALLSEQ_END.
1831 LegalizeOp(LastCALLSEQ_END);
1832 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1833 return Result;
1834 }
1835 case ISD::CALLSEQ_END:
1836 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1837 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greif1c80d112008-08-28 21:40:38 +00001838 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001839 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1840 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001841 assert(I != LegalizedNodes.end() &&
1842 "Legalizing the call start should have legalized this node!");
1843 return I->second;
1844 }
1845
1846 // Otherwise, the call start has been legalized and everything is going
1847 // according to plan. Just legalize ourselves normally here.
1848 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1849 // Do not try to legalize the target-specific arguments (#1+), except for
1850 // an optional flag input.
1851 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1852 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001853 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001854 Ops[0] = Tmp1;
1855 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1856 }
1857 } else {
1858 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1859 if (Tmp1 != Node->getOperand(0) ||
1860 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001861 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001862 Ops[0] = Tmp1;
1863 Ops.back() = Tmp2;
1864 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1865 }
1866 }
1867 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1868 // This finishes up call legalization.
1869 IsLegalizingCall = false;
1870
1871 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001872 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001873 if (Node->getNumValues() == 2)
Dan Gohman8181bd12008-07-27 21:46:04 +00001874 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001875 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001876 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands92c43912008-06-06 12:08:01 +00001877 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001878 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1879 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1880 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1881 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1882
1883 Tmp1 = Result.getValue(0);
1884 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001885 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001886 default: assert(0 && "This action is not supported yet!");
1887 case TargetLowering::Expand: {
1888 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1889 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1890 " not tell us which reg is the stack pointer!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001891 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001892
1893 // Chain the dynamic stack allocation so that it doesn't modify the stack
1894 // pointer when other instructions are using the stack.
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001895 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling22f8deb2007-11-13 00:44:25 +00001896
Dan Gohman8181bd12008-07-27 21:46:04 +00001897 SDValue Size = Tmp2.getOperand(1);
1898 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Chenga448bc42007-08-16 23:50:06 +00001899 Chain = SP.getValue(1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001900 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Chenga448bc42007-08-16 23:50:06 +00001901 unsigned StackAlign =
1902 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1903 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001904 SP = DAG.getNode(ISD::AND, VT, SP,
1905 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001906 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling22f8deb2007-11-13 00:44:25 +00001907 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1908
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001909 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1910 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling22f8deb2007-11-13 00:44:25 +00001911
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001912 Tmp1 = LegalizeOp(Tmp1);
1913 Tmp2 = LegalizeOp(Tmp2);
1914 break;
1915 }
1916 case TargetLowering::Custom:
1917 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001918 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001919 Tmp1 = LegalizeOp(Tmp3);
1920 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1921 }
1922 break;
1923 case TargetLowering::Legal:
1924 break;
1925 }
1926 // Since this op produce two values, make sure to remember that we
1927 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001928 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1929 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001930 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001931 }
1932 case ISD::INLINEASM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001933 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001934 bool Changed = false;
1935 // Legalize all of the operands of the inline asm, in case they are nodes
1936 // that need to be expanded or something. Note we skip the asm string and
1937 // all of the TargetConstant flags.
Dan Gohman8181bd12008-07-27 21:46:04 +00001938 SDValue Op = LegalizeOp(Ops[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001939 Changed = Op != Ops[0];
1940 Ops[0] = Op;
1941
1942 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1943 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001944 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001945 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001946 SDValue Op = LegalizeOp(Ops[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001947 if (Op != Ops[i]) {
1948 Changed = true;
1949 Ops[i] = Op;
1950 }
1951 }
1952 }
1953
1954 if (HasInFlag) {
1955 Op = LegalizeOp(Ops.back());
1956 Changed |= Op != Ops.back();
1957 Ops.back() = Op;
1958 }
1959
1960 if (Changed)
1961 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1962
1963 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman8181bd12008-07-27 21:46:04 +00001964 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1965 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001966 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001967 }
1968 case ISD::BR:
1969 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1970 // Ensure that libcalls are emitted before a branch.
1971 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1972 Tmp1 = LegalizeOp(Tmp1);
1973 LastCALLSEQ_END = DAG.getEntryNode();
1974
1975 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1976 break;
1977 case ISD::BRIND:
1978 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1979 // Ensure that libcalls are emitted before a branch.
1980 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1981 Tmp1 = LegalizeOp(Tmp1);
1982 LastCALLSEQ_END = DAG.getEntryNode();
1983
1984 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1985 default: assert(0 && "Indirect target must be legal type (pointer)!");
1986 case Legal:
1987 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1988 break;
1989 }
1990 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1991 break;
1992 case ISD::BR_JT:
1993 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1994 // Ensure that libcalls are emitted before a branch.
1995 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1996 Tmp1 = LegalizeOp(Tmp1);
1997 LastCALLSEQ_END = DAG.getEntryNode();
1998
1999 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
2000 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2001
2002 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
2003 default: assert(0 && "This action is not supported yet!");
2004 case TargetLowering::Legal: break;
2005 case TargetLowering::Custom:
2006 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002007 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002008 break;
2009 case TargetLowering::Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002010 SDValue Chain = Result.getOperand(0);
2011 SDValue Table = Result.getOperand(1);
2012 SDValue Index = Result.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002013
Duncan Sands92c43912008-06-06 12:08:01 +00002014 MVT PTy = TLI.getPointerTy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002015 MachineFunction &MF = DAG.getMachineFunction();
2016 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
2017 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman8181bd12008-07-27 21:46:04 +00002018 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002019
Duncan Sands12ddc802008-12-12 08:13:38 +00002020 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
2021 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, Chain, Addr,
2022 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Cheng6fb06762007-11-09 01:32:10 +00002023 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002024 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2025 // For PIC, the sequence is:
2026 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00002027 // RelocBase can be JumpTable, GOT or some sort of global base.
Evan Cheng6fb06762007-11-09 01:32:10 +00002028 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
2029 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002030 }
Evan Cheng6fb06762007-11-09 01:32:10 +00002031 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002032 }
2033 }
2034 break;
2035 case ISD::BRCOND:
2036 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2037 // Ensure that libcalls are emitted before a return.
2038 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2039 Tmp1 = LegalizeOp(Tmp1);
2040 LastCALLSEQ_END = DAG.getEntryNode();
2041
2042 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2043 case Expand: assert(0 && "It's impossible to expand bools");
2044 case Legal:
2045 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2046 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002047 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002048 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
2049
2050 // The top bits of the promoted condition are not necessarily zero, ensure
2051 // that the value is properly zero extended.
Dan Gohman07961cd2008-02-25 21:11:39 +00002052 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002053 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman07961cd2008-02-25 21:11:39 +00002054 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002055 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
2056 break;
2057 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002058 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002059
2060 // Basic block destination (Op#2) is always legal.
2061 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2062
2063 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
2064 default: assert(0 && "This action is not supported yet!");
2065 case TargetLowering::Legal: break;
2066 case TargetLowering::Custom:
2067 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002068 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002069 break;
2070 case TargetLowering::Expand:
2071 // Expand brcond's setcc into its constituent parts and create a BR_CC
2072 // Node.
2073 if (Tmp2.getOpcode() == ISD::SETCC) {
2074 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
2075 Tmp2.getOperand(0), Tmp2.getOperand(1),
2076 Node->getOperand(2));
2077 } else {
2078 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
2079 DAG.getCondCode(ISD::SETNE), Tmp2,
2080 DAG.getConstant(0, Tmp2.getValueType()),
2081 Node->getOperand(2));
2082 }
2083 break;
2084 }
2085 break;
2086 case ISD::BR_CC:
2087 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2088 // Ensure that libcalls are emitted before a branch.
2089 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2090 Tmp1 = LegalizeOp(Tmp1);
2091 Tmp2 = Node->getOperand(2); // LHS
2092 Tmp3 = Node->getOperand(3); // RHS
2093 Tmp4 = Node->getOperand(1); // CC
2094
Duncan Sands4a361272009-01-01 15:52:00 +00002095 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()), Tmp2, Tmp3,Tmp4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002096 LastCALLSEQ_END = DAG.getEntryNode();
2097
Evan Cheng71343822008-10-15 02:05:31 +00002098 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002099 // the LHS is a legal SETCC itself. In this case, we need to compare
2100 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00002101 if (Tmp3.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002102 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2103 Tmp4 = DAG.getCondCode(ISD::SETNE);
2104 }
2105
2106 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2107 Node->getOperand(4));
2108
2109 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2110 default: assert(0 && "Unexpected action for BR_CC!");
2111 case TargetLowering::Legal: break;
2112 case TargetLowering::Custom:
2113 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002114 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002115 break;
2116 }
2117 break;
2118 case ISD::LOAD: {
2119 LoadSDNode *LD = cast<LoadSDNode>(Node);
2120 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2121 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
2122
2123 ISD::LoadExtType ExtType = LD->getExtensionType();
2124 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands92c43912008-06-06 12:08:01 +00002125 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002126 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2127 Tmp3 = Result.getValue(0);
2128 Tmp4 = Result.getValue(1);
2129
2130 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2131 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002132 case TargetLowering::Legal:
2133 // If this is an unaligned load and the target doesn't support it,
2134 // expand it.
2135 if (!TLI.allowsUnalignedMemoryAccesses()) {
2136 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002137 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002138 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002139 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002140 TLI);
2141 Tmp3 = Result.getOperand(0);
2142 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00002143 Tmp3 = LegalizeOp(Tmp3);
2144 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002145 }
2146 }
2147 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002148 case TargetLowering::Custom:
2149 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002150 if (Tmp1.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002151 Tmp3 = LegalizeOp(Tmp1);
2152 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2153 }
2154 break;
2155 case TargetLowering::Promote: {
2156 // Only promote a load of vector type to another.
Duncan Sands92c43912008-06-06 12:08:01 +00002157 assert(VT.isVector() && "Cannot promote this load!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002158 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002159 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002160
2161 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
2162 LD->getSrcValueOffset(),
2163 LD->isVolatile(), LD->getAlignment());
2164 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2165 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2166 break;
2167 }
2168 }
2169 // Since loads produce two values, make sure to remember that we
2170 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002171 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2172 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif46bf5472008-08-26 22:36:50 +00002173 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002174 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00002175 MVT SrcVT = LD->getMemoryVT();
2176 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sands082524c2008-01-23 20:39:46 +00002177 int SVOffset = LD->getSrcValueOffset();
2178 unsigned Alignment = LD->getAlignment();
2179 bool isVolatile = LD->isVolatile();
2180
Duncan Sands92c43912008-06-06 12:08:01 +00002181 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002182 // Some targets pretend to have an i1 loading operation, and actually
2183 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2184 // bits are guaranteed to be zero; it helps the optimizers understand
2185 // that these bits are zero. It is also useful for EXTLOAD, since it
2186 // tells the optimizers that those bits are undefined. It would be
2187 // nice to have an effective generic way of getting these benefits...
2188 // Until such a way is found, don't insist on promoting i1 here.
2189 (SrcVT != MVT::i1 ||
Evan Cheng08c171a2008-10-14 21:26:46 +00002190 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002191 // Promote to a byte-sized load if not loading an integral number of
2192 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands92c43912008-06-06 12:08:01 +00002193 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2194 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002195 SDValue Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002196
2197 // The extra bits are guaranteed to be zero, since we stored them that
2198 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2199
2200 ISD::LoadExtType NewExtType =
2201 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2202
2203 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2204 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2205 NVT, isVolatile, Alignment);
2206
2207 Ch = Result.getValue(1); // The chain.
2208
2209 if (ExtType == ISD::SEXTLOAD)
2210 // Having the top bits zero doesn't help when sign extending.
2211 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2212 Result, DAG.getValueType(SrcVT));
2213 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2214 // All the top bits are guaranteed to be zero - inform the optimizers.
2215 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2216 DAG.getValueType(SrcVT));
2217
2218 Tmp1 = LegalizeOp(Result);
2219 Tmp2 = LegalizeOp(Ch);
2220 } else if (SrcWidth & (SrcWidth - 1)) {
2221 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands92c43912008-06-06 12:08:01 +00002222 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002223 "Unsupported extload!");
2224 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2225 assert(RoundWidth < SrcWidth);
2226 unsigned ExtraWidth = SrcWidth - RoundWidth;
2227 assert(ExtraWidth < RoundWidth);
2228 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2229 "Load size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002230 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2231 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002232 SDValue Lo, Hi, Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002233 unsigned IncrementSize;
2234
2235 if (TLI.isLittleEndian()) {
2236 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2237 // Load the bottom RoundWidth bits.
2238 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2239 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2240 Alignment);
2241
2242 // Load the remaining ExtraWidth bits.
2243 IncrementSize = RoundWidth / 8;
2244 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2245 DAG.getIntPtrConstant(IncrementSize));
2246 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2247 LD->getSrcValue(), SVOffset + IncrementSize,
2248 ExtraVT, isVolatile,
2249 MinAlign(Alignment, IncrementSize));
2250
2251 // Build a factor node to remember that this load is independent of the
2252 // other one.
2253 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2254 Hi.getValue(1));
2255
2256 // Move the top bits to the right place.
2257 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2258 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2259
2260 // Join the hi and lo parts.
2261 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002262 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00002263 // Big endian - avoid unaligned loads.
2264 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2265 // Load the top RoundWidth bits.
2266 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2267 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2268 Alignment);
2269
2270 // Load the remaining ExtraWidth bits.
2271 IncrementSize = RoundWidth / 8;
2272 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2273 DAG.getIntPtrConstant(IncrementSize));
2274 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2275 LD->getSrcValue(), SVOffset + IncrementSize,
2276 ExtraVT, isVolatile,
2277 MinAlign(Alignment, IncrementSize));
2278
2279 // Build a factor node to remember that this load is independent of the
2280 // other one.
2281 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2282 Hi.getValue(1));
2283
2284 // Move the top bits to the right place.
2285 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2286 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2287
2288 // Join the hi and lo parts.
2289 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2290 }
2291
2292 Tmp1 = LegalizeOp(Result);
2293 Tmp2 = LegalizeOp(Ch);
2294 } else {
Evan Cheng08c171a2008-10-14 21:26:46 +00002295 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002296 default: assert(0 && "This action is not supported yet!");
2297 case TargetLowering::Custom:
2298 isCustom = true;
2299 // FALLTHROUGH
2300 case TargetLowering::Legal:
2301 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2302 Tmp1 = Result.getValue(0);
2303 Tmp2 = Result.getValue(1);
2304
2305 if (isCustom) {
2306 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002307 if (Tmp3.getNode()) {
Duncan Sands082524c2008-01-23 20:39:46 +00002308 Tmp1 = LegalizeOp(Tmp3);
2309 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2310 }
2311 } else {
2312 // If this is an unaligned load and the target doesn't support it,
2313 // expand it.
2314 if (!TLI.allowsUnalignedMemoryAccesses()) {
2315 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002316 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sands082524c2008-01-23 20:39:46 +00002317 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002318 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sands082524c2008-01-23 20:39:46 +00002319 TLI);
2320 Tmp1 = Result.getOperand(0);
2321 Tmp2 = Result.getOperand(1);
2322 Tmp1 = LegalizeOp(Tmp1);
2323 Tmp2 = LegalizeOp(Tmp2);
2324 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002325 }
2326 }
Duncan Sands082524c2008-01-23 20:39:46 +00002327 break;
2328 case TargetLowering::Expand:
2329 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2330 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002331 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sands082524c2008-01-23 20:39:46 +00002332 LD->getSrcValueOffset(),
2333 LD->isVolatile(), LD->getAlignment());
2334 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2335 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2336 Tmp2 = LegalizeOp(Load.getValue(1));
2337 break;
2338 }
2339 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2340 // Turn the unsupported load into an EXTLOAD followed by an explicit
2341 // zero/sign extend inreg.
2342 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2343 Tmp1, Tmp2, LD->getSrcValue(),
2344 LD->getSrcValueOffset(), SrcVT,
2345 LD->isVolatile(), LD->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00002346 SDValue ValRes;
Duncan Sands082524c2008-01-23 20:39:46 +00002347 if (ExtType == ISD::SEXTLOAD)
2348 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2349 Result, DAG.getValueType(SrcVT));
2350 else
2351 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2352 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2353 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002354 break;
2355 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002356 }
Duncan Sands082524c2008-01-23 20:39:46 +00002357
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002358 // Since loads produce two values, make sure to remember that we legalized
2359 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002360 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2361 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002362 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002363 }
2364 }
2365 case ISD::EXTRACT_ELEMENT: {
Duncan Sands92c43912008-06-06 12:08:01 +00002366 MVT OpTy = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002367 switch (getTypeAction(OpTy)) {
2368 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2369 case Legal:
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002370 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002371 // 1 -> Hi
2372 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands92c43912008-06-06 12:08:01 +00002373 DAG.getConstant(OpTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002374 TLI.getShiftAmountTy()));
2375 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2376 } else {
2377 // 0 -> Lo
2378 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2379 Node->getOperand(0));
2380 }
2381 break;
2382 case Expand:
2383 // Get both the low and high parts.
2384 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002385 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002386 Result = Tmp2; // 1 -> Hi
2387 else
2388 Result = Tmp1; // 0 -> Lo
2389 break;
2390 }
2391 break;
2392 }
2393
2394 case ISD::CopyToReg:
2395 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2396
2397 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2398 "Register type must be legal!");
2399 // Legalize the incoming value (must be a legal type).
2400 Tmp2 = LegalizeOp(Node->getOperand(2));
2401 if (Node->getNumValues() == 1) {
2402 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2403 } else {
2404 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2405 if (Node->getNumOperands() == 4) {
2406 Tmp3 = LegalizeOp(Node->getOperand(3));
2407 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2408 Tmp3);
2409 } else {
2410 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2411 }
2412
2413 // Since this produces two values, make sure to remember that we legalized
2414 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002415 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2416 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002417 return Result;
2418 }
2419 break;
2420
2421 case ISD::RET:
2422 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2423
2424 // Ensure that libcalls are emitted before a return.
2425 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2426 Tmp1 = LegalizeOp(Tmp1);
2427 LastCALLSEQ_END = DAG.getEntryNode();
2428
2429 switch (Node->getNumOperands()) {
2430 case 3: // ret val
2431 Tmp2 = Node->getOperand(1);
2432 Tmp3 = Node->getOperand(2); // Signness
2433 switch (getTypeAction(Tmp2.getValueType())) {
2434 case Legal:
2435 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2436 break;
2437 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00002438 if (!Tmp2.getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002439 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002440 ExpandOp(Tmp2, Lo, Hi);
2441
2442 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002443 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002444 std::swap(Lo, Hi);
2445
Gabor Greif1c80d112008-08-28 21:40:38 +00002446 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002447 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2448 else
2449 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2450 Result = LegalizeOp(Result);
2451 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +00002452 SDNode *InVal = Tmp2.getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002453 int InIx = Tmp2.getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002454 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2455 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002456
2457 // Figure out if there is a simple type corresponding to this Vector
2458 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002459 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002460 if (TLI.isTypeLegal(TVT)) {
2461 // Turn this into a return of the vector type.
2462 Tmp2 = LegalizeOp(Tmp2);
2463 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2464 } else if (NumElems == 1) {
2465 // Turn this into a return of the scalar type.
2466 Tmp2 = ScalarizeVectorOp(Tmp2);
2467 Tmp2 = LegalizeOp(Tmp2);
2468 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2469
2470 // FIXME: Returns of gcc generic vectors smaller than a legal type
2471 // should be returned in integer registers!
2472
2473 // The scalarized value type may not be legal, e.g. it might require
2474 // promotion or expansion. Relegalize the return.
2475 Result = LegalizeOp(Result);
2476 } else {
2477 // FIXME: Returns of gcc generic vectors larger than a legal vector
2478 // type should be returned by reference!
Dan Gohman8181bd12008-07-27 21:46:04 +00002479 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002480 SplitVectorOp(Tmp2, Lo, Hi);
2481 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2482 Result = LegalizeOp(Result);
2483 }
2484 }
2485 break;
2486 case Promote:
2487 Tmp2 = PromoteOp(Node->getOperand(1));
2488 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2489 Result = LegalizeOp(Result);
2490 break;
2491 }
2492 break;
2493 case 1: // ret void
2494 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2495 break;
2496 default: { // ret <values>
Dan Gohman8181bd12008-07-27 21:46:04 +00002497 SmallVector<SDValue, 8> NewValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002498 NewValues.push_back(Tmp1);
2499 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2500 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2501 case Legal:
2502 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2503 NewValues.push_back(Node->getOperand(i+1));
2504 break;
2505 case Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002506 SDValue Lo, Hi;
Duncan Sands92c43912008-06-06 12:08:01 +00002507 assert(!Node->getOperand(i).getValueType().isExtended() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002508 "FIXME: TODO: implement returning non-legal vector types!");
2509 ExpandOp(Node->getOperand(i), Lo, Hi);
2510 NewValues.push_back(Lo);
2511 NewValues.push_back(Node->getOperand(i+1));
Gabor Greif1c80d112008-08-28 21:40:38 +00002512 if (Hi.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002513 NewValues.push_back(Hi);
2514 NewValues.push_back(Node->getOperand(i+1));
2515 }
2516 break;
2517 }
2518 case Promote:
2519 assert(0 && "Can't promote multiple return value yet!");
2520 }
2521
2522 if (NewValues.size() == Node->getNumOperands())
2523 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2524 else
2525 Result = DAG.getNode(ISD::RET, MVT::Other,
2526 &NewValues[0], NewValues.size());
2527 break;
2528 }
2529 }
2530
2531 if (Result.getOpcode() == ISD::RET) {
2532 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2533 default: assert(0 && "This action is not supported yet!");
2534 case TargetLowering::Legal: break;
2535 case TargetLowering::Custom:
2536 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002537 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002538 break;
2539 }
2540 }
2541 break;
2542 case ISD::STORE: {
2543 StoreSDNode *ST = cast<StoreSDNode>(Node);
2544 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2545 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2546 int SVOffset = ST->getSrcValueOffset();
2547 unsigned Alignment = ST->getAlignment();
2548 bool isVolatile = ST->isVolatile();
2549
2550 if (!ST->isTruncatingStore()) {
2551 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2552 // FIXME: We shouldn't do this for TargetConstantFP's.
2553 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2554 // to phase ordering between legalized code and the dag combiner. This
2555 // probably means that we need to integrate dag combiner and legalizer
2556 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002557 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002558 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002559 if (CFP->getValueType(0) == MVT::f32 &&
2560 getTypeAction(MVT::i32) == Legal) {
Dan Gohman39509762008-03-11 00:11:06 +00002561 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002562 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen1616e902007-09-11 18:32:33 +00002563 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002564 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2565 SVOffset, isVolatile, Alignment);
2566 break;
2567 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002568 // If this target supports 64-bit registers, do a single 64-bit store.
2569 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002570 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman39509762008-03-11 00:11:06 +00002571 zextOrTrunc(64), MVT::i64);
Chris Lattner19f229a2007-10-15 05:46:06 +00002572 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2573 SVOffset, isVolatile, Alignment);
2574 break;
Duncan Sands2418bec2008-06-13 19:07:40 +00002575 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002576 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2577 // stores. If the target supports neither 32- nor 64-bits, this
2578 // xform is certainly not worth it.
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002579 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman8181bd12008-07-27 21:46:04 +00002580 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2581 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002582 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002583
2584 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2585 SVOffset, isVolatile, Alignment);
2586 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002587 DAG.getIntPtrConstant(4));
Chris Lattner19f229a2007-10-15 05:46:06 +00002588 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002589 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002590
2591 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2592 break;
2593 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002594 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002595 }
2596
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002597 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002598 case Legal: {
2599 Tmp3 = LegalizeOp(ST->getValue());
2600 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2601 ST->getOffset());
2602
Duncan Sands92c43912008-06-06 12:08:01 +00002603 MVT VT = Tmp3.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002604 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2605 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002606 case TargetLowering::Legal:
2607 // If this is an unaligned store and the target doesn't support it,
2608 // expand it.
2609 if (!TLI.allowsUnalignedMemoryAccesses()) {
2610 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002611 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002612 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002613 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002614 TLI);
2615 }
2616 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002617 case TargetLowering::Custom:
2618 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002619 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620 break;
2621 case TargetLowering::Promote:
Duncan Sands92c43912008-06-06 12:08:01 +00002622 assert(VT.isVector() && "Unknown legal promote case!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002623 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2624 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2625 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2626 ST->getSrcValue(), SVOffset, isVolatile,
2627 Alignment);
2628 break;
2629 }
2630 break;
2631 }
2632 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002633 if (!ST->getMemoryVT().isVector()) {
2634 // Truncate the value and store the result.
2635 Tmp3 = PromoteOp(ST->getValue());
2636 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2637 SVOffset, ST->getMemoryVT(),
2638 isVolatile, Alignment);
2639 break;
2640 }
2641 // Fall thru to expand for vector
2642 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002643 unsigned IncrementSize = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002644 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002645
2646 // If this is a vector type, then we have to calculate the increment as
2647 // the product of the element size in bytes, and the number of elements
2648 // in the high half of the vector.
Duncan Sands92c43912008-06-06 12:08:01 +00002649 if (ST->getValue().getValueType().isVector()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002650 SDNode *InVal = ST->getValue().getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002651 int InIx = ST->getValue().getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002652 MVT InVT = InVal->getValueType(InIx);
2653 unsigned NumElems = InVT.getVectorNumElements();
2654 MVT EVT = InVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002655
2656 // Figure out if there is a simple type corresponding to this Vector
2657 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002658 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002659 if (TLI.isTypeLegal(TVT)) {
2660 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002661 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002662 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2663 SVOffset, isVolatile, Alignment);
2664 Result = LegalizeOp(Result);
2665 break;
2666 } else if (NumElems == 1) {
2667 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002668 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002669 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2670 SVOffset, isVolatile, Alignment);
2671 // The scalarized value type may not be legal, e.g. it might require
2672 // promotion or expansion. Relegalize the scalar store.
2673 Result = LegalizeOp(Result);
2674 break;
2675 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +00002676 // Check if we have widen this node with another value
2677 std::map<SDValue, SDValue>::iterator I =
2678 WidenNodes.find(ST->getValue());
2679 if (I != WidenNodes.end()) {
2680 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2681 break;
2682 }
2683 else {
2684 SplitVectorOp(ST->getValue(), Lo, Hi);
2685 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2686 EVT.getSizeInBits()/8;
2687 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002688 }
2689 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002690 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greif1c80d112008-08-28 21:40:38 +00002691 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002692
Richard Pennington73ae9e42008-09-25 16:15:10 +00002693 if (Hi.getNode() && TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002694 std::swap(Lo, Hi);
2695 }
2696
2697 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2698 SVOffset, isVolatile, Alignment);
2699
Gabor Greif1c80d112008-08-28 21:40:38 +00002700 if (Hi.getNode() == NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002701 // Must be int <-> float one-to-one expansion.
2702 Result = Lo;
2703 break;
2704 }
2705
2706 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002707 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002708 assert(isTypeLegal(Tmp2.getValueType()) &&
2709 "Pointers must be legal!");
2710 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002711 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002712 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2713 SVOffset, isVolatile, Alignment);
2714 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2715 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00002716 } // case Expand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002717 }
2718 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002719 switch (getTypeAction(ST->getValue().getValueType())) {
2720 case Legal:
2721 Tmp3 = LegalizeOp(ST->getValue());
2722 break;
2723 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002724 if (!ST->getValue().getValueType().isVector()) {
2725 // We can promote the value, the truncstore will still take care of it.
2726 Tmp3 = PromoteOp(ST->getValue());
2727 break;
2728 }
2729 // Vector case falls through to expand
Chris Lattner3bc08502008-01-17 19:59:44 +00002730 case Expand:
2731 // Just store the low part. This may become a non-trunc store, so make
2732 // sure to use getTruncStore, not UpdateNodeOperands below.
2733 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2734 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2735 SVOffset, MVT::i8, isVolatile, Alignment);
2736 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002737
Duncan Sands92c43912008-06-06 12:08:01 +00002738 MVT StVT = ST->getMemoryVT();
2739 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands40676662008-01-22 07:17:34 +00002740
Duncan Sands92c43912008-06-06 12:08:01 +00002741 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands40676662008-01-22 07:17:34 +00002742 // Promote to a byte-sized store with upper bits zero if not
2743 // storing an integral number of bytes. For example, promote
2744 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands92c43912008-06-06 12:08:01 +00002745 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands40676662008-01-22 07:17:34 +00002746 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2747 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2748 SVOffset, NVT, isVolatile, Alignment);
2749 } else if (StWidth & (StWidth - 1)) {
2750 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands92c43912008-06-06 12:08:01 +00002751 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands40676662008-01-22 07:17:34 +00002752 "Unsupported truncstore!");
2753 unsigned RoundWidth = 1 << Log2_32(StWidth);
2754 assert(RoundWidth < StWidth);
2755 unsigned ExtraWidth = StWidth - RoundWidth;
2756 assert(ExtraWidth < RoundWidth);
2757 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2758 "Store size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002759 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2760 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002761 SDValue Lo, Hi;
Duncan Sands40676662008-01-22 07:17:34 +00002762 unsigned IncrementSize;
2763
2764 if (TLI.isLittleEndian()) {
2765 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2766 // Store the bottom RoundWidth bits.
2767 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2768 SVOffset, RoundVT,
2769 isVolatile, Alignment);
2770
2771 // Store the remaining ExtraWidth bits.
2772 IncrementSize = RoundWidth / 8;
2773 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2774 DAG.getIntPtrConstant(IncrementSize));
2775 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2776 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2777 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2778 SVOffset + IncrementSize, ExtraVT, isVolatile,
2779 MinAlign(Alignment, IncrementSize));
2780 } else {
2781 // Big endian - avoid unaligned stores.
2782 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2783 // Store the top RoundWidth bits.
2784 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2785 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2786 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2787 RoundVT, isVolatile, Alignment);
2788
2789 // Store the remaining ExtraWidth bits.
2790 IncrementSize = RoundWidth / 8;
2791 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2792 DAG.getIntPtrConstant(IncrementSize));
2793 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2794 SVOffset + IncrementSize, ExtraVT, isVolatile,
2795 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002796 }
Duncan Sands40676662008-01-22 07:17:34 +00002797
2798 // The order of the stores doesn't matter.
2799 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2800 } else {
2801 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2802 Tmp2 != ST->getBasePtr())
2803 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2804 ST->getOffset());
2805
2806 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2807 default: assert(0 && "This action is not supported yet!");
2808 case TargetLowering::Legal:
2809 // If this is an unaligned store and the target doesn't support it,
2810 // expand it.
2811 if (!TLI.allowsUnalignedMemoryAccesses()) {
2812 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002813 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands40676662008-01-22 07:17:34 +00002814 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002815 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands40676662008-01-22 07:17:34 +00002816 TLI);
2817 }
2818 break;
2819 case TargetLowering::Custom:
2820 Result = TLI.LowerOperation(Result, DAG);
2821 break;
2822 case Expand:
2823 // TRUNCSTORE:i16 i32 -> STORE i16
2824 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2825 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2826 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2827 isVolatile, Alignment);
2828 break;
2829 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002830 }
2831 }
2832 break;
2833 }
2834 case ISD::PCMARKER:
2835 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2836 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2837 break;
2838 case ISD::STACKSAVE:
2839 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2840 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2841 Tmp1 = Result.getValue(0);
2842 Tmp2 = Result.getValue(1);
2843
2844 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2845 default: assert(0 && "This action is not supported yet!");
2846 case TargetLowering::Legal: break;
2847 case TargetLowering::Custom:
2848 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002849 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002850 Tmp1 = LegalizeOp(Tmp3);
2851 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2852 }
2853 break;
2854 case TargetLowering::Expand:
2855 // Expand to CopyFromReg if the target set
2856 // StackPointerRegisterToSaveRestore.
2857 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2858 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2859 Node->getValueType(0));
2860 Tmp2 = Tmp1.getValue(1);
2861 } else {
2862 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2863 Tmp2 = Node->getOperand(0);
2864 }
2865 break;
2866 }
2867
2868 // Since stacksave produce two values, make sure to remember that we
2869 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002870 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2871 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002872 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002873
2874 case ISD::STACKRESTORE:
2875 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2876 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2877 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2878
2879 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2880 default: assert(0 && "This action is not supported yet!");
2881 case TargetLowering::Legal: break;
2882 case TargetLowering::Custom:
2883 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002884 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002885 break;
2886 case TargetLowering::Expand:
2887 // Expand to CopyToReg if the target set
2888 // StackPointerRegisterToSaveRestore.
2889 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2890 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2891 } else {
2892 Result = Tmp1;
2893 }
2894 break;
2895 }
2896 break;
2897
2898 case ISD::READCYCLECOUNTER:
2899 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2900 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2901 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2902 Node->getValueType(0))) {
2903 default: assert(0 && "This action is not supported yet!");
2904 case TargetLowering::Legal:
2905 Tmp1 = Result.getValue(0);
2906 Tmp2 = Result.getValue(1);
2907 break;
2908 case TargetLowering::Custom:
2909 Result = TLI.LowerOperation(Result, DAG);
2910 Tmp1 = LegalizeOp(Result.getValue(0));
2911 Tmp2 = LegalizeOp(Result.getValue(1));
2912 break;
2913 }
2914
2915 // Since rdcc produce two values, make sure to remember that we legalized
2916 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002917 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2918 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002919 return Result;
2920
2921 case ISD::SELECT:
2922 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2923 case Expand: assert(0 && "It's impossible to expand bools");
2924 case Legal:
2925 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2926 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002927 case Promote: {
Mon P Wang1448aad2008-10-30 08:01:45 +00002928 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002929 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2930 // Make sure the condition is either zero or one.
Dan Gohman07961cd2008-02-25 21:11:39 +00002931 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002932 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman07961cd2008-02-25 21:11:39 +00002933 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002934 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2935 break;
2936 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002937 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002938 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2939 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2940
2941 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2942
2943 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2944 default: assert(0 && "This action is not supported yet!");
2945 case TargetLowering::Legal: break;
2946 case TargetLowering::Custom: {
2947 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002948 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002949 break;
2950 }
2951 case TargetLowering::Expand:
2952 if (Tmp1.getOpcode() == ISD::SETCC) {
2953 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2954 Tmp2, Tmp3,
2955 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2956 } else {
2957 Result = DAG.getSelectCC(Tmp1,
2958 DAG.getConstant(0, Tmp1.getValueType()),
2959 Tmp2, Tmp3, ISD::SETNE);
2960 }
2961 break;
2962 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00002963 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002964 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2965 unsigned ExtOp, TruncOp;
Duncan Sands92c43912008-06-06 12:08:01 +00002966 if (Tmp2.getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002967 ExtOp = ISD::BIT_CONVERT;
2968 TruncOp = ISD::BIT_CONVERT;
Duncan Sands92c43912008-06-06 12:08:01 +00002969 } else if (Tmp2.getValueType().isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002970 ExtOp = ISD::ANY_EXTEND;
2971 TruncOp = ISD::TRUNCATE;
2972 } else {
2973 ExtOp = ISD::FP_EXTEND;
2974 TruncOp = ISD::FP_ROUND;
2975 }
2976 // Promote each of the values to the new type.
2977 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2978 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2979 // Perform the larger operation, then round down.
2980 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00002981 if (TruncOp != ISD::FP_ROUND)
2982 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2983 else
2984 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2985 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002986 break;
2987 }
2988 }
2989 break;
2990 case ISD::SELECT_CC: {
2991 Tmp1 = Node->getOperand(0); // LHS
2992 Tmp2 = Node->getOperand(1); // RHS
2993 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2994 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman8181bd12008-07-27 21:46:04 +00002995 SDValue CC = Node->getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002996
Duncan Sands4a361272009-01-01 15:52:00 +00002997 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002998
Evan Cheng71343822008-10-15 02:05:31 +00002999 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003000 // the LHS is a legal SETCC itself. In this case, we need to compare
3001 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00003002 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003003 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3004 CC = DAG.getCondCode(ISD::SETNE);
3005 }
3006 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
3007
3008 // Everything is legal, see if we should expand this op or something.
3009 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
3010 default: assert(0 && "This action is not supported yet!");
3011 case TargetLowering::Legal: break;
3012 case TargetLowering::Custom:
3013 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003014 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003015 break;
3016 }
3017 break;
3018 }
3019 case ISD::SETCC:
3020 Tmp1 = Node->getOperand(0);
3021 Tmp2 = Node->getOperand(1);
3022 Tmp3 = Node->getOperand(2);
Evan Cheng71343822008-10-15 02:05:31 +00003023 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003024
3025 // If we had to Expand the SetCC operands into a SELECT node, then it may
3026 // not always be possible to return a true LHS & RHS. In this case, just
3027 // return the value we legalized, returned in the LHS
Gabor Greif1c80d112008-08-28 21:40:38 +00003028 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003029 Result = Tmp1;
3030 break;
3031 }
3032
3033 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
3034 default: assert(0 && "Cannot handle this action for SETCC yet!");
3035 case TargetLowering::Custom:
3036 isCustom = true;
3037 // FALLTHROUGH.
3038 case TargetLowering::Legal:
3039 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3040 if (isCustom) {
3041 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003042 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003043 }
3044 break;
3045 case TargetLowering::Promote: {
3046 // First step, figure out the appropriate operation to use.
3047 // Allow SETCC to not be supported for all legal data types
3048 // Mostly this targets FP
Duncan Sands92c43912008-06-06 12:08:01 +00003049 MVT NewInTy = Node->getOperand(0).getValueType();
3050 MVT OldVT = NewInTy; OldVT = OldVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003051
3052 // Scan for the appropriate larger type to use.
3053 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00003054 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003055
Duncan Sands92c43912008-06-06 12:08:01 +00003056 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003057 "Fell off of the edge of the integer world");
Duncan Sands92c43912008-06-06 12:08:01 +00003058 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003059 "Fell off of the edge of the floating point world");
3060
3061 // If the target supports SETCC of this type, use it.
3062 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
3063 break;
3064 }
Duncan Sands92c43912008-06-06 12:08:01 +00003065 if (NewInTy.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003066 assert(0 && "Cannot promote Legal Integer SETCC yet");
3067 else {
3068 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
3069 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
3070 }
3071 Tmp1 = LegalizeOp(Tmp1);
3072 Tmp2 = LegalizeOp(Tmp2);
3073 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3074 Result = LegalizeOp(Result);
3075 break;
3076 }
3077 case TargetLowering::Expand:
3078 // Expand a setcc node into a select_cc of the same condition, lhs, and
3079 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands92c43912008-06-06 12:08:01 +00003080 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003081 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3082 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3083 Tmp3);
3084 break;
3085 }
3086 break;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003087 case ISD::VSETCC: {
3088 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3089 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman8181bd12008-07-27 21:46:04 +00003090 SDValue CC = Node->getOperand(2);
Nate Begeman9a1ce152008-05-12 19:40:03 +00003091
3092 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3093
3094 // Everything is legal, see if we should expand this op or something.
3095 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3096 default: assert(0 && "This action is not supported yet!");
3097 case TargetLowering::Legal: break;
3098 case TargetLowering::Custom:
3099 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003100 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003101 break;
Mon P Wangec428ad2008-12-13 08:15:14 +00003102 case TargetLowering::Expand: {
3103 // Unroll into a nasty set of scalar code for now.
3104 MVT VT = Node->getValueType(0);
3105 unsigned NumElems = VT.getVectorNumElements();
3106 MVT EltVT = VT.getVectorElementType();
3107 MVT TmpEltVT = Tmp1.getValueType().getVectorElementType();
3108 SmallVector<SDValue, 8> Ops(NumElems);
3109 for (unsigned i = 0; i < NumElems; ++i) {
3110 SDValue In1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3111 Tmp1, DAG.getIntPtrConstant(i));
Duncan Sands4a361272009-01-01 15:52:00 +00003112 Ops[i] = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(TmpEltVT), In1,
Mon P Wang77bc9cd2008-12-17 08:49:47 +00003113 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3114 Tmp2, DAG.getIntPtrConstant(i)),
3115 CC);
3116 Ops[i] = DAG.getNode(ISD::SELECT, EltVT, Ops[i],
3117 DAG.getConstant(EltVT.getIntegerVTBitMask(),EltVT),
3118 DAG.getConstant(0, EltVT));
Mon P Wangec428ad2008-12-13 08:15:14 +00003119 }
3120 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], NumElems);
3121 break;
3122 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00003123 }
3124 break;
3125 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003126
3127 case ISD::SHL_PARTS:
3128 case ISD::SRA_PARTS:
3129 case ISD::SRL_PARTS: {
Dan Gohman8181bd12008-07-27 21:46:04 +00003130 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003131 bool Changed = false;
3132 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3133 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3134 Changed |= Ops.back() != Node->getOperand(i);
3135 }
3136 if (Changed)
3137 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
3138
3139 switch (TLI.getOperationAction(Node->getOpcode(),
3140 Node->getValueType(0))) {
3141 default: assert(0 && "This action is not supported yet!");
3142 case TargetLowering::Legal: break;
3143 case TargetLowering::Custom:
3144 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003145 if (Tmp1.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003146 SDValue Tmp2, RetVal(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003147 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
3148 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman8181bd12008-07-27 21:46:04 +00003149 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00003150 if (i == Op.getResNo())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003151 RetVal = Tmp2;
3152 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003153 assert(RetVal.getNode() && "Illegal result number");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003154 return RetVal;
3155 }
3156 break;
3157 }
3158
3159 // Since these produce multiple values, make sure to remember that we
3160 // legalized all of them.
3161 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman8181bd12008-07-27 21:46:04 +00003162 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00003163 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003164 }
3165
3166 // Binary operators
3167 case ISD::ADD:
3168 case ISD::SUB:
3169 case ISD::MUL:
3170 case ISD::MULHS:
3171 case ISD::MULHU:
3172 case ISD::UDIV:
3173 case ISD::SDIV:
3174 case ISD::AND:
3175 case ISD::OR:
3176 case ISD::XOR:
3177 case ISD::SHL:
3178 case ISD::SRL:
3179 case ISD::SRA:
3180 case ISD::FADD:
3181 case ISD::FSUB:
3182 case ISD::FMUL:
3183 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00003184 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003185 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3186 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3187 case Expand: assert(0 && "Not possible");
3188 case Legal:
3189 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3190 break;
3191 case Promote:
3192 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3193 break;
3194 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003195
3196 if ((Node->getOpcode() == ISD::SHL ||
3197 Node->getOpcode() == ISD::SRL ||
3198 Node->getOpcode() == ISD::SRA) &&
3199 !Node->getValueType(0).isVector()) {
Mon P Wang9901e732008-12-09 05:46:39 +00003200 Tmp2 = LegalizeShiftAmount(Tmp2);
Mon P Wangec428ad2008-12-13 08:15:14 +00003201 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003202
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003203 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangbff5d9c2008-11-10 04:46:22 +00003204
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003205 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3206 default: assert(0 && "BinOp legalize operation not supported");
3207 case TargetLowering::Legal: break;
3208 case TargetLowering::Custom:
3209 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003210 if (Tmp1.getNode()) {
Nate Begemanbb1ce942008-07-29 15:49:41 +00003211 Result = Tmp1;
3212 break;
Nate Begeman7569e762008-07-29 19:07:27 +00003213 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003214 // Fall through if the custom lower can't deal with the operation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003215 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00003216 MVT VT = Op.getValueType();
Mon P Wang1448aad2008-10-30 08:01:45 +00003217
Dan Gohman5a199552007-10-08 18:33:35 +00003218 // See if multiply or divide can be lowered using two-result operations.
3219 SDVTList VTs = DAG.getVTList(VT, VT);
3220 if (Node->getOpcode() == ISD::MUL) {
3221 // We just need the low half of the multiply; try both the signed
3222 // and unsigned forms. If the target supports both SMUL_LOHI and
3223 // UMUL_LOHI, form a preference by checking which forms of plain
3224 // MULH it supports.
3225 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3226 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3227 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3228 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3229 unsigned OpToUse = 0;
3230 if (HasSMUL_LOHI && !HasMULHS) {
3231 OpToUse = ISD::SMUL_LOHI;
3232 } else if (HasUMUL_LOHI && !HasMULHU) {
3233 OpToUse = ISD::UMUL_LOHI;
3234 } else if (HasSMUL_LOHI) {
3235 OpToUse = ISD::SMUL_LOHI;
3236 } else if (HasUMUL_LOHI) {
3237 OpToUse = ISD::UMUL_LOHI;
3238 }
3239 if (OpToUse) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003240 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003241 break;
3242 }
3243 }
3244 if (Node->getOpcode() == ISD::MULHS &&
3245 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003246 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3247 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003248 break;
3249 }
3250 if (Node->getOpcode() == ISD::MULHU &&
3251 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003252 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3253 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003254 break;
3255 }
3256 if (Node->getOpcode() == ISD::SDIV &&
3257 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003258 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3259 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003260 break;
3261 }
3262 if (Node->getOpcode() == ISD::UDIV &&
3263 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003264 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3265 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003266 break;
3267 }
Mon P Wang26342922008-12-18 20:03:17 +00003268
Dan Gohman6d05cac2007-10-11 23:57:53 +00003269 // Check to see if we have a libcall for this operator.
3270 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3271 bool isSigned = false;
3272 switch (Node->getOpcode()) {
3273 case ISD::UDIV:
3274 case ISD::SDIV:
3275 if (VT == MVT::i32) {
3276 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang1448aad2008-10-30 08:01:45 +00003277 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003278 isSigned = Node->getOpcode() == ISD::SDIV;
3279 }
3280 break;
Chris Lattner48188652008-10-04 21:27:46 +00003281 case ISD::MUL:
3282 if (VT == MVT::i32)
3283 LC = RTLIB::MUL_I32;
Scott Michel81215042008-12-29 03:21:37 +00003284 else if (VT == MVT::i64)
3285 LC = RTLIB::MUL_I64;
Chris Lattner48188652008-10-04 21:27:46 +00003286 break;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003287 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00003288 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3289 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003290 break;
3291 default: break;
3292 }
3293 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003294 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003295 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 break;
3297 }
Mon P Wang1448aad2008-10-30 08:01:45 +00003298
Duncan Sands92c43912008-06-06 12:08:01 +00003299 assert(Node->getValueType(0).isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003300 "Cannot expand this binary operator!");
3301 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003302 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003303 break;
3304 }
3305 case TargetLowering::Promote: {
3306 switch (Node->getOpcode()) {
3307 default: assert(0 && "Do not know how to promote this BinOp!");
3308 case ISD::AND:
3309 case ISD::OR:
3310 case ISD::XOR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003311 MVT OVT = Node->getValueType(0);
3312 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3313 assert(OVT.isVector() && "Cannot promote this BinOp!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003314 // Bit convert each of the values to the new type.
3315 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3316 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3317 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3318 // Bit convert the result back the original type.
3319 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3320 break;
3321 }
3322 }
3323 }
3324 }
3325 break;
3326
Dan Gohman475cd732007-10-05 14:17:22 +00003327 case ISD::SMUL_LOHI:
3328 case ISD::UMUL_LOHI:
3329 case ISD::SDIVREM:
3330 case ISD::UDIVREM:
3331 // These nodes will only be produced by target-specific lowering, so
3332 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003333 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003334 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003335
3336 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3337 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3338 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003339 break;
3340
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003341 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3342 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3343 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3344 case Expand: assert(0 && "Not possible");
3345 case Legal:
3346 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3347 break;
3348 case Promote:
3349 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3350 break;
3351 }
3352
3353 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3354
3355 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3356 default: assert(0 && "Operation not supported");
3357 case TargetLowering::Custom:
3358 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003359 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003360 break;
3361 case TargetLowering::Legal: break;
3362 case TargetLowering::Expand: {
3363 // If this target supports fabs/fneg natively and select is cheap,
3364 // do this efficiently.
3365 if (!TLI.isSelectExpensive() &&
3366 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3367 TargetLowering::Legal &&
3368 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3369 TargetLowering::Legal) {
3370 // Get the sign bit of the RHS.
Duncan Sands92c43912008-06-06 12:08:01 +00003371 MVT IVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003372 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman8181bd12008-07-27 21:46:04 +00003373 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Duncan Sands4a361272009-01-01 15:52:00 +00003374 SignBit = DAG.getSetCC(TLI.getSetCCResultType(IVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003375 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3376 // Get the absolute value of the result.
Dan Gohman8181bd12008-07-27 21:46:04 +00003377 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003378 // Select between the nabs and abs value based on the sign bit of
3379 // the input.
3380 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3381 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3382 AbsVal),
3383 AbsVal);
3384 Result = LegalizeOp(Result);
3385 break;
3386 }
3387
3388 // Otherwise, do bitwise ops!
Duncan Sands92c43912008-06-06 12:08:01 +00003389 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003390 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3391 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3392 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3393 Result = LegalizeOp(Result);
3394 break;
3395 }
3396 }
3397 break;
3398
3399 case ISD::ADDC:
3400 case ISD::SUBC:
3401 Tmp1 = LegalizeOp(Node->getOperand(0));
3402 Tmp2 = LegalizeOp(Node->getOperand(1));
3403 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003404 Tmp3 = Result.getValue(0);
3405 Tmp4 = Result.getValue(1);
3406
3407 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3408 default: assert(0 && "This action is not supported yet!");
3409 case TargetLowering::Legal:
3410 break;
3411 case TargetLowering::Custom:
3412 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3413 if (Tmp1.getNode() != NULL) {
Sanjiv Guptad57f2e12008-11-27 05:58:04 +00003414 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003415 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3416 }
3417 break;
3418 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003419 // Since this produces two values, make sure to remember that we legalized
3420 // both of them.
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003421 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3422 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3423 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003424
3425 case ISD::ADDE:
3426 case ISD::SUBE:
3427 Tmp1 = LegalizeOp(Node->getOperand(0));
3428 Tmp2 = LegalizeOp(Node->getOperand(1));
3429 Tmp3 = LegalizeOp(Node->getOperand(2));
3430 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003431 Tmp3 = Result.getValue(0);
3432 Tmp4 = Result.getValue(1);
3433
3434 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3435 default: assert(0 && "This action is not supported yet!");
3436 case TargetLowering::Legal:
3437 break;
3438 case TargetLowering::Custom:
3439 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3440 if (Tmp1.getNode() != NULL) {
Sanjiv Guptad57f2e12008-11-27 05:58:04 +00003441 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003442 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3443 }
3444 break;
3445 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003446 // Since this produces two values, make sure to remember that we legalized
3447 // both of them.
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003448 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3449 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3450 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003451
3452 case ISD::BUILD_PAIR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003453 MVT PairTy = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003454 // TODO: handle the case where the Lo and Hi operands are not of legal type
3455 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3456 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3457 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3458 case TargetLowering::Promote:
3459 case TargetLowering::Custom:
3460 assert(0 && "Cannot promote/custom this yet!");
3461 case TargetLowering::Legal:
3462 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3463 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3464 break;
3465 case TargetLowering::Expand:
3466 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3467 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3468 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003469 DAG.getConstant(PairTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003470 TLI.getShiftAmountTy()));
3471 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3472 break;
3473 }
3474 break;
3475 }
3476
3477 case ISD::UREM:
3478 case ISD::SREM:
3479 case ISD::FREM:
3480 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3481 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3482
3483 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3484 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3485 case TargetLowering::Custom:
3486 isCustom = true;
3487 // FALLTHROUGH
3488 case TargetLowering::Legal:
3489 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3490 if (isCustom) {
3491 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003492 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003493 }
3494 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003495 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003496 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3497 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands92c43912008-06-06 12:08:01 +00003498 MVT VT = Node->getValueType(0);
Dan Gohman5a199552007-10-08 18:33:35 +00003499
3500 // See if remainder can be lowered using two-result operations.
3501 SDVTList VTs = DAG.getVTList(VT, VT);
3502 if (Node->getOpcode() == ISD::SREM &&
3503 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003504 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003505 break;
3506 }
3507 if (Node->getOpcode() == ISD::UREM &&
3508 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003509 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003510 break;
3511 }
3512
Duncan Sands92c43912008-06-06 12:08:01 +00003513 if (VT.isInteger()) {
Dan Gohman5a199552007-10-08 18:33:35 +00003514 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003515 TargetLowering::Legal) {
3516 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003517 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3518 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3519 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands92c43912008-06-06 12:08:01 +00003520 } else if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003521 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003522 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003523 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003524 "Cannot expand this binary operator!");
3525 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3526 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman8181bd12008-07-27 21:46:04 +00003527 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003528 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003529 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003530 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00003531 assert(VT.isFloatingPoint() &&
Dan Gohman59b4b102007-11-06 22:11:54 +00003532 "remainder op must have integer or floating-point type");
Duncan Sands92c43912008-06-06 12:08:01 +00003533 if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003534 Result = LegalizeOp(UnrollVectorOp(Op));
3535 } else {
3536 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003537 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3538 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003539 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003540 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003541 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003542 }
3543 break;
3544 }
Dan Gohman5a199552007-10-08 18:33:35 +00003545 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003546 break;
3547 case ISD::VAARG: {
3548 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3549 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3550
Duncan Sands92c43912008-06-06 12:08:01 +00003551 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003552 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3553 default: assert(0 && "This action is not supported yet!");
3554 case TargetLowering::Custom:
3555 isCustom = true;
3556 // FALLTHROUGH
3557 case TargetLowering::Legal:
3558 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3559 Result = Result.getValue(0);
3560 Tmp1 = Result.getValue(1);
3561
3562 if (isCustom) {
3563 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003564 if (Tmp2.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003565 Result = LegalizeOp(Tmp2);
3566 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3567 }
3568 }
3569 break;
3570 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003571 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003572 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003573 // Increment the pointer, VAList, to the next vaarg
Duncan Sands55a4c232008-11-03 11:51:11 +00003574 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sandsd68f13b2009-01-12 20:38:59 +00003575 DAG.getConstant(TLI.getTargetData()->
3576 getTypePaddedSize(VT.getTypeForMVT()),
3577 TLI.getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003578 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00003579 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003580 // Load the actual argument out of the pointer VAList
3581 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3582 Tmp1 = LegalizeOp(Result.getValue(1));
3583 Result = LegalizeOp(Result);
3584 break;
3585 }
3586 }
3587 // Since VAARG produces two values, make sure to remember that we
3588 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003589 AddLegalizedOperand(SDValue(Node, 0), Result);
3590 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00003591 return Op.getResNo() ? Tmp1 : Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003592 }
3593
3594 case ISD::VACOPY:
3595 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3596 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3597 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3598
3599 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3600 default: assert(0 && "This action is not supported yet!");
3601 case TargetLowering::Custom:
3602 isCustom = true;
3603 // FALLTHROUGH
3604 case TargetLowering::Legal:
3605 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3606 Node->getOperand(3), Node->getOperand(4));
3607 if (isCustom) {
3608 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003609 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003610 }
3611 break;
3612 case TargetLowering::Expand:
3613 // This defaults to loading a pointer from the input and storing it to the
3614 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003615 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3616 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman6b9a08e2008-04-17 02:09:26 +00003617 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3618 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003619 break;
3620 }
3621 break;
3622
3623 case ISD::VAEND:
3624 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3625 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3626
3627 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3628 default: assert(0 && "This action is not supported yet!");
3629 case TargetLowering::Custom:
3630 isCustom = true;
3631 // FALLTHROUGH
3632 case TargetLowering::Legal:
3633 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3634 if (isCustom) {
3635 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003636 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003637 }
3638 break;
3639 case TargetLowering::Expand:
3640 Result = Tmp1; // Default to a no-op, return the chain
3641 break;
3642 }
3643 break;
3644
3645 case ISD::VASTART:
3646 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3647 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3648
3649 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3650
3651 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3652 default: assert(0 && "This action is not supported yet!");
3653 case TargetLowering::Legal: break;
3654 case TargetLowering::Custom:
3655 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003656 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003657 break;
3658 }
3659 break;
3660
3661 case ISD::ROTL:
3662 case ISD::ROTR:
3663 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3664 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3665 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3666 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3667 default:
3668 assert(0 && "ROTL/ROTR legalize operation not supported");
3669 break;
3670 case TargetLowering::Legal:
3671 break;
3672 case TargetLowering::Custom:
3673 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003674 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003675 break;
3676 case TargetLowering::Promote:
3677 assert(0 && "Do not know how to promote ROTL/ROTR");
3678 break;
3679 case TargetLowering::Expand:
3680 assert(0 && "Do not know how to expand ROTL/ROTR");
3681 break;
3682 }
3683 break;
3684
3685 case ISD::BSWAP:
3686 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3687 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3688 case TargetLowering::Custom:
3689 assert(0 && "Cannot custom legalize this yet!");
3690 case TargetLowering::Legal:
3691 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3692 break;
3693 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003694 MVT OVT = Tmp1.getValueType();
3695 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3696 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003697
3698 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3699 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3700 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3701 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3702 break;
3703 }
3704 case TargetLowering::Expand:
3705 Result = ExpandBSWAP(Tmp1);
3706 break;
3707 }
3708 break;
3709
3710 case ISD::CTPOP:
3711 case ISD::CTTZ:
3712 case ISD::CTLZ:
3713 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3714 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003715 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003716 case TargetLowering::Legal:
3717 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003718 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003719 TargetLowering::Custom) {
3720 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003721 if (Tmp1.getNode()) {
Scott Michelbc62b412007-08-02 02:22:46 +00003722 Result = Tmp1;
3723 }
Scott Michel48b63e62007-07-30 21:00:31 +00003724 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003725 break;
3726 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003727 MVT OVT = Tmp1.getValueType();
3728 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003729
3730 // Zero extend the argument.
3731 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3732 // Perform the larger operation, then subtract if needed.
3733 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3734 switch (Node->getOpcode()) {
3735 case ISD::CTPOP:
3736 Result = Tmp1;
3737 break;
3738 case ISD::CTTZ:
3739 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Duncan Sands4a361272009-01-01 15:52:00 +00003740 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003741 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003742 ISD::SETEQ);
3743 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003744 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003745 break;
3746 case ISD::CTLZ:
3747 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3748 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003749 DAG.getConstant(NVT.getSizeInBits() -
3750 OVT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003751 break;
3752 }
3753 break;
3754 }
3755 case TargetLowering::Expand:
3756 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3757 break;
3758 }
3759 break;
3760
3761 // Unary operators
3762 case ISD::FABS:
3763 case ISD::FNEG:
3764 case ISD::FSQRT:
3765 case ISD::FSIN:
3766 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003767 case ISD::FLOG:
3768 case ISD::FLOG2:
3769 case ISD::FLOG10:
3770 case ISD::FEXP:
3771 case ISD::FEXP2:
Dan Gohmanc8b20e22008-08-21 17:55:02 +00003772 case ISD::FTRUNC:
3773 case ISD::FFLOOR:
3774 case ISD::FCEIL:
3775 case ISD::FRINT:
3776 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003777 Tmp1 = LegalizeOp(Node->getOperand(0));
3778 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3779 case TargetLowering::Promote:
3780 case TargetLowering::Custom:
3781 isCustom = true;
3782 // FALLTHROUGH
3783 case TargetLowering::Legal:
3784 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3785 if (isCustom) {
3786 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003787 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003788 }
3789 break;
3790 case TargetLowering::Expand:
3791 switch (Node->getOpcode()) {
3792 default: assert(0 && "Unreachable!");
3793 case ISD::FNEG:
3794 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3795 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3796 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3797 break;
3798 case ISD::FABS: {
3799 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands92c43912008-06-06 12:08:01 +00003800 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003801 Tmp2 = DAG.getConstantFP(0.0, VT);
Duncan Sands4a361272009-01-01 15:52:00 +00003802 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
3803 Tmp1, Tmp2, ISD::SETUGT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003804 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3805 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3806 break;
3807 }
Evan Cheng1fac6952008-09-09 23:35:53 +00003808 case ISD::FSQRT:
3809 case ISD::FSIN:
3810 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003811 case ISD::FLOG:
3812 case ISD::FLOG2:
3813 case ISD::FLOG10:
3814 case ISD::FEXP:
3815 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00003816 case ISD::FTRUNC:
3817 case ISD::FFLOOR:
3818 case ISD::FCEIL:
3819 case ISD::FRINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00003820 case ISD::FNEARBYINT: {
Duncan Sands92c43912008-06-06 12:08:01 +00003821 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003822
3823 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003824 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003825 Result = LegalizeOp(UnrollVectorOp(Op));
3826 break;
3827 }
3828
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003829 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3830 switch(Node->getOpcode()) {
3831 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003832 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3833 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003834 break;
3835 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003836 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3837 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003838 break;
3839 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003840 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3841 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003842 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00003843 case ISD::FLOG:
3844 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3845 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3846 break;
3847 case ISD::FLOG2:
3848 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3849 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3850 break;
3851 case ISD::FLOG10:
3852 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3853 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3854 break;
3855 case ISD::FEXP:
3856 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3857 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3858 break;
3859 case ISD::FEXP2:
3860 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3861 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3862 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00003863 case ISD::FTRUNC:
3864 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3865 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3866 break;
3867 case ISD::FFLOOR:
3868 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3869 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3870 break;
3871 case ISD::FCEIL:
3872 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3873 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3874 break;
3875 case ISD::FRINT:
3876 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3877 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3878 break;
3879 case ISD::FNEARBYINT:
3880 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3881 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3882 break;
Evan Cheng1fac6952008-09-09 23:35:53 +00003883 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003884 default: assert(0 && "Unreachable!");
3885 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003886 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003887 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003888 break;
3889 }
3890 }
3891 break;
3892 }
3893 break;
3894 case ISD::FPOWI: {
Duncan Sands92c43912008-06-06 12:08:01 +00003895 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003896
3897 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003898 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003899 Result = LegalizeOp(UnrollVectorOp(Op));
3900 break;
3901 }
3902
3903 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003904 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3905 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003906 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003907 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003908 break;
3909 }
3910 case ISD::BIT_CONVERT:
3911 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003912 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3913 Node->getValueType(0));
Duncan Sands92c43912008-06-06 12:08:01 +00003914 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003915 // The input has to be a vector type, we have to either scalarize it, pack
3916 // it, or convert it based on whether the input vector type is legal.
Gabor Greif1c80d112008-08-28 21:40:38 +00003917 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00003918 int InIx = Node->getOperand(0).getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00003919 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3920 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003921
3922 // Figure out if there is a simple type corresponding to this Vector
3923 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00003924 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003925 if (TLI.isTypeLegal(TVT)) {
3926 // Turn this into a bit convert of the vector input.
3927 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3928 LegalizeOp(Node->getOperand(0)));
3929 break;
3930 } else if (NumElems == 1) {
3931 // Turn this into a bit convert of the scalar input.
3932 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3933 ScalarizeVectorOp(Node->getOperand(0)));
3934 break;
3935 } else {
3936 // FIXME: UNIMP! Store then reload
3937 assert(0 && "Cast from unsupported vector type not implemented yet!");
3938 }
3939 } else {
3940 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3941 Node->getOperand(0).getValueType())) {
3942 default: assert(0 && "Unknown operation action!");
3943 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003944 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3945 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003946 break;
3947 case TargetLowering::Legal:
3948 Tmp1 = LegalizeOp(Node->getOperand(0));
3949 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3950 break;
3951 }
3952 }
3953 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003954 case ISD::CONVERT_RNDSAT: {
3955 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3956 switch (CvtCode) {
3957 default: assert(0 && "Unknown cvt code!");
3958 case ISD::CVT_SF:
3959 case ISD::CVT_UF:
Mon P Wang73d31542008-11-10 20:54:11 +00003960 case ISD::CVT_FF:
Mon P Wang2fc3f9e2008-12-09 07:27:39 +00003961 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003962 case ISD::CVT_FS:
3963 case ISD::CVT_FU:
3964 case ISD::CVT_SS:
3965 case ISD::CVT_SU:
3966 case ISD::CVT_US:
3967 case ISD::CVT_UU: {
3968 SDValue DTyOp = Node->getOperand(1);
3969 SDValue STyOp = Node->getOperand(2);
3970 SDValue RndOp = Node->getOperand(3);
3971 SDValue SatOp = Node->getOperand(4);
3972 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3973 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3974 case Legal:
3975 Tmp1 = LegalizeOp(Node->getOperand(0));
3976 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3977 RndOp, SatOp);
3978 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3979 TargetLowering::Custom) {
3980 Tmp1 = TLI.LowerOperation(Result, DAG);
3981 if (Tmp1.getNode()) Result = Tmp1;
3982 }
3983 break;
3984 case Promote:
3985 Result = PromoteOp(Node->getOperand(0));
3986 // For FP, make Op1 a i32
3987
Mon P Wang2fc3f9e2008-12-09 07:27:39 +00003988 Result = DAG.getConvertRndSat(Op.getValueType(), Result,
Mon P Wang73d31542008-11-10 20:54:11 +00003989 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3990 break;
3991 }
3992 break;
3993 }
3994 } // end switch CvtCode
3995 break;
3996 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003997 // Conversion operators. The source and destination have different types.
3998 case ISD::SINT_TO_FP:
3999 case ISD::UINT_TO_FP: {
4000 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman29c3cef2008-08-14 20:04:46 +00004001 Result = LegalizeINT_TO_FP(Result, isSigned,
4002 Node->getValueType(0), Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004003 break;
4004 }
4005 case ISD::TRUNCATE:
4006 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4007 case Legal:
4008 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michele9b8a402008-12-02 19:55:08 +00004009 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
4010 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
4011 case TargetLowering::Custom:
Mon P Wang72fe5462008-12-11 00:44:22 +00004012 isCustom = true;
4013 // FALLTHROUGH
Scott Michele9b8a402008-12-02 19:55:08 +00004014 case TargetLowering::Legal:
Mon P Wang72fe5462008-12-11 00:44:22 +00004015 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4016 if (isCustom) {
4017 Tmp1 = TLI.LowerOperation(Result, DAG);
4018 if (Tmp1.getNode()) Result = Tmp1;
4019 }
4020 break;
Mon P Wang83edba52008-12-12 01:25:51 +00004021 case TargetLowering::Expand:
4022 assert(Result.getValueType().isVector() && "must be vector type");
4023 // Unroll the truncate. We should do better.
4024 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Schellerbfc55ee2008-12-02 12:12:25 +00004025 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004026 break;
4027 case Expand:
4028 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4029
4030 // Since the result is legal, we should just be able to truncate the low
4031 // part of the source.
4032 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
4033 break;
4034 case Promote:
4035 Result = PromoteOp(Node->getOperand(0));
4036 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
4037 break;
4038 }
4039 break;
4040
4041 case ISD::FP_TO_SINT:
4042 case ISD::FP_TO_UINT:
4043 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4044 case Legal:
4045 Tmp1 = LegalizeOp(Node->getOperand(0));
4046
4047 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
4048 default: assert(0 && "Unknown operation action!");
4049 case TargetLowering::Custom:
4050 isCustom = true;
4051 // FALLTHROUGH
4052 case TargetLowering::Legal:
4053 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4054 if (isCustom) {
4055 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004056 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004057 }
4058 break;
4059 case TargetLowering::Promote:
4060 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
4061 Node->getOpcode() == ISD::FP_TO_SINT);
4062 break;
4063 case TargetLowering::Expand:
4064 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004065 SDValue True, False;
Duncan Sands92c43912008-06-06 12:08:01 +00004066 MVT VT = Node->getOperand(0).getValueType();
4067 MVT NVT = Node->getValueType(0);
Dale Johannesen958b08b2007-09-19 23:55:34 +00004068 const uint64_t zero[] = {0, 0};
Duncan Sands92c43912008-06-06 12:08:01 +00004069 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
4070 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohman88ae8c52008-02-29 01:44:25 +00004071 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00004072 Tmp2 = DAG.getConstantFP(apf, VT);
Duncan Sands4a361272009-01-01 15:52:00 +00004073 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(VT), Node->getOperand(0),
4074 Tmp2, ISD::SETLT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004075 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
4076 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
4077 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
4078 Tmp2));
4079 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman88ae8c52008-02-29 01:44:25 +00004080 DAG.getConstant(x, NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004081 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
4082 break;
4083 } else {
4084 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
4085 }
4086 break;
4087 }
4088 break;
4089 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00004090 MVT VT = Op.getValueType();
4091 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00004092 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004093 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00004094 if (Node->getOpcode() == ISD::FP_TO_SINT) {
4095 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
4096 Node->getOperand(0), DAG.getValueType(MVT::f64));
4097 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
4098 DAG.getIntPtrConstant(1));
4099 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
4100 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00004101 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4102 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4103 Tmp2 = DAG.getConstantFP(apf, OVT);
4104 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4105 // FIXME: generated code sucks.
4106 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
4107 DAG.getNode(ISD::ADD, MVT::i32,
4108 DAG.getNode(ISD::FP_TO_SINT, VT,
4109 DAG.getNode(ISD::FSUB, OVT,
4110 Node->getOperand(0), Tmp2)),
4111 DAG.getConstant(0x80000000, MVT::i32)),
4112 DAG.getNode(ISD::FP_TO_SINT, VT,
4113 Node->getOperand(0)),
4114 DAG.getCondCode(ISD::SETGE));
4115 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004116 break;
4117 }
Dan Gohmanec51f642008-03-10 23:03:31 +00004118 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsf68dffb2008-07-17 02:36:29 +00004119 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4120 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4121 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman8181bd12008-07-27 21:46:04 +00004122 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00004123 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004124 break;
4125 }
4126 case Promote:
4127 Tmp1 = PromoteOp(Node->getOperand(0));
4128 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4129 Result = LegalizeOp(Result);
4130 break;
4131 }
4132 break;
4133
Chris Lattner56ecde32008-01-16 06:57:07 +00004134 case ISD::FP_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004135 MVT DstVT = Op.getValueType();
4136 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004137 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4138 // The only other way we can lower this is to turn it into a STORE,
4139 // LOAD pair, targetting a temporary location (a stack slot).
4140 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
4141 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00004142 }
4143 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4144 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4145 case Legal:
4146 Tmp1 = LegalizeOp(Node->getOperand(0));
4147 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4148 break;
4149 case Promote:
4150 Tmp1 = PromoteOp(Node->getOperand(0));
4151 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4152 break;
4153 }
4154 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004155 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004156 case ISD::FP_ROUND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004157 MVT DstVT = Op.getValueType();
4158 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004159 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4160 if (SrcVT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004161 SDValue Lo;
Dale Johannesena0d36082008-01-20 01:18:38 +00004162 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00004163 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00004164 if (DstVT!=MVT::f64)
4165 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00004166 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004167 }
Chris Lattner5872a362008-01-17 07:00:52 +00004168 // The only other way we can lower this is to turn it into a STORE,
4169 // LOAD pair, targetting a temporary location (a stack slot).
4170 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4171 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004172 }
Chris Lattner56ecde32008-01-16 06:57:07 +00004173 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4174 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4175 case Legal:
4176 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004177 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004178 break;
4179 case Promote:
4180 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004181 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4182 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004183 break;
4184 }
4185 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004186 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004187 case ISD::ANY_EXTEND:
4188 case ISD::ZERO_EXTEND:
4189 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004190 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4191 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4192 case Legal:
4193 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac54d002008-04-30 00:26:38 +00004194 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michelac7091c2008-02-15 23:05:48 +00004195 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4196 TargetLowering::Custom) {
Scott Michelac54d002008-04-30 00:26:38 +00004197 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004198 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelac7091c2008-02-15 23:05:48 +00004199 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004200 break;
4201 case Promote:
4202 switch (Node->getOpcode()) {
4203 case ISD::ANY_EXTEND:
4204 Tmp1 = PromoteOp(Node->getOperand(0));
4205 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
4206 break;
4207 case ISD::ZERO_EXTEND:
4208 Result = PromoteOp(Node->getOperand(0));
4209 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4210 Result = DAG.getZeroExtendInReg(Result,
4211 Node->getOperand(0).getValueType());
4212 break;
4213 case ISD::SIGN_EXTEND:
4214 Result = PromoteOp(Node->getOperand(0));
4215 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4216 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4217 Result,
4218 DAG.getValueType(Node->getOperand(0).getValueType()));
4219 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004220 }
4221 }
4222 break;
4223 case ISD::FP_ROUND_INREG:
4224 case ISD::SIGN_EXTEND_INREG: {
4225 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004226 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004227
4228 // If this operation is not supported, convert it to a shl/shr or load/store
4229 // pair.
4230 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4231 default: assert(0 && "This action not supported for this op yet!");
4232 case TargetLowering::Legal:
4233 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
4234 break;
4235 case TargetLowering::Expand:
4236 // If this is an integer extend and shifts are supported, do that.
4237 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
4238 // NOTE: we could fall back on load/store here too for targets without
4239 // SAR. However, it is doubtful that any exist.
Duncan Sands92c43912008-06-06 12:08:01 +00004240 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4241 ExtraVT.getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00004242 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004243 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4244 Node->getOperand(0), ShiftCst);
4245 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4246 Result, ShiftCst);
4247 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
4248 // The only way we can lower this is to turn it into a TRUNCSTORE,
4249 // EXTLOAD pair, targetting a temporary location (a stack slot).
4250
4251 // NOTE: there is a choice here between constantly creating new stack
4252 // slots and always reusing the same one. We currently always create
4253 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00004254 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4255 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004256 } else {
4257 assert(0 && "Unknown op");
4258 }
4259 break;
4260 }
4261 break;
4262 }
Duncan Sands38947cd2007-07-27 12:58:54 +00004263 case ISD::TRAMPOLINE: {
Dan Gohman8181bd12008-07-27 21:46:04 +00004264 SDValue Ops[6];
Duncan Sands38947cd2007-07-27 12:58:54 +00004265 for (unsigned i = 0; i != 6; ++i)
4266 Ops[i] = LegalizeOp(Node->getOperand(i));
4267 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4268 // The only option for this node is to custom lower it.
4269 Result = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004270 assert(Result.getNode() && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00004271
4272 // Since trampoline produces two values, make sure to remember that we
4273 // legalized both of them.
4274 Tmp1 = LegalizeOp(Result.getValue(1));
4275 Result = LegalizeOp(Result);
Dan Gohman8181bd12008-07-27 21:46:04 +00004276 AddLegalizedOperand(SDValue(Node, 0), Result);
4277 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00004278 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00004279 }
Dan Gohmane8e4a412008-05-14 00:43:10 +00004280 case ISD::FLT_ROUNDS_: {
Duncan Sands92c43912008-06-06 12:08:01 +00004281 MVT VT = Node->getValueType(0);
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004282 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4283 default: assert(0 && "This action not supported for this op yet!");
4284 case TargetLowering::Custom:
4285 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004286 if (Result.getNode()) break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004287 // Fall Thru
4288 case TargetLowering::Legal:
4289 // If this operation is not supported, lower it to constant 1
4290 Result = DAG.getConstant(1, VT);
4291 break;
4292 }
Dan Gohmane09dc8c2008-05-12 16:07:15 +00004293 break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004294 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004295 case ISD::TRAP: {
Duncan Sands92c43912008-06-06 12:08:01 +00004296 MVT VT = Node->getValueType(0);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004297 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4298 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00004299 case TargetLowering::Legal:
4300 Tmp1 = LegalizeOp(Node->getOperand(0));
4301 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4302 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004303 case TargetLowering::Custom:
4304 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004305 if (Result.getNode()) break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004306 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00004307 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004308 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00004309 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004310 TargetLowering::ArgListTy Args;
Dan Gohman8181bd12008-07-27 21:46:04 +00004311 std::pair<SDValue,SDValue> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004312 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00004313 false, false, false, false, CallingConv::C, false,
Bill Wendlingfef06052008-09-16 21:48:12 +00004314 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner88e03932008-01-15 22:09:33 +00004315 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004316 Result = CallResult.second;
4317 break;
4318 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004319 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004320 }
Bill Wendling913dcf32008-11-22 00:22:52 +00004321
Bill Wendling7e04be62008-12-09 22:08:41 +00004322 case ISD::SADDO:
4323 case ISD::SSUBO: {
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004324 MVT VT = Node->getValueType(0);
4325 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4326 default: assert(0 && "This action not supported for this op yet!");
4327 case TargetLowering::Custom:
4328 Result = TLI.LowerOperation(Op, DAG);
4329 if (Result.getNode()) break;
4330 // FALLTHROUGH
4331 case TargetLowering::Legal: {
4332 SDValue LHS = LegalizeOp(Node->getOperand(0));
4333 SDValue RHS = LegalizeOp(Node->getOperand(1));
4334
Bill Wendling7e04be62008-12-09 22:08:41 +00004335 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
4336 ISD::ADD : ISD::SUB, LHS.getValueType(),
4337 LHS, RHS);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004338 MVT OType = Node->getValueType(1);
4339
Bill Wendlingc65e6e42008-11-25 08:19:22 +00004340 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004341
Bill Wendlingcf4de122008-11-25 19:40:17 +00004342 // LHSSign -> LHS >= 0
4343 // RHSSign -> RHS >= 0
4344 // SumSign -> Sum >= 0
4345 //
Bill Wendling7e04be62008-12-09 22:08:41 +00004346 // Add:
Bill Wendlingcf4de122008-11-25 19:40:17 +00004347 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling7e04be62008-12-09 22:08:41 +00004348 // Sub:
4349 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendlingcf4de122008-11-25 19:40:17 +00004350 //
4351 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4352 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
Bill Wendling7e04be62008-12-09 22:08:41 +00004353 SDValue SignsMatch = DAG.getSetCC(OType, LHSSign, RHSSign,
4354 Node->getOpcode() == ISD::SADDO ?
4355 ISD::SETEQ : ISD::SETNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004356
Bill Wendlingcf4de122008-11-25 19:40:17 +00004357 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4358 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004359
Bill Wendling7e04be62008-12-09 22:08:41 +00004360 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsMatch, SumSignNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004361
4362 MVT ValueVTs[] = { LHS.getValueType(), OType };
4363 SDValue Ops[] = { Sum, Cmp };
4364
Duncan Sands42d7bb82008-12-01 11:41:29 +00004365 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4366 &Ops[0], 2);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004367 SDNode *RNode = Result.getNode();
4368 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4369 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4370 break;
4371 }
4372 }
4373
4374 break;
4375 }
Bill Wendling7e04be62008-12-09 22:08:41 +00004376 case ISD::UADDO:
4377 case ISD::USUBO: {
Bill Wendling4c134df2008-11-24 19:21:46 +00004378 MVT VT = Node->getValueType(0);
4379 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4380 default: assert(0 && "This action not supported for this op yet!");
4381 case TargetLowering::Custom:
4382 Result = TLI.LowerOperation(Op, DAG);
4383 if (Result.getNode()) break;
4384 // FALLTHROUGH
4385 case TargetLowering::Legal: {
4386 SDValue LHS = LegalizeOp(Node->getOperand(0));
4387 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling913dcf32008-11-22 00:22:52 +00004388
Bill Wendling7e04be62008-12-09 22:08:41 +00004389 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
4390 ISD::ADD : ISD::SUB, LHS.getValueType(),
4391 LHS, RHS);
Bill Wendling4c134df2008-11-24 19:21:46 +00004392 MVT OType = Node->getValueType(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00004393 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS,
4394 Node->getOpcode () == ISD::UADDO ?
4395 ISD::SETULT : ISD::SETUGT);
Bill Wendling913dcf32008-11-22 00:22:52 +00004396
Bill Wendling4c134df2008-11-24 19:21:46 +00004397 MVT ValueVTs[] = { LHS.getValueType(), OType };
4398 SDValue Ops[] = { Sum, Cmp };
Bill Wendling913dcf32008-11-22 00:22:52 +00004399
Duncan Sands42d7bb82008-12-01 11:41:29 +00004400 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4401 &Ops[0], 2);
Bill Wendling4c134df2008-11-24 19:21:46 +00004402 SDNode *RNode = Result.getNode();
4403 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4404 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4405 break;
4406 }
4407 }
4408
Bill Wendling913dcf32008-11-22 00:22:52 +00004409 break;
4410 }
Bill Wendling7e04be62008-12-09 22:08:41 +00004411 case ISD::SMULO:
4412 case ISD::UMULO: {
4413 MVT VT = Node->getValueType(0);
4414 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4415 default: assert(0 && "This action is not supported at all!");
4416 case TargetLowering::Custom:
4417 Result = TLI.LowerOperation(Op, DAG);
4418 if (Result.getNode()) break;
4419 // Fall Thru
4420 case TargetLowering::Legal:
4421 // FIXME: According to Hacker's Delight, this can be implemented in
4422 // target independent lowering, but it would be inefficient, since it
Bill Wendling35f1a9d2008-12-10 02:01:32 +00004423 // requires a division + a branch.
Bill Wendling7e04be62008-12-09 22:08:41 +00004424 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4425 break;
4426 }
4427 break;
4428 }
4429
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004430 }
4431
4432 assert(Result.getValueType() == Op.getValueType() &&
4433 "Bad legalization!");
4434
4435 // Make sure that the generated code is itself legal.
4436 if (Result != Op)
4437 Result = LegalizeOp(Result);
4438
4439 // Note that LegalizeOp may be reentered even from single-use nodes, which
4440 // means that we always must cache transformed nodes.
4441 AddLegalizedOperand(Op, Result);
4442 return Result;
4443}
4444
4445/// PromoteOp - Given an operation that produces a value in an invalid type,
4446/// promote it to compute the value into a larger type. The produced value will
4447/// have the correct bits for the low portion of the register, but no guarantee
4448/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +00004449SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00004450 MVT VT = Op.getValueType();
4451 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004452 assert(getTypeAction(VT) == Promote &&
4453 "Caller should expand or legalize operands that are not promotable!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004454 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004455 "Cannot promote to smaller type!");
4456
Dan Gohman8181bd12008-07-27 21:46:04 +00004457 SDValue Tmp1, Tmp2, Tmp3;
4458 SDValue Result;
Gabor Greif1c80d112008-08-28 21:40:38 +00004459 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004460
Dan Gohman8181bd12008-07-27 21:46:04 +00004461 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004462 if (I != PromotedNodes.end()) return I->second;
4463
4464 switch (Node->getOpcode()) {
4465 case ISD::CopyFromReg:
4466 assert(0 && "CopyFromReg must be legal!");
4467 default:
4468#ifndef NDEBUG
4469 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4470#endif
4471 assert(0 && "Do not know how to promote this operator!");
4472 abort();
4473 case ISD::UNDEF:
4474 Result = DAG.getNode(ISD::UNDEF, NVT);
4475 break;
4476 case ISD::Constant:
4477 if (VT != MVT::i1)
4478 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4479 else
4480 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
4481 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4482 break;
4483 case ISD::ConstantFP:
4484 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4485 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4486 break;
4487
Duncan Sands4a361272009-01-01 15:52:00 +00004488 case ISD::SETCC: {
4489 MVT VT0 = Node->getOperand(0).getValueType();
4490 assert(isTypeLegal(TLI.getSetCCResultType(VT0))
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004491 && "SetCC type is not legal??");
Duncan Sands4a361272009-01-01 15:52:00 +00004492 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(VT0),
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004493 Node->getOperand(0), Node->getOperand(1),
4494 Node->getOperand(2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004495 break;
Duncan Sands4a361272009-01-01 15:52:00 +00004496 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004497 case ISD::TRUNCATE:
4498 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4499 case Legal:
4500 Result = LegalizeOp(Node->getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00004501 assert(Result.getValueType().bitsGE(NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004502 "This truncation doesn't make sense!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004503 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004504 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4505 break;
4506 case Promote:
4507 // The truncation is not required, because we don't guarantee anything
4508 // about high bits anyway.
4509 Result = PromoteOp(Node->getOperand(0));
4510 break;
4511 case Expand:
4512 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4513 // Truncate the low part of the expanded value to the result type
4514 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4515 }
4516 break;
4517 case ISD::SIGN_EXTEND:
4518 case ISD::ZERO_EXTEND:
4519 case ISD::ANY_EXTEND:
4520 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4521 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4522 case Legal:
4523 // Input is legal? Just do extend all the way to the larger type.
4524 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4525 break;
4526 case Promote:
4527 // Promote the reg if it's smaller.
4528 Result = PromoteOp(Node->getOperand(0));
4529 // The high bits are not guaranteed to be anything. Insert an extend.
4530 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4531 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4532 DAG.getValueType(Node->getOperand(0).getValueType()));
4533 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4534 Result = DAG.getZeroExtendInReg(Result,
4535 Node->getOperand(0).getValueType());
4536 break;
4537 }
4538 break;
Mon P Wang73d31542008-11-10 20:54:11 +00004539 case ISD::CONVERT_RNDSAT: {
4540 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4541 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4542 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4543 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4544 "can only promote integers");
4545 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4546 Node->getOperand(1), Node->getOperand(2),
4547 Node->getOperand(3), Node->getOperand(4),
4548 CvtCode);
4549 break;
4550
4551 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004552 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004553 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4554 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004555 Result = PromoteOp(Result);
4556 break;
4557
4558 case ISD::FP_EXTEND:
4559 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4560 case ISD::FP_ROUND:
4561 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4562 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4563 case Promote: assert(0 && "Unreachable with 2 FP types!");
4564 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004565 if (Node->getConstantOperandVal(1) == 0) {
4566 // Input is legal? Do an FP_ROUND_INREG.
4567 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4568 DAG.getValueType(VT));
4569 } else {
4570 // Just remove the truncate, it isn't affecting the value.
4571 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4572 Node->getOperand(1));
4573 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004574 break;
4575 }
4576 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004577 case ISD::SINT_TO_FP:
4578 case ISD::UINT_TO_FP:
4579 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4580 case Legal:
4581 // No extra round required here.
4582 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4583 break;
4584
4585 case Promote:
4586 Result = PromoteOp(Node->getOperand(0));
4587 if (Node->getOpcode() == ISD::SINT_TO_FP)
4588 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4589 Result,
4590 DAG.getValueType(Node->getOperand(0).getValueType()));
4591 else
4592 Result = DAG.getZeroExtendInReg(Result,
4593 Node->getOperand(0).getValueType());
4594 // No extra round required here.
4595 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4596 break;
4597 case Expand:
4598 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4599 Node->getOperand(0));
4600 // Round if we cannot tolerate excess precision.
4601 if (NoExcessFPPrecision)
4602 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4603 DAG.getValueType(VT));
4604 break;
4605 }
4606 break;
4607
4608 case ISD::SIGN_EXTEND_INREG:
4609 Result = PromoteOp(Node->getOperand(0));
4610 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4611 Node->getOperand(1));
4612 break;
4613 case ISD::FP_TO_SINT:
4614 case ISD::FP_TO_UINT:
4615 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4616 case Legal:
4617 case Expand:
4618 Tmp1 = Node->getOperand(0);
4619 break;
4620 case Promote:
4621 // The input result is prerounded, so we don't have to do anything
4622 // special.
4623 Tmp1 = PromoteOp(Node->getOperand(0));
4624 break;
4625 }
4626 // If we're promoting a UINT to a larger size, check to see if the new node
4627 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4628 // we can use that instead. This allows us to generate better code for
4629 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4630 // legal, such as PowerPC.
4631 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4632 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4633 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4634 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4635 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4636 } else {
4637 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4638 }
4639 break;
4640
4641 case ISD::FABS:
4642 case ISD::FNEG:
4643 Tmp1 = PromoteOp(Node->getOperand(0));
4644 assert(Tmp1.getValueType() == NVT);
4645 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4646 // NOTE: we do not have to do any extra rounding here for
4647 // NoExcessFPPrecision, because we know the input will have the appropriate
4648 // precision, and these operations don't modify precision at all.
4649 break;
4650
Dale Johannesen92b33082008-09-04 00:47:13 +00004651 case ISD::FLOG:
4652 case ISD::FLOG2:
4653 case ISD::FLOG10:
4654 case ISD::FEXP:
4655 case ISD::FEXP2:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004656 case ISD::FSQRT:
4657 case ISD::FSIN:
4658 case ISD::FCOS:
Dan Gohmanb2158232008-08-21 18:38:14 +00004659 case ISD::FTRUNC:
4660 case ISD::FFLOOR:
4661 case ISD::FCEIL:
4662 case ISD::FRINT:
4663 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004664 Tmp1 = PromoteOp(Node->getOperand(0));
4665 assert(Tmp1.getValueType() == NVT);
4666 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4667 if (NoExcessFPPrecision)
4668 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4669 DAG.getValueType(VT));
4670 break;
4671
Evan Cheng1fac6952008-09-09 23:35:53 +00004672 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004673 case ISD::FPOWI: {
Evan Cheng1fac6952008-09-09 23:35:53 +00004674 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004675 // directly as well, which may be better.
4676 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng1fac6952008-09-09 23:35:53 +00004677 Tmp2 = Node->getOperand(1);
4678 if (Node->getOpcode() == ISD::FPOW)
4679 Tmp2 = PromoteOp(Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004680 assert(Tmp1.getValueType() == NVT);
Evan Cheng1fac6952008-09-09 23:35:53 +00004681 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004682 if (NoExcessFPPrecision)
4683 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4684 DAG.getValueType(VT));
4685 break;
4686 }
4687
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004688 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004689 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004690 Tmp2 = PromoteOp(Node->getOperand(2));
4691 Tmp3 = PromoteOp(Node->getOperand(3));
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004692 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getMemoryVT(),
4693 AtomNode->getChain(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004694 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004695 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004696 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004697 // Remember that we legalized the chain.
4698 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4699 break;
4700 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004701 case ISD::ATOMIC_LOAD_ADD:
4702 case ISD::ATOMIC_LOAD_SUB:
4703 case ISD::ATOMIC_LOAD_AND:
4704 case ISD::ATOMIC_LOAD_OR:
4705 case ISD::ATOMIC_LOAD_XOR:
4706 case ISD::ATOMIC_LOAD_NAND:
4707 case ISD::ATOMIC_LOAD_MIN:
4708 case ISD::ATOMIC_LOAD_MAX:
4709 case ISD::ATOMIC_LOAD_UMIN:
4710 case ISD::ATOMIC_LOAD_UMAX:
4711 case ISD::ATOMIC_SWAP: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004712 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004713 Tmp2 = PromoteOp(Node->getOperand(2));
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004714 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getMemoryVT(),
4715 AtomNode->getChain(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004716 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004717 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004718 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004719 // Remember that we legalized the chain.
4720 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4721 break;
4722 }
4723
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004724 case ISD::AND:
4725 case ISD::OR:
4726 case ISD::XOR:
4727 case ISD::ADD:
4728 case ISD::SUB:
4729 case ISD::MUL:
4730 // The input may have strange things in the top bits of the registers, but
4731 // these operations don't care. They may have weird bits going out, but
4732 // that too is okay if they are integer operations.
4733 Tmp1 = PromoteOp(Node->getOperand(0));
4734 Tmp2 = PromoteOp(Node->getOperand(1));
4735 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4736 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4737 break;
4738 case ISD::FADD:
4739 case ISD::FSUB:
4740 case ISD::FMUL:
4741 Tmp1 = PromoteOp(Node->getOperand(0));
4742 Tmp2 = PromoteOp(Node->getOperand(1));
4743 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4744 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4745
4746 // Floating point operations will give excess precision that we may not be
4747 // able to tolerate. If we DO allow excess precision, just leave it,
4748 // otherwise excise it.
4749 // FIXME: Why would we need to round FP ops more than integer ones?
4750 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4751 if (NoExcessFPPrecision)
4752 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4753 DAG.getValueType(VT));
4754 break;
4755
4756 case ISD::SDIV:
4757 case ISD::SREM:
4758 // These operators require that their input be sign extended.
4759 Tmp1 = PromoteOp(Node->getOperand(0));
4760 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004761 if (NVT.isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004762 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4763 DAG.getValueType(VT));
4764 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4765 DAG.getValueType(VT));
4766 }
4767 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4768
4769 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands92c43912008-06-06 12:08:01 +00004770 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004771 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4772 DAG.getValueType(VT));
4773 break;
4774 case ISD::FDIV:
4775 case ISD::FREM:
4776 case ISD::FCOPYSIGN:
4777 // These operators require that their input be fp extended.
4778 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004779 case Expand: assert(0 && "not implemented");
4780 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4781 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004782 }
4783 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004784 case Expand: assert(0 && "not implemented");
4785 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4786 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004787 }
4788 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4789
4790 // Perform FP_ROUND: this is probably overly pessimistic.
4791 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4792 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4793 DAG.getValueType(VT));
4794 break;
4795
4796 case ISD::UDIV:
4797 case ISD::UREM:
4798 // These operators require that their input be zero extended.
4799 Tmp1 = PromoteOp(Node->getOperand(0));
4800 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004801 assert(NVT.isInteger() && "Operators don't apply to FP!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004802 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4803 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4804 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4805 break;
4806
4807 case ISD::SHL:
4808 Tmp1 = PromoteOp(Node->getOperand(0));
4809 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4810 break;
4811 case ISD::SRA:
4812 // The input value must be properly sign extended.
4813 Tmp1 = PromoteOp(Node->getOperand(0));
4814 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4815 DAG.getValueType(VT));
4816 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4817 break;
4818 case ISD::SRL:
4819 // The input value must be properly zero extended.
4820 Tmp1 = PromoteOp(Node->getOperand(0));
4821 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4822 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4823 break;
4824
4825 case ISD::VAARG:
4826 Tmp1 = Node->getOperand(0); // Get the chain.
4827 Tmp2 = Node->getOperand(1); // Get the pointer.
4828 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4829 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sandsac496a12008-07-04 11:47:58 +00004830 Result = TLI.LowerOperation(Tmp3, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004831 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004832 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00004833 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004834 // Increment the pointer, VAList, to the next vaarg
4835 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands92c43912008-06-06 12:08:01 +00004836 DAG.getConstant(VT.getSizeInBits()/8,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004837 TLI.getPointerTy()));
4838 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00004839 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004840 // Load the actual argument out of the pointer VAList
4841 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4842 }
4843 // Remember that we legalized the chain.
4844 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4845 break;
4846
4847 case ISD::LOAD: {
4848 LoadSDNode *LD = cast<LoadSDNode>(Node);
4849 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4850 ? ISD::EXTLOAD : LD->getExtensionType();
4851 Result = DAG.getExtLoad(ExtType, NVT,
4852 LD->getChain(), LD->getBasePtr(),
4853 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004854 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004855 LD->isVolatile(),
4856 LD->getAlignment());
4857 // Remember that we legalized the chain.
4858 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4859 break;
4860 }
Scott Michel67224b22008-06-02 22:18:03 +00004861 case ISD::SELECT: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004862 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4863 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel67224b22008-06-02 22:18:03 +00004864
Duncan Sands92c43912008-06-06 12:08:01 +00004865 MVT VT2 = Tmp2.getValueType();
Scott Michel67224b22008-06-02 22:18:03 +00004866 assert(VT2 == Tmp3.getValueType()
Scott Michel7b54de02008-06-03 19:13:20 +00004867 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4868 // Ensure that the resulting node is at least the same size as the operands'
4869 // value types, because we cannot assume that TLI.getSetCCValueType() is
4870 // constant.
4871 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004872 break;
Scott Michel67224b22008-06-02 22:18:03 +00004873 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004874 case ISD::SELECT_CC:
4875 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4876 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4877 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4878 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4879 break;
4880 case ISD::BSWAP:
4881 Tmp1 = Node->getOperand(0);
4882 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4883 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4884 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004885 DAG.getConstant(NVT.getSizeInBits() -
4886 VT.getSizeInBits(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004887 TLI.getShiftAmountTy()));
4888 break;
4889 case ISD::CTPOP:
4890 case ISD::CTTZ:
4891 case ISD::CTLZ:
4892 // Zero extend the argument
4893 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4894 // Perform the larger operation, then subtract if needed.
4895 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4896 switch(Node->getOpcode()) {
4897 case ISD::CTPOP:
4898 Result = Tmp1;
4899 break;
4900 case ISD::CTTZ:
4901 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Duncan Sands4a361272009-01-01 15:52:00 +00004902 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004903 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004904 ISD::SETEQ);
4905 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00004906 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004907 break;
4908 case ISD::CTLZ:
4909 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4910 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004911 DAG.getConstant(NVT.getSizeInBits() -
4912 VT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004913 break;
4914 }
4915 break;
4916 case ISD::EXTRACT_SUBVECTOR:
4917 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4918 break;
4919 case ISD::EXTRACT_VECTOR_ELT:
4920 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4921 break;
4922 }
4923
Gabor Greif1c80d112008-08-28 21:40:38 +00004924 assert(Result.getNode() && "Didn't set a result!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004925
4926 // Make sure the result is itself legal.
4927 Result = LegalizeOp(Result);
4928
4929 // Remember that we promoted this!
4930 AddPromotedOperand(Op, Result);
4931 return Result;
4932}
4933
4934/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4935/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4936/// based on the vector type. The return type of this matches the element type
4937/// of the vector, which may not be legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00004938SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004939 // We know that operand #0 is the Vec vector. If the index is a constant
4940 // or if the invec is a supported hardware type, we can use it. Otherwise,
4941 // lower to a store then an indexed load.
Dan Gohman8181bd12008-07-27 21:46:04 +00004942 SDValue Vec = Op.getOperand(0);
4943 SDValue Idx = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004944
Duncan Sands92c43912008-06-06 12:08:01 +00004945 MVT TVT = Vec.getValueType();
4946 unsigned NumElems = TVT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004947
4948 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4949 default: assert(0 && "This action is not supported yet!");
4950 case TargetLowering::Custom: {
4951 Vec = LegalizeOp(Vec);
4952 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004953 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004954 if (Tmp3.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004955 return Tmp3;
4956 break;
4957 }
4958 case TargetLowering::Legal:
4959 if (isTypeLegal(TVT)) {
4960 Vec = LegalizeOp(Vec);
4961 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004962 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004963 }
4964 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00004965 case TargetLowering::Promote:
4966 assert(TVT.isVector() && "not vector type");
4967 // fall thru to expand since vectors are by default are promote
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004968 case TargetLowering::Expand:
4969 break;
4970 }
4971
4972 if (NumElems == 1) {
4973 // This must be an access of the only element. Return it.
4974 Op = ScalarizeVectorOp(Vec);
4975 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00004976 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004977 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004978 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004979 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004980 if (CIdx->getZExtValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004981 Vec = Lo;
4982 } else {
4983 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004984 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004985 Idx.getValueType());
4986 }
4987
4988 // It's now an extract from the appropriate high or low part. Recurse.
4989 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4990 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4991 } else {
4992 // Store the value to a temporary stack slot, then LOAD the scalar
4993 // element back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00004994 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4995 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004996
4997 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +00004998 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004999 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
5000 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00005001
Duncan Sandsec142ee2008-06-08 20:54:56 +00005002 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00005003 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00005004 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00005005 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00005006
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005007 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
5008
5009 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
5010 }
5011 return Op;
5012}
5013
5014/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
5015/// we assume the operation can be split if it is not already legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00005016SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005017 // We know that operand #0 is the Vec vector. For now we assume the index
5018 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman8181bd12008-07-27 21:46:04 +00005019 SDValue Vec = Op.getOperand(0);
5020 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005021
Duncan Sands92c43912008-06-06 12:08:01 +00005022 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005023
Duncan Sands92c43912008-06-06 12:08:01 +00005024 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005025 // This must be an access of the desired vector length. Return it.
5026 return Vec;
5027 }
5028
5029 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00005030 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005031 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005032 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005033 Vec = Lo;
5034 } else {
5035 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005036 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
5037 Idx.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005038 }
5039
5040 // It's now an extract from the appropriate high or low part. Recurse.
5041 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
5042 return ExpandEXTRACT_SUBVECTOR(Op);
5043}
5044
5045/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
5046/// with condition CC on the current target. This usually involves legalizing
5047/// or promoting the arguments. In the case where LHS and RHS must be expanded,
5048/// there may be no choice but to create a new SetCC node to represent the
5049/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman8181bd12008-07-27 21:46:04 +00005050/// LHS, and the SDValue returned in RHS has a nil SDNode value.
5051void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
5052 SDValue &RHS,
5053 SDValue &CC) {
5054 SDValue Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005055
5056 switch (getTypeAction(LHS.getValueType())) {
5057 case Legal:
5058 Tmp1 = LegalizeOp(LHS); // LHS
5059 Tmp2 = LegalizeOp(RHS); // RHS
5060 break;
5061 case Promote:
5062 Tmp1 = PromoteOp(LHS); // LHS
5063 Tmp2 = PromoteOp(RHS); // RHS
5064
5065 // If this is an FP compare, the operands have already been extended.
Duncan Sands92c43912008-06-06 12:08:01 +00005066 if (LHS.getValueType().isInteger()) {
5067 MVT VT = LHS.getValueType();
5068 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005069
5070 // Otherwise, we have to insert explicit sign or zero extends. Note
5071 // that we could insert sign extends for ALL conditions, but zero extend
5072 // is cheaper on many machines (an AND instead of two shifts), so prefer
5073 // it.
5074 switch (cast<CondCodeSDNode>(CC)->get()) {
5075 default: assert(0 && "Unknown integer comparison!");
5076 case ISD::SETEQ:
5077 case ISD::SETNE:
5078 case ISD::SETUGE:
5079 case ISD::SETUGT:
5080 case ISD::SETULE:
5081 case ISD::SETULT:
5082 // ALL of these operations will work if we either sign or zero extend
5083 // the operands (including the unsigned comparisons!). Zero extend is
5084 // usually a simpler/cheaper operation, so prefer it.
5085 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
5086 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
5087 break;
5088 case ISD::SETGE:
5089 case ISD::SETGT:
5090 case ISD::SETLT:
5091 case ISD::SETLE:
5092 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
5093 DAG.getValueType(VT));
5094 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
5095 DAG.getValueType(VT));
Evan Chengd901b662008-10-13 18:46:18 +00005096 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5097 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005098 break;
5099 }
5100 }
5101 break;
5102 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00005103 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005104 if (VT == MVT::f32 || VT == MVT::f64) {
5105 // Expand into one or more soft-fp libcall(s).
Evan Cheng24108632008-07-01 21:35:46 +00005106 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005107 switch (cast<CondCodeSDNode>(CC)->get()) {
5108 case ISD::SETEQ:
5109 case ISD::SETOEQ:
5110 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5111 break;
5112 case ISD::SETNE:
5113 case ISD::SETUNE:
5114 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
5115 break;
5116 case ISD::SETGE:
5117 case ISD::SETOGE:
5118 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5119 break;
5120 case ISD::SETLT:
5121 case ISD::SETOLT:
5122 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5123 break;
5124 case ISD::SETLE:
5125 case ISD::SETOLE:
5126 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5127 break;
5128 case ISD::SETGT:
5129 case ISD::SETOGT:
5130 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
5131 break;
5132 case ISD::SETUO:
5133 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5134 break;
5135 case ISD::SETO:
5136 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
5137 break;
5138 default:
5139 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5140 switch (cast<CondCodeSDNode>(CC)->get()) {
5141 case ISD::SETONE:
5142 // SETONE = SETOLT | SETOGT
5143 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5144 // Fallthrough
5145 case ISD::SETUGT:
5146 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
5147 break;
5148 case ISD::SETUGE:
5149 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5150 break;
5151 case ISD::SETULT:
5152 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5153 break;
5154 case ISD::SETULE:
5155 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5156 break;
5157 case ISD::SETUEQ:
5158 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5159 break;
5160 default: assert(0 && "Unsupported FP setcc!");
5161 }
5162 }
Duncan Sandsf19591c2008-06-30 10:19:09 +00005163
Dan Gohman8181bd12008-07-27 21:46:04 +00005164 SDValue Dummy;
5165 SDValue Ops[2] = { LHS, RHS };
Gabor Greif1c80d112008-08-28 21:40:38 +00005166 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005167 false /*sign irrelevant*/, Dummy);
5168 Tmp2 = DAG.getConstant(0, MVT::i32);
5169 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
5170 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Duncan Sands4a361272009-01-01 15:52:00 +00005171 Tmp1 = DAG.getNode(ISD::SETCC,
5172 TLI.getSetCCResultType(Tmp1.getValueType()),
5173 Tmp1, Tmp2, CC);
Gabor Greif1c80d112008-08-28 21:40:38 +00005174 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005175 false /*sign irrelevant*/, Dummy);
Duncan Sands4a361272009-01-01 15:52:00 +00005176 Tmp2 = DAG.getNode(ISD::SETCC,
5177 TLI.getSetCCResultType(LHS.getValueType()), LHS,
5178 Tmp2, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005179 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005180 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005181 }
Evan Cheng18a1ab12008-07-07 07:18:09 +00005182 LHS = LegalizeOp(Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005183 RHS = Tmp2;
5184 return;
5185 }
5186
Dan Gohman8181bd12008-07-27 21:46:04 +00005187 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005188 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005189 ExpandOp(RHS, RHSLo, RHSHi);
5190 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5191
5192 if (VT==MVT::ppcf128) {
5193 // FIXME: This generated code sucks. We want to generate
Dale Johannesen26317b62008-09-12 00:30:56 +00005194 // FCMPU crN, hi1, hi2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005195 // BNE crN, L:
Dale Johannesen26317b62008-09-12 00:30:56 +00005196 // FCMPU crN, lo1, lo2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005197 // The following can be improved, but not that much.
Duncan Sands4a361272009-01-01 15:52:00 +00005198 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5199 LHSHi, RHSHi, ISD::SETOEQ);
5200 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5201 LHSLo, RHSLo, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005202 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Duncan Sands4a361272009-01-01 15:52:00 +00005203 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5204 LHSHi, RHSHi, ISD::SETUNE);
5205 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5206 LHSHi, RHSHi, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005207 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5208 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00005209 Tmp2 = SDValue();
Dale Johannesen472d15d2007-10-06 01:24:11 +00005210 break;
5211 }
5212
5213 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005214 case ISD::SETEQ:
5215 case ISD::SETNE:
5216 if (RHSLo == RHSHi)
5217 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5218 if (RHSCST->isAllOnesValue()) {
5219 // Comparison to -1.
5220 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5221 Tmp2 = RHSLo;
5222 break;
5223 }
5224
5225 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5226 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5227 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5228 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5229 break;
5230 default:
5231 // If this is a comparison of the sign bit, just look at the top part.
5232 // X > -1, x < 0
5233 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5234 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00005235 CST->isNullValue()) || // X < 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005236 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5237 CST->isAllOnesValue())) { // X > -1
5238 Tmp1 = LHSHi;
5239 Tmp2 = RHSHi;
5240 break;
5241 }
5242
5243 // FIXME: This generated code sucks.
5244 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005245 switch (CCCode) {
5246 default: assert(0 && "Unknown integer setcc!");
5247 case ISD::SETLT:
5248 case ISD::SETULT: LowCC = ISD::SETULT; break;
5249 case ISD::SETGT:
5250 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5251 case ISD::SETLE:
5252 case ISD::SETULE: LowCC = ISD::SETULE; break;
5253 case ISD::SETGE:
5254 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5255 }
5256
5257 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5258 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5259 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5260
5261 // NOTE: on targets without efficient SELECT of bools, we can always use
5262 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
5263 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Duncan Sands4a361272009-01-01 15:52:00 +00005264 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5265 LHSLo, RHSLo, LowCC, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005266 if (!Tmp1.getNode())
Duncan Sands4a361272009-01-01 15:52:00 +00005267 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5268 LHSLo, RHSLo, LowCC);
5269 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5270 LHSHi, RHSHi, CCCode, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005271 if (!Tmp2.getNode())
Duncan Sands4a361272009-01-01 15:52:00 +00005272 Tmp2 = DAG.getNode(ISD::SETCC,
5273 TLI.getSetCCResultType(LHSHi.getValueType()),
5274 LHSHi, RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005275
Gabor Greif1c80d112008-08-28 21:40:38 +00005276 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5277 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman9d24dc72008-03-13 22:13:53 +00005278 if ((Tmp1C && Tmp1C->isNullValue()) ||
5279 (Tmp2C && Tmp2C->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005280 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5281 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman9d24dc72008-03-13 22:13:53 +00005282 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005283 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5284 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5285 // low part is known false, returns high part.
5286 // For LE / GE, if high part is known false, ignore the low part.
5287 // For LT / GT, if high part is known true, ignore the low part.
5288 Tmp1 = Tmp2;
Dan Gohman8181bd12008-07-27 21:46:04 +00005289 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005290 } else {
Duncan Sands4a361272009-01-01 15:52:00 +00005291 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5292 LHSHi, RHSHi, ISD::SETEQ, false,
5293 DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005294 if (!Result.getNode())
Duncan Sands4a361272009-01-01 15:52:00 +00005295 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5296 LHSHi, RHSHi, ISD::SETEQ);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005297 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5298 Result, Tmp1, Tmp2));
5299 Tmp1 = Result;
Dan Gohman8181bd12008-07-27 21:46:04 +00005300 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005301 }
5302 }
5303 }
5304 }
5305 LHS = Tmp1;
5306 RHS = Tmp2;
5307}
5308
Evan Cheng71343822008-10-15 02:05:31 +00005309/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5310/// condition code CC on the current target. This routine assumes LHS and rHS
5311/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5312/// illegal condition code into AND / OR of multiple SETCC values.
5313void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5314 SDValue &LHS, SDValue &RHS,
5315 SDValue &CC) {
5316 MVT OpVT = LHS.getValueType();
5317 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5318 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5319 default: assert(0 && "Unknown condition code action!");
5320 case TargetLowering::Legal:
5321 // Nothing to do.
5322 break;
5323 case TargetLowering::Expand: {
5324 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5325 unsigned Opc = 0;
5326 switch (CCCode) {
5327 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohman2b5b9ca2008-10-21 03:12:54 +00005328 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5329 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5330 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5331 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5332 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5333 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5334 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5335 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5336 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5337 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5338 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5339 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng71343822008-10-15 02:05:31 +00005340 // FIXME: Implement more expansions.
5341 }
5342
5343 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5344 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5345 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5346 RHS = SDValue();
5347 CC = SDValue();
5348 break;
5349 }
5350 }
5351}
5352
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005353/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5354/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5355/// a load from the stack slot to DestVT, extending it if needed.
5356/// The resultant code need not be legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00005357SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5358 MVT SlotVT,
5359 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005360 // Create the stack frame object.
Mon P Wang55854cc2008-07-05 20:40:31 +00005361 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5362 SrcOp.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00005363 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang55854cc2008-07-05 20:40:31 +00005364
Dan Gohman20e37962008-02-11 18:58:42 +00005365 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005366 int SPFI = StackPtrFI->getIndex();
Mon P Wang55854cc2008-07-05 20:40:31 +00005367
Duncan Sands92c43912008-06-06 12:08:01 +00005368 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5369 unsigned SlotSize = SlotVT.getSizeInBits();
5370 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang55854cc2008-07-05 20:40:31 +00005371 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5372 DestVT.getTypeForMVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005373
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005374 // Emit a store to the stack slot. Use a truncstore if the input value is
5375 // later than DestVT.
Dan Gohman8181bd12008-07-27 21:46:04 +00005376 SDValue Store;
Mon P Wang55854cc2008-07-05 20:40:31 +00005377
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005378 if (SrcSize > SlotSize)
Dan Gohman12a9c082008-02-06 22:27:42 +00005379 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005380 PseudoSourceValue::getFixedStack(SPFI), 0,
5381 SlotVT, false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005382 else {
5383 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman12a9c082008-02-06 22:27:42 +00005384 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005385 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang55854cc2008-07-05 20:40:31 +00005386 false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005387 }
5388
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005389 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005390 if (SlotSize == DestSize)
Mon P Wang55854cc2008-07-05 20:40:31 +00005391 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005392
5393 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang55854cc2008-07-05 20:40:31 +00005394 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5395 false, DestAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005396}
5397
Dan Gohman8181bd12008-07-27 21:46:04 +00005398SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005399 // Create a vector sized/aligned stack slot, store the value to element #0,
5400 // then load the whole vector back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00005401 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman12a9c082008-02-06 22:27:42 +00005402
Dan Gohman20e37962008-02-11 18:58:42 +00005403 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005404 int SPFI = StackPtrFI->getIndex();
5405
Dan Gohman8181bd12008-07-27 21:46:04 +00005406 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005407 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00005408 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005409 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005410}
5411
5412
5413/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
5414/// support the operation, but do support the resultant vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00005415SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005416
5417 // If the only non-undef value is the low element, turn this into a
5418 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
5419 unsigned NumElems = Node->getNumOperands();
5420 bool isOnlyLowElement = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005421 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005422
Dan Gohman8181bd12008-07-27 21:46:04 +00005423 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerd8cee732008-03-09 00:29:42 +00005424 // and use a bitmask instead of a list of elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00005425 std::map<SDValue, std::vector<unsigned> > Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005426 Values[SplatValue].push_back(0);
5427 bool isConstant = true;
5428 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5429 SplatValue.getOpcode() != ISD::UNDEF)
5430 isConstant = false;
5431
5432 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005433 SDValue V = Node->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005434 Values[V].push_back(i);
5435 if (V.getOpcode() != ISD::UNDEF)
5436 isOnlyLowElement = false;
5437 if (SplatValue != V)
Dan Gohman8181bd12008-07-27 21:46:04 +00005438 SplatValue = SDValue(0,0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005439
5440 // If this isn't a constant element or an undef, we can't use a constant
5441 // pool load.
5442 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5443 V.getOpcode() != ISD::UNDEF)
5444 isConstant = false;
5445 }
5446
5447 if (isOnlyLowElement) {
5448 // If the low element is an undef too, then this whole things is an undef.
5449 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5450 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5451 // Otherwise, turn this into a scalar_to_vector node.
5452 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5453 Node->getOperand(0));
5454 }
5455
5456 // If all elements are constants, create a load from the constant pool.
5457 if (isConstant) {
Duncan Sands92c43912008-06-06 12:08:01 +00005458 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005459 std::vector<Constant*> CV;
5460 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5461 if (ConstantFPSDNode *V =
5462 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005463 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005464 } else if (ConstantSDNode *V =
Chris Lattner5e0610f2008-04-20 00:41:09 +00005465 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005466 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005467 } else {
5468 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner5e0610f2008-04-20 00:41:09 +00005469 const Type *OpNTy =
Duncan Sands92c43912008-06-06 12:08:01 +00005470 Node->getOperand(0).getValueType().getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005471 CV.push_back(UndefValue::get(OpNTy));
5472 }
5473 }
5474 Constant *CP = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005475 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005476 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman12a9c082008-02-06 22:27:42 +00005477 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005478 PseudoSourceValue::getConstantPool(), 0,
5479 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005480 }
5481
Gabor Greif1c80d112008-08-28 21:40:38 +00005482 if (SplatValue.getNode()) { // Splat of one value?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005483 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands92c43912008-06-06 12:08:01 +00005484 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman8181bd12008-07-27 21:46:04 +00005485 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5486 std::vector<SDValue> ZeroVec(NumElems, Zero);
5487 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005488 &ZeroVec[0], ZeroVec.size());
5489
5490 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
5491 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
5492 // Get the splatted value into the low element of a vector register.
Dan Gohman8181bd12008-07-27 21:46:04 +00005493 SDValue LowValVec =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005494 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5495
5496 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5497 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5498 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5499 SplatMask);
5500 }
5501 }
5502
5503 // If there are only two unique elements, we may be able to turn this into a
5504 // vector shuffle.
5505 if (Values.size() == 2) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005506 // Get the two values in deterministic order.
Dan Gohman8181bd12008-07-27 21:46:04 +00005507 SDValue Val1 = Node->getOperand(1);
5508 SDValue Val2;
5509 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerd8cee732008-03-09 00:29:42 +00005510 if (MI->first != Val1)
5511 Val2 = MI->first;
5512 else
5513 Val2 = (++MI)->first;
5514
5515 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5516 // vector shuffle has the undef vector on the RHS.
5517 if (Val1.getOpcode() == ISD::UNDEF)
5518 std::swap(Val1, Val2);
5519
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005520 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands92c43912008-06-06 12:08:01 +00005521 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5522 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005523 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005524
5525 // Set elements of the shuffle mask for Val1.
5526 std::vector<unsigned> &Val1Elts = Values[Val1];
5527 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5528 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5529
5530 // Set elements of the shuffle mask for Val2.
5531 std::vector<unsigned> &Val2Elts = Values[Val2];
5532 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5533 if (Val2.getOpcode() != ISD::UNDEF)
5534 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5535 else
5536 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5537
Dan Gohman8181bd12008-07-27 21:46:04 +00005538 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005539 &MaskVec[0], MaskVec.size());
5540
Chris Lattnerd8cee732008-03-09 00:29:42 +00005541 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005542 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5543 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005544 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5545 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005546 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005547
5548 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerd8cee732008-03-09 00:29:42 +00005549 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005550 }
5551 }
5552
5553 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5554 // aligned object on the stack, store each element into it, then load
5555 // the result as a vector.
Duncan Sands92c43912008-06-06 12:08:01 +00005556 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005557 // Create the stack frame object.
Dan Gohman8181bd12008-07-27 21:46:04 +00005558 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005559
5560 // Emit a store of each element to the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00005561 SmallVector<SDValue, 8> Stores;
Duncan Sands92c43912008-06-06 12:08:01 +00005562 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005563 // Store (in the right endianness) the elements to memory.
5564 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5565 // Ignore undef elements.
5566 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5567
5568 unsigned Offset = TypeByteSize*i;
5569
Dan Gohman8181bd12008-07-27 21:46:04 +00005570 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005571 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5572
5573 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
5574 NULL, 0));
5575 }
5576
Dan Gohman8181bd12008-07-27 21:46:04 +00005577 SDValue StoreChain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005578 if (!Stores.empty()) // Not all undef elements?
5579 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5580 &Stores[0], Stores.size());
5581 else
5582 StoreChain = DAG.getEntryNode();
5583
5584 // Result is a load from the stack slot.
5585 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
5586}
5587
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005588void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman8181bd12008-07-27 21:46:04 +00005589 SDValue Op, SDValue Amt,
5590 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005591 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00005592 SDValue LHSL, LHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005593 ExpandOp(Op, LHSL, LHSH);
5594
Dan Gohman8181bd12008-07-27 21:46:04 +00005595 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands92c43912008-06-06 12:08:01 +00005596 MVT VT = LHSL.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005597 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5598 Hi = Lo.getValue(1);
5599}
5600
5601
5602/// ExpandShift - Try to find a clever way to expand this shift operation out to
5603/// smaller elements. If we can't find a way that is more efficient than a
5604/// libcall on this target, return false. Otherwise, return true with the
5605/// low-parts expanded into Lo and Hi.
Dan Gohman8181bd12008-07-27 21:46:04 +00005606bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5607 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005608 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5609 "This is not a shift!");
5610
Duncan Sands92c43912008-06-06 12:08:01 +00005611 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman8181bd12008-07-27 21:46:04 +00005612 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands92c43912008-06-06 12:08:01 +00005613 MVT ShTy = ShAmt.getValueType();
5614 unsigned ShBits = ShTy.getSizeInBits();
5615 unsigned VTBits = Op.getValueType().getSizeInBits();
5616 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005617
Chris Lattner8c931452007-10-14 20:35:12 +00005618 // Handle the case when Amt is an immediate.
Gabor Greif1c80d112008-08-28 21:40:38 +00005619 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005620 unsigned Cst = CN->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005621 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005622 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005623 ExpandOp(Op, InL, InH);
5624 switch(Opc) {
5625 case ISD::SHL:
5626 if (Cst > VTBits) {
5627 Lo = DAG.getConstant(0, NVT);
5628 Hi = DAG.getConstant(0, NVT);
5629 } else if (Cst > NVTBits) {
5630 Lo = DAG.getConstant(0, NVT);
5631 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5632 } else if (Cst == NVTBits) {
5633 Lo = DAG.getConstant(0, NVT);
5634 Hi = InL;
5635 } else {
5636 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5637 Hi = DAG.getNode(ISD::OR, NVT,
5638 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5639 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5640 }
5641 return true;
5642 case ISD::SRL:
5643 if (Cst > VTBits) {
5644 Lo = DAG.getConstant(0, NVT);
5645 Hi = DAG.getConstant(0, NVT);
5646 } else if (Cst > NVTBits) {
5647 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5648 Hi = DAG.getConstant(0, NVT);
5649 } else if (Cst == NVTBits) {
5650 Lo = InH;
5651 Hi = DAG.getConstant(0, NVT);
5652 } else {
5653 Lo = DAG.getNode(ISD::OR, NVT,
5654 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5655 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5656 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5657 }
5658 return true;
5659 case ISD::SRA:
5660 if (Cst > VTBits) {
5661 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5662 DAG.getConstant(NVTBits-1, ShTy));
5663 } else if (Cst > NVTBits) {
5664 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5665 DAG.getConstant(Cst-NVTBits, ShTy));
5666 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5667 DAG.getConstant(NVTBits-1, ShTy));
5668 } else if (Cst == NVTBits) {
5669 Lo = InH;
5670 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5671 DAG.getConstant(NVTBits-1, ShTy));
5672 } else {
5673 Lo = DAG.getNode(ISD::OR, NVT,
5674 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5675 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5676 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5677 }
5678 return true;
5679 }
5680 }
5681
5682 // Okay, the shift amount isn't constant. However, if we can tell that it is
5683 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005684 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5685 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005686 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5687
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005688 // If we know that if any of the high bits of the shift amount are one, then
5689 // we can do this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005690 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005691 // Mask out the high bit, which we know is set.
5692 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005693 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005694
5695 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005696 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005697 ExpandOp(Op, InL, InH);
5698 switch(Opc) {
5699 case ISD::SHL:
5700 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5701 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5702 return true;
5703 case ISD::SRL:
5704 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5705 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5706 return true;
5707 case ISD::SRA:
5708 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5709 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5710 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5711 return true;
5712 }
5713 }
5714
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005715 // If we know that the high bits of the shift amount are all zero, then we can
5716 // do this as a couple of simple shifts.
5717 if ((KnownZero & Mask) == Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005718 // Compute 32-amt.
Dan Gohman8181bd12008-07-27 21:46:04 +00005719 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005720 DAG.getConstant(NVTBits, Amt.getValueType()),
5721 Amt);
5722
5723 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005724 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005725 ExpandOp(Op, InL, InH);
5726 switch(Opc) {
5727 case ISD::SHL:
5728 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5729 Hi = DAG.getNode(ISD::OR, NVT,
5730 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5731 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5732 return true;
5733 case ISD::SRL:
5734 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5735 Lo = DAG.getNode(ISD::OR, NVT,
5736 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5737 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5738 return true;
5739 case ISD::SRA:
5740 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5741 Lo = DAG.getNode(ISD::OR, NVT,
5742 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5743 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5744 return true;
5745 }
5746 }
5747
5748 return false;
5749}
5750
5751
5752// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5753// does not fit into a register, return the lo part and set the hi part to the
5754// by-reg argument. If it does fit into a single register, return the result
5755// and leave the Hi part unset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005756SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5757 bool isSigned, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005758 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5759 // The input chain to this libcall is the entry node of the function.
5760 // Legalizing the call will automatically add the previous call to the
5761 // dependence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005762 SDValue InChain = DAG.getEntryNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005763
5764 TargetLowering::ArgListTy Args;
5765 TargetLowering::ArgListEntry Entry;
5766 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00005767 MVT ArgVT = Node->getOperand(i).getValueType();
5768 const Type *ArgTy = ArgVT.getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005769 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5770 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005771 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005772 Args.push_back(Entry);
5773 }
Bill Wendlingfef06052008-09-16 21:48:12 +00005774 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang1448aad2008-10-30 08:01:45 +00005775 TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005776
5777 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands92c43912008-06-06 12:08:01 +00005778 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman8181bd12008-07-27 21:46:04 +00005779 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen67cc9b62008-09-26 19:31:26 +00005780 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5781 CallingConv::C, false, Callee, Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005782
5783 // Legalize the call sequence, starting with the chain. This will advance
5784 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5785 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5786 LegalizeOp(CallInfo.second);
Dan Gohman8181bd12008-07-27 21:46:04 +00005787 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005788 switch (getTypeAction(CallInfo.first.getValueType())) {
5789 default: assert(0 && "Unknown thing");
5790 case Legal:
5791 Result = CallInfo.first;
5792 break;
5793 case Expand:
5794 ExpandOp(CallInfo.first, Result, Hi);
5795 break;
5796 }
5797 return Result;
5798}
5799
Dan Gohman29c3cef2008-08-14 20:04:46 +00005800/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5801///
5802SDValue SelectionDAGLegalize::
5803LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5804 bool isCustom = false;
5805 SDValue Tmp1;
5806 switch (getTypeAction(Op.getValueType())) {
5807 case Legal:
5808 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5809 Op.getValueType())) {
5810 default: assert(0 && "Unknown operation action!");
5811 case TargetLowering::Custom:
5812 isCustom = true;
5813 // FALLTHROUGH
5814 case TargetLowering::Legal:
5815 Tmp1 = LegalizeOp(Op);
Gabor Greif1c80d112008-08-28 21:40:38 +00005816 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005817 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5818 else
5819 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5820 DestTy, Tmp1);
5821 if (isCustom) {
5822 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005823 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman29c3cef2008-08-14 20:04:46 +00005824 }
5825 break;
5826 case TargetLowering::Expand:
5827 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5828 break;
5829 case TargetLowering::Promote:
5830 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5831 break;
5832 }
5833 break;
5834 case Expand:
5835 Result = ExpandIntToFP(isSigned, DestTy, Op);
5836 break;
5837 case Promote:
5838 Tmp1 = PromoteOp(Op);
5839 if (isSigned) {
5840 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5841 Tmp1, DAG.getValueType(Op.getValueType()));
5842 } else {
5843 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5844 Op.getValueType());
5845 }
Gabor Greif1c80d112008-08-28 21:40:38 +00005846 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005847 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5848 else
5849 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5850 DestTy, Tmp1);
5851 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5852 break;
5853 }
5854 return Result;
5855}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005856
5857/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5858///
Dan Gohman8181bd12008-07-27 21:46:04 +00005859SDValue SelectionDAGLegalize::
5860ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands92c43912008-06-06 12:08:01 +00005861 MVT SourceVT = Source.getValueType();
Dan Gohman8b232ff2008-03-11 01:59:03 +00005862 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005863
Dan Gohman29c3cef2008-08-14 20:04:46 +00005864 // Expand unsupported int-to-fp vector casts by unrolling them.
5865 if (DestTy.isVector()) {
5866 if (!ExpandSource)
5867 return LegalizeOp(UnrollVectorOp(Source));
5868 MVT DestEltTy = DestTy.getVectorElementType();
5869 if (DestTy.getVectorNumElements() == 1) {
5870 SDValue Scalar = ScalarizeVectorOp(Source);
5871 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5872 DestEltTy, Scalar);
5873 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5874 }
5875 SDValue Lo, Hi;
5876 SplitVectorOp(Source, Lo, Hi);
5877 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5878 DestTy.getVectorNumElements() / 2);
5879 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5880 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengd901b662008-10-13 18:46:18 +00005881 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5882 HiResult));
Dan Gohman29c3cef2008-08-14 20:04:46 +00005883 }
5884
Evan Chengf99a7752008-04-01 02:18:22 +00005885 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5886 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmana193dba2008-03-05 02:07:31 +00005887 // The integer value loaded will be incorrectly if the 'sign bit' of the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005888 // incoming integer is set. To handle this, we dynamically test to see if
5889 // it is set, and, if so, add a fudge factor.
Dan Gohman8181bd12008-07-27 21:46:04 +00005890 SDValue Hi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005891 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005892 SDValue Lo;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005893 ExpandOp(Source, Lo, Hi);
5894 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5895 } else {
5896 // The comparison for the sign bit will use the entire operand.
5897 Hi = Source;
5898 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005899
Dale Johannesen96db7962008-11-04 20:52:49 +00005900 // Check to see if the target has a custom way to lower this. If so, use
5901 // it. (Note we've already expanded the operand in this case.)
Dale Johannesena359b8b2008-10-21 20:50:01 +00005902 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5903 default: assert(0 && "This action not implemented for this operation!");
5904 case TargetLowering::Legal:
5905 case TargetLowering::Expand:
5906 break; // This case is handled below.
5907 case TargetLowering::Custom: {
5908 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5909 Source), DAG);
5910 if (NV.getNode())
5911 return LegalizeOp(NV);
5912 break; // The target decided this was legal after all
5913 }
5914 }
5915
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005916 // If this is unsigned, and not supported, first perform the conversion to
5917 // signed, then adjust the result if the sign bit is set.
Dan Gohman8181bd12008-07-27 21:46:04 +00005918 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005919
Duncan Sands4a361272009-01-01 15:52:00 +00005920 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi.getValueType()),
5921 Hi, DAG.getConstant(0, Hi.getValueType()),
5922 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005923 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5924 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005925 SignSet, Four, Zero);
5926 uint64_t FF = 0x5f800000ULL;
5927 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmana193dba2008-03-05 02:07:31 +00005928 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005929
Dan Gohman8181bd12008-07-27 21:46:04 +00005930 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005931 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005932 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00005933 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00005934 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005935 if (DestTy == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005936 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005937 PseudoSourceValue::getConstantPool(), 0,
5938 false, Alignment);
Duncan Sandsec142ee2008-06-08 20:54:56 +00005939 else if (DestTy.bitsGT(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005940 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005941 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00005942 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005943 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00005944 MVT::f32, false, Alignment);
Dale Johannesen2fc20782007-09-14 22:26:36 +00005945 else
5946 assert(0 && "Unexpected conversion");
5947
Duncan Sands92c43912008-06-06 12:08:01 +00005948 MVT SCVT = SignedConv.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005949 if (SCVT != DestTy) {
5950 // Destination type needs to be expanded as well. The FADD now we are
5951 // constructing will be expanded into a libcall.
Duncan Sands92c43912008-06-06 12:08:01 +00005952 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5953 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmanc98645c2008-03-05 01:08:17 +00005954 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005955 SignedConv, SignedConv.getValue(1));
5956 }
5957 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5958 }
5959 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5960 }
5961
5962 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmanc98645c2008-03-05 01:08:17 +00005963 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005964 default: assert(0 && "This action not implemented for this operation!");
5965 case TargetLowering::Legal:
5966 case TargetLowering::Expand:
5967 break; // This case is handled below.
5968 case TargetLowering::Custom: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005969 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005970 Source), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005971 if (NV.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005972 return LegalizeOp(NV);
5973 break; // The target decided this was legal after all
5974 }
5975 }
5976
5977 // Expand the source, then glue it back together for the call. We must expand
5978 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman8b232ff2008-03-11 01:59:03 +00005979 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005980 SDValue SrcLo, SrcHi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005981 ExpandOp(Source, SrcLo, SrcHi);
5982 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5983 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005984
Duncan Sandsf68dffb2008-07-17 02:36:29 +00005985 RTLIB::Libcall LC = isSigned ?
5986 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5987 RTLIB::getUINTTOFP(SourceVT, DestTy);
5988 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5989
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005990 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman8181bd12008-07-27 21:46:04 +00005991 SDValue HiPart;
Gabor Greif1c80d112008-08-28 21:40:38 +00005992 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5993 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmanec51f642008-03-10 23:03:31 +00005994 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5995 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005996}
5997
5998/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5999/// INT_TO_FP operation of the specified operand when the target requests that
6000/// we expand it. At this point, we know that the result and operand types are
6001/// legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00006002SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
6003 SDValue Op0,
6004 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006005 if (Op0.getValueType() == MVT::i32) {
6006 // simple 32-bit [signed|unsigned] integer to float/double expansion
6007
Chris Lattner0aeb1d02008-01-16 07:03:22 +00006008 // Get the stack frame index of a 8 byte buffer.
Dan Gohman8181bd12008-07-27 21:46:04 +00006009 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner0aeb1d02008-01-16 07:03:22 +00006010
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006011 // word offset constant for Hi/Lo address computation
Dan Gohman8181bd12008-07-27 21:46:04 +00006012 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006013 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman8181bd12008-07-27 21:46:04 +00006014 SDValue Hi = StackSlot;
6015 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006016 if (TLI.isLittleEndian())
6017 std::swap(Hi, Lo);
6018
6019 // if signed map to unsigned space
Dan Gohman8181bd12008-07-27 21:46:04 +00006020 SDValue Op0Mapped;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006021 if (isSigned) {
6022 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman8181bd12008-07-27 21:46:04 +00006023 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006024 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
6025 } else {
6026 Op0Mapped = Op0;
6027 }
6028 // store the lo of the constructed double - based on integer input
Dan Gohman8181bd12008-07-27 21:46:04 +00006029 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006030 Op0Mapped, Lo, NULL, 0);
6031 // initial hi portion of constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00006032 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006033 // store the hi of the constructed double - biased exponent
Dan Gohman8181bd12008-07-27 21:46:04 +00006034 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006035 // load the constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00006036 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006037 // FP constant to bias correct the final result
Dan Gohman8181bd12008-07-27 21:46:04 +00006038 SDValue Bias = DAG.getConstantFP(isSigned ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006039 BitsToDouble(0x4330000080000000ULL)
6040 : BitsToDouble(0x4330000000000000ULL),
6041 MVT::f64);
6042 // subtract the bias
Dan Gohman8181bd12008-07-27 21:46:04 +00006043 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006044 // final result
Dan Gohman8181bd12008-07-27 21:46:04 +00006045 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006046 // handle final rounding
6047 if (DestVT == MVT::f64) {
6048 // do nothing
6049 Result = Sub;
Duncan Sandsec142ee2008-06-08 20:54:56 +00006050 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner5872a362008-01-17 07:00:52 +00006051 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
6052 DAG.getIntPtrConstant(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00006053 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenb17a7a22007-09-16 16:51:49 +00006054 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006055 }
6056 return Result;
6057 }
6058 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman8181bd12008-07-27 21:46:04 +00006059 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006060
Duncan Sands4a361272009-01-01 15:52:00 +00006061 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0.getValueType()),
6062 Op0, DAG.getConstant(0, Op0.getValueType()),
6063 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00006064 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
6065 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006066 SignSet, Four, Zero);
6067
6068 // If the sign bit of the integer is set, the large number will be treated
6069 // as a negative number. To counteract this, the dynamic code adds an
6070 // offset depending on the data type.
6071 uint64_t FF;
Duncan Sands92c43912008-06-06 12:08:01 +00006072 switch (Op0.getValueType().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006073 default: assert(0 && "Unsupported integer type!");
6074 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6075 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6076 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6077 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6078 }
6079 if (TLI.isLittleEndian()) FF <<= 32;
6080 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
6081
Dan Gohman8181bd12008-07-27 21:46:04 +00006082 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00006083 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006084 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00006085 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00006086 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006087 if (DestVT == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00006088 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00006089 PseudoSourceValue::getConstantPool(), 0,
6090 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006091 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00006092 FudgeInReg =
6093 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
6094 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006095 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00006096 MVT::f32, false, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006097 }
6098
6099 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
6100}
6101
6102/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6103/// *INT_TO_FP operation of the specified operand when the target requests that
6104/// we promote it. At this point, we know that the result and operand types are
6105/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6106/// operation that takes a larger input.
Dan Gohman8181bd12008-07-27 21:46:04 +00006107SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6108 MVT DestVT,
6109 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006110 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00006111 MVT NewInTy = LegalOp.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006112
6113 unsigned OpToUse = 0;
6114
6115 // Scan for the appropriate larger type to use.
6116 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00006117 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6118 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006119
6120 // If the target supports SINT_TO_FP of this type, use it.
6121 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6122 default: break;
6123 case TargetLowering::Legal:
6124 if (!TLI.isTypeLegal(NewInTy))
6125 break; // Can't use this datatype.
6126 // FALL THROUGH.
6127 case TargetLowering::Custom:
6128 OpToUse = ISD::SINT_TO_FP;
6129 break;
6130 }
6131 if (OpToUse) break;
6132 if (isSigned) continue;
6133
6134 // If the target supports UINT_TO_FP of this type, use it.
6135 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6136 default: break;
6137 case TargetLowering::Legal:
6138 if (!TLI.isTypeLegal(NewInTy))
6139 break; // Can't use this datatype.
6140 // FALL THROUGH.
6141 case TargetLowering::Custom:
6142 OpToUse = ISD::UINT_TO_FP;
6143 break;
6144 }
6145 if (OpToUse) break;
6146
6147 // Otherwise, try a larger type.
6148 }
6149
6150 // Okay, we found the operation and type to use. Zero extend our input to the
6151 // desired type then run the operation on it.
6152 return DAG.getNode(OpToUse, DestVT,
6153 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
6154 NewInTy, LegalOp));
6155}
6156
6157/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6158/// FP_TO_*INT operation of the specified operand when the target requests that
6159/// we promote it. At this point, we know that the result and operand types are
6160/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6161/// operation that returns a larger result.
Dan Gohman8181bd12008-07-27 21:46:04 +00006162SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6163 MVT DestVT,
6164 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006165 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00006166 MVT NewOutTy = DestVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006167
6168 unsigned OpToUse = 0;
6169
6170 // Scan for the appropriate larger type to use.
6171 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00006172 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6173 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006174
6175 // If the target supports FP_TO_SINT returning this type, use it.
6176 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6177 default: break;
6178 case TargetLowering::Legal:
6179 if (!TLI.isTypeLegal(NewOutTy))
6180 break; // Can't use this datatype.
6181 // FALL THROUGH.
6182 case TargetLowering::Custom:
6183 OpToUse = ISD::FP_TO_SINT;
6184 break;
6185 }
6186 if (OpToUse) break;
6187
6188 // If the target supports FP_TO_UINT of this type, use it.
6189 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6190 default: break;
6191 case TargetLowering::Legal:
6192 if (!TLI.isTypeLegal(NewOutTy))
6193 break; // Can't use this datatype.
6194 // FALL THROUGH.
6195 case TargetLowering::Custom:
6196 OpToUse = ISD::FP_TO_UINT;
6197 break;
6198 }
6199 if (OpToUse) break;
6200
6201 // Otherwise, try a larger type.
6202 }
6203
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006204
6205 // Okay, we found the operation and type to use.
Dan Gohman8181bd12008-07-27 21:46:04 +00006206 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sandsac496a12008-07-04 11:47:58 +00006207
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006208 // If the operation produces an invalid type, it must be custom lowered. Use
6209 // the target lowering hooks to expand it. Just keep the low part of the
6210 // expanded operation, we know that we're truncating anyway.
6211 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006212 SmallVector<SDValue, 2> Results;
6213 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6214 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6215 Operation = Results[0];
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006216 }
Duncan Sandsac496a12008-07-04 11:47:58 +00006217
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006218 // Truncate the result of the extended FP_TO_*INT operation to the desired
6219 // size.
6220 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006221}
6222
6223/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6224///
Dan Gohman8181bd12008-07-27 21:46:04 +00006225SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00006226 MVT VT = Op.getValueType();
6227 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00006228 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands92c43912008-06-06 12:08:01 +00006229 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006230 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6231 case MVT::i16:
6232 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6233 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6234 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6235 case MVT::i32:
6236 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6237 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6238 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6239 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6240 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6241 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6242 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6243 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6244 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6245 case MVT::i64:
6246 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6247 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6248 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6249 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6250 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6251 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6252 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6253 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6254 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6255 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6256 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6257 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6258 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6259 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6260 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6261 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6262 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6263 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6264 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6265 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6266 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6267 }
6268}
6269
6270/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6271///
Dan Gohman8181bd12008-07-27 21:46:04 +00006272SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006273 switch (Opc) {
6274 default: assert(0 && "Cannot expand this yet!");
6275 case ISD::CTPOP: {
6276 static const uint64_t mask[6] = {
6277 0x5555555555555555ULL, 0x3333333333333333ULL,
6278 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6279 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6280 };
Duncan Sands92c43912008-06-06 12:08:01 +00006281 MVT VT = Op.getValueType();
6282 MVT ShVT = TLI.getShiftAmountTy();
6283 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006284 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6285 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman8181bd12008-07-27 21:46:04 +00006286 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6287 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006288 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6289 DAG.getNode(ISD::AND, VT,
6290 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6291 }
6292 return Op;
6293 }
6294 case ISD::CTLZ: {
6295 // for now, we do this:
6296 // x = x | (x >> 1);
6297 // x = x | (x >> 2);
6298 // ...
6299 // x = x | (x >>16);
6300 // x = x | (x >>32); // for 64-bit input
6301 // return popcount(~x);
6302 //
6303 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006304 MVT VT = Op.getValueType();
6305 MVT ShVT = TLI.getShiftAmountTy();
6306 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006307 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006308 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006309 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6310 }
6311 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6312 return DAG.getNode(ISD::CTPOP, VT, Op);
6313 }
6314 case ISD::CTTZ: {
6315 // for now, we use: { return popcount(~x & (x - 1)); }
6316 // unless the target has ctlz but not ctpop, in which case we use:
6317 // { return 32 - nlz(~x & (x-1)); }
6318 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006319 MVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006320 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6321 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006322 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6323 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6324 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6325 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6326 TLI.isOperationLegal(ISD::CTLZ, VT))
6327 return DAG.getNode(ISD::SUB, VT,
Duncan Sands92c43912008-06-06 12:08:01 +00006328 DAG.getConstant(VT.getSizeInBits(), VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006329 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6330 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6331 }
6332 }
6333}
6334
Dan Gohman8181bd12008-07-27 21:46:04 +00006335/// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006336/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman4fc03742008-10-01 15:07:49 +00006337/// LegalizedNodes map is filled in for any results that are not expanded, the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006338/// ExpandedNodes map is filled in for any results that are expanded, and the
6339/// Lo/Hi values are returned.
Dan Gohman8181bd12008-07-27 21:46:04 +00006340void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands92c43912008-06-06 12:08:01 +00006341 MVT VT = Op.getValueType();
6342 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greif1c80d112008-08-28 21:40:38 +00006343 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006344 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00006345 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands92c43912008-06-06 12:08:01 +00006346 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006347
6348 // See if we already expanded it.
Dan Gohman8181bd12008-07-27 21:46:04 +00006349 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006350 = ExpandedNodes.find(Op);
6351 if (I != ExpandedNodes.end()) {
6352 Lo = I->second.first;
6353 Hi = I->second.second;
6354 return;
6355 }
6356
6357 switch (Node->getOpcode()) {
6358 case ISD::CopyFromReg:
6359 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006360 case ISD::FP_ROUND_INREG:
6361 if (VT == MVT::ppcf128 &&
6362 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6363 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006364 SDValue SrcLo, SrcHi, Src;
Dale Johannesend3b6af32007-10-11 23:32:15 +00006365 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6366 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006367 SDValue Result = TLI.LowerOperation(
Dale Johannesend3b6af32007-10-11 23:32:15 +00006368 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006369 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6370 Lo = Result.getNode()->getOperand(0);
6371 Hi = Result.getNode()->getOperand(1);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006372 break;
6373 }
6374 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006375 default:
6376#ifndef NDEBUG
6377 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
6378#endif
6379 assert(0 && "Do not know how to expand this operator!");
6380 abort();
Dan Gohman550c8462008-02-27 01:52:30 +00006381 case ISD::EXTRACT_ELEMENT:
6382 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006383 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman550c8462008-02-27 01:52:30 +00006384 return ExpandOp(Hi, Lo, Hi);
Dan Gohman7e7aa2c2008-02-27 19:44:57 +00006385 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006386 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006387 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6388 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6389 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006390 case ISD::UNDEF:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006391 Lo = DAG.getNode(ISD::UNDEF, NVT);
6392 Hi = DAG.getNode(ISD::UNDEF, NVT);
6393 break;
6394 case ISD::Constant: {
Duncan Sands92c43912008-06-06 12:08:01 +00006395 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman97f1f8e2008-03-03 22:20:46 +00006396 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6397 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6398 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006399 break;
6400 }
6401 case ISD::ConstantFP: {
6402 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00006403 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00006404 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen2aef5692007-10-11 18:07:22 +00006405 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6406 MVT::f64);
6407 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6408 MVT::f64);
6409 break;
6410 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006411 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
6412 if (getTypeAction(Lo.getValueType()) == Expand)
6413 ExpandOp(Lo, Lo, Hi);
6414 break;
6415 }
6416 case ISD::BUILD_PAIR:
6417 // Return the operands.
6418 Lo = Node->getOperand(0);
6419 Hi = Node->getOperand(1);
6420 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006421
6422 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00006423 if (Node->getNumValues() == 1) {
6424 ExpandOp(Op.getOperand(0), Lo, Hi);
6425 break;
6426 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006427 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif46bf5472008-08-26 22:36:50 +00006428 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006429 Op.getValue(1).getValueType() == MVT::Other &&
6430 "unhandled MERGE_VALUES");
6431 ExpandOp(Op.getOperand(0), Lo, Hi);
6432 // Remember that we legalized the chain.
6433 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6434 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006435
6436 case ISD::SIGN_EXTEND_INREG:
6437 ExpandOp(Node->getOperand(0), Lo, Hi);
6438 // sext_inreg the low part if needed.
6439 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6440
6441 // The high part gets the sign extension from the lo-part. This handles
6442 // things like sextinreg V:i64 from i8.
6443 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands92c43912008-06-06 12:08:01 +00006444 DAG.getConstant(NVT.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006445 TLI.getShiftAmountTy()));
6446 break;
6447
6448 case ISD::BSWAP: {
6449 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006450 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006451 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6452 Lo = TempLo;
6453 break;
6454 }
6455
6456 case ISD::CTPOP:
6457 ExpandOp(Node->getOperand(0), Lo, Hi);
6458 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6459 DAG.getNode(ISD::CTPOP, NVT, Lo),
6460 DAG.getNode(ISD::CTPOP, NVT, Hi));
6461 Hi = DAG.getConstant(0, NVT);
6462 break;
6463
6464 case ISD::CTLZ: {
6465 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
6466 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006467 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6468 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Duncan Sands4a361272009-01-01 15:52:00 +00006469 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), HLZ, BitsC,
6470 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006471 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006472 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6473
6474 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6475 Hi = DAG.getConstant(0, NVT);
6476 break;
6477 }
6478
6479 case ISD::CTTZ: {
6480 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
6481 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006482 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6483 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Duncan Sands4a361272009-01-01 15:52:00 +00006484 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), LTZ, BitsC,
6485 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006486 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006487 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6488
6489 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6490 Hi = DAG.getConstant(0, NVT);
6491 break;
6492 }
6493
6494 case ISD::VAARG: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006495 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6496 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006497 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6498 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6499
6500 // Remember that we legalized the chain.
6501 Hi = LegalizeOp(Hi);
6502 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006503 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006504 std::swap(Lo, Hi);
6505 break;
6506 }
6507
6508 case ISD::LOAD: {
6509 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00006510 SDValue Ch = LD->getChain(); // Legalize the chain.
6511 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006512 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman29c3cef2008-08-14 20:04:46 +00006513 const Value *SV = LD->getSrcValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006514 int SVOffset = LD->getSrcValueOffset();
6515 unsigned Alignment = LD->getAlignment();
6516 bool isVolatile = LD->isVolatile();
6517
6518 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman29c3cef2008-08-14 20:04:46 +00006519 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006520 isVolatile, Alignment);
6521 if (VT == MVT::f32 || VT == MVT::f64) {
6522 // f32->i32 or f64->i64 one to one expansion.
6523 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006524 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006525 // Recursively expand the new load.
6526 if (getTypeAction(NVT) == Expand)
6527 ExpandOp(Lo, Lo, Hi);
6528 break;
6529 }
6530
6531 // Increment the pointer to the other half.
Duncan Sands92c43912008-06-06 12:08:01 +00006532 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006533 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006534 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006535 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006536 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00006537 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006538 isVolatile, Alignment);
6539
6540 // Build a factor node to remember that this load is independent of the
6541 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00006542 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006543 Hi.getValue(1));
6544
6545 // Remember that we legalized the chain.
6546 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006547 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006548 std::swap(Lo, Hi);
6549 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00006550 MVT EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006551
Dale Johannesen2550e3a2007-10-19 20:29:00 +00006552 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6553 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006554 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman29c3cef2008-08-14 20:04:46 +00006555 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006556 SVOffset, isVolatile, Alignment);
6557 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006558 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006559 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6560 break;
6561 }
6562
6563 if (EVT == NVT)
Dan Gohman29c3cef2008-08-14 20:04:46 +00006564 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006565 SVOffset, isVolatile, Alignment);
6566 else
Dan Gohman29c3cef2008-08-14 20:04:46 +00006567 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006568 SVOffset, EVT, isVolatile,
6569 Alignment);
6570
6571 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006572 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006573
6574 if (ExtType == ISD::SEXTLOAD) {
6575 // The high part is obtained by SRA'ing all but one of the bits of the
6576 // lo part.
Duncan Sands92c43912008-06-06 12:08:01 +00006577 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006578 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6579 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6580 } else if (ExtType == ISD::ZEXTLOAD) {
6581 // The high part is just a zero.
6582 Hi = DAG.getConstant(0, NVT);
6583 } else /* if (ExtType == ISD::EXTLOAD) */ {
6584 // The high part is undefined.
6585 Hi = DAG.getNode(ISD::UNDEF, NVT);
6586 }
6587 }
6588 break;
6589 }
6590 case ISD::AND:
6591 case ISD::OR:
6592 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +00006593 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006594 ExpandOp(Node->getOperand(0), LL, LH);
6595 ExpandOp(Node->getOperand(1), RL, RH);
6596 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6597 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6598 break;
6599 }
6600 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006601 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006602 ExpandOp(Node->getOperand(1), LL, LH);
6603 ExpandOp(Node->getOperand(2), RL, RH);
6604 if (getTypeAction(NVT) == Expand)
6605 NVT = TLI.getTypeToExpandTo(NVT);
6606 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
6607 if (VT != MVT::f32)
6608 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
6609 break;
6610 }
6611 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006612 SDValue TL, TH, FL, FH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006613 ExpandOp(Node->getOperand(2), TL, TH);
6614 ExpandOp(Node->getOperand(3), FL, FH);
6615 if (getTypeAction(NVT) == Expand)
6616 NVT = TLI.getTypeToExpandTo(NVT);
6617 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6618 Node->getOperand(1), TL, FL, Node->getOperand(4));
6619 if (VT != MVT::f32)
6620 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6621 Node->getOperand(1), TH, FH, Node->getOperand(4));
6622 break;
6623 }
6624 case ISD::ANY_EXTEND:
6625 // The low part is any extension of the input (which degenerates to a copy).
6626 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6627 // The high part is undefined.
6628 Hi = DAG.getNode(ISD::UNDEF, NVT);
6629 break;
6630 case ISD::SIGN_EXTEND: {
6631 // The low part is just a sign extension of the input (which degenerates to
6632 // a copy).
6633 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
6634
6635 // The high part is obtained by SRA'ing all but one of the bits of the lo
6636 // part.
Duncan Sands92c43912008-06-06 12:08:01 +00006637 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006638 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6639 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6640 break;
6641 }
6642 case ISD::ZERO_EXTEND:
6643 // The low part is just a zero extension of the input (which degenerates to
6644 // a copy).
6645 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
6646
6647 // The high part is just a zero.
6648 Hi = DAG.getConstant(0, NVT);
6649 break;
6650
6651 case ISD::TRUNCATE: {
6652 // The input value must be larger than this value. Expand *it*.
Dan Gohman8181bd12008-07-27 21:46:04 +00006653 SDValue NewLo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006654 ExpandOp(Node->getOperand(0), NewLo, Hi);
6655
6656 // The low part is now either the right size, or it is closer. If not the
6657 // right size, make an illegal truncate so we recursively expand it.
6658 if (NewLo.getValueType() != Node->getValueType(0))
6659 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6660 ExpandOp(NewLo, Lo, Hi);
6661 break;
6662 }
6663
6664 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006665 SDValue Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006666 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6667 // If the target wants to, allow it to lower this itself.
6668 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6669 case Expand: assert(0 && "cannot expand FP!");
6670 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6671 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6672 }
6673 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6674 }
6675
6676 // f32 / f64 must be expanded to i32 / i64.
6677 if (VT == MVT::f32 || VT == MVT::f64) {
6678 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6679 if (getTypeAction(NVT) == Expand)
6680 ExpandOp(Lo, Lo, Hi);
6681 break;
6682 }
6683
6684 // If source operand will be expanded to the same type as VT, i.e.
6685 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands92c43912008-06-06 12:08:01 +00006686 MVT VT0 = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006687 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6688 ExpandOp(Node->getOperand(0), Lo, Hi);
6689 break;
6690 }
6691
6692 // Turn this into a load/store pair by default.
Gabor Greif1c80d112008-08-28 21:40:38 +00006693 if (Tmp.getNode() == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00006694 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006695
6696 ExpandOp(Tmp, Lo, Hi);
6697 break;
6698 }
6699
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006700 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006701 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6702 TargetLowering::Custom &&
6703 "Must custom expand ReadCycleCounter");
Dan Gohman8181bd12008-07-27 21:46:04 +00006704 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006705 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006706 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006707 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006708 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006709 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006710 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006711
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006712 case ISD::ATOMIC_CMP_SWAP: {
Dale Johannesen44eb5372008-10-03 19:41:08 +00006713 // This operation does not need a loop.
6714 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6715 assert(Tmp.getNode() && "Node must be custom expanded!");
6716 ExpandOp(Tmp.getValue(0), Lo, Hi);
6717 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6718 LegalizeOp(Tmp.getValue(1)));
6719 break;
6720 }
6721
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006722 case ISD::ATOMIC_LOAD_ADD:
6723 case ISD::ATOMIC_LOAD_SUB:
6724 case ISD::ATOMIC_LOAD_AND:
6725 case ISD::ATOMIC_LOAD_OR:
6726 case ISD::ATOMIC_LOAD_XOR:
6727 case ISD::ATOMIC_LOAD_NAND:
6728 case ISD::ATOMIC_SWAP: {
Dale Johannesen44eb5372008-10-03 19:41:08 +00006729 // These operations require a loop to be generated. We can't do that yet,
6730 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesenf160d802008-10-02 18:53:47 +00006731 SDValue In2Lo, In2Hi, In2;
6732 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6733 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006734 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6735 SDValue Replace =
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006736 DAG.getAtomic(Op.getOpcode(), Anode->getMemoryVT(),
6737 Op.getOperand(0), Op.getOperand(1), In2,
Dale Johannesen44eb5372008-10-03 19:41:08 +00006738 Anode->getSrcValue(), Anode->getAlignment());
6739 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006740 ExpandOp(Result.getValue(0), Lo, Hi);
6741 // Remember that we legalized the chain.
6742 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharth81580822008-03-05 01:15:49 +00006743 break;
6744 }
6745
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006746 // These operators cannot be expanded directly, emit them as calls to
6747 // library functions.
6748 case ISD::FP_TO_SINT: {
6749 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006750 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006751 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6752 case Expand: assert(0 && "cannot expand FP!");
6753 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6754 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6755 }
6756
6757 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6758
6759 // Now that the custom expander is done, expand the result, which is still
6760 // VT.
Gabor Greif1c80d112008-08-28 21:40:38 +00006761 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006762 ExpandOp(Op, Lo, Hi);
6763 break;
6764 }
6765 }
6766
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006767 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6768 VT);
6769 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6770 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006771 break;
6772 }
6773
6774 case ISD::FP_TO_UINT: {
6775 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006776 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006777 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6778 case Expand: assert(0 && "cannot expand FP!");
6779 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6780 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6781 }
6782
6783 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6784
6785 // Now that the custom expander is done, expand the result.
Gabor Greif1c80d112008-08-28 21:40:38 +00006786 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006787 ExpandOp(Op, Lo, Hi);
6788 break;
6789 }
6790 }
6791
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006792 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6793 VT);
6794 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6795 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006796 break;
6797 }
6798
6799 case ISD::SHL: {
6800 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006801 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006802 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006803 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006804 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006805 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006806 // Now that the custom expander is done, expand the result, which is
6807 // still VT.
6808 ExpandOp(Op, Lo, Hi);
6809 break;
6810 }
6811 }
6812
6813 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6814 // this X << 1 as X+X.
6815 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00006816 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006817 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006818 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006819 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6820 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6821 LoOps[1] = LoOps[0];
6822 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6823
6824 HiOps[1] = HiOps[0];
6825 HiOps[2] = Lo.getValue(1);
6826 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6827 break;
6828 }
6829 }
6830
6831 // If we can emit an efficient shift operation, do so now.
6832 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6833 break;
6834
6835 // If this target supports SHL_PARTS, use it.
6836 TargetLowering::LegalizeAction Action =
6837 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6838 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6839 Action == TargetLowering::Custom) {
6840 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6841 break;
6842 }
6843
6844 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006845 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006846 break;
6847 }
6848
6849 case ISD::SRA: {
6850 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006851 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006852 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006853 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006854 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006855 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006856 // Now that the custom expander is done, expand the result, which is
6857 // still VT.
6858 ExpandOp(Op, Lo, Hi);
6859 break;
6860 }
6861 }
6862
6863 // If we can emit an efficient shift operation, do so now.
6864 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6865 break;
6866
6867 // If this target supports SRA_PARTS, use it.
6868 TargetLowering::LegalizeAction Action =
6869 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6870 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6871 Action == TargetLowering::Custom) {
6872 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6873 break;
6874 }
6875
6876 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006877 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006878 break;
6879 }
6880
6881 case ISD::SRL: {
6882 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006883 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006884 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006885 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006886 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006887 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006888 // Now that the custom expander is done, expand the result, which is
6889 // still VT.
6890 ExpandOp(Op, Lo, Hi);
6891 break;
6892 }
6893 }
6894
6895 // If we can emit an efficient shift operation, do so now.
6896 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6897 break;
6898
6899 // If this target supports SRL_PARTS, use it.
6900 TargetLowering::LegalizeAction Action =
6901 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6902 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6903 Action == TargetLowering::Custom) {
6904 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6905 break;
6906 }
6907
6908 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006909 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006910 break;
6911 }
6912
6913 case ISD::ADD:
6914 case ISD::SUB: {
6915 // If the target wants to custom expand this, let them.
6916 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6917 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006918 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006919 if (Result.getNode()) {
Duncan Sands4c3885b2008-06-22 09:42:16 +00006920 ExpandOp(Result, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006921 break;
6922 }
6923 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006924 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006925 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006926 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6927 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman8181bd12008-07-27 21:46:04 +00006928 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006929 LoOps[0] = LHSL;
6930 LoOps[1] = RHSL;
6931 HiOps[0] = LHSH;
6932 HiOps[1] = RHSH;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006933
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006934 //cascaded check to see if any smaller size has a a carry flag.
6935 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6936 bool hasCarry = false;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006937 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6938 MVT AVT = MVT::getIntegerVT(BitSize);
6939 if (TLI.isOperationLegal(OpV, AVT)) {
6940 hasCarry = true;
6941 break;
6942 }
6943 }
6944
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006945 if(hasCarry) {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00006946 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006947 if (Node->getOpcode() == ISD::ADD) {
6948 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6949 HiOps[2] = Lo.getValue(1);
6950 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6951 } else {
6952 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6953 HiOps[2] = Lo.getValue(1);
6954 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6955 }
6956 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006957 } else {
Andrew Lenharth5e814462008-10-07 14:15:42 +00006958 if (Node->getOpcode() == ISD::ADD) {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00006959 Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
6960 Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
Duncan Sands4a361272009-01-01 15:52:00 +00006961 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(NVT),
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006962 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006963 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6964 DAG.getConstant(1, NVT),
6965 DAG.getConstant(0, NVT));
Duncan Sands4a361272009-01-01 15:52:00 +00006966 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(NVT),
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006967 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006968 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6969 DAG.getConstant(1, NVT),
6970 Carry1);
6971 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6972 } else {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00006973 Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
6974 Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006975 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6976 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6977 DAG.getConstant(1, NVT),
6978 DAG.getConstant(0, NVT));
6979 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6980 }
6981 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006982 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006983 }
6984
6985 case ISD::ADDC:
6986 case ISD::SUBC: {
6987 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006988 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006989 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6990 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6991 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006992 SDValue LoOps[2] = { LHSL, RHSL };
6993 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006994
6995 if (Node->getOpcode() == ISD::ADDC) {
6996 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6997 HiOps[2] = Lo.getValue(1);
6998 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6999 } else {
7000 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
7001 HiOps[2] = Lo.getValue(1);
7002 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
7003 }
7004 // Remember that we legalized the flag.
7005 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7006 break;
7007 }
7008 case ISD::ADDE:
7009 case ISD::SUBE: {
7010 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00007011 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007012 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7013 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7014 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00007015 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
7016 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007017
7018 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
7019 HiOps[2] = Lo.getValue(1);
7020 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
7021
7022 // Remember that we legalized the flag.
7023 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7024 break;
7025 }
7026 case ISD::MUL: {
7027 // If the target wants to custom expand this, let them.
7028 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007029 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00007030 if (New.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007031 ExpandOp(New, Lo, Hi);
7032 break;
7033 }
7034 }
7035
7036 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
7037 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00007038 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
7039 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
7040 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007041 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007042 ExpandOp(Node->getOperand(0), LL, LH);
7043 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman07961cd2008-02-25 21:11:39 +00007044 unsigned OuterBitSize = Op.getValueSizeInBits();
7045 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman5a199552007-10-08 18:33:35 +00007046 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
7047 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman2594d942008-03-10 20:42:19 +00007048 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
7049 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
7050 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman5a199552007-10-08 18:33:35 +00007051 // The inputs are both zero-extended.
7052 if (HasUMUL_LOHI) {
7053 // We can emit a umul_lohi.
7054 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00007055 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00007056 break;
7057 }
7058 if (HasMULHU) {
7059 // We can emit a mulhu+mul.
7060 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7061 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7062 break;
7063 }
Dan Gohman5a199552007-10-08 18:33:35 +00007064 }
Dan Gohman07961cd2008-02-25 21:11:39 +00007065 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman5a199552007-10-08 18:33:35 +00007066 // The input values are both sign-extended.
7067 if (HasSMUL_LOHI) {
7068 // We can emit a smul_lohi.
7069 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00007070 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00007071 break;
7072 }
7073 if (HasMULHS) {
7074 // We can emit a mulhs+mul.
7075 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7076 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
7077 break;
7078 }
7079 }
7080 if (HasUMUL_LOHI) {
7081 // Lo,Hi = umul LHS, RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00007082 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman5a199552007-10-08 18:33:35 +00007083 DAG.getVTList(NVT, NVT), LL, RL);
7084 Lo = UMulLOHI;
7085 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007086 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7087 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7088 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7089 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7090 break;
7091 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00007092 if (HasMULHU) {
7093 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7094 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7095 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7096 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7097 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7098 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7099 break;
7100 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007101 }
7102
Dan Gohman5a199552007-10-08 18:33:35 +00007103 // If nothing else, we can make a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007104 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007105 break;
7106 }
7107 case ISD::SDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007108 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007109 break;
7110 case ISD::UDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007111 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007112 break;
7113 case ISD::SREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007114 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007115 break;
7116 case ISD::UREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007117 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007118 break;
7119
7120 case ISD::FADD:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007121 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7122 RTLIB::ADD_F64,
7123 RTLIB::ADD_F80,
7124 RTLIB::ADD_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007125 Node, false, Hi);
7126 break;
7127 case ISD::FSUB:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007128 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7129 RTLIB::SUB_F64,
7130 RTLIB::SUB_F80,
7131 RTLIB::SUB_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007132 Node, false, Hi);
7133 break;
7134 case ISD::FMUL:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007135 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7136 RTLIB::MUL_F64,
7137 RTLIB::MUL_F80,
7138 RTLIB::MUL_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007139 Node, false, Hi);
7140 break;
7141 case ISD::FDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007142 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7143 RTLIB::DIV_F64,
7144 RTLIB::DIV_F80,
7145 RTLIB::DIV_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007146 Node, false, Hi);
7147 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007148 case ISD::FP_EXTEND: {
Dale Johannesen4c14d512007-10-12 01:37:08 +00007149 if (VT == MVT::ppcf128) {
7150 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7151 Node->getOperand(0).getValueType()==MVT::f64);
7152 const uint64_t zero = 0;
7153 if (Node->getOperand(0).getValueType()==MVT::f32)
7154 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
7155 else
7156 Hi = Node->getOperand(0);
7157 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7158 break;
7159 }
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007160 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7161 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7162 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007163 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007164 }
7165 case ISD::FP_ROUND: {
7166 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7167 VT);
7168 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7169 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007170 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007171 }
Evan Cheng5316b392008-09-09 23:02:14 +00007172 case ISD::FSQRT:
7173 case ISD::FSIN:
7174 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007175 case ISD::FLOG:
7176 case ISD::FLOG2:
7177 case ISD::FLOG10:
7178 case ISD::FEXP:
7179 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00007180 case ISD::FTRUNC:
7181 case ISD::FFLOOR:
7182 case ISD::FCEIL:
7183 case ISD::FRINT:
7184 case ISD::FNEARBYINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00007185 case ISD::FPOW:
7186 case ISD::FPOWI: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007187 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
7188 switch(Node->getOpcode()) {
7189 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00007190 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7191 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007192 break;
7193 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00007194 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7195 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007196 break;
7197 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00007198 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7199 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007200 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00007201 case ISD::FLOG:
7202 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7203 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7204 break;
7205 case ISD::FLOG2:
7206 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7207 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7208 break;
7209 case ISD::FLOG10:
7210 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7211 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7212 break;
7213 case ISD::FEXP:
7214 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7215 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7216 break;
7217 case ISD::FEXP2:
7218 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7219 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7220 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00007221 case ISD::FTRUNC:
7222 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7223 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7224 break;
7225 case ISD::FFLOOR:
7226 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7227 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7228 break;
7229 case ISD::FCEIL:
7230 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7231 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7232 break;
7233 case ISD::FRINT:
7234 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7235 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7236 break;
7237 case ISD::FNEARBYINT:
7238 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7239 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7240 break;
Evan Cheng5316b392008-09-09 23:02:14 +00007241 case ISD::FPOW:
7242 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7243 RTLIB::POW_PPCF128);
7244 break;
7245 case ISD::FPOWI:
7246 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7247 RTLIB::POWI_PPCF128);
7248 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007249 default: assert(0 && "Unreachable!");
7250 }
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007251 Lo = ExpandLibCall(LC, Node, false, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007252 break;
7253 }
7254 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007255 if (VT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007256 SDValue Tmp;
Dale Johannesen5707ef82007-10-12 19:02:17 +00007257 ExpandOp(Node->getOperand(0), Lo, Tmp);
7258 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7259 // lo = hi==fabs(hi) ? lo : -lo;
7260 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7261 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7262 DAG.getCondCode(ISD::SETEQ));
7263 break;
7264 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007265 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007266 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7267 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7268 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7269 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7270 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7271 if (getTypeAction(NVT) == Expand)
7272 ExpandOp(Lo, Lo, Hi);
7273 break;
7274 }
7275 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007276 if (VT == MVT::ppcf128) {
7277 ExpandOp(Node->getOperand(0), Lo, Hi);
7278 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7279 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7280 break;
7281 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007282 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007283 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7284 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7285 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7286 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7287 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7288 if (getTypeAction(NVT) == Expand)
7289 ExpandOp(Lo, Lo, Hi);
7290 break;
7291 }
7292 case ISD::FCOPYSIGN: {
7293 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7294 if (getTypeAction(NVT) == Expand)
7295 ExpandOp(Lo, Lo, Hi);
7296 break;
7297 }
7298 case ISD::SINT_TO_FP:
7299 case ISD::UINT_TO_FP: {
7300 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands92c43912008-06-06 12:08:01 +00007301 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007302
7303 // Promote the operand if needed. Do this before checking for
7304 // ppcf128 so conversions of i16 and i8 work.
7305 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007306 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesen6a779c82008-03-18 17:28:38 +00007307 Tmp = isSigned
7308 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7309 DAG.getValueType(SrcVT))
7310 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greif1c80d112008-08-28 21:40:38 +00007311 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007312 SrcVT = Node->getOperand(0).getValueType();
7313 }
7314
Dan Gohmanec51f642008-03-10 23:03:31 +00007315 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman84d00962008-02-25 21:39:34 +00007316 static const uint64_t zero = 0;
Dale Johannesen4c14d512007-10-12 01:37:08 +00007317 if (isSigned) {
7318 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7319 Node->getOperand(0)));
7320 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7321 } else {
Dan Gohman84d00962008-02-25 21:39:34 +00007322 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen4c14d512007-10-12 01:37:08 +00007323 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7324 Node->getOperand(0)));
7325 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7326 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007327 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00007328 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7329 DAG.getConstant(0, MVT::i32),
7330 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7331 DAG.getConstantFP(
7332 APFloat(APInt(128, 2, TwoE32)),
7333 MVT::ppcf128)),
7334 Hi,
7335 DAG.getCondCode(ISD::SETLT)),
7336 Lo, Hi);
7337 }
7338 break;
7339 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007340 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7341 // si64->ppcf128 done by libcall, below
Dan Gohman84d00962008-02-25 21:39:34 +00007342 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007343 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7344 Lo, Hi);
7345 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7346 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7347 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7348 DAG.getConstant(0, MVT::i64),
7349 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7350 DAG.getConstantFP(
7351 APFloat(APInt(128, 2, TwoE64)),
7352 MVT::ppcf128)),
7353 Hi,
7354 DAG.getCondCode(ISD::SETLT)),
7355 Lo, Hi);
7356 break;
7357 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007358
Dan Gohmanec51f642008-03-10 23:03:31 +00007359 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7360 Node->getOperand(0));
Evan Chenga8740032008-04-01 01:50:16 +00007361 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng4a2f6df2008-04-01 01:51:26 +00007362 // float to i32 etc. can be 'expanded' to a single node.
Evan Chenga8740032008-04-01 01:50:16 +00007363 ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007364 break;
7365 }
7366 }
7367
7368 // Make sure the resultant values have been legalized themselves, unless this
7369 // is a type that requires multi-step expansion.
7370 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7371 Lo = LegalizeOp(Lo);
Gabor Greif1c80d112008-08-28 21:40:38 +00007372 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007373 // Don't legalize the high part if it is expanded to a single node.
7374 Hi = LegalizeOp(Hi);
7375 }
7376
7377 // Remember in a map if the values will be reused later.
Dan Gohman55d19662008-07-07 17:46:23 +00007378 bool isNew =
7379 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007380 assert(isNew && "Value already expanded?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007381 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007382}
7383
7384/// SplitVectorOp - Given an operand of vector type, break it down into
7385/// two smaller values, still of vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00007386void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7387 SDValue &Hi) {
Duncan Sands92c43912008-06-06 12:08:01 +00007388 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007389 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007390 unsigned NumElements = Op.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007391 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00007392
Duncan Sands92c43912008-06-06 12:08:01 +00007393 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman4a365ad2007-11-15 21:15:26 +00007394
7395 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7396 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7397
Duncan Sands92c43912008-06-06 12:08:01 +00007398 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7399 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007400
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007401 // See if we already split it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007402 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007403 = SplitNodes.find(Op);
7404 if (I != SplitNodes.end()) {
7405 Lo = I->second.first;
7406 Hi = I->second.second;
7407 return;
7408 }
7409
7410 switch (Node->getOpcode()) {
7411 default:
7412#ifndef NDEBUG
7413 Node->dump(&DAG);
7414#endif
7415 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00007416 case ISD::UNDEF:
7417 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7418 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7419 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007420 case ISD::BUILD_PAIR:
7421 Lo = Node->getOperand(0);
7422 Hi = Node->getOperand(1);
7423 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007424 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007425 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7426 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007427 unsigned Index = Idx->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007428 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007429 if (Index < NewNumElts_Lo)
7430 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7431 DAG.getIntPtrConstant(Index));
7432 else
7433 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7434 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7435 break;
7436 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007437 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007438 Node->getOperand(1),
7439 Node->getOperand(2));
7440 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007441 break;
7442 }
Chris Lattner587c46d2007-11-19 21:16:54 +00007443 case ISD::VECTOR_SHUFFLE: {
7444 // Build the low part.
Dan Gohman8181bd12008-07-27 21:46:04 +00007445 SDValue Mask = Node->getOperand(2);
7446 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +00007447 MVT PtrVT = TLI.getPointerTy();
Chris Lattner587c46d2007-11-19 21:16:54 +00007448
7449 // Insert all of the elements from the input that are needed. We use
7450 // buildvector of extractelement here because the input vectors will have
7451 // to be legalized, so this makes the code simpler.
7452 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007453 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007454 if (IdxNode.getOpcode() == ISD::UNDEF) {
7455 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7456 continue;
7457 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007458 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007459 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007460 if (Idx >= NumElements) {
7461 InVec = Node->getOperand(1);
7462 Idx -= NumElements;
7463 }
7464 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7465 DAG.getConstant(Idx, PtrVT)));
7466 }
7467 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7468 Ops.clear();
7469
7470 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007471 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007472 if (IdxNode.getOpcode() == ISD::UNDEF) {
7473 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7474 continue;
7475 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007476 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007477 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007478 if (Idx >= NumElements) {
7479 InVec = Node->getOperand(1);
7480 Idx -= NumElements;
7481 }
7482 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7483 DAG.getConstant(Idx, PtrVT)));
7484 }
Mon P Wang2e89b112008-07-25 01:30:26 +00007485 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner587c46d2007-11-19 21:16:54 +00007486 break;
7487 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007488 case ISD::BUILD_VECTOR: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007489 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00007490 Node->op_begin()+NewNumElts_Lo);
7491 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007492
Dan Gohman8181bd12008-07-27 21:46:04 +00007493 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007494 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007495 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007496 break;
7497 }
7498 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00007499 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007500 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7501 if (NewNumSubvectors == 1) {
7502 Lo = Node->getOperand(0);
7503 Hi = Node->getOperand(1);
7504 } else {
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007505 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7506 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007507 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007508
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007509 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007510 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007511 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007512 }
7513 break;
7514 }
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007515 case ISD::EXTRACT_SUBVECTOR: {
7516 SDValue Vec = Op.getOperand(0);
7517 SDValue Idx = Op.getOperand(1);
7518 MVT IdxVT = Idx.getValueType();
7519
7520 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7521 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7522 if (CIdx) {
7523 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7524 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7525 IdxVT));
7526 } else {
7527 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7528 DAG.getConstant(NewNumElts_Lo, IdxVT));
7529 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7530 }
7531 break;
7532 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00007533 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007534 SDValue Cond = Node->getOperand(0);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007535
Dan Gohman8181bd12008-07-27 21:46:04 +00007536 SDValue LL, LH, RL, RH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007537 SplitVectorOp(Node->getOperand(1), LL, LH);
7538 SplitVectorOp(Node->getOperand(2), RL, RH);
7539
Duncan Sands92c43912008-06-06 12:08:01 +00007540 if (Cond.getValueType().isVector()) {
Dan Gohmand5d4c872007-10-17 14:48:28 +00007541 // Handle a vector merge.
Dan Gohman8181bd12008-07-27 21:46:04 +00007542 SDValue CL, CH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007543 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007544 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7545 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007546 } else {
7547 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00007548 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7549 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007550 }
7551 break;
7552 }
Chris Lattnerc7471452008-06-30 02:43:01 +00007553 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007554 SDValue CondLHS = Node->getOperand(0);
7555 SDValue CondRHS = Node->getOperand(1);
7556 SDValue CondCode = Node->getOperand(4);
Chris Lattnerc7471452008-06-30 02:43:01 +00007557
Dan Gohman8181bd12008-07-27 21:46:04 +00007558 SDValue LL, LH, RL, RH;
Chris Lattnerc7471452008-06-30 02:43:01 +00007559 SplitVectorOp(Node->getOperand(2), LL, LH);
7560 SplitVectorOp(Node->getOperand(3), RL, RH);
7561
7562 // Handle a simple select with vector operands.
7563 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7564 LL, RL, CondCode);
7565 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7566 LH, RH, CondCode);
7567 break;
7568 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00007569 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007570 SDValue LL, LH, RL, RH;
Nate Begeman9a1ce152008-05-12 19:40:03 +00007571 SplitVectorOp(Node->getOperand(0), LL, LH);
7572 SplitVectorOp(Node->getOperand(1), RL, RH);
7573 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7574 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7575 break;
7576 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007577 case ISD::ADD:
7578 case ISD::SUB:
7579 case ISD::MUL:
7580 case ISD::FADD:
7581 case ISD::FSUB:
7582 case ISD::FMUL:
7583 case ISD::SDIV:
7584 case ISD::UDIV:
7585 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007586 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007587 case ISD::AND:
7588 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00007589 case ISD::XOR:
7590 case ISD::UREM:
7591 case ISD::SREM:
Mon P Wang26342922008-12-18 20:03:17 +00007592 case ISD::FREM:
7593 case ISD::SHL:
7594 case ISD::SRA:
7595 case ISD::SRL: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007596 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007597 SplitVectorOp(Node->getOperand(0), LL, LH);
7598 SplitVectorOp(Node->getOperand(1), RL, RH);
7599
Nate Begeman4a365ad2007-11-15 21:15:26 +00007600 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7601 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007602 break;
7603 }
Dan Gohman29c3cef2008-08-14 20:04:46 +00007604 case ISD::FP_ROUND:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007605 case ISD::FPOWI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007606 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007607 SplitVectorOp(Node->getOperand(0), L, H);
7608
Nate Begeman4a365ad2007-11-15 21:15:26 +00007609 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7610 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00007611 break;
7612 }
7613 case ISD::CTTZ:
7614 case ISD::CTLZ:
7615 case ISD::CTPOP:
7616 case ISD::FNEG:
7617 case ISD::FABS:
7618 case ISD::FSQRT:
7619 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00007620 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007621 case ISD::FLOG:
7622 case ISD::FLOG2:
7623 case ISD::FLOG10:
7624 case ISD::FEXP:
7625 case ISD::FEXP2:
Nate Begeman78246ca2007-11-17 03:58:34 +00007626 case ISD::FP_TO_SINT:
7627 case ISD::FP_TO_UINT:
7628 case ISD::SINT_TO_FP:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007629 case ISD::UINT_TO_FP:
7630 case ISD::TRUNCATE:
7631 case ISD::ANY_EXTEND:
7632 case ISD::SIGN_EXTEND:
7633 case ISD::ZERO_EXTEND:
7634 case ISD::FP_EXTEND: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007635 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007636 SplitVectorOp(Node->getOperand(0), L, H);
7637
Nate Begeman4a365ad2007-11-15 21:15:26 +00007638 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7639 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00007640 break;
7641 }
Mon P Wang73d31542008-11-10 20:54:11 +00007642 case ISD::CONVERT_RNDSAT: {
7643 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7644 SDValue L, H;
7645 SplitVectorOp(Node->getOperand(0), L, H);
7646 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7647 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7648 SDValue STyOpL = DAG.getValueType(L.getValueType());
7649 SDValue STyOpH = DAG.getValueType(H.getValueType());
7650
7651 SDValue RndOp = Node->getOperand(3);
7652 SDValue SatOp = Node->getOperand(4);
7653
7654 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7655 RndOp, SatOp, CvtCode);
7656 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7657 RndOp, SatOp, CvtCode);
7658 break;
7659 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007660 case ISD::LOAD: {
7661 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007662 SDValue Ch = LD->getChain();
7663 SDValue Ptr = LD->getBasePtr();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007664 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007665 const Value *SV = LD->getSrcValue();
7666 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007667 MVT MemoryVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007668 unsigned Alignment = LD->getAlignment();
7669 bool isVolatile = LD->isVolatile();
7670
Dan Gohman29c3cef2008-08-14 20:04:46 +00007671 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7672 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7673
7674 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7675 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7676 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7677
7678 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7679 NewVT_Lo, Ch, Ptr, Offset,
7680 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7681 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007682 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00007683 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007684 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00007685 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00007686 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7687 NewVT_Hi, Ch, Ptr, Offset,
7688 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007689
7690 // Build a factor node to remember that this load is independent of the
7691 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00007692 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007693 Hi.getValue(1));
7694
7695 // Remember that we legalized the chain.
7696 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
7697 break;
7698 }
7699 case ISD::BIT_CONVERT: {
7700 // We know the result is a vector. The input may be either a vector or a
7701 // scalar value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007702 SDValue InOp = Node->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007703 if (!InOp.getValueType().isVector() ||
7704 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007705 // The input is a scalar or single-element vector.
7706 // Lower to a store/load so that it can be split.
7707 // FIXME: this could be improved probably.
Mon P Wang36b59ac2008-07-15 05:28:34 +00007708 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7709 Op.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00007710 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greif1c80d112008-08-28 21:40:38 +00007711 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007712
Dan Gohman8181bd12008-07-27 21:46:04 +00007713 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00007714 InOp, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007715 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00007716 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007717 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007718 }
7719 // Split the vector and convert each of the pieces now.
7720 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007721 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7722 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007723 break;
7724 }
7725 }
7726
7727 // Remember in a map if the values will be reused later.
7728 bool isNew =
7729 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
7730 assert(isNew && "Value already split?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007731 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007732}
7733
7734
7735/// ScalarizeVectorOp - Given an operand of single-element vector type
7736/// (e.g. v1f32), convert it into the equivalent operation that returns a
7737/// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007738SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00007739 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007740 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007741 MVT NewVT = Op.getValueType().getVectorElementType();
7742 assert(Op.getValueType().getVectorNumElements() == 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007743
7744 // See if we already scalarized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007745 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007746 if (I != ScalarizedNodes.end()) return I->second;
7747
Dan Gohman8181bd12008-07-27 21:46:04 +00007748 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007749 switch (Node->getOpcode()) {
7750 default:
7751#ifndef NDEBUG
7752 Node->dump(&DAG); cerr << "\n";
7753#endif
7754 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7755 case ISD::ADD:
7756 case ISD::FADD:
7757 case ISD::SUB:
7758 case ISD::FSUB:
7759 case ISD::MUL:
7760 case ISD::FMUL:
7761 case ISD::SDIV:
7762 case ISD::UDIV:
7763 case ISD::FDIV:
7764 case ISD::SREM:
7765 case ISD::UREM:
7766 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007767 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007768 case ISD::AND:
7769 case ISD::OR:
7770 case ISD::XOR:
7771 Result = DAG.getNode(Node->getOpcode(),
7772 NewVT,
7773 ScalarizeVectorOp(Node->getOperand(0)),
7774 ScalarizeVectorOp(Node->getOperand(1)));
7775 break;
7776 case ISD::FNEG:
7777 case ISD::FABS:
7778 case ISD::FSQRT:
7779 case ISD::FSIN:
7780 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007781 case ISD::FLOG:
7782 case ISD::FLOG2:
7783 case ISD::FLOG10:
7784 case ISD::FEXP:
7785 case ISD::FEXP2:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007786 case ISD::FP_TO_SINT:
7787 case ISD::FP_TO_UINT:
7788 case ISD::SINT_TO_FP:
7789 case ISD::UINT_TO_FP:
7790 case ISD::SIGN_EXTEND:
7791 case ISD::ZERO_EXTEND:
7792 case ISD::ANY_EXTEND:
7793 case ISD::TRUNCATE:
7794 case ISD::FP_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007795 Result = DAG.getNode(Node->getOpcode(),
7796 NewVT,
7797 ScalarizeVectorOp(Node->getOperand(0)));
7798 break;
Mon P Wang73d31542008-11-10 20:54:11 +00007799 case ISD::CONVERT_RNDSAT: {
7800 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7801 Result = DAG.getConvertRndSat(NewVT, Op0,
7802 DAG.getValueType(NewVT),
7803 DAG.getValueType(Op0.getValueType()),
7804 Node->getOperand(3),
7805 Node->getOperand(4),
7806 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7807 break;
7808 }
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007809 case ISD::FPOWI:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007810 case ISD::FP_ROUND:
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007811 Result = DAG.getNode(Node->getOpcode(),
7812 NewVT,
7813 ScalarizeVectorOp(Node->getOperand(0)),
7814 Node->getOperand(1));
7815 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007816 case ISD::LOAD: {
7817 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007818 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7819 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman29c3cef2008-08-14 20:04:46 +00007820 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007821 const Value *SV = LD->getSrcValue();
7822 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007823 MVT MemoryVT = LD->getMemoryVT();
7824 unsigned Alignment = LD->getAlignment();
7825 bool isVolatile = LD->isVolatile();
7826
7827 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7828 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7829
7830 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7831 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7832 MemoryVT.getVectorElementType(),
7833 isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007834
7835 // Remember that we legalized the chain.
7836 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7837 break;
7838 }
7839 case ISD::BUILD_VECTOR:
7840 Result = Node->getOperand(0);
7841 break;
7842 case ISD::INSERT_VECTOR_ELT:
7843 // Returning the inserted scalar element.
7844 Result = Node->getOperand(1);
7845 break;
7846 case ISD::CONCAT_VECTORS:
7847 assert(Node->getOperand(0).getValueType() == NewVT &&
7848 "Concat of non-legal vectors not yet supported!");
7849 Result = Node->getOperand(0);
7850 break;
7851 case ISD::VECTOR_SHUFFLE: {
7852 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007853 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007854 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007855 Result = ScalarizeVectorOp(Node->getOperand(1));
7856 else
7857 Result = ScalarizeVectorOp(Node->getOperand(0));
7858 break;
7859 }
7860 case ISD::EXTRACT_SUBVECTOR:
Mon P Wang927daf52008-11-06 22:52:21 +00007861 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007862 Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007863 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007864 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007865 SDValue Op0 = Op.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007866 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng2cc16e72008-05-16 17:19:05 +00007867 Op0 = ScalarizeVectorOp(Op0);
7868 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007869 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007870 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007871 case ISD::SELECT:
7872 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
7873 ScalarizeVectorOp(Op.getOperand(1)),
7874 ScalarizeVectorOp(Op.getOperand(2)));
7875 break;
Chris Lattnerc7471452008-06-30 02:43:01 +00007876 case ISD::SELECT_CC:
7877 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7878 Node->getOperand(1),
7879 ScalarizeVectorOp(Op.getOperand(2)),
7880 ScalarizeVectorOp(Op.getOperand(3)),
7881 Node->getOperand(4));
7882 break;
Nate Begeman78ca4f92008-05-12 23:09:43 +00007883 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007884 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7885 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Duncan Sands4a361272009-01-01 15:52:00 +00007886 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0.getValueType()),
7887 Op0, Op1, Op.getOperand(2));
Nate Begeman78ca4f92008-05-12 23:09:43 +00007888 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7889 DAG.getConstant(-1ULL, NewVT),
7890 DAG.getConstant(0ULL, NewVT));
7891 break;
7892 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007893 }
7894
7895 if (TLI.isTypeLegal(NewVT))
7896 Result = LegalizeOp(Result);
7897 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7898 assert(isNew && "Value already scalarized?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007899 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007900 return Result;
7901}
7902
7903
Mon P Wang1448aad2008-10-30 08:01:45 +00007904SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7905 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7906 if (I != WidenNodes.end()) return I->second;
7907
7908 MVT VT = Op.getValueType();
7909 assert(VT.isVector() && "Cannot widen non-vector type!");
7910
7911 SDValue Result;
7912 SDNode *Node = Op.getNode();
7913 MVT EVT = VT.getVectorElementType();
7914
7915 unsigned NumElts = VT.getVectorNumElements();
7916 unsigned NewNumElts = WidenVT.getVectorNumElements();
7917 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7918 assert(NewNumElts < 17);
7919
7920 // When widen is called, it is assumed that it is more efficient to use a
7921 // wide type. The default action is to widen to operation to a wider legal
7922 // vector type and then do the operation if it is legal by calling LegalizeOp
7923 // again. If there is no vector equivalent, we will unroll the operation, do
7924 // it, and rebuild the vector. If most of the operations are vectorizible to
7925 // the legal type, the resulting code will be more efficient. If this is not
7926 // the case, the resulting code will preform badly as we end up generating
7927 // code to pack/unpack the results. It is the function that calls widen
Mon P Wanga5a239f2008-11-06 05:31:54 +00007928 // that is responsible for seeing this doesn't happen.
Mon P Wang1448aad2008-10-30 08:01:45 +00007929 switch (Node->getOpcode()) {
7930 default:
7931#ifndef NDEBUG
7932 Node->dump(&DAG);
7933#endif
7934 assert(0 && "Unexpected operation in WidenVectorOp!");
7935 break;
7936 case ISD::CopyFromReg:
Mon P Wang257e1c72008-11-15 06:05:52 +00007937 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang1448aad2008-10-30 08:01:45 +00007938 case ISD::Constant:
7939 case ISD::ConstantFP:
7940 // To build a vector of these elements, clients should call BuildVector
7941 // and with each element instead of creating a node with a vector type
7942 assert(0 && "Unexpected operation in WidenVectorOp!");
7943 case ISD::VAARG:
7944 // Variable Arguments with vector types doesn't make any sense to me
7945 assert(0 && "Unexpected operation in WidenVectorOp!");
7946 break;
Mon P Wang257e1c72008-11-15 06:05:52 +00007947 case ISD::UNDEF:
7948 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7949 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00007950 case ISD::BUILD_VECTOR: {
7951 // Build a vector with undefined for the new nodes
7952 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7953 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7954 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7955 }
7956 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7957 break;
7958 }
7959 case ISD::INSERT_VECTOR_ELT: {
7960 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7961 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7962 Node->getOperand(1), Node->getOperand(2));
7963 break;
7964 }
7965 case ISD::VECTOR_SHUFFLE: {
7966 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7967 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7968 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7969 // used as permutation array. We build the vector here instead of widening
7970 // because we don't want to legalize and have it turned to something else.
7971 SDValue PermOp = Node->getOperand(2);
7972 SDValueVector NewOps;
7973 MVT PVT = PermOp.getValueType().getVectorElementType();
7974 for (unsigned i = 0; i < NumElts; ++i) {
7975 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7976 NewOps.push_back(PermOp.getOperand(i));
7977 } else {
7978 unsigned Idx =
Mon P Wangec428ad2008-12-13 08:15:14 +00007979 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
Mon P Wang1448aad2008-10-30 08:01:45 +00007980 if (Idx < NumElts) {
7981 NewOps.push_back(PermOp.getOperand(i));
7982 }
7983 else {
7984 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7985 PermOp.getOperand(i).getValueType()));
7986 }
7987 }
7988 }
7989 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7990 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
7991 }
7992
7993 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
7994 MVT::getVectorVT(PVT, NewOps.size()),
7995 &NewOps[0], NewOps.size());
7996
7997 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
7998 break;
7999 }
8000 case ISD::LOAD: {
8001 // If the load widen returns true, we can use a single load for the
8002 // vector. Otherwise, it is returning a token factor for multiple
8003 // loads.
8004 SDValue TFOp;
8005 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
8006 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
8007 else
8008 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
8009 break;
8010 }
8011
8012 case ISD::BIT_CONVERT: {
8013 SDValue Tmp1 = Node->getOperand(0);
8014 // Converts between two different types so we need to determine
8015 // the correct widen type for the input operand.
Mon P Wang26342922008-12-18 20:03:17 +00008016 MVT InVT = Tmp1.getValueType();
8017 unsigned WidenSize = WidenVT.getSizeInBits();
8018 if (InVT.isVector()) {
8019 MVT InEltVT = InVT.getVectorElementType();
8020 unsigned InEltSize = InEltVT.getSizeInBits();
8021 assert(WidenSize % InEltSize == 0 &&
8022 "can not widen bit convert that are not multiple of element type");
8023 MVT NewInWidenVT = MVT::getVectorVT(InEltVT, WidenSize / InEltSize);
8024 Tmp1 = WidenVectorOp(Tmp1, NewInWidenVT);
8025 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
8026 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Tmp1);
8027 } else {
8028 // If the result size is a multiple of the input size, widen the input
8029 // and then convert.
8030 unsigned InSize = InVT.getSizeInBits();
8031 assert(WidenSize % InSize == 0 &&
8032 "can not widen bit convert that are not multiple of element type");
8033 unsigned NewNumElts = WidenSize / InSize;
8034 SmallVector<SDValue, 16> Ops(NewNumElts);
8035 SDValue UndefVal = DAG.getNode(ISD::UNDEF, InVT);
8036 Ops[0] = Tmp1;
8037 for (unsigned i = 1; i < NewNumElts; ++i)
8038 Ops[i] = UndefVal;
Mon P Wang1448aad2008-10-30 08:01:45 +00008039
Mon P Wang26342922008-12-18 20:03:17 +00008040 MVT NewInVT = MVT::getVectorVT(InVT, NewNumElts);
8041 Result = DAG.getNode(ISD::BUILD_VECTOR, NewInVT, &Ops[0], NewNumElts);
8042 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Result);
Mon P Wang1448aad2008-10-30 08:01:45 +00008043 }
8044 break;
8045 }
8046
8047 case ISD::SINT_TO_FP:
8048 case ISD::UINT_TO_FP:
8049 case ISD::FP_TO_SINT:
Mon P Wang26342922008-12-18 20:03:17 +00008050 case ISD::FP_TO_UINT:
8051 case ISD::FP_ROUND: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008052 SDValue Tmp1 = Node->getOperand(0);
8053 // Converts between two different types so we need to determine
8054 // the correct widen type for the input operand.
8055 MVT TVT = Tmp1.getValueType();
8056 assert(TVT.isVector() && "can not widen non vector type");
8057 MVT TEVT = TVT.getVectorElementType();
8058 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
8059 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
8060 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
8061 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang1448aad2008-10-30 08:01:45 +00008062 break;
8063 }
8064
8065 case ISD::FP_EXTEND:
8066 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
8067 case ISD::TRUNCATE:
8068 case ISD::SIGN_EXTEND:
8069 case ISD::ZERO_EXTEND:
8070 case ISD::ANY_EXTEND:
Mon P Wang1448aad2008-10-30 08:01:45 +00008071 case ISD::SIGN_EXTEND_INREG:
8072 case ISD::FABS:
8073 case ISD::FNEG:
8074 case ISD::FSQRT:
8075 case ISD::FSIN:
Mon P Wang257e1c72008-11-15 06:05:52 +00008076 case ISD::FCOS:
8077 case ISD::CTPOP:
8078 case ISD::CTTZ:
8079 case ISD::CTLZ: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008080 // Unary op widening
Mon P Wang26342922008-12-18 20:03:17 +00008081 SDValue Tmp1;
Mon P Wang1448aad2008-10-30 08:01:45 +00008082 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8083 assert(Tmp1.getValueType() == WidenVT);
8084 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang1448aad2008-10-30 08:01:45 +00008085 break;
8086 }
Mon P Wang73d31542008-11-10 20:54:11 +00008087 case ISD::CONVERT_RNDSAT: {
8088 SDValue RndOp = Node->getOperand(3);
8089 SDValue SatOp = Node->getOperand(4);
Mon P Wang73d31542008-11-10 20:54:11 +00008090 SDValue SrcOp = Node->getOperand(0);
8091
8092 // Converts between two different types so we need to determine
8093 // the correct widen type for the input operand.
8094 MVT SVT = SrcOp.getValueType();
8095 assert(SVT.isVector() && "can not widen non vector type");
8096 MVT SEVT = SVT.getVectorElementType();
8097 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8098
8099 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8100 assert(SrcOp.getValueType() == WidenVT);
8101 SDValue DTyOp = DAG.getValueType(WidenVT);
8102 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8103 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8104
8105 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
8106 RndOp, SatOp, CvtCode);
Mon P Wang73d31542008-11-10 20:54:11 +00008107 break;
8108 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008109 case ISD::FPOW:
8110 case ISD::FPOWI:
8111 case ISD::ADD:
8112 case ISD::SUB:
8113 case ISD::MUL:
8114 case ISD::MULHS:
8115 case ISD::MULHU:
8116 case ISD::AND:
8117 case ISD::OR:
8118 case ISD::XOR:
8119 case ISD::FADD:
8120 case ISD::FSUB:
8121 case ISD::FMUL:
8122 case ISD::SDIV:
8123 case ISD::SREM:
8124 case ISD::FDIV:
8125 case ISD::FREM:
8126 case ISD::FCOPYSIGN:
8127 case ISD::UDIV:
8128 case ISD::UREM:
8129 case ISD::BSWAP: {
8130 // Binary op widening
Mon P Wang1448aad2008-10-30 08:01:45 +00008131 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8132 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8133 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8134 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
Mon P Wang1448aad2008-10-30 08:01:45 +00008135 break;
8136 }
8137
8138 case ISD::SHL:
8139 case ISD::SRA:
8140 case ISD::SRL: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008141 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8142 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangd5638262008-12-02 07:35:08 +00008143 SDValue ShOp = Node->getOperand(1);
8144 MVT ShVT = ShOp.getValueType();
8145 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8146 WidenVT.getVectorNumElements());
8147 ShOp = WidenVectorOp(ShOp, NewShVT);
8148 assert(ShOp.getValueType() == NewShVT);
8149 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, ShOp);
Mon P Wang1448aad2008-10-30 08:01:45 +00008150 break;
8151 }
Mon P Wangd5638262008-12-02 07:35:08 +00008152
Mon P Wang1448aad2008-10-30 08:01:45 +00008153 case ISD::EXTRACT_VECTOR_ELT: {
8154 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8155 assert(Tmp1.getValueType() == WidenVT);
8156 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8157 break;
8158 }
8159 case ISD::CONCAT_VECTORS: {
8160 // We concurrently support only widen on a multiple of the incoming vector.
8161 // We could widen on a multiple of the incoming operand if necessary.
8162 unsigned NumConcat = NewNumElts / NumElts;
8163 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Mon P Wangd5638262008-12-02 07:35:08 +00008164 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
Mon P Wang1448aad2008-10-30 08:01:45 +00008165 SmallVector<SDValue, 8> MOps;
8166 MOps.push_back(Op);
8167 for (unsigned i = 1; i != NumConcat; ++i) {
8168 MOps.push_back(UndefVal);
8169 }
8170 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8171 &MOps[0], MOps.size()));
8172 break;
8173 }
8174 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang257e1c72008-11-15 06:05:52 +00008175 SDValue Tmp1 = Node->getOperand(0);
8176 SDValue Idx = Node->getOperand(1);
8177 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8178 if (CIdx && CIdx->getZExtValue() == 0) {
8179 // Since we are access the start of the vector, the incoming
8180 // vector type might be the proper.
8181 MVT Tmp1VT = Tmp1.getValueType();
8182 if (Tmp1VT == WidenVT)
8183 return Tmp1;
8184 else {
8185 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8186 if (Tmp1VTNumElts < NewNumElts)
8187 Result = WidenVectorOp(Tmp1, WidenVT);
8188 else
8189 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8190 }
8191 } else if (NewNumElts % NumElts == 0) {
8192 // Widen the extracted subvector.
8193 unsigned NumConcat = NewNumElts / NumElts;
8194 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8195 SmallVector<SDValue, 8> MOps;
8196 MOps.push_back(Op);
8197 for (unsigned i = 1; i != NumConcat; ++i) {
8198 MOps.push_back(UndefVal);
8199 }
8200 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8201 &MOps[0], MOps.size()));
8202 } else {
8203 assert(0 && "can not widen extract subvector");
8204 // This could be implemented using insert and build vector but I would
8205 // like to see when this happens.
8206 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008207 break;
8208 }
8209
8210 case ISD::SELECT: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008211 // Determine new condition widen type and widen
8212 SDValue Cond1 = Node->getOperand(0);
8213 MVT CondVT = Cond1.getValueType();
8214 assert(CondVT.isVector() && "can not widen non vector type");
8215 MVT CondEVT = CondVT.getVectorElementType();
8216 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8217 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8218 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8219
8220 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8221 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8222 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8223 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang1448aad2008-10-30 08:01:45 +00008224 break;
8225 }
8226
8227 case ISD::SELECT_CC: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008228 // Determine new condition widen type and widen
8229 SDValue Cond1 = Node->getOperand(0);
8230 SDValue Cond2 = Node->getOperand(1);
8231 MVT CondVT = Cond1.getValueType();
8232 assert(CondVT.isVector() && "can not widen non vector type");
8233 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8234 MVT CondEVT = CondVT.getVectorElementType();
8235 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8236 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8237 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8238 assert(Cond1.getValueType() == CondWidenVT &&
8239 Cond2.getValueType() == CondWidenVT && "condition not widen");
8240
8241 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8242 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8243 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8244 "operands not widen");
8245 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8246 Tmp2, Node->getOperand(4));
Mon P Wang1448aad2008-10-30 08:01:45 +00008247 break;
Mon P Wang42ac14e2008-10-30 18:21:52 +00008248 }
8249 case ISD::VSETCC: {
8250 // Determine widen for the operand
8251 SDValue Tmp1 = Node->getOperand(0);
8252 MVT TmpVT = Tmp1.getValueType();
8253 assert(TmpVT.isVector() && "can not widen non vector type");
8254 MVT TmpEVT = TmpVT.getVectorElementType();
8255 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8256 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8257 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
Mon P Wang26342922008-12-18 20:03:17 +00008258 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
Mon P Wang42ac14e2008-10-30 18:21:52 +00008259 Node->getOperand(2));
Mon P Wang1448aad2008-10-30 08:01:45 +00008260 break;
8261 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00008262 case ISD::ATOMIC_CMP_SWAP:
8263 case ISD::ATOMIC_LOAD_ADD:
8264 case ISD::ATOMIC_LOAD_SUB:
8265 case ISD::ATOMIC_LOAD_AND:
8266 case ISD::ATOMIC_LOAD_OR:
8267 case ISD::ATOMIC_LOAD_XOR:
8268 case ISD::ATOMIC_LOAD_NAND:
8269 case ISD::ATOMIC_LOAD_MIN:
8270 case ISD::ATOMIC_LOAD_MAX:
8271 case ISD::ATOMIC_LOAD_UMIN:
8272 case ISD::ATOMIC_LOAD_UMAX:
8273 case ISD::ATOMIC_SWAP: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008274 // For now, we assume that using vectors for these operations don't make
8275 // much sense so we just split it. We return an empty result
8276 SDValue X, Y;
8277 SplitVectorOp(Op, X, Y);
8278 return Result;
8279 break;
8280 }
8281
8282 } // end switch (Node->getOpcode())
8283
8284 assert(Result.getNode() && "Didn't set a result!");
8285 if (Result != Op)
8286 Result = LegalizeOp(Result);
8287
Mon P Wanga5a239f2008-11-06 05:31:54 +00008288 AddWidenedOperand(Op, Result);
Mon P Wang1448aad2008-10-30 08:01:45 +00008289 return Result;
8290}
8291
8292// Utility function to find a legal vector type and its associated element
8293// type from a preferred width and whose vector type must be the same size
8294// as the VVT.
8295// TLI: Target lowering used to determine legal types
8296// Width: Preferred width of element type
8297// VVT: Vector value type whose size we must match.
8298// Returns VecEVT and EVT - the vector type and its associated element type
8299static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8300 MVT& EVT, MVT& VecEVT) {
8301 // We start with the preferred width, make it a power of 2 and see if
8302 // we can find a vector type of that width. If not, we reduce it by
8303 // another power of 2. If we have widen the type, a vector of bytes should
8304 // always be legal.
8305 assert(TLI.isTypeLegal(VVT));
8306 unsigned EWidth = Width + 1;
8307 do {
8308 assert(EWidth > 0);
8309 EWidth = (1 << Log2_32(EWidth-1));
8310 EVT = MVT::getIntegerVT(EWidth);
8311 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8312 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8313 } while (!TLI.isTypeLegal(VecEVT) ||
8314 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8315}
8316
8317SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8318 SDValue Chain,
8319 SDValue BasePtr,
8320 const Value *SV,
8321 int SVOffset,
8322 unsigned Alignment,
8323 bool isVolatile,
8324 unsigned LdWidth,
8325 MVT ResType) {
8326 // We assume that we have good rules to handle loading power of two loads so
8327 // we break down the operations to power of 2 loads. The strategy is to
8328 // load the largest power of 2 that we can easily transform to a legal vector
8329 // and then insert into that vector, and the cast the result into the legal
8330 // vector that we want. This avoids unnecessary stack converts.
8331 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8332 // the load is nonvolatile, we an use a wider load for the value.
8333 // Find a vector length we can load a large chunk
8334 MVT EVT, VecEVT;
8335 unsigned EVTWidth;
8336 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8337 EVTWidth = EVT.getSizeInBits();
8338
8339 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8340 isVolatile, Alignment);
8341 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8342 LdChain.push_back(LdOp.getValue(1));
8343
8344 // Check if we can load the element with one instruction
8345 if (LdWidth == EVTWidth) {
8346 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8347 }
8348
8349 // The vector element order is endianness dependent.
8350 unsigned Idx = 1;
8351 LdWidth -= EVTWidth;
8352 unsigned Offset = 0;
8353
8354 while (LdWidth > 0) {
8355 unsigned Increment = EVTWidth / 8;
8356 Offset += Increment;
8357 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8358 DAG.getIntPtrConstant(Increment));
8359
8360 if (LdWidth < EVTWidth) {
8361 // Our current type we are using is too large, use a smaller size by
8362 // using a smaller power of 2
8363 unsigned oEVTWidth = EVTWidth;
8364 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8365 EVTWidth = EVT.getSizeInBits();
8366 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008367 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang1448aad2008-10-30 08:01:45 +00008368 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8369 }
8370
8371 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8372 SVOffset+Offset, isVolatile,
8373 MinAlign(Alignment, Offset));
8374 LdChain.push_back(LdOp.getValue(1));
8375 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8376 DAG.getIntPtrConstant(Idx++));
8377
8378 LdWidth -= EVTWidth;
8379 }
8380
8381 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8382}
8383
8384bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8385 SDValue& TFOp,
8386 SDValue Op,
8387 MVT NVT) {
8388 // TODO: Add support for ConcatVec and the ability to load many vector
8389 // types (e.g., v4i8). This will not work when a vector register
8390 // to memory mapping is strange (e.g., vector elements are not
8391 // stored in some sequential order).
8392
8393 // It must be true that the widen vector type is bigger than where
8394 // we need to load from.
8395 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8396 MVT LdVT = LD->getMemoryVT();
8397 assert(LdVT.isVector() && NVT.isVector());
8398 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8399
8400 // Load information
8401 SDValue Chain = LD->getChain();
8402 SDValue BasePtr = LD->getBasePtr();
8403 int SVOffset = LD->getSrcValueOffset();
8404 unsigned Alignment = LD->getAlignment();
8405 bool isVolatile = LD->isVolatile();
8406 const Value *SV = LD->getSrcValue();
8407 unsigned int LdWidth = LdVT.getSizeInBits();
8408
8409 // Load value as a large register
8410 SDValueVector LdChain;
8411 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8412 Alignment, isVolatile, LdWidth, NVT);
8413
8414 if (LdChain.size() == 1) {
8415 TFOp = LdChain[0];
8416 return true;
8417 }
8418 else {
8419 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8420 return false;
8421 }
8422}
8423
8424
8425void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8426 SDValue Chain,
8427 SDValue BasePtr,
8428 const Value *SV,
8429 int SVOffset,
8430 unsigned Alignment,
8431 bool isVolatile,
Mon P Wang257e1c72008-11-15 06:05:52 +00008432 SDValue ValOp,
Mon P Wang1448aad2008-10-30 08:01:45 +00008433 unsigned StWidth) {
8434 // Breaks the stores into a series of power of 2 width stores. For any
8435 // width, we convert the vector to the vector of element size that we
8436 // want to store. This avoids requiring a stack convert.
8437
8438 // Find a width of the element type we can store with
8439 MVT VVT = ValOp.getValueType();
8440 MVT EVT, VecEVT;
8441 unsigned EVTWidth;
8442 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8443 EVTWidth = EVT.getSizeInBits();
8444
8445 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8446 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang927daf52008-11-06 22:52:21 +00008447 DAG.getIntPtrConstant(0));
Mon P Wang1448aad2008-10-30 08:01:45 +00008448 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8449 isVolatile, Alignment);
8450 StChain.push_back(StOp);
8451
8452 // Check if we are done
8453 if (StWidth == EVTWidth) {
8454 return;
8455 }
8456
8457 unsigned Idx = 1;
8458 StWidth -= EVTWidth;
8459 unsigned Offset = 0;
8460
8461 while (StWidth > 0) {
8462 unsigned Increment = EVTWidth / 8;
8463 Offset += Increment;
8464 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8465 DAG.getIntPtrConstant(Increment));
8466
8467 if (StWidth < EVTWidth) {
8468 // Our current type we are using is too large, use a smaller size by
8469 // using a smaller power of 2
8470 unsigned oEVTWidth = EVTWidth;
8471 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8472 EVTWidth = EVT.getSizeInBits();
8473 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008474 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang1448aad2008-10-30 08:01:45 +00008475 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8476 }
8477
8478 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang257e1c72008-11-15 06:05:52 +00008479 DAG.getIntPtrConstant(Idx++));
Mon P Wang1448aad2008-10-30 08:01:45 +00008480 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8481 SVOffset + Offset, isVolatile,
8482 MinAlign(Alignment, Offset)));
8483 StWidth -= EVTWidth;
8484 }
8485}
8486
8487
8488SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8489 SDValue Chain,
8490 SDValue BasePtr) {
8491 // TODO: It might be cleaner if we can use SplitVector and have more legal
8492 // vector types that can be stored into memory (e.g., v4xi8 can
8493 // be stored as a word). This will not work when a vector register
8494 // to memory mapping is strange (e.g., vector elements are not
8495 // stored in some sequential order).
8496
8497 MVT StVT = ST->getMemoryVT();
8498 SDValue ValOp = ST->getValue();
8499
8500 // Check if we have widen this node with another value
8501 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8502 if (I != WidenNodes.end())
8503 ValOp = I->second;
8504
8505 MVT VVT = ValOp.getValueType();
8506
8507 // It must be true that we the widen vector type is bigger than where
8508 // we need to store.
8509 assert(StVT.isVector() && VVT.isVector());
8510 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8511 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8512
8513 // Store value
8514 SDValueVector StChain;
8515 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8516 ST->getSrcValueOffset(), ST->getAlignment(),
8517 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8518 if (StChain.size() == 1)
8519 return StChain[0];
8520 else
8521 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8522}
8523
8524
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008525// SelectionDAG::Legalize - This is the entry point for the file.
8526//
Duncan Sandse016a2e2008-12-14 09:43:15 +00008527void SelectionDAG::Legalize(bool TypesNeedLegalizing) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008528 /// run - This is the main entry point to this class.
8529 ///
Duncan Sandse016a2e2008-12-14 09:43:15 +00008530 SelectionDAGLegalize(*this, TypesNeedLegalizing).LegalizeDAG();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008531}
8532