blob: 4400fed8fe0af8abada06e8c5d882fd4ecc62fce [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000019#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga448bc42007-08-16 23:50:06 +000020#include "llvm/Target/TargetFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Target/TargetLowering.h"
22#include "llvm/Target/TargetData.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetOptions.h"
Dan Gohmane8b391e2008-04-12 04:36:06 +000025#include "llvm/Target/TargetSubtarget.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/CallingConv.h"
27#include "llvm/Constants.h"
28#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/Compiler.h"
Duncan Sandsa3691432007-10-28 12:59:45 +000031#include "llvm/Support/MathExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/ADT/DenseMap.h"
33#include "llvm/ADT/SmallVector.h"
34#include "llvm/ADT/SmallPtrSet.h"
35#include <map>
36using namespace llvm;
37
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038//===----------------------------------------------------------------------===//
39/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
40/// hacks on it until the target machine can handle it. This involves
41/// eliminating value sizes the machine cannot handle (promoting small sizes to
42/// large sizes or splitting up large values into small values) as well as
43/// eliminating operations the machine cannot handle.
44///
45/// This code also does a small amount of optimization and recognition of idioms
46/// as part of its processing. For example, if a target does not support a
47/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
48/// will attempt merge setcc and brc instructions into brcc's.
49///
50namespace {
51class VISIBILITY_HIDDEN SelectionDAGLegalize {
52 TargetLowering &TLI;
53 SelectionDAG &DAG;
54
55 // Libcall insertion helpers.
56
57 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
58 /// legalized. We use this to ensure that calls are properly serialized
59 /// against each other, including inserted libcalls.
Dan Gohman8181bd12008-07-27 21:46:04 +000060 SDValue LastCALLSEQ_END;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061
62 /// IsLegalizingCall - This member is used *only* for purposes of providing
63 /// helpful assertions that a libcall isn't created while another call is
64 /// being legalized (which could lead to non-serialized call sequences).
65 bool IsLegalizingCall;
66
67 enum LegalizeAction {
68 Legal, // The target natively supports this operation.
69 Promote, // This operation should be executed in a larger type.
70 Expand // Try to expand this to other ops, otherwise use a libcall.
71 };
72
73 /// ValueTypeActions - This is a bitvector that contains two bits for each
74 /// value type, where the two bits correspond to the LegalizeAction enum.
75 /// This can be queried with "getTypeAction(VT)".
76 TargetLowering::ValueTypeActionImpl ValueTypeActions;
77
78 /// LegalizedNodes - For nodes that are of legal width, and that have more
79 /// than one use, this map indicates what regularized operand to use. This
80 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000081 DenseMap<SDValue, SDValue> LegalizedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082
83 /// PromotedNodes - For nodes that are below legal width, and that have more
84 /// than one use, this map indicates what promoted value to use. This allows
85 /// us to avoid promoting the same thing more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000086 DenseMap<SDValue, SDValue> PromotedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
88 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang1448aad2008-10-30 08:01:45 +000089 /// which operands are the expanded version of the input. This allows
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 /// us to avoid expanding the same node more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000091 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
93 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang1448aad2008-10-30 08:01:45 +000094 /// which operands are the split version of the input. This allows us
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 /// to avoid splitting the same node more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000096 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097
98 /// ScalarizedNodes - For nodes that need to be converted from vector types to
99 /// scalar types, this contains the mapping of ones we have already
100 /// processed to the result.
Dan Gohman8181bd12008-07-27 21:46:04 +0000101 std::map<SDValue, SDValue> ScalarizedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102
Mon P Wanga5a239f2008-11-06 05:31:54 +0000103 /// WidenNodes - For nodes that need to be widened from one vector type to
104 /// another, this contains the mapping of those that we have already widen.
105 /// This allows us to avoid widening more than once.
Mon P Wang1448aad2008-10-30 08:01:45 +0000106 std::map<SDValue, SDValue> WidenNodes;
107
Dan Gohman8181bd12008-07-27 21:46:04 +0000108 void AddLegalizedOperand(SDValue From, SDValue To) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 LegalizedNodes.insert(std::make_pair(From, To));
110 // If someone requests legalization of the new node, return itself.
111 if (From != To)
112 LegalizedNodes.insert(std::make_pair(To, To));
113 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000114 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman55d19662008-07-07 17:46:23 +0000115 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 assert(isNew && "Got into the map somehow?");
Evan Chengcf576fd2008-11-24 07:09:49 +0000117 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 // If someone requests legalization of the new node, return itself.
119 LegalizedNodes.insert(std::make_pair(To, To));
120 }
Mon P Wanga5a239f2008-11-06 05:31:54 +0000121 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang1448aad2008-10-30 08:01:45 +0000122 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
123 assert(isNew && "Got into the map somehow?");
Evan Chengcf576fd2008-11-24 07:09:49 +0000124 isNew = isNew;
Mon P Wang1448aad2008-10-30 08:01:45 +0000125 // If someone requests legalization of the new node, return itself.
126 LegalizedNodes.insert(std::make_pair(To, To));
127 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128
129public:
Dan Gohmane887fdf2008-07-07 18:00:37 +0000130 explicit SelectionDAGLegalize(SelectionDAG &DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131
132 /// getTypeAction - Return how we should legalize values of this type, either
133 /// it is already legal or we need to expand it into multiple registers of
134 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands92c43912008-06-06 12:08:01 +0000135 LegalizeAction getTypeAction(MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
137 }
138
139 /// isTypeLegal - Return true if this type is legal on this target.
140 ///
Duncan Sands92c43912008-06-06 12:08:01 +0000141 bool isTypeLegal(MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 return getTypeAction(VT) == Legal;
143 }
144
145 void LegalizeDAG();
146
147private:
148 /// HandleOp - Legalize, Promote, or Expand the specified operand as
149 /// appropriate for its type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000150 void HandleOp(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151
152 /// LegalizeOp - We know that the specified value has a legal type.
153 /// Recursively ensure that the operands have legal types, then return the
154 /// result.
Dan Gohman8181bd12008-07-27 21:46:04 +0000155 SDValue LegalizeOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156
Dan Gohman6d05cac2007-10-11 23:57:53 +0000157 /// UnrollVectorOp - We know that the given vector has a legal type, however
158 /// the operation it performs is not legal and is an operation that we have
159 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
160 /// operating on each element individually.
Dan Gohman8181bd12008-07-27 21:46:04 +0000161 SDValue UnrollVectorOp(SDValue O);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000162
163 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
164 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
165 /// is necessary to spill the vector being inserted into to memory, perform
166 /// the insert there, and then read the result back.
Dan Gohman8181bd12008-07-27 21:46:04 +0000167 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
168 SDValue Idx);
Dan Gohman6d05cac2007-10-11 23:57:53 +0000169
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 /// PromoteOp - Given an operation that produces a value in an invalid type,
171 /// promote it to compute the value into a larger type. The produced value
172 /// will have the correct bits for the low portion of the register, but no
173 /// guarantee is made about the top bits: it may be zero, sign-extended, or
174 /// garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +0000175 SDValue PromoteOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176
Dan Gohman8181bd12008-07-27 21:46:04 +0000177 /// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman4fc03742008-10-01 15:07:49 +0000179 /// the LegalizedNodes map is filled in for any results that are not expanded,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 /// the ExpandedNodes map is filled in for any results that are expanded, and
181 /// the Lo/Hi values are returned. This applies to integer types and Vector
182 /// types.
Dan Gohman8181bd12008-07-27 21:46:04 +0000183 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184
Mon P Wanga5a239f2008-11-06 05:31:54 +0000185 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
186 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
187 /// for the existing elements but no guarantee is made about the new elements
188 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
189 /// when we have an instruction operating on an illegal vector type and we
190 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang1448aad2008-10-30 08:01:45 +0000191 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
192
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 /// SplitVectorOp - Given an operand of vector type, break it down into
194 /// two smaller values.
Dan Gohman8181bd12008-07-27 21:46:04 +0000195 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196
197 /// ScalarizeVectorOp - Given an operand of single-element vector type
198 /// (e.g. v1f32), convert it into the equivalent operation that returns a
199 /// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +0000200 SDValue ScalarizeVectorOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201
Mon P Wanga5a239f2008-11-06 05:31:54 +0000202 /// Useful 16 element vector type that is used to pass operands for widening.
Mon P Wang1448aad2008-10-30 08:01:45 +0000203 typedef SmallVector<SDValue, 16> SDValueVector;
204
205 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
206 /// the LdChain contains a single load and false if it contains a token
207 /// factor for multiple loads. It takes
208 /// Result: location to return the result
209 /// LdChain: location to return the load chain
210 /// Op: load operation to widen
211 /// NVT: widen vector result type we want for the load
212 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
213 SDValue Op, MVT NVT);
214
215 /// Helper genWidenVectorLoads - Helper function to generate a set of
216 /// loads to load a vector with a resulting wider type. It takes
217 /// LdChain: list of chains for the load we have generated
218 /// Chain: incoming chain for the ld vector
219 /// BasePtr: base pointer to load from
220 /// SV: memory disambiguation source value
221 /// SVOffset: memory disambiugation offset
222 /// Alignment: alignment of the memory
223 /// isVolatile: volatile load
224 /// LdWidth: width of memory that we want to load
225 /// ResType: the wider result result type for the resulting loaded vector
226 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
227 SDValue BasePtr, const Value *SV,
228 int SVOffset, unsigned Alignment,
229 bool isVolatile, unsigned LdWidth,
230 MVT ResType);
231
232 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
233 /// location. It takes
234 /// ST: store node that we want to replace
235 /// Chain: incoming store chain
236 /// BasePtr: base address of where we want to store into
237 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
238 SDValue BasePtr);
239
240 /// Helper genWidenVectorStores - Helper function to generate a set of
241 /// stores to store a widen vector into non widen memory
242 // It takes
243 // StChain: list of chains for the stores we have generated
244 // Chain: incoming chain for the ld vector
245 // BasePtr: base pointer to load from
246 // SV: memory disambiguation source value
247 // SVOffset: memory disambiugation offset
248 // Alignment: alignment of the memory
249 // isVolatile: volatile lod
250 // ValOp: value to store
251 // StWidth: width of memory that we want to store
252 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
253 SDValue BasePtr, const Value *SV,
254 int SVOffset, unsigned Alignment,
255 bool isVolatile, SDValue ValOp,
256 unsigned StWidth);
257
Duncan Sandsd3ace282008-07-21 10:20:31 +0000258 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 /// specified mask and type. Targets can specify exactly which masks they
260 /// support and the code generator is tasked with not creating illegal masks.
261 ///
262 /// Note that this will also return true for shuffles that are promoted to a
263 /// different type.
264 ///
265 /// If this is a legal shuffle, this method returns the (possibly promoted)
266 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman8181bd12008-07-27 21:46:04 +0000267 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268
269 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
270 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
271
Dan Gohman8181bd12008-07-27 21:46:04 +0000272 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Evan Cheng71343822008-10-15 02:05:31 +0000273 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC);
274 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC) {
275 LegalizeSetCCOperands(LHS, RHS, CC);
276 LegalizeSetCCCondCode(VT, LHS, RHS, CC);
277 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278
Dan Gohman8181bd12008-07-27 21:46:04 +0000279 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
280 SDValue &Hi);
281 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282
Dan Gohman8181bd12008-07-27 21:46:04 +0000283 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
284 SDValue ExpandBUILD_VECTOR(SDNode *Node);
285 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dan Gohman29c3cef2008-08-14 20:04:46 +0000286 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op);
Dan Gohman8181bd12008-07-27 21:46:04 +0000287 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
288 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
289 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290
Dan Gohman8181bd12008-07-27 21:46:04 +0000291 SDValue ExpandBSWAP(SDValue Op);
292 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
293 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
294 SDValue &Lo, SDValue &Hi);
295 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
296 SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297
Dan Gohman8181bd12008-07-27 21:46:04 +0000298 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
299 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300};
301}
302
303/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
304/// specified mask and type. Targets can specify exactly which masks they
305/// support and the code generator is tasked with not creating illegal masks.
306///
307/// Note that this will also return true for shuffles that are promoted to a
308/// different type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000309SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
311 default: return 0;
312 case TargetLowering::Legal:
313 case TargetLowering::Custom:
314 break;
315 case TargetLowering::Promote: {
316 // If this is promoted to a different type, convert the shuffle mask and
317 // ask if it is legal in the promoted type!
Duncan Sands92c43912008-06-06 12:08:01 +0000318 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd3ace282008-07-21 10:20:31 +0000319 MVT EltVT = NVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320
321 // If we changed # elements, change the shuffle mask.
322 unsigned NumEltsGrowth =
Duncan Sands92c43912008-06-06 12:08:01 +0000323 NVT.getVectorNumElements() / VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
325 if (NumEltsGrowth > 1) {
326 // Renumber the elements.
Dan Gohman8181bd12008-07-27 21:46:04 +0000327 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000329 SDValue InOp = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
331 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd3ace282008-07-21 10:20:31 +0000332 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000334 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd3ace282008-07-21 10:20:31 +0000335 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 }
337 }
338 }
339 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
340 }
341 VT = NVT;
342 break;
343 }
344 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000345 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346}
347
348SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
349 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
350 ValueTypeActions(TLI.getValueTypeActions()) {
351 assert(MVT::LAST_VALUETYPE <= 32 &&
352 "Too many value types for ValueTypeActions to hold!");
353}
354
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355void SelectionDAGLegalize::LegalizeDAG() {
356 LastCALLSEQ_END = DAG.getEntryNode();
357 IsLegalizingCall = false;
358
359 // The legalize process is inherently a bottom-up recursive process (users
360 // legalize their uses before themselves). Given infinite stack space, we
361 // could just start legalizing on the root and traverse the whole graph. In
362 // practice however, this causes us to run out of stack space on large basic
363 // blocks. To avoid this problem, compute an ordering of the nodes where each
364 // node is only legalized after all of its operands are legalized.
Dan Gohman2d2a7a32008-09-30 18:30:35 +0000365 DAG.AssignTopologicalOrder();
366 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
367 E = prior(DAG.allnodes_end()); I != next(E); ++I)
368 HandleOp(SDValue(I, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369
370 // Finally, it's possible the root changed. Get the new root.
Dan Gohman8181bd12008-07-27 21:46:04 +0000371 SDValue OldRoot = DAG.getRoot();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
373 DAG.setRoot(LegalizedNodes[OldRoot]);
374
375 ExpandedNodes.clear();
376 LegalizedNodes.clear();
377 PromotedNodes.clear();
378 SplitNodes.clear();
379 ScalarizedNodes.clear();
Mon P Wang1448aad2008-10-30 08:01:45 +0000380 WidenNodes.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381
382 // Remove dead nodes now.
383 DAG.RemoveDeadNodes();
384}
385
386
387/// FindCallEndFromCallStart - Given a chained node that is part of a call
388/// sequence, find the CALLSEQ_END node that terminates the call sequence.
389static SDNode *FindCallEndFromCallStart(SDNode *Node) {
390 if (Node->getOpcode() == ISD::CALLSEQ_END)
391 return Node;
392 if (Node->use_empty())
393 return 0; // No CallSeqEnd
394
395 // The chain is usually at the end.
Dan Gohman8181bd12008-07-27 21:46:04 +0000396 SDValue TheChain(Node, Node->getNumValues()-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 if (TheChain.getValueType() != MVT::Other) {
398 // Sometimes it's at the beginning.
Dan Gohman8181bd12008-07-27 21:46:04 +0000399 TheChain = SDValue(Node, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 if (TheChain.getValueType() != MVT::Other) {
401 // Otherwise, hunt for it.
402 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
403 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000404 TheChain = SDValue(Node, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 break;
406 }
407
408 // Otherwise, we walked into a node without a chain.
409 if (TheChain.getValueType() != MVT::Other)
410 return 0;
411 }
412 }
413
414 for (SDNode::use_iterator UI = Node->use_begin(),
415 E = Node->use_end(); UI != E; ++UI) {
416
417 // Make sure to only follow users of our token chain.
Dan Gohman0c97f1d2008-07-27 20:43:25 +0000418 SDNode *User = *UI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
420 if (User->getOperand(i) == TheChain)
421 if (SDNode *Result = FindCallEndFromCallStart(User))
422 return Result;
423 }
424 return 0;
425}
426
427/// FindCallStartFromCallEnd - Given a chained node that is part of a call
428/// sequence, find the CALLSEQ_START node that initiates the call sequence.
429static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
430 assert(Node && "Didn't find callseq_start for a call??");
431 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
432
433 assert(Node->getOperand(0).getValueType() == MVT::Other &&
434 "Node doesn't have a token chain argument!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000435 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436}
437
438/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
439/// see if any uses can reach Dest. If no dest operands can get to dest,
440/// legalize them, legalize ourself, and return false, otherwise, return true.
441///
442/// Keep track of the nodes we fine that actually do lead to Dest in
443/// NodesLeadingTo. This avoids retraversing them exponential number of times.
444///
445bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
446 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
447 if (N == Dest) return true; // N certainly leads to Dest :)
448
449 // If we've already processed this node and it does lead to Dest, there is no
450 // need to reprocess it.
451 if (NodesLeadingTo.count(N)) return true;
452
453 // If the first result of this node has been already legalized, then it cannot
454 // reach N.
455 switch (getTypeAction(N->getValueType(0))) {
456 case Legal:
Dan Gohman8181bd12008-07-27 21:46:04 +0000457 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 break;
459 case Promote:
Dan Gohman8181bd12008-07-27 21:46:04 +0000460 if (PromotedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 break;
462 case Expand:
Dan Gohman8181bd12008-07-27 21:46:04 +0000463 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 break;
465 }
466
467 // Okay, this node has not already been legalized. Check and legalize all
468 // operands. If none lead to Dest, then we can legalize this node.
469 bool OperandsLeadToDest = false;
470 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
471 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greif1c80d112008-08-28 21:40:38 +0000472 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473
474 if (OperandsLeadToDest) {
475 NodesLeadingTo.insert(N);
476 return true;
477 }
478
479 // Okay, this node looks safe, legalize it and return false.
Dan Gohman8181bd12008-07-27 21:46:04 +0000480 HandleOp(SDValue(N, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481 return false;
482}
483
Mon P Wang1448aad2008-10-30 08:01:45 +0000484/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485/// appropriate for its type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000486void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000487 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 switch (getTypeAction(VT)) {
489 default: assert(0 && "Bad type action!");
490 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang1448aad2008-10-30 08:01:45 +0000491 case Promote:
492 if (!VT.isVector()) {
493 (void)PromoteOp(Op);
494 break;
495 }
496 else {
497 // See if we can widen otherwise use Expand to either scalarize or split
498 MVT WidenVT = TLI.getWidenVectorType(VT);
499 if (WidenVT != MVT::Other) {
500 (void) WidenVectorOp(Op, WidenVT);
501 break;
502 }
503 // else fall thru to expand since we can't widen the vector
504 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +0000506 if (!VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 // If this is an illegal scalar, expand it into its two component
508 // pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +0000509 SDValue X, Y;
Chris Lattnerdad577b2007-08-25 01:00:22 +0000510 if (Op.getOpcode() == ISD::TargetConstant)
511 break; // Allow illegal target nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 ExpandOp(Op, X, Y);
Duncan Sands92c43912008-06-06 12:08:01 +0000513 } else if (VT.getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 // If this is an illegal single element vector, convert it to a
515 // scalar operation.
516 (void)ScalarizeVectorOp(Op);
517 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +0000518 // This is an illegal multiple element vector.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 // Split it in half and legalize both parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000520 SDValue X, Y;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 SplitVectorOp(Op, X, Y);
522 }
523 break;
524 }
525}
526
527/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
528/// a load from the constant pool.
Dan Gohman8181bd12008-07-27 21:46:04 +0000529static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 SelectionDAG &DAG, TargetLowering &TLI) {
531 bool Extend = false;
532
533 // If a FP immediate is precise when represented as a float and if the
534 // target can do an extending load from float to double, we put it into
535 // the constant pool as a float, even if it's is statically typed as a
Chris Lattnere718cc52008-03-05 06:46:58 +0000536 // double. This shrinks FP constants and canonicalizes them for targets where
537 // an FP extending load is the same cost as a normal load (such as on the x87
538 // fp stack or PPC FP unit).
Duncan Sands92c43912008-06-06 12:08:01 +0000539 MVT VT = CFP->getValueType(0);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000540 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541 if (!UseCP) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000542 if (VT!=MVT::f64 && VT!=MVT::f32)
543 assert(0 && "Invalid type expansion");
Dale Johannesen49cc7ce2008-10-09 18:53:47 +0000544 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Cheng354be062008-03-04 08:05:30 +0000545 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 }
547
Duncan Sands92c43912008-06-06 12:08:01 +0000548 MVT OrigVT = VT;
549 MVT SVT = VT;
Evan Cheng354be062008-03-04 08:05:30 +0000550 while (SVT != MVT::f32) {
Duncan Sands92c43912008-06-06 12:08:01 +0000551 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Cheng354be062008-03-04 08:05:30 +0000552 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
553 // Only do this if the target has a native EXTLOAD instruction from
554 // smaller type.
Evan Cheng08c171a2008-10-14 21:26:46 +0000555 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattnere718cc52008-03-05 06:46:58 +0000556 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000557 const Type *SType = SVT.getTypeForMVT();
Evan Cheng354be062008-03-04 08:05:30 +0000558 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
559 VT = SVT;
560 Extend = true;
561 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 }
563
Dan Gohman8181bd12008-07-27 21:46:04 +0000564 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +0000565 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Cheng354be062008-03-04 08:05:30 +0000566 if (Extend)
567 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohmanfb020b62008-02-07 18:41:25 +0000568 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman04637d12008-09-16 22:05:41 +0000569 0, VT, false, Alignment);
Evan Cheng354be062008-03-04 08:05:30 +0000570 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +0000571 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572}
573
574
575/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
576/// operations.
577static
Dan Gohman8181bd12008-07-27 21:46:04 +0000578SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
579 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +0000580 MVT VT = Node->getValueType(0);
581 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
583 "fcopysign expansion only supported for f32 and f64");
Duncan Sands92c43912008-06-06 12:08:01 +0000584 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585
586 // First get the sign bit of second operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000587 SDValue Mask1 = (SrcVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
589 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
590 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000591 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
593 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands92c43912008-06-06 12:08:01 +0000594 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 if (SizeDiff > 0) {
596 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
597 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
598 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnere6fa1452008-07-10 23:46:13 +0000599 } else if (SizeDiff < 0) {
600 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
601 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
602 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
603 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604
605 // Clear the sign bit of first operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000606 SDValue Mask2 = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
608 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
609 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman8181bd12008-07-27 21:46:04 +0000610 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
612
613 // Or the value with the sign bit.
614 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
615 return Result;
616}
617
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000618/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
619static
Dan Gohman8181bd12008-07-27 21:46:04 +0000620SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
621 TargetLowering &TLI) {
622 SDValue Chain = ST->getChain();
623 SDValue Ptr = ST->getBasePtr();
624 SDValue Val = ST->getValue();
Duncan Sands92c43912008-06-06 12:08:01 +0000625 MVT VT = Val.getValueType();
Dale Johannesen08275382007-09-08 19:29:23 +0000626 int Alignment = ST->getAlignment();
627 int SVOffset = ST->getSrcValueOffset();
Duncan Sands92c43912008-06-06 12:08:01 +0000628 if (ST->getMemoryVT().isFloatingPoint() ||
629 ST->getMemoryVT().isVector()) {
Dale Johannesen08275382007-09-08 19:29:23 +0000630 // Expand to a bitconvert of the value to the integer type of the
631 // same size, then a (misaligned) int store.
Duncan Sands92c43912008-06-06 12:08:01 +0000632 MVT intVT;
633 if (VT.is128BitVector() || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesendc0ee192008-02-27 22:36:00 +0000634 intVT = MVT::i128;
Duncan Sands92c43912008-06-06 12:08:01 +0000635 else if (VT.is64BitVector() || VT==MVT::f64)
Dale Johannesen08275382007-09-08 19:29:23 +0000636 intVT = MVT::i64;
637 else if (VT==MVT::f32)
638 intVT = MVT::i32;
639 else
Dale Johannesenb1d1ab92008-02-28 18:36:51 +0000640 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen08275382007-09-08 19:29:23 +0000641
Dan Gohman8181bd12008-07-27 21:46:04 +0000642 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
Dale Johannesen08275382007-09-08 19:29:23 +0000643 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
644 SVOffset, ST->isVolatile(), Alignment);
645 }
Duncan Sands92c43912008-06-06 12:08:01 +0000646 assert(ST->getMemoryVT().isInteger() &&
647 !ST->getMemoryVT().isVector() &&
Dale Johannesen08275382007-09-08 19:29:23 +0000648 "Unaligned store of unknown type.");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000649 // Get the half-size VT
Duncan Sands92c43912008-06-06 12:08:01 +0000650 MVT NewStoredVT =
651 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
652 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000653 int IncrementSize = NumBits / 8;
654
655 // Divide the stored value in two parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000656 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
657 SDValue Lo = Val;
658 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000659
660 // Store the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000661 SDValue Store1, Store2;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000662 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
663 ST->getSrcValue(), SVOffset, NewStoredVT,
664 ST->isVolatile(), Alignment);
665 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
666 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsa3691432007-10-28 12:59:45 +0000667 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000668 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
669 ST->getSrcValue(), SVOffset + IncrementSize,
670 NewStoredVT, ST->isVolatile(), Alignment);
671
672 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
673}
674
675/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
676static
Dan Gohman8181bd12008-07-27 21:46:04 +0000677SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
678 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000679 int SVOffset = LD->getSrcValueOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +0000680 SDValue Chain = LD->getChain();
681 SDValue Ptr = LD->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +0000682 MVT VT = LD->getValueType(0);
683 MVT LoadedVT = LD->getMemoryVT();
684 if (VT.isFloatingPoint() || VT.isVector()) {
Dale Johannesen08275382007-09-08 19:29:23 +0000685 // Expand to a (misaligned) integer load of the same size,
Dale Johannesendc0ee192008-02-27 22:36:00 +0000686 // then bitconvert to floating point or vector.
Duncan Sands92c43912008-06-06 12:08:01 +0000687 MVT intVT;
688 if (LoadedVT.is128BitVector() ||
Dale Johannesenf8c1e852008-03-01 03:40:57 +0000689 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesendc0ee192008-02-27 22:36:00 +0000690 intVT = MVT::i128;
Duncan Sands92c43912008-06-06 12:08:01 +0000691 else if (LoadedVT.is64BitVector() || LoadedVT == MVT::f64)
Dale Johannesen08275382007-09-08 19:29:23 +0000692 intVT = MVT::i64;
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000693 else if (LoadedVT == MVT::f32)
Dale Johannesen08275382007-09-08 19:29:23 +0000694 intVT = MVT::i32;
695 else
Dale Johannesendc0ee192008-02-27 22:36:00 +0000696 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen08275382007-09-08 19:29:23 +0000697
Dan Gohman8181bd12008-07-27 21:46:04 +0000698 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
Dale Johannesen08275382007-09-08 19:29:23 +0000699 SVOffset, LD->isVolatile(),
700 LD->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +0000701 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Duncan Sands92c43912008-06-06 12:08:01 +0000702 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesen08275382007-09-08 19:29:23 +0000703 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
704
Dan Gohman8181bd12008-07-27 21:46:04 +0000705 SDValue Ops[] = { Result, Chain };
Duncan Sands698842f2008-07-02 17:40:58 +0000706 return DAG.getMergeValues(Ops, 2);
Dale Johannesen08275382007-09-08 19:29:23 +0000707 }
Duncan Sands92c43912008-06-06 12:08:01 +0000708 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000709 "Unaligned load of unsupported type.");
710
Dale Johannesendc0ee192008-02-27 22:36:00 +0000711 // Compute the new VT that is half the size of the old one. This is an
712 // integer MVT.
Duncan Sands92c43912008-06-06 12:08:01 +0000713 unsigned NumBits = LoadedVT.getSizeInBits();
714 MVT NewLoadedVT;
715 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000716 NumBits >>= 1;
717
718 unsigned Alignment = LD->getAlignment();
719 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000720 ISD::LoadExtType HiExtType = LD->getExtensionType();
721
722 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
723 if (HiExtType == ISD::NON_EXTLOAD)
724 HiExtType = ISD::ZEXTLOAD;
725
726 // Load the value in two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000727 SDValue Lo, Hi;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000728 if (TLI.isLittleEndian()) {
729 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
730 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
731 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
732 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
733 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
734 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000735 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000736 } else {
737 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
738 NewLoadedVT,LD->isVolatile(), Alignment);
739 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
740 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
741 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
742 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000743 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000744 }
745
746 // aggregate the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000747 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
748 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000749 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
750
Dan Gohman8181bd12008-07-27 21:46:04 +0000751 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000752 Hi.getValue(1));
753
Dan Gohman8181bd12008-07-27 21:46:04 +0000754 SDValue Ops[] = { Result, TF };
Duncan Sands698842f2008-07-02 17:40:58 +0000755 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000756}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757
Dan Gohman6d05cac2007-10-11 23:57:53 +0000758/// UnrollVectorOp - We know that the given vector has a legal type, however
759/// the operation it performs is not legal and is an operation that we have
760/// no way of lowering. "Unroll" the vector, splitting out the scalars and
761/// operating on each element individually.
Dan Gohman8181bd12008-07-27 21:46:04 +0000762SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000763 MVT VT = Op.getValueType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000764 assert(isTypeLegal(VT) &&
765 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000766 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman6d05cac2007-10-11 23:57:53 +0000767 "Can't unroll a vector with multiple results!");
Duncan Sands92c43912008-06-06 12:08:01 +0000768 unsigned NE = VT.getVectorNumElements();
769 MVT EltVT = VT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000770
Dan Gohman8181bd12008-07-27 21:46:04 +0000771 SmallVector<SDValue, 8> Scalars;
772 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman6d05cac2007-10-11 23:57:53 +0000773 for (unsigned i = 0; i != NE; ++i) {
774 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000775 SDValue Operand = Op.getOperand(j);
Duncan Sands92c43912008-06-06 12:08:01 +0000776 MVT OperandVT = Operand.getValueType();
777 if (OperandVT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +0000778 // A vector operand; extract a single element.
Duncan Sands92c43912008-06-06 12:08:01 +0000779 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000780 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
781 OperandEltVT,
782 Operand,
783 DAG.getConstant(i, MVT::i32));
784 } else {
785 // A scalar operand; just use it as is.
786 Operands[j] = Operand;
787 }
788 }
789 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
790 &Operands[0], Operands.size()));
791 }
792
793 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
794}
795
Duncan Sands37a3f472008-01-10 10:28:30 +0000796/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands92c43912008-06-06 12:08:01 +0000797static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands37a3f472008-01-10 10:28:30 +0000798 RTLIB::Libcall Call_F32,
799 RTLIB::Libcall Call_F64,
800 RTLIB::Libcall Call_F80,
801 RTLIB::Libcall Call_PPCF128) {
802 return
803 VT == MVT::f32 ? Call_F32 :
804 VT == MVT::f64 ? Call_F64 :
805 VT == MVT::f80 ? Call_F80 :
806 VT == MVT::ppcf128 ? Call_PPCF128 :
807 RTLIB::UNKNOWN_LIBCALL;
808}
809
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000810/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
811/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
812/// is necessary to spill the vector being inserted into to memory, perform
813/// the insert there, and then read the result back.
Dan Gohman8181bd12008-07-27 21:46:04 +0000814SDValue SelectionDAGLegalize::
815PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
816 SDValue Tmp1 = Vec;
817 SDValue Tmp2 = Val;
818 SDValue Tmp3 = Idx;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000819
820 // If the target doesn't support this, we have to spill the input vector
821 // to a temporary stack slot, update the element, then reload it. This is
822 // badness. We could also load the value into a vector register (either
823 // with a "move to register" or "extload into register" instruction, then
824 // permute it into place, if the idx is a constant and if the idx is
825 // supported by the target.
Duncan Sands92c43912008-06-06 12:08:01 +0000826 MVT VT = Tmp1.getValueType();
827 MVT EltVT = VT.getVectorElementType();
828 MVT IdxVT = Tmp3.getValueType();
829 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +0000830 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000831
Gabor Greif1c80d112008-08-28 21:40:38 +0000832 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000833
834 // Store the vector.
Dan Gohman8181bd12008-07-27 21:46:04 +0000835 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang1448aad2008-10-30 08:01:45 +0000836 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000837
838 // Truncate or zero extend offset to target pointer type.
Duncan Sandsec142ee2008-06-08 20:54:56 +0000839 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000840 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
841 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +0000842 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000843 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman8181bd12008-07-27 21:46:04 +0000844 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000845 // Store the scalar value.
846 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000847 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000848 // Load the updated vector.
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000849 return DAG.getLoad(VT, Ch, StackPtr,
850 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000851}
852
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000853/// LegalizeOp - We know that the specified value has a legal type, and
854/// that its operands are legal. Now ensure that the operation itself
855/// is legal, recursively ensuring that the operands' operations remain
856/// legal.
Dan Gohman8181bd12008-07-27 21:46:04 +0000857SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattnerdad577b2007-08-25 01:00:22 +0000858 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
859 return Op;
860
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861 assert(isTypeLegal(Op.getValueType()) &&
862 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000863 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864
865 // If this operation defines any values that cannot be represented in a
866 // register on this target, make sure to expand or promote them.
867 if (Node->getNumValues() > 1) {
868 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
869 if (getTypeAction(Node->getValueType(i)) != Legal) {
870 HandleOp(Op.getValue(i));
871 assert(LegalizedNodes.count(Op) &&
872 "Handling didn't add legal operands!");
873 return LegalizedNodes[Op];
874 }
875 }
876
877 // Note that LegalizeOp may be reentered even from single-use nodes, which
878 // means that we always must cache transformed nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +0000879 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880 if (I != LegalizedNodes.end()) return I->second;
881
Dan Gohman8181bd12008-07-27 21:46:04 +0000882 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
883 SDValue Result = Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884 bool isCustom = false;
885
886 switch (Node->getOpcode()) {
887 case ISD::FrameIndex:
888 case ISD::EntryToken:
889 case ISD::Register:
890 case ISD::BasicBlock:
891 case ISD::TargetFrameIndex:
892 case ISD::TargetJumpTable:
893 case ISD::TargetConstant:
894 case ISD::TargetConstantFP:
895 case ISD::TargetConstantPool:
896 case ISD::TargetGlobalAddress:
897 case ISD::TargetGlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +0000898 case ISD::TargetExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000899 case ISD::VALUETYPE:
900 case ISD::SRCVALUE:
Dan Gohman12a9c082008-02-06 22:27:42 +0000901 case ISD::MEMOPERAND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902 case ISD::CONDCODE:
Duncan Sandsc93fae32008-03-21 09:14:45 +0000903 case ISD::ARG_FLAGS:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +0000905 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 "This must be legal!");
907 break;
908 default:
909 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
910 // If this is a target node, legalize it by legalizing the operands then
911 // passing it through.
Dan Gohman8181bd12008-07-27 21:46:04 +0000912 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
914 Ops.push_back(LegalizeOp(Node->getOperand(i)));
915
916 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
917
918 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
919 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +0000920 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 }
922 // Otherwise this is an unhandled builtin node. splat.
923#ifndef NDEBUG
924 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
925#endif
926 assert(0 && "Do not know how to legalize this operator!");
927 abort();
928 case ISD::GLOBAL_OFFSET_TABLE:
929 case ISD::GlobalAddress:
930 case ISD::GlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +0000931 case ISD::ExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 case ISD::ConstantPool:
933 case ISD::JumpTable: // Nothing to do.
934 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
935 default: assert(0 && "This action is not supported yet!");
936 case TargetLowering::Custom:
937 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +0000938 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000939 // FALLTHROUGH if the target doesn't want to lower this op after all.
940 case TargetLowering::Legal:
941 break;
942 }
943 break;
944 case ISD::FRAMEADDR:
945 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946 // The only option for these nodes is to custom lower them. If the target
947 // does not custom lower them, then return zero.
948 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +0000949 if (Tmp1.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000950 Result = Tmp1;
951 else
952 Result = DAG.getConstant(0, TLI.getPointerTy());
953 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000954 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands92c43912008-06-06 12:08:01 +0000955 MVT VT = Node->getValueType(0);
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000956 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
957 default: assert(0 && "This action is not supported yet!");
958 case TargetLowering::Custom:
959 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +0000960 if (Result.getNode()) break;
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000961 // Fall Thru
962 case TargetLowering::Legal:
963 Result = DAG.getConstant(0, VT);
964 break;
965 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000966 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000967 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000968 case ISD::EXCEPTIONADDR: {
969 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +0000970 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
972 default: assert(0 && "This action is not supported yet!");
973 case TargetLowering::Expand: {
974 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000975 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976 }
977 break;
978 case TargetLowering::Custom:
979 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +0000980 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000981 // Fall Thru
982 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000983 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands698842f2008-07-02 17:40:58 +0000984 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985 break;
986 }
987 }
988 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000989 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000990
Gabor Greif1c80d112008-08-28 21:40:38 +0000991 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000992 "Cannot return more than two values!");
993
994 // Since we produced two values, make sure to remember that we
995 // legalized both of them.
996 Tmp1 = LegalizeOp(Result);
997 Tmp2 = LegalizeOp(Result.getValue(1));
998 AddLegalizedOperand(Op.getValue(0), Tmp1);
999 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001000 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001001 case ISD::EHSELECTION: {
1002 Tmp1 = LegalizeOp(Node->getOperand(0));
1003 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00001004 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001005 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1006 default: assert(0 && "This action is not supported yet!");
1007 case TargetLowering::Expand: {
1008 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001009 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 }
1011 break;
1012 case TargetLowering::Custom:
1013 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001014 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 // Fall Thru
1016 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001017 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands698842f2008-07-02 17:40:58 +00001018 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001019 break;
1020 }
1021 }
1022 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001023 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001024
Gabor Greif1c80d112008-08-28 21:40:38 +00001025 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001026 "Cannot return more than two values!");
1027
1028 // Since we produced two values, make sure to remember that we
1029 // legalized both of them.
1030 Tmp1 = LegalizeOp(Result);
1031 Tmp2 = LegalizeOp(Result.getValue(1));
1032 AddLegalizedOperand(Op.getValue(0), Tmp1);
1033 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001034 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 case ISD::EH_RETURN: {
Duncan Sands92c43912008-06-06 12:08:01 +00001036 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001037 // The only "good" option for this node is to custom lower it.
1038 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1039 default: assert(0 && "This action is not supported at all!");
1040 case TargetLowering::Custom:
1041 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001042 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001043 // Fall Thru
1044 case TargetLowering::Legal:
1045 // Target does not know, how to lower this, lower to noop
1046 Result = LegalizeOp(Node->getOperand(0));
1047 break;
1048 }
1049 }
1050 break;
1051 case ISD::AssertSext:
1052 case ISD::AssertZext:
1053 Tmp1 = LegalizeOp(Node->getOperand(0));
1054 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1055 break;
1056 case ISD::MERGE_VALUES:
1057 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif46bf5472008-08-26 22:36:50 +00001058 Result = Node->getOperand(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001059 break;
1060 case ISD::CopyFromReg:
1061 Tmp1 = LegalizeOp(Node->getOperand(0));
1062 Result = Op.getValue(0);
1063 if (Node->getNumValues() == 2) {
1064 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1065 } else {
1066 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
1067 if (Node->getNumOperands() == 3) {
1068 Tmp2 = LegalizeOp(Node->getOperand(2));
1069 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1070 } else {
1071 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1072 }
1073 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1074 }
1075 // Since CopyFromReg produces two values, make sure to remember that we
1076 // legalized both of them.
1077 AddLegalizedOperand(Op.getValue(0), Result);
1078 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001079 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001080 case ISD::UNDEF: {
Duncan Sands92c43912008-06-06 12:08:01 +00001081 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001082 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
1083 default: assert(0 && "This action is not supported yet!");
1084 case TargetLowering::Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00001085 if (VT.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086 Result = DAG.getConstant(0, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00001087 else if (VT.isFloatingPoint())
1088 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesen20b76352007-09-26 17:26:49 +00001089 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001090 else
1091 assert(0 && "Unknown value type!");
1092 break;
1093 case TargetLowering::Legal:
1094 break;
1095 }
1096 break;
1097 }
1098
1099 case ISD::INTRINSIC_W_CHAIN:
1100 case ISD::INTRINSIC_WO_CHAIN:
1101 case ISD::INTRINSIC_VOID: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001102 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001103 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1104 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1105 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1106
1107 // Allow the target to custom lower its intrinsics if it wants to.
1108 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1109 TargetLowering::Custom) {
1110 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001111 if (Tmp3.getNode()) Result = Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001112 }
1113
Gabor Greif1c80d112008-08-28 21:40:38 +00001114 if (Result.getNode()->getNumValues() == 1) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001115
1116 // Must have return value and chain result.
Gabor Greif1c80d112008-08-28 21:40:38 +00001117 assert(Result.getNode()->getNumValues() == 2 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001118 "Cannot return more than two values!");
1119
1120 // Since loads produce two values, make sure to remember that we
1121 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001122 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1123 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001124 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125 }
1126
Dan Gohman472d12c2008-06-30 20:59:49 +00001127 case ISD::DBG_STOPPOINT:
1128 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001129 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1130
Dan Gohman472d12c2008-06-30 20:59:49 +00001131 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001132 case TargetLowering::Promote:
1133 default: assert(0 && "This action is not supported yet!");
1134 case TargetLowering::Expand: {
1135 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1136 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001137 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001138
Dan Gohman472d12c2008-06-30 20:59:49 +00001139 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001140 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman472d12c2008-06-30 20:59:49 +00001141 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1142 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143
Dan Gohman472d12c2008-06-30 20:59:49 +00001144 unsigned Line = DSP->getLine();
1145 unsigned Col = DSP->getColumn();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001146
1147 if (useDEBUG_LOC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001148 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Chengd6f57682008-07-08 20:06:39 +00001149 DAG.getConstant(Col, MVT::i32),
1150 DAG.getConstant(SrcFile, MVT::i32) };
1151 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001152 } else {
Evan Cheng69eda822008-02-01 02:05:57 +00001153 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001154 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001155 }
1156 } else {
1157 Result = Tmp1; // chain
1158 }
1159 break;
1160 }
Evan Chengd6f57682008-07-08 20:06:39 +00001161 case TargetLowering::Legal: {
1162 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1163 if (Action == Legal && Tmp1 == Node->getOperand(0))
1164 break;
1165
Dan Gohman8181bd12008-07-27 21:46:04 +00001166 SmallVector<SDValue, 8> Ops;
Evan Chengd6f57682008-07-08 20:06:39 +00001167 Ops.push_back(Tmp1);
1168 if (Action == Legal) {
1169 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1170 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1171 } else {
1172 // Otherwise promote them.
1173 Ops.push_back(PromoteOp(Node->getOperand(1)));
1174 Ops.push_back(PromoteOp(Node->getOperand(2)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001175 }
Evan Chengd6f57682008-07-08 20:06:39 +00001176 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1177 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1178 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001179 break;
1180 }
Evan Chengd6f57682008-07-08 20:06:39 +00001181 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001182 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001183
1184 case ISD::DECLARE:
1185 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1186 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1187 default: assert(0 && "This action is not supported yet!");
1188 case TargetLowering::Legal:
1189 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1190 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1191 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1192 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1193 break;
Chris Lattner203cd052008-02-28 05:53:40 +00001194 case TargetLowering::Expand:
1195 Result = LegalizeOp(Node->getOperand(0));
1196 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001197 }
1198 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001199
1200 case ISD::DEBUG_LOC:
1201 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1202 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1203 default: assert(0 && "This action is not supported yet!");
Evan Chengd6f57682008-07-08 20:06:39 +00001204 case TargetLowering::Legal: {
1205 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001206 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Chengd6f57682008-07-08 20:06:39 +00001207 if (Action == Legal && Tmp1 == Node->getOperand(0))
1208 break;
1209 if (Action == Legal) {
1210 Tmp2 = Node->getOperand(1);
1211 Tmp3 = Node->getOperand(2);
1212 Tmp4 = Node->getOperand(3);
1213 } else {
1214 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1215 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1216 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1217 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001218 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1219 break;
1220 }
Evan Chengd6f57682008-07-08 20:06:39 +00001221 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001222 break;
1223
Dan Gohmanfa607c92008-07-01 00:05:16 +00001224 case ISD::DBG_LABEL:
1225 case ISD::EH_LABEL:
1226 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1227 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001228 default: assert(0 && "This action is not supported yet!");
1229 case TargetLowering::Legal:
1230 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohmanfa607c92008-07-01 00:05:16 +00001231 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001232 break;
1233 case TargetLowering::Expand:
1234 Result = LegalizeOp(Node->getOperand(0));
1235 break;
1236 }
1237 break;
1238
Evan Chengd1d68072008-03-08 00:58:38 +00001239 case ISD::PREFETCH:
1240 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1241 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1242 default: assert(0 && "This action is not supported yet!");
1243 case TargetLowering::Legal:
1244 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1245 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1246 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1247 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1248 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1249 break;
1250 case TargetLowering::Expand:
1251 // It's a noop.
1252 Result = LegalizeOp(Node->getOperand(0));
1253 break;
1254 }
1255 break;
1256
Andrew Lenharth785610d2008-02-16 01:24:58 +00001257 case ISD::MEMBARRIER: {
1258 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001259 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1260 default: assert(0 && "This action is not supported yet!");
1261 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001262 SDValue Ops[6];
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001263 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands3ee041a2008-02-27 08:53:44 +00001264 for (int x = 1; x < 6; ++x) {
1265 Ops[x] = Node->getOperand(x);
1266 if (!isTypeLegal(Ops[x].getValueType()))
1267 Ops[x] = PromoteOp(Ops[x]);
1268 }
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001269 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1270 break;
1271 }
1272 case TargetLowering::Expand:
1273 //There is no libgcc call for this op
1274 Result = Node->getOperand(0); // Noop
1275 break;
1276 }
Andrew Lenharth785610d2008-02-16 01:24:58 +00001277 break;
1278 }
1279
Dale Johannesenbc187662008-08-28 02:44:49 +00001280 case ISD::ATOMIC_CMP_SWAP_8:
1281 case ISD::ATOMIC_CMP_SWAP_16:
1282 case ISD::ATOMIC_CMP_SWAP_32:
1283 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001284 unsigned int num_operands = 4;
1285 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001286 SDValue Ops[4];
Mon P Wang078a62d2008-05-05 19:05:59 +00001287 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001288 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang078a62d2008-05-05 19:05:59 +00001289 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1290
1291 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1292 default: assert(0 && "This action is not supported yet!");
1293 case TargetLowering::Custom:
1294 Result = TLI.LowerOperation(Result, DAG);
1295 break;
1296 case TargetLowering::Legal:
1297 break;
1298 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001299 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1300 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001301 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001302 }
Dale Johannesenbc187662008-08-28 02:44:49 +00001303 case ISD::ATOMIC_LOAD_ADD_8:
1304 case ISD::ATOMIC_LOAD_SUB_8:
1305 case ISD::ATOMIC_LOAD_AND_8:
1306 case ISD::ATOMIC_LOAD_OR_8:
1307 case ISD::ATOMIC_LOAD_XOR_8:
1308 case ISD::ATOMIC_LOAD_NAND_8:
1309 case ISD::ATOMIC_LOAD_MIN_8:
1310 case ISD::ATOMIC_LOAD_MAX_8:
1311 case ISD::ATOMIC_LOAD_UMIN_8:
1312 case ISD::ATOMIC_LOAD_UMAX_8:
1313 case ISD::ATOMIC_SWAP_8:
1314 case ISD::ATOMIC_LOAD_ADD_16:
1315 case ISD::ATOMIC_LOAD_SUB_16:
1316 case ISD::ATOMIC_LOAD_AND_16:
1317 case ISD::ATOMIC_LOAD_OR_16:
1318 case ISD::ATOMIC_LOAD_XOR_16:
1319 case ISD::ATOMIC_LOAD_NAND_16:
1320 case ISD::ATOMIC_LOAD_MIN_16:
1321 case ISD::ATOMIC_LOAD_MAX_16:
1322 case ISD::ATOMIC_LOAD_UMIN_16:
1323 case ISD::ATOMIC_LOAD_UMAX_16:
1324 case ISD::ATOMIC_SWAP_16:
1325 case ISD::ATOMIC_LOAD_ADD_32:
1326 case ISD::ATOMIC_LOAD_SUB_32:
1327 case ISD::ATOMIC_LOAD_AND_32:
1328 case ISD::ATOMIC_LOAD_OR_32:
1329 case ISD::ATOMIC_LOAD_XOR_32:
1330 case ISD::ATOMIC_LOAD_NAND_32:
1331 case ISD::ATOMIC_LOAD_MIN_32:
1332 case ISD::ATOMIC_LOAD_MAX_32:
1333 case ISD::ATOMIC_LOAD_UMIN_32:
1334 case ISD::ATOMIC_LOAD_UMAX_32:
1335 case ISD::ATOMIC_SWAP_32:
1336 case ISD::ATOMIC_LOAD_ADD_64:
1337 case ISD::ATOMIC_LOAD_SUB_64:
1338 case ISD::ATOMIC_LOAD_AND_64:
1339 case ISD::ATOMIC_LOAD_OR_64:
1340 case ISD::ATOMIC_LOAD_XOR_64:
1341 case ISD::ATOMIC_LOAD_NAND_64:
1342 case ISD::ATOMIC_LOAD_MIN_64:
1343 case ISD::ATOMIC_LOAD_MAX_64:
1344 case ISD::ATOMIC_LOAD_UMIN_64:
1345 case ISD::ATOMIC_LOAD_UMAX_64:
1346 case ISD::ATOMIC_SWAP_64: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001347 unsigned int num_operands = 3;
1348 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001349 SDValue Ops[3];
Mon P Wang078a62d2008-05-05 19:05:59 +00001350 for (unsigned int x = 0; x < num_operands; ++x)
1351 Ops[x] = LegalizeOp(Node->getOperand(x));
1352 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sandsac496a12008-07-04 11:47:58 +00001353
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001354 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001355 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001356 case TargetLowering::Custom:
1357 Result = TLI.LowerOperation(Result, DAG);
1358 break;
1359 case TargetLowering::Legal:
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001360 break;
1361 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001362 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1363 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001364 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001365 }
Scott Michelf2e2b702007-08-08 23:23:31 +00001366 case ISD::Constant: {
1367 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1368 unsigned opAction =
1369 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1370
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001371 // We know we don't need to expand constants here, constants only have one
1372 // value and we check that it is fine above.
1373
Scott Michelf2e2b702007-08-08 23:23:31 +00001374 if (opAction == TargetLowering::Custom) {
1375 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001376 if (Tmp1.getNode())
Scott Michelf2e2b702007-08-08 23:23:31 +00001377 Result = Tmp1;
1378 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001380 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001381 case ISD::ConstantFP: {
1382 // Spill FP immediates to the constant pool if the target cannot directly
1383 // codegen them. Targets often have some immediate values that can be
1384 // efficiently generated into an FP register without a load. We explicitly
1385 // leave these constants as ConstantFP nodes for the target to deal with.
1386 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1387
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1389 default: assert(0 && "This action is not supported yet!");
Nate Begemane2ba64f2008-02-14 08:57:00 +00001390 case TargetLowering::Legal:
1391 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001392 case TargetLowering::Custom:
1393 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001394 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001395 Result = Tmp3;
1396 break;
1397 }
1398 // FALLTHROUGH
Nate Begemane2ba64f2008-02-14 08:57:00 +00001399 case TargetLowering::Expand: {
1400 // Check to see if this FP immediate is already legal.
1401 bool isLegal = false;
1402 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1403 E = TLI.legal_fpimm_end(); I != E; ++I) {
1404 if (CFP->isExactlyValue(*I)) {
1405 isLegal = true;
1406 break;
1407 }
1408 }
1409 // If this is a legal constant, turn it into a TargetConstantFP node.
1410 if (isLegal)
1411 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001412 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1413 }
Nate Begemane2ba64f2008-02-14 08:57:00 +00001414 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001415 break;
1416 }
1417 case ISD::TokenFactor:
1418 if (Node->getNumOperands() == 2) {
1419 Tmp1 = LegalizeOp(Node->getOperand(0));
1420 Tmp2 = LegalizeOp(Node->getOperand(1));
1421 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1422 } else if (Node->getNumOperands() == 3) {
1423 Tmp1 = LegalizeOp(Node->getOperand(0));
1424 Tmp2 = LegalizeOp(Node->getOperand(1));
1425 Tmp3 = LegalizeOp(Node->getOperand(2));
1426 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1427 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00001428 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001429 // Legalize the operands.
1430 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1431 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1432 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1433 }
1434 break;
1435
1436 case ISD::FORMAL_ARGUMENTS:
1437 case ISD::CALL:
1438 // The only option for this is to custom lower it.
1439 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001440 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesenac246272008-03-05 19:14:03 +00001441 // A call within a calling sequence must be legalized to something
1442 // other than the normal CALLSEQ_END. Violating this gets Legalize
1443 // into an infinite loop.
1444 assert ((!IsLegalizingCall ||
1445 Node->getOpcode() != ISD::CALL ||
Gabor Greif1c80d112008-08-28 21:40:38 +00001446 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesenac246272008-03-05 19:14:03 +00001447 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001448
1449 // The number of incoming and outgoing values should match; unless the final
1450 // outgoing value is a flag.
Gabor Greif1c80d112008-08-28 21:40:38 +00001451 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1452 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1453 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling22f8deb2007-11-13 00:44:25 +00001454 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001455 "Lowering call/formal_arguments produced unexpected # results!");
1456
1457 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1458 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greif1c80d112008-08-28 21:40:38 +00001459 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1460 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling22f8deb2007-11-13 00:44:25 +00001461 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001462 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00001463 if (Op.getResNo() == i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001464 Tmp2 = Tmp1;
Dan Gohman8181bd12008-07-27 21:46:04 +00001465 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001466 }
1467 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001468 case ISD::EXTRACT_SUBREG: {
1469 Tmp1 = LegalizeOp(Node->getOperand(0));
1470 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1471 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001472 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001473 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1474 }
1475 break;
1476 case ISD::INSERT_SUBREG: {
1477 Tmp1 = LegalizeOp(Node->getOperand(0));
1478 Tmp2 = LegalizeOp(Node->getOperand(1));
1479 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1480 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001481 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001482 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1483 }
1484 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001485 case ISD::BUILD_VECTOR:
1486 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1487 default: assert(0 && "This action is not supported yet!");
1488 case TargetLowering::Custom:
1489 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001490 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491 Result = Tmp3;
1492 break;
1493 }
1494 // FALLTHROUGH
1495 case TargetLowering::Expand:
Gabor Greif1c80d112008-08-28 21:40:38 +00001496 Result = ExpandBUILD_VECTOR(Result.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001497 break;
1498 }
1499 break;
1500 case ISD::INSERT_VECTOR_ELT:
1501 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001503
1504 // The type of the value to insert may not be legal, even though the vector
1505 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1506 // here.
1507 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1508 default: assert(0 && "Cannot expand insert element operand");
1509 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1510 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang1448aad2008-10-30 08:01:45 +00001511 case Expand:
1512 // FIXME: An alternative would be to check to see if the target is not
1513 // going to custom lower this operation, we could bitcast to half elt
1514 // width and perform two inserts at that width, if that is legal.
1515 Tmp2 = Node->getOperand(1);
1516 break;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001517 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1519
1520 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1521 Node->getValueType(0))) {
1522 default: assert(0 && "This action is not supported yet!");
1523 case TargetLowering::Legal:
1524 break;
1525 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001526 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001527 if (Tmp4.getNode()) {
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001528 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001529 break;
1530 }
1531 // FALLTHROUGH
Mon P Wang1448aad2008-10-30 08:01:45 +00001532 case TargetLowering::Promote:
1533 // Fall thru for vector case
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001534 case TargetLowering::Expand: {
1535 // If the insert index is a constant, codegen this as a scalar_to_vector,
1536 // then a shuffle that inserts it into the right position in the vector.
1537 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001538 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1539 // match the element type of the vector being created.
1540 if (Tmp2.getValueType() ==
Duncan Sands92c43912008-06-06 12:08:01 +00001541 Op.getValueType().getVectorElementType()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001542 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001543 Tmp1.getValueType(), Tmp2);
1544
Duncan Sands92c43912008-06-06 12:08:01 +00001545 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1546 MVT ShufMaskVT =
1547 MVT::getIntVectorWithNumElements(NumElts);
1548 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001549
1550 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1551 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1552 // elt 0 of the RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00001553 SmallVector<SDValue, 8> ShufOps;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001554 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001555 if (i != InsertPos->getZExtValue())
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001556 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1557 else
1558 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1559 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001560 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001561 &ShufOps[0], ShufOps.size());
1562
1563 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1564 Tmp1, ScVec, ShufMask);
1565 Result = LegalizeOp(Result);
1566 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001567 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001568 }
Nate Begeman7c9e4b72008-04-25 18:07:40 +00001569 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001570 break;
1571 }
1572 }
1573 break;
1574 case ISD::SCALAR_TO_VECTOR:
1575 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1576 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1577 break;
1578 }
1579
1580 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1581 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1582 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1583 Node->getValueType(0))) {
1584 default: assert(0 && "This action is not supported yet!");
1585 case TargetLowering::Legal:
1586 break;
1587 case TargetLowering::Custom:
1588 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001589 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001590 Result = Tmp3;
1591 break;
1592 }
1593 // FALLTHROUGH
1594 case TargetLowering::Expand:
1595 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1596 break;
1597 }
1598 break;
1599 case ISD::VECTOR_SHUFFLE:
1600 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1601 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1602 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1603
1604 // Allow targets to custom lower the SHUFFLEs they support.
1605 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1606 default: assert(0 && "Unknown operation action!");
1607 case TargetLowering::Legal:
1608 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1609 "vector shuffle should not be created if not legal!");
1610 break;
1611 case TargetLowering::Custom:
1612 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001613 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001614 Result = Tmp3;
1615 break;
1616 }
1617 // FALLTHROUGH
1618 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00001619 MVT VT = Node->getValueType(0);
1620 MVT EltVT = VT.getVectorElementType();
1621 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00001622 SDValue Mask = Node->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001623 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00001624 SmallVector<SDValue,8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001625 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001626 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001627 if (Arg.getOpcode() == ISD::UNDEF) {
1628 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1629 } else {
1630 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001631 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001632 if (Idx < NumElems)
1633 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1634 DAG.getConstant(Idx, PtrVT)));
1635 else
1636 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1637 DAG.getConstant(Idx - NumElems, PtrVT)));
1638 }
1639 }
1640 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1641 break;
1642 }
1643 case TargetLowering::Promote: {
1644 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00001645 MVT OVT = Node->getValueType(0);
1646 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001647
1648 // Cast the two input vectors.
1649 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1650 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1651
1652 // Convert the shuffle mask to the right # elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00001653 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001654 assert(Tmp3.getNode() && "Shuffle not legal?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001655 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1656 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1657 break;
1658 }
1659 }
1660 break;
1661
1662 case ISD::EXTRACT_VECTOR_ELT:
1663 Tmp1 = Node->getOperand(0);
1664 Tmp2 = LegalizeOp(Node->getOperand(1));
1665 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1666 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1667 break;
1668
1669 case ISD::EXTRACT_SUBVECTOR:
1670 Tmp1 = Node->getOperand(0);
1671 Tmp2 = LegalizeOp(Node->getOperand(1));
1672 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1673 Result = ExpandEXTRACT_SUBVECTOR(Result);
1674 break;
1675
Mon P Wang1448aad2008-10-30 08:01:45 +00001676 case ISD::CONCAT_VECTORS: {
1677 // Use extract/insert/build vector for now. We might try to be
1678 // more clever later.
1679 MVT PtrVT = TLI.getPointerTy();
1680 SmallVector<SDValue, 8> Ops;
1681 unsigned NumOperands = Node->getNumOperands();
1682 for (unsigned i=0; i < NumOperands; ++i) {
1683 SDValue SubOp = Node->getOperand(i);
1684 MVT VVT = SubOp.getNode()->getValueType(0);
1685 MVT EltVT = VVT.getVectorElementType();
1686 unsigned NumSubElem = VVT.getVectorNumElements();
1687 for (unsigned j=0; j < NumSubElem; ++j) {
1688 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1689 DAG.getConstant(j, PtrVT)));
1690 }
1691 }
1692 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1693 &Ops[0], Ops.size()));
1694 }
1695
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001696 case ISD::CALLSEQ_START: {
1697 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1698
1699 // Recursively Legalize all of the inputs of the call end that do not lead
1700 // to this call start. This ensures that any libcalls that need be inserted
1701 // are inserted *before* the CALLSEQ_START.
1702 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1703 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greif1c80d112008-08-28 21:40:38 +00001704 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001705 NodesLeadingTo);
1706 }
1707
1708 // Now that we legalized all of the inputs (which may have inserted
1709 // libcalls) create the new CALLSEQ_START node.
1710 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1711
1712 // Merge in the last call, to ensure that this call start after the last
1713 // call ended.
1714 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1715 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1716 Tmp1 = LegalizeOp(Tmp1);
1717 }
1718
1719 // Do not try to legalize the target-specific arguments (#1+).
1720 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001721 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001722 Ops[0] = Tmp1;
1723 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1724 }
1725
1726 // Remember that the CALLSEQ_START is legalized.
1727 AddLegalizedOperand(Op.getValue(0), Result);
1728 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1729 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1730
1731 // Now that the callseq_start and all of the non-call nodes above this call
1732 // sequence have been legalized, legalize the call itself. During this
1733 // process, no libcalls can/will be inserted, guaranteeing that no calls
1734 // can overlap.
1735 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001736 // Note that we are selecting this call!
Dan Gohman8181bd12008-07-27 21:46:04 +00001737 LastCALLSEQ_END = SDValue(CallEnd, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001738 IsLegalizingCall = true;
1739
1740 // Legalize the call, starting from the CALLSEQ_END.
1741 LegalizeOp(LastCALLSEQ_END);
1742 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1743 return Result;
1744 }
1745 case ISD::CALLSEQ_END:
1746 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1747 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greif1c80d112008-08-28 21:40:38 +00001748 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001749 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1750 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001751 assert(I != LegalizedNodes.end() &&
1752 "Legalizing the call start should have legalized this node!");
1753 return I->second;
1754 }
1755
1756 // Otherwise, the call start has been legalized and everything is going
1757 // according to plan. Just legalize ourselves normally here.
1758 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1759 // Do not try to legalize the target-specific arguments (#1+), except for
1760 // an optional flag input.
1761 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1762 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001763 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001764 Ops[0] = Tmp1;
1765 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1766 }
1767 } else {
1768 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1769 if (Tmp1 != Node->getOperand(0) ||
1770 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001771 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001772 Ops[0] = Tmp1;
1773 Ops.back() = Tmp2;
1774 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1775 }
1776 }
1777 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1778 // This finishes up call legalization.
1779 IsLegalizingCall = false;
1780
1781 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001782 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001783 if (Node->getNumValues() == 2)
Dan Gohman8181bd12008-07-27 21:46:04 +00001784 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001785 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001786 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands92c43912008-06-06 12:08:01 +00001787 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001788 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1789 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1790 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1791 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1792
1793 Tmp1 = Result.getValue(0);
1794 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001795 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001796 default: assert(0 && "This action is not supported yet!");
1797 case TargetLowering::Expand: {
1798 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1799 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1800 " not tell us which reg is the stack pointer!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001801 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001802
1803 // Chain the dynamic stack allocation so that it doesn't modify the stack
1804 // pointer when other instructions are using the stack.
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001805 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling22f8deb2007-11-13 00:44:25 +00001806
Dan Gohman8181bd12008-07-27 21:46:04 +00001807 SDValue Size = Tmp2.getOperand(1);
1808 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Chenga448bc42007-08-16 23:50:06 +00001809 Chain = SP.getValue(1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001810 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Chenga448bc42007-08-16 23:50:06 +00001811 unsigned StackAlign =
1812 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1813 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001814 SP = DAG.getNode(ISD::AND, VT, SP,
1815 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001816 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling22f8deb2007-11-13 00:44:25 +00001817 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1818
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001819 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1820 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling22f8deb2007-11-13 00:44:25 +00001821
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001822 Tmp1 = LegalizeOp(Tmp1);
1823 Tmp2 = LegalizeOp(Tmp2);
1824 break;
1825 }
1826 case TargetLowering::Custom:
1827 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001828 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001829 Tmp1 = LegalizeOp(Tmp3);
1830 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1831 }
1832 break;
1833 case TargetLowering::Legal:
1834 break;
1835 }
1836 // Since this op produce two values, make sure to remember that we
1837 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001838 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1839 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001840 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001841 }
1842 case ISD::INLINEASM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001843 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001844 bool Changed = false;
1845 // Legalize all of the operands of the inline asm, in case they are nodes
1846 // that need to be expanded or something. Note we skip the asm string and
1847 // all of the TargetConstant flags.
Dan Gohman8181bd12008-07-27 21:46:04 +00001848 SDValue Op = LegalizeOp(Ops[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001849 Changed = Op != Ops[0];
1850 Ops[0] = Op;
1851
1852 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1853 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001854 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001855 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001856 SDValue Op = LegalizeOp(Ops[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001857 if (Op != Ops[i]) {
1858 Changed = true;
1859 Ops[i] = Op;
1860 }
1861 }
1862 }
1863
1864 if (HasInFlag) {
1865 Op = LegalizeOp(Ops.back());
1866 Changed |= Op != Ops.back();
1867 Ops.back() = Op;
1868 }
1869
1870 if (Changed)
1871 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1872
1873 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman8181bd12008-07-27 21:46:04 +00001874 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1875 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001876 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001877 }
1878 case ISD::BR:
1879 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1880 // Ensure that libcalls are emitted before a branch.
1881 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1882 Tmp1 = LegalizeOp(Tmp1);
1883 LastCALLSEQ_END = DAG.getEntryNode();
1884
1885 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1886 break;
1887 case ISD::BRIND:
1888 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1889 // Ensure that libcalls are emitted before a branch.
1890 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1891 Tmp1 = LegalizeOp(Tmp1);
1892 LastCALLSEQ_END = DAG.getEntryNode();
1893
1894 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1895 default: assert(0 && "Indirect target must be legal type (pointer)!");
1896 case Legal:
1897 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1898 break;
1899 }
1900 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1901 break;
1902 case ISD::BR_JT:
1903 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1904 // Ensure that libcalls are emitted before a branch.
1905 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1906 Tmp1 = LegalizeOp(Tmp1);
1907 LastCALLSEQ_END = DAG.getEntryNode();
1908
1909 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1910 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1911
1912 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1913 default: assert(0 && "This action is not supported yet!");
1914 case TargetLowering::Legal: break;
1915 case TargetLowering::Custom:
1916 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001917 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001918 break;
1919 case TargetLowering::Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001920 SDValue Chain = Result.getOperand(0);
1921 SDValue Table = Result.getOperand(1);
1922 SDValue Index = Result.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001923
Duncan Sands92c43912008-06-06 12:08:01 +00001924 MVT PTy = TLI.getPointerTy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001925 MachineFunction &MF = DAG.getMachineFunction();
1926 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
1927 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman8181bd12008-07-27 21:46:04 +00001928 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001929
Dan Gohman8181bd12008-07-27 21:46:04 +00001930 SDValue LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001931 switch (EntrySize) {
1932 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001933 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001934 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001935 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001936 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001937 }
1938
Evan Cheng6fb06762007-11-09 01:32:10 +00001939 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001940 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1941 // For PIC, the sequence is:
1942 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00001943 // RelocBase can be JumpTable, GOT or some sort of global base.
1944 if (PTy != MVT::i32)
1945 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1946 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1947 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001948 }
Evan Cheng6fb06762007-11-09 01:32:10 +00001949 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001950 }
1951 }
1952 break;
1953 case ISD::BRCOND:
1954 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1955 // Ensure that libcalls are emitted before a return.
1956 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1957 Tmp1 = LegalizeOp(Tmp1);
1958 LastCALLSEQ_END = DAG.getEntryNode();
1959
1960 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1961 case Expand: assert(0 && "It's impossible to expand bools");
1962 case Legal:
1963 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1964 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00001965 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001966 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1967
1968 // The top bits of the promoted condition are not necessarily zero, ensure
1969 // that the value is properly zero extended.
Dan Gohman07961cd2008-02-25 21:11:39 +00001970 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001971 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman07961cd2008-02-25 21:11:39 +00001972 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001973 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1974 break;
1975 }
Dan Gohman07961cd2008-02-25 21:11:39 +00001976 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001977
1978 // Basic block destination (Op#2) is always legal.
1979 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1980
1981 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1982 default: assert(0 && "This action is not supported yet!");
1983 case TargetLowering::Legal: break;
1984 case TargetLowering::Custom:
1985 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001986 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001987 break;
1988 case TargetLowering::Expand:
1989 // Expand brcond's setcc into its constituent parts and create a BR_CC
1990 // Node.
1991 if (Tmp2.getOpcode() == ISD::SETCC) {
1992 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1993 Tmp2.getOperand(0), Tmp2.getOperand(1),
1994 Node->getOperand(2));
1995 } else {
1996 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1997 DAG.getCondCode(ISD::SETNE), Tmp2,
1998 DAG.getConstant(0, Tmp2.getValueType()),
1999 Node->getOperand(2));
2000 }
2001 break;
2002 }
2003 break;
2004 case ISD::BR_CC:
2005 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2006 // Ensure that libcalls are emitted before a branch.
2007 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2008 Tmp1 = LegalizeOp(Tmp1);
2009 Tmp2 = Node->getOperand(2); // LHS
2010 Tmp3 = Node->getOperand(3); // RHS
2011 Tmp4 = Node->getOperand(1); // CC
2012
Dale Johannesen32100b22008-11-07 22:54:33 +00002013 LegalizeSetCC(TLI.getSetCCResultType(Tmp2), Tmp2, Tmp3, Tmp4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002014 LastCALLSEQ_END = DAG.getEntryNode();
2015
Evan Cheng71343822008-10-15 02:05:31 +00002016 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002017 // the LHS is a legal SETCC itself. In this case, we need to compare
2018 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00002019 if (Tmp3.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002020 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2021 Tmp4 = DAG.getCondCode(ISD::SETNE);
2022 }
2023
2024 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2025 Node->getOperand(4));
2026
2027 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2028 default: assert(0 && "Unexpected action for BR_CC!");
2029 case TargetLowering::Legal: break;
2030 case TargetLowering::Custom:
2031 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002032 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002033 break;
2034 }
2035 break;
2036 case ISD::LOAD: {
2037 LoadSDNode *LD = cast<LoadSDNode>(Node);
2038 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2039 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
2040
2041 ISD::LoadExtType ExtType = LD->getExtensionType();
2042 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands92c43912008-06-06 12:08:01 +00002043 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002044 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2045 Tmp3 = Result.getValue(0);
2046 Tmp4 = Result.getValue(1);
2047
2048 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2049 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002050 case TargetLowering::Legal:
2051 // If this is an unaligned load and the target doesn't support it,
2052 // expand it.
2053 if (!TLI.allowsUnalignedMemoryAccesses()) {
2054 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002055 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002056 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002057 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002058 TLI);
2059 Tmp3 = Result.getOperand(0);
2060 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00002061 Tmp3 = LegalizeOp(Tmp3);
2062 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002063 }
2064 }
2065 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002066 case TargetLowering::Custom:
2067 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002068 if (Tmp1.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002069 Tmp3 = LegalizeOp(Tmp1);
2070 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2071 }
2072 break;
2073 case TargetLowering::Promote: {
2074 // Only promote a load of vector type to another.
Duncan Sands92c43912008-06-06 12:08:01 +00002075 assert(VT.isVector() && "Cannot promote this load!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002076 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002077 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002078
2079 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
2080 LD->getSrcValueOffset(),
2081 LD->isVolatile(), LD->getAlignment());
2082 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2083 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2084 break;
2085 }
2086 }
2087 // Since loads produce two values, make sure to remember that we
2088 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002089 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2090 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif46bf5472008-08-26 22:36:50 +00002091 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002092 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00002093 MVT SrcVT = LD->getMemoryVT();
2094 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sands082524c2008-01-23 20:39:46 +00002095 int SVOffset = LD->getSrcValueOffset();
2096 unsigned Alignment = LD->getAlignment();
2097 bool isVolatile = LD->isVolatile();
2098
Duncan Sands92c43912008-06-06 12:08:01 +00002099 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002100 // Some targets pretend to have an i1 loading operation, and actually
2101 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2102 // bits are guaranteed to be zero; it helps the optimizers understand
2103 // that these bits are zero. It is also useful for EXTLOAD, since it
2104 // tells the optimizers that those bits are undefined. It would be
2105 // nice to have an effective generic way of getting these benefits...
2106 // Until such a way is found, don't insist on promoting i1 here.
2107 (SrcVT != MVT::i1 ||
Evan Cheng08c171a2008-10-14 21:26:46 +00002108 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002109 // Promote to a byte-sized load if not loading an integral number of
2110 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands92c43912008-06-06 12:08:01 +00002111 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2112 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002113 SDValue Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002114
2115 // The extra bits are guaranteed to be zero, since we stored them that
2116 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2117
2118 ISD::LoadExtType NewExtType =
2119 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2120
2121 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2122 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2123 NVT, isVolatile, Alignment);
2124
2125 Ch = Result.getValue(1); // The chain.
2126
2127 if (ExtType == ISD::SEXTLOAD)
2128 // Having the top bits zero doesn't help when sign extending.
2129 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2130 Result, DAG.getValueType(SrcVT));
2131 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2132 // All the top bits are guaranteed to be zero - inform the optimizers.
2133 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2134 DAG.getValueType(SrcVT));
2135
2136 Tmp1 = LegalizeOp(Result);
2137 Tmp2 = LegalizeOp(Ch);
2138 } else if (SrcWidth & (SrcWidth - 1)) {
2139 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands92c43912008-06-06 12:08:01 +00002140 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002141 "Unsupported extload!");
2142 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2143 assert(RoundWidth < SrcWidth);
2144 unsigned ExtraWidth = SrcWidth - RoundWidth;
2145 assert(ExtraWidth < RoundWidth);
2146 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2147 "Load size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002148 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2149 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002150 SDValue Lo, Hi, Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002151 unsigned IncrementSize;
2152
2153 if (TLI.isLittleEndian()) {
2154 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2155 // Load the bottom RoundWidth bits.
2156 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2157 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2158 Alignment);
2159
2160 // Load the remaining ExtraWidth bits.
2161 IncrementSize = RoundWidth / 8;
2162 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2163 DAG.getIntPtrConstant(IncrementSize));
2164 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2165 LD->getSrcValue(), SVOffset + IncrementSize,
2166 ExtraVT, isVolatile,
2167 MinAlign(Alignment, IncrementSize));
2168
2169 // Build a factor node to remember that this load is independent of the
2170 // other one.
2171 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2172 Hi.getValue(1));
2173
2174 // Move the top bits to the right place.
2175 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2176 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2177
2178 // Join the hi and lo parts.
2179 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002180 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00002181 // Big endian - avoid unaligned loads.
2182 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2183 // Load the top RoundWidth bits.
2184 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2185 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2186 Alignment);
2187
2188 // Load the remaining ExtraWidth bits.
2189 IncrementSize = RoundWidth / 8;
2190 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2191 DAG.getIntPtrConstant(IncrementSize));
2192 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2193 LD->getSrcValue(), SVOffset + IncrementSize,
2194 ExtraVT, isVolatile,
2195 MinAlign(Alignment, IncrementSize));
2196
2197 // Build a factor node to remember that this load is independent of the
2198 // other one.
2199 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2200 Hi.getValue(1));
2201
2202 // Move the top bits to the right place.
2203 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2204 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2205
2206 // Join the hi and lo parts.
2207 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2208 }
2209
2210 Tmp1 = LegalizeOp(Result);
2211 Tmp2 = LegalizeOp(Ch);
2212 } else {
Evan Cheng08c171a2008-10-14 21:26:46 +00002213 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002214 default: assert(0 && "This action is not supported yet!");
2215 case TargetLowering::Custom:
2216 isCustom = true;
2217 // FALLTHROUGH
2218 case TargetLowering::Legal:
2219 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2220 Tmp1 = Result.getValue(0);
2221 Tmp2 = Result.getValue(1);
2222
2223 if (isCustom) {
2224 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002225 if (Tmp3.getNode()) {
Duncan Sands082524c2008-01-23 20:39:46 +00002226 Tmp1 = LegalizeOp(Tmp3);
2227 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2228 }
2229 } else {
2230 // If this is an unaligned load and the target doesn't support it,
2231 // expand it.
2232 if (!TLI.allowsUnalignedMemoryAccesses()) {
2233 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002234 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sands082524c2008-01-23 20:39:46 +00002235 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002236 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sands082524c2008-01-23 20:39:46 +00002237 TLI);
2238 Tmp1 = Result.getOperand(0);
2239 Tmp2 = Result.getOperand(1);
2240 Tmp1 = LegalizeOp(Tmp1);
2241 Tmp2 = LegalizeOp(Tmp2);
2242 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002243 }
2244 }
Duncan Sands082524c2008-01-23 20:39:46 +00002245 break;
2246 case TargetLowering::Expand:
2247 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2248 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002249 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sands082524c2008-01-23 20:39:46 +00002250 LD->getSrcValueOffset(),
2251 LD->isVolatile(), LD->getAlignment());
2252 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2253 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2254 Tmp2 = LegalizeOp(Load.getValue(1));
2255 break;
2256 }
2257 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2258 // Turn the unsupported load into an EXTLOAD followed by an explicit
2259 // zero/sign extend inreg.
2260 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2261 Tmp1, Tmp2, LD->getSrcValue(),
2262 LD->getSrcValueOffset(), SrcVT,
2263 LD->isVolatile(), LD->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00002264 SDValue ValRes;
Duncan Sands082524c2008-01-23 20:39:46 +00002265 if (ExtType == ISD::SEXTLOAD)
2266 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2267 Result, DAG.getValueType(SrcVT));
2268 else
2269 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2270 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2271 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002272 break;
2273 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002274 }
Duncan Sands082524c2008-01-23 20:39:46 +00002275
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002276 // Since loads produce two values, make sure to remember that we legalized
2277 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002278 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2279 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002280 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002281 }
2282 }
2283 case ISD::EXTRACT_ELEMENT: {
Duncan Sands92c43912008-06-06 12:08:01 +00002284 MVT OpTy = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002285 switch (getTypeAction(OpTy)) {
2286 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2287 case Legal:
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002288 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002289 // 1 -> Hi
2290 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands92c43912008-06-06 12:08:01 +00002291 DAG.getConstant(OpTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002292 TLI.getShiftAmountTy()));
2293 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2294 } else {
2295 // 0 -> Lo
2296 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2297 Node->getOperand(0));
2298 }
2299 break;
2300 case Expand:
2301 // Get both the low and high parts.
2302 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002303 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002304 Result = Tmp2; // 1 -> Hi
2305 else
2306 Result = Tmp1; // 0 -> Lo
2307 break;
2308 }
2309 break;
2310 }
2311
2312 case ISD::CopyToReg:
2313 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2314
2315 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2316 "Register type must be legal!");
2317 // Legalize the incoming value (must be a legal type).
2318 Tmp2 = LegalizeOp(Node->getOperand(2));
2319 if (Node->getNumValues() == 1) {
2320 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2321 } else {
2322 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2323 if (Node->getNumOperands() == 4) {
2324 Tmp3 = LegalizeOp(Node->getOperand(3));
2325 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2326 Tmp3);
2327 } else {
2328 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2329 }
2330
2331 // Since this produces two values, make sure to remember that we legalized
2332 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002333 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2334 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002335 return Result;
2336 }
2337 break;
2338
2339 case ISD::RET:
2340 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2341
2342 // Ensure that libcalls are emitted before a return.
2343 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2344 Tmp1 = LegalizeOp(Tmp1);
2345 LastCALLSEQ_END = DAG.getEntryNode();
2346
2347 switch (Node->getNumOperands()) {
2348 case 3: // ret val
2349 Tmp2 = Node->getOperand(1);
2350 Tmp3 = Node->getOperand(2); // Signness
2351 switch (getTypeAction(Tmp2.getValueType())) {
2352 case Legal:
2353 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2354 break;
2355 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00002356 if (!Tmp2.getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002357 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002358 ExpandOp(Tmp2, Lo, Hi);
2359
2360 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002361 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002362 std::swap(Lo, Hi);
2363
Gabor Greif1c80d112008-08-28 21:40:38 +00002364 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002365 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2366 else
2367 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2368 Result = LegalizeOp(Result);
2369 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +00002370 SDNode *InVal = Tmp2.getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002371 int InIx = Tmp2.getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002372 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2373 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002374
2375 // Figure out if there is a simple type corresponding to this Vector
2376 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002377 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002378 if (TLI.isTypeLegal(TVT)) {
2379 // Turn this into a return of the vector type.
2380 Tmp2 = LegalizeOp(Tmp2);
2381 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2382 } else if (NumElems == 1) {
2383 // Turn this into a return of the scalar type.
2384 Tmp2 = ScalarizeVectorOp(Tmp2);
2385 Tmp2 = LegalizeOp(Tmp2);
2386 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2387
2388 // FIXME: Returns of gcc generic vectors smaller than a legal type
2389 // should be returned in integer registers!
2390
2391 // The scalarized value type may not be legal, e.g. it might require
2392 // promotion or expansion. Relegalize the return.
2393 Result = LegalizeOp(Result);
2394 } else {
2395 // FIXME: Returns of gcc generic vectors larger than a legal vector
2396 // type should be returned by reference!
Dan Gohman8181bd12008-07-27 21:46:04 +00002397 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002398 SplitVectorOp(Tmp2, Lo, Hi);
2399 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2400 Result = LegalizeOp(Result);
2401 }
2402 }
2403 break;
2404 case Promote:
2405 Tmp2 = PromoteOp(Node->getOperand(1));
2406 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2407 Result = LegalizeOp(Result);
2408 break;
2409 }
2410 break;
2411 case 1: // ret void
2412 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2413 break;
2414 default: { // ret <values>
Dan Gohman8181bd12008-07-27 21:46:04 +00002415 SmallVector<SDValue, 8> NewValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 NewValues.push_back(Tmp1);
2417 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2418 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2419 case Legal:
2420 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2421 NewValues.push_back(Node->getOperand(i+1));
2422 break;
2423 case Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002424 SDValue Lo, Hi;
Duncan Sands92c43912008-06-06 12:08:01 +00002425 assert(!Node->getOperand(i).getValueType().isExtended() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002426 "FIXME: TODO: implement returning non-legal vector types!");
2427 ExpandOp(Node->getOperand(i), Lo, Hi);
2428 NewValues.push_back(Lo);
2429 NewValues.push_back(Node->getOperand(i+1));
Gabor Greif1c80d112008-08-28 21:40:38 +00002430 if (Hi.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002431 NewValues.push_back(Hi);
2432 NewValues.push_back(Node->getOperand(i+1));
2433 }
2434 break;
2435 }
2436 case Promote:
2437 assert(0 && "Can't promote multiple return value yet!");
2438 }
2439
2440 if (NewValues.size() == Node->getNumOperands())
2441 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2442 else
2443 Result = DAG.getNode(ISD::RET, MVT::Other,
2444 &NewValues[0], NewValues.size());
2445 break;
2446 }
2447 }
2448
2449 if (Result.getOpcode() == ISD::RET) {
2450 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2451 default: assert(0 && "This action is not supported yet!");
2452 case TargetLowering::Legal: break;
2453 case TargetLowering::Custom:
2454 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002455 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002456 break;
2457 }
2458 }
2459 break;
2460 case ISD::STORE: {
2461 StoreSDNode *ST = cast<StoreSDNode>(Node);
2462 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2463 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2464 int SVOffset = ST->getSrcValueOffset();
2465 unsigned Alignment = ST->getAlignment();
2466 bool isVolatile = ST->isVolatile();
2467
2468 if (!ST->isTruncatingStore()) {
2469 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2470 // FIXME: We shouldn't do this for TargetConstantFP's.
2471 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2472 // to phase ordering between legalized code and the dag combiner. This
2473 // probably means that we need to integrate dag combiner and legalizer
2474 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002475 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002476 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002477 if (CFP->getValueType(0) == MVT::f32 &&
2478 getTypeAction(MVT::i32) == Legal) {
Dan Gohman39509762008-03-11 00:11:06 +00002479 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002480 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen1616e902007-09-11 18:32:33 +00002481 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002482 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2483 SVOffset, isVolatile, Alignment);
2484 break;
2485 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002486 // If this target supports 64-bit registers, do a single 64-bit store.
2487 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002488 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman39509762008-03-11 00:11:06 +00002489 zextOrTrunc(64), MVT::i64);
Chris Lattner19f229a2007-10-15 05:46:06 +00002490 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2491 SVOffset, isVolatile, Alignment);
2492 break;
Duncan Sands2418bec2008-06-13 19:07:40 +00002493 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002494 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2495 // stores. If the target supports neither 32- nor 64-bits, this
2496 // xform is certainly not worth it.
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002497 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman8181bd12008-07-27 21:46:04 +00002498 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2499 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002500 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002501
2502 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2503 SVOffset, isVolatile, Alignment);
2504 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002505 DAG.getIntPtrConstant(4));
Chris Lattner19f229a2007-10-15 05:46:06 +00002506 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002507 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002508
2509 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2510 break;
2511 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002512 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002513 }
2514
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002515 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002516 case Legal: {
2517 Tmp3 = LegalizeOp(ST->getValue());
2518 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2519 ST->getOffset());
2520
Duncan Sands92c43912008-06-06 12:08:01 +00002521 MVT VT = Tmp3.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002522 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2523 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002524 case TargetLowering::Legal:
2525 // If this is an unaligned store and the target doesn't support it,
2526 // expand it.
2527 if (!TLI.allowsUnalignedMemoryAccesses()) {
2528 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002529 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002530 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002531 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002532 TLI);
2533 }
2534 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002535 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 case TargetLowering::Promote:
Duncan Sands92c43912008-06-06 12:08:01 +00002540 assert(VT.isVector() && "Unknown legal promote case!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002541 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2542 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2543 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2544 ST->getSrcValue(), SVOffset, isVolatile,
2545 Alignment);
2546 break;
2547 }
2548 break;
2549 }
2550 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002551 if (!ST->getMemoryVT().isVector()) {
2552 // Truncate the value and store the result.
2553 Tmp3 = PromoteOp(ST->getValue());
2554 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2555 SVOffset, ST->getMemoryVT(),
2556 isVolatile, Alignment);
2557 break;
2558 }
2559 // Fall thru to expand for vector
2560 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002561 unsigned IncrementSize = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002562 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002563
2564 // If this is a vector type, then we have to calculate the increment as
2565 // the product of the element size in bytes, and the number of elements
2566 // in the high half of the vector.
Duncan Sands92c43912008-06-06 12:08:01 +00002567 if (ST->getValue().getValueType().isVector()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002568 SDNode *InVal = ST->getValue().getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002569 int InIx = ST->getValue().getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002570 MVT InVT = InVal->getValueType(InIx);
2571 unsigned NumElems = InVT.getVectorNumElements();
2572 MVT EVT = InVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002573
2574 // Figure out if there is a simple type corresponding to this Vector
2575 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002576 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002577 if (TLI.isTypeLegal(TVT)) {
2578 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002579 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002580 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2581 SVOffset, isVolatile, Alignment);
2582 Result = LegalizeOp(Result);
2583 break;
2584 } else if (NumElems == 1) {
2585 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002586 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002587 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2588 SVOffset, isVolatile, Alignment);
2589 // The scalarized value type may not be legal, e.g. it might require
2590 // promotion or expansion. Relegalize the scalar store.
2591 Result = LegalizeOp(Result);
2592 break;
2593 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +00002594 // Check if we have widen this node with another value
2595 std::map<SDValue, SDValue>::iterator I =
2596 WidenNodes.find(ST->getValue());
2597 if (I != WidenNodes.end()) {
2598 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2599 break;
2600 }
2601 else {
2602 SplitVectorOp(ST->getValue(), Lo, Hi);
2603 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2604 EVT.getSizeInBits()/8;
2605 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002606 }
2607 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002608 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greif1c80d112008-08-28 21:40:38 +00002609 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002610
Richard Pennington73ae9e42008-09-25 16:15:10 +00002611 if (Hi.getNode() && TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002612 std::swap(Lo, Hi);
2613 }
2614
2615 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2616 SVOffset, isVolatile, Alignment);
2617
Gabor Greif1c80d112008-08-28 21:40:38 +00002618 if (Hi.getNode() == NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002619 // Must be int <-> float one-to-one expansion.
2620 Result = Lo;
2621 break;
2622 }
2623
2624 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002625 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002626 assert(isTypeLegal(Tmp2.getValueType()) &&
2627 "Pointers must be legal!");
2628 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002629 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002630 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2631 SVOffset, isVolatile, Alignment);
2632 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2633 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00002634 } // case Expand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002635 }
2636 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002637 switch (getTypeAction(ST->getValue().getValueType())) {
2638 case Legal:
2639 Tmp3 = LegalizeOp(ST->getValue());
2640 break;
2641 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002642 if (!ST->getValue().getValueType().isVector()) {
2643 // We can promote the value, the truncstore will still take care of it.
2644 Tmp3 = PromoteOp(ST->getValue());
2645 break;
2646 }
2647 // Vector case falls through to expand
Chris Lattner3bc08502008-01-17 19:59:44 +00002648 case Expand:
2649 // Just store the low part. This may become a non-trunc store, so make
2650 // sure to use getTruncStore, not UpdateNodeOperands below.
2651 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2652 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2653 SVOffset, MVT::i8, isVolatile, Alignment);
2654 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002655
Duncan Sands92c43912008-06-06 12:08:01 +00002656 MVT StVT = ST->getMemoryVT();
2657 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands40676662008-01-22 07:17:34 +00002658
Duncan Sands92c43912008-06-06 12:08:01 +00002659 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands40676662008-01-22 07:17:34 +00002660 // Promote to a byte-sized store with upper bits zero if not
2661 // storing an integral number of bytes. For example, promote
2662 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands92c43912008-06-06 12:08:01 +00002663 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands40676662008-01-22 07:17:34 +00002664 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2665 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2666 SVOffset, NVT, isVolatile, Alignment);
2667 } else if (StWidth & (StWidth - 1)) {
2668 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands92c43912008-06-06 12:08:01 +00002669 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands40676662008-01-22 07:17:34 +00002670 "Unsupported truncstore!");
2671 unsigned RoundWidth = 1 << Log2_32(StWidth);
2672 assert(RoundWidth < StWidth);
2673 unsigned ExtraWidth = StWidth - RoundWidth;
2674 assert(ExtraWidth < RoundWidth);
2675 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2676 "Store size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002677 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2678 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002679 SDValue Lo, Hi;
Duncan Sands40676662008-01-22 07:17:34 +00002680 unsigned IncrementSize;
2681
2682 if (TLI.isLittleEndian()) {
2683 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2684 // Store the bottom RoundWidth bits.
2685 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2686 SVOffset, RoundVT,
2687 isVolatile, Alignment);
2688
2689 // Store the remaining ExtraWidth bits.
2690 IncrementSize = RoundWidth / 8;
2691 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2692 DAG.getIntPtrConstant(IncrementSize));
2693 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2694 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2695 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2696 SVOffset + IncrementSize, ExtraVT, isVolatile,
2697 MinAlign(Alignment, IncrementSize));
2698 } else {
2699 // Big endian - avoid unaligned stores.
2700 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2701 // Store the top RoundWidth bits.
2702 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2703 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2704 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2705 RoundVT, isVolatile, Alignment);
2706
2707 // Store the remaining ExtraWidth bits.
2708 IncrementSize = RoundWidth / 8;
2709 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2710 DAG.getIntPtrConstant(IncrementSize));
2711 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2712 SVOffset + IncrementSize, ExtraVT, isVolatile,
2713 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002714 }
Duncan Sands40676662008-01-22 07:17:34 +00002715
2716 // The order of the stores doesn't matter.
2717 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2718 } else {
2719 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2720 Tmp2 != ST->getBasePtr())
2721 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2722 ST->getOffset());
2723
2724 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2725 default: assert(0 && "This action is not supported yet!");
2726 case TargetLowering::Legal:
2727 // If this is an unaligned store and the target doesn't support it,
2728 // expand it.
2729 if (!TLI.allowsUnalignedMemoryAccesses()) {
2730 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002731 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands40676662008-01-22 07:17:34 +00002732 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002733 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands40676662008-01-22 07:17:34 +00002734 TLI);
2735 }
2736 break;
2737 case TargetLowering::Custom:
2738 Result = TLI.LowerOperation(Result, DAG);
2739 break;
2740 case Expand:
2741 // TRUNCSTORE:i16 i32 -> STORE i16
2742 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2743 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2744 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2745 isVolatile, Alignment);
2746 break;
2747 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002748 }
2749 }
2750 break;
2751 }
2752 case ISD::PCMARKER:
2753 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2754 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2755 break;
2756 case ISD::STACKSAVE:
2757 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2758 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2759 Tmp1 = Result.getValue(0);
2760 Tmp2 = Result.getValue(1);
2761
2762 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2763 default: assert(0 && "This action is not supported yet!");
2764 case TargetLowering::Legal: break;
2765 case TargetLowering::Custom:
2766 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002767 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002768 Tmp1 = LegalizeOp(Tmp3);
2769 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2770 }
2771 break;
2772 case TargetLowering::Expand:
2773 // Expand to CopyFromReg if the target set
2774 // StackPointerRegisterToSaveRestore.
2775 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2776 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2777 Node->getValueType(0));
2778 Tmp2 = Tmp1.getValue(1);
2779 } else {
2780 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2781 Tmp2 = Node->getOperand(0);
2782 }
2783 break;
2784 }
2785
2786 // Since stacksave produce two values, make sure to remember that we
2787 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002788 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2789 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002790 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002791
2792 case ISD::STACKRESTORE:
2793 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2794 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2795 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2796
2797 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2798 default: assert(0 && "This action is not supported yet!");
2799 case TargetLowering::Legal: break;
2800 case TargetLowering::Custom:
2801 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002802 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002803 break;
2804 case TargetLowering::Expand:
2805 // Expand to CopyToReg if the target set
2806 // StackPointerRegisterToSaveRestore.
2807 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2808 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2809 } else {
2810 Result = Tmp1;
2811 }
2812 break;
2813 }
2814 break;
2815
2816 case ISD::READCYCLECOUNTER:
2817 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2818 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2819 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2820 Node->getValueType(0))) {
2821 default: assert(0 && "This action is not supported yet!");
2822 case TargetLowering::Legal:
2823 Tmp1 = Result.getValue(0);
2824 Tmp2 = Result.getValue(1);
2825 break;
2826 case TargetLowering::Custom:
2827 Result = TLI.LowerOperation(Result, DAG);
2828 Tmp1 = LegalizeOp(Result.getValue(0));
2829 Tmp2 = LegalizeOp(Result.getValue(1));
2830 break;
2831 }
2832
2833 // Since rdcc produce two values, make sure to remember that we legalized
2834 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002835 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2836 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002837 return Result;
2838
2839 case ISD::SELECT:
2840 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2841 case Expand: assert(0 && "It's impossible to expand bools");
2842 case Legal:
2843 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2844 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002845 case Promote: {
Mon P Wang1448aad2008-10-30 08:01:45 +00002846 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002847 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2848 // Make sure the condition is either zero or one.
Dan Gohman07961cd2008-02-25 21:11:39 +00002849 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002850 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman07961cd2008-02-25 21:11:39 +00002851 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002852 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2853 break;
2854 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002855 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002856 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2857 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2858
2859 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2860
2861 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2862 default: assert(0 && "This action is not supported yet!");
2863 case TargetLowering::Legal: break;
2864 case TargetLowering::Custom: {
2865 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002866 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002867 break;
2868 }
2869 case TargetLowering::Expand:
2870 if (Tmp1.getOpcode() == ISD::SETCC) {
2871 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2872 Tmp2, Tmp3,
2873 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2874 } else {
2875 Result = DAG.getSelectCC(Tmp1,
2876 DAG.getConstant(0, Tmp1.getValueType()),
2877 Tmp2, Tmp3, ISD::SETNE);
2878 }
2879 break;
2880 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00002881 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002882 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2883 unsigned ExtOp, TruncOp;
Duncan Sands92c43912008-06-06 12:08:01 +00002884 if (Tmp2.getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002885 ExtOp = ISD::BIT_CONVERT;
2886 TruncOp = ISD::BIT_CONVERT;
Duncan Sands92c43912008-06-06 12:08:01 +00002887 } else if (Tmp2.getValueType().isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002888 ExtOp = ISD::ANY_EXTEND;
2889 TruncOp = ISD::TRUNCATE;
2890 } else {
2891 ExtOp = ISD::FP_EXTEND;
2892 TruncOp = ISD::FP_ROUND;
2893 }
2894 // Promote each of the values to the new type.
2895 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2896 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2897 // Perform the larger operation, then round down.
2898 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00002899 if (TruncOp != ISD::FP_ROUND)
2900 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2901 else
2902 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2903 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002904 break;
2905 }
2906 }
2907 break;
2908 case ISD::SELECT_CC: {
2909 Tmp1 = Node->getOperand(0); // LHS
2910 Tmp2 = Node->getOperand(1); // RHS
2911 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2912 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman8181bd12008-07-27 21:46:04 +00002913 SDValue CC = Node->getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002914
Dale Johannesen32100b22008-11-07 22:54:33 +00002915 LegalizeSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2, CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002916
Evan Cheng71343822008-10-15 02:05:31 +00002917 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002918 // the LHS is a legal SETCC itself. In this case, we need to compare
2919 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00002920 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002921 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2922 CC = DAG.getCondCode(ISD::SETNE);
2923 }
2924 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2925
2926 // Everything is legal, see if we should expand this op or something.
2927 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2928 default: assert(0 && "This action is not supported yet!");
2929 case TargetLowering::Legal: break;
2930 case TargetLowering::Custom:
2931 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002932 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002933 break;
2934 }
2935 break;
2936 }
2937 case ISD::SETCC:
2938 Tmp1 = Node->getOperand(0);
2939 Tmp2 = Node->getOperand(1);
2940 Tmp3 = Node->getOperand(2);
Evan Cheng71343822008-10-15 02:05:31 +00002941 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002942
2943 // If we had to Expand the SetCC operands into a SELECT node, then it may
2944 // not always be possible to return a true LHS & RHS. In this case, just
2945 // return the value we legalized, returned in the LHS
Gabor Greif1c80d112008-08-28 21:40:38 +00002946 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002947 Result = Tmp1;
2948 break;
2949 }
2950
2951 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
2952 default: assert(0 && "Cannot handle this action for SETCC yet!");
2953 case TargetLowering::Custom:
2954 isCustom = true;
2955 // FALLTHROUGH.
2956 case TargetLowering::Legal:
2957 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2958 if (isCustom) {
2959 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002960 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002961 }
2962 break;
2963 case TargetLowering::Promote: {
2964 // First step, figure out the appropriate operation to use.
2965 // Allow SETCC to not be supported for all legal data types
2966 // Mostly this targets FP
Duncan Sands92c43912008-06-06 12:08:01 +00002967 MVT NewInTy = Node->getOperand(0).getValueType();
2968 MVT OldVT = NewInTy; OldVT = OldVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002969
2970 // Scan for the appropriate larger type to use.
2971 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00002972 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002973
Duncan Sands92c43912008-06-06 12:08:01 +00002974 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002975 "Fell off of the edge of the integer world");
Duncan Sands92c43912008-06-06 12:08:01 +00002976 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002977 "Fell off of the edge of the floating point world");
2978
2979 // If the target supports SETCC of this type, use it.
2980 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
2981 break;
2982 }
Duncan Sands92c43912008-06-06 12:08:01 +00002983 if (NewInTy.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002984 assert(0 && "Cannot promote Legal Integer SETCC yet");
2985 else {
2986 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2987 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2988 }
2989 Tmp1 = LegalizeOp(Tmp1);
2990 Tmp2 = LegalizeOp(Tmp2);
2991 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2992 Result = LegalizeOp(Result);
2993 break;
2994 }
2995 case TargetLowering::Expand:
2996 // Expand a setcc node into a select_cc of the same condition, lhs, and
2997 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands92c43912008-06-06 12:08:01 +00002998 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002999 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3000 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3001 Tmp3);
3002 break;
3003 }
3004 break;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003005 case ISD::VSETCC: {
3006 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3007 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman8181bd12008-07-27 21:46:04 +00003008 SDValue CC = Node->getOperand(2);
Nate Begeman9a1ce152008-05-12 19:40:03 +00003009
3010 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3011
3012 // Everything is legal, see if we should expand this op or something.
3013 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3014 default: assert(0 && "This action is not supported yet!");
3015 case TargetLowering::Legal: break;
3016 case TargetLowering::Custom:
3017 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003018 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003019 break;
3020 }
3021 break;
3022 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003023
3024 case ISD::SHL_PARTS:
3025 case ISD::SRA_PARTS:
3026 case ISD::SRL_PARTS: {
Dan Gohman8181bd12008-07-27 21:46:04 +00003027 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003028 bool Changed = false;
3029 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3030 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3031 Changed |= Ops.back() != Node->getOperand(i);
3032 }
3033 if (Changed)
3034 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
3035
3036 switch (TLI.getOperationAction(Node->getOpcode(),
3037 Node->getValueType(0))) {
3038 default: assert(0 && "This action is not supported yet!");
3039 case TargetLowering::Legal: break;
3040 case TargetLowering::Custom:
3041 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003042 if (Tmp1.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003043 SDValue Tmp2, RetVal(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003044 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
3045 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman8181bd12008-07-27 21:46:04 +00003046 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00003047 if (i == Op.getResNo())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003048 RetVal = Tmp2;
3049 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003050 assert(RetVal.getNode() && "Illegal result number");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003051 return RetVal;
3052 }
3053 break;
3054 }
3055
3056 // Since these produce multiple values, make sure to remember that we
3057 // legalized all of them.
3058 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman8181bd12008-07-27 21:46:04 +00003059 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00003060 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003061 }
3062
3063 // Binary operators
3064 case ISD::ADD:
3065 case ISD::SUB:
3066 case ISD::MUL:
3067 case ISD::MULHS:
3068 case ISD::MULHU:
3069 case ISD::UDIV:
3070 case ISD::SDIV:
3071 case ISD::AND:
3072 case ISD::OR:
3073 case ISD::XOR:
3074 case ISD::SHL:
3075 case ISD::SRL:
3076 case ISD::SRA:
3077 case ISD::FADD:
3078 case ISD::FSUB:
3079 case ISD::FMUL:
3080 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00003081 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003082 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3083 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3084 case Expand: assert(0 && "Not possible");
3085 case Legal:
3086 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3087 break;
3088 case Promote:
3089 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3090 break;
3091 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003092
3093 if ((Node->getOpcode() == ISD::SHL ||
3094 Node->getOpcode() == ISD::SRL ||
3095 Node->getOpcode() == ISD::SRA) &&
3096 !Node->getValueType(0).isVector()) {
3097 if (TLI.getShiftAmountTy().bitsLT(Tmp2.getValueType()))
3098 Tmp2 = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), Tmp2);
3099 else if (TLI.getShiftAmountTy().bitsGT(Tmp2.getValueType()))
3100 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Tmp2);
3101 }
3102
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003103 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangbff5d9c2008-11-10 04:46:22 +00003104
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003105 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3106 default: assert(0 && "BinOp legalize operation not supported");
3107 case TargetLowering::Legal: break;
3108 case TargetLowering::Custom:
3109 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003110 if (Tmp1.getNode()) {
Nate Begemanbb1ce942008-07-29 15:49:41 +00003111 Result = Tmp1;
3112 break;
Nate Begeman7569e762008-07-29 19:07:27 +00003113 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003114 // Fall through if the custom lower can't deal with the operation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003115 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00003116 MVT VT = Op.getValueType();
Mon P Wang1448aad2008-10-30 08:01:45 +00003117
Dan Gohman5a199552007-10-08 18:33:35 +00003118 // See if multiply or divide can be lowered using two-result operations.
3119 SDVTList VTs = DAG.getVTList(VT, VT);
3120 if (Node->getOpcode() == ISD::MUL) {
3121 // We just need the low half of the multiply; try both the signed
3122 // and unsigned forms. If the target supports both SMUL_LOHI and
3123 // UMUL_LOHI, form a preference by checking which forms of plain
3124 // MULH it supports.
3125 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3126 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3127 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3128 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3129 unsigned OpToUse = 0;
3130 if (HasSMUL_LOHI && !HasMULHS) {
3131 OpToUse = ISD::SMUL_LOHI;
3132 } else if (HasUMUL_LOHI && !HasMULHU) {
3133 OpToUse = ISD::UMUL_LOHI;
3134 } else if (HasSMUL_LOHI) {
3135 OpToUse = ISD::SMUL_LOHI;
3136 } else if (HasUMUL_LOHI) {
3137 OpToUse = ISD::UMUL_LOHI;
3138 }
3139 if (OpToUse) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003140 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003141 break;
3142 }
3143 }
3144 if (Node->getOpcode() == ISD::MULHS &&
3145 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003146 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3147 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003148 break;
3149 }
3150 if (Node->getOpcode() == ISD::MULHU &&
3151 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003152 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3153 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003154 break;
3155 }
3156 if (Node->getOpcode() == ISD::SDIV &&
3157 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003158 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3159 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003160 break;
3161 }
3162 if (Node->getOpcode() == ISD::UDIV &&
3163 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003164 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3165 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003166 break;
3167 }
Mon P Wang1448aad2008-10-30 08:01:45 +00003168
Dan Gohman6d05cac2007-10-11 23:57:53 +00003169 // Check to see if we have a libcall for this operator.
3170 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3171 bool isSigned = false;
3172 switch (Node->getOpcode()) {
3173 case ISD::UDIV:
3174 case ISD::SDIV:
3175 if (VT == MVT::i32) {
3176 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang1448aad2008-10-30 08:01:45 +00003177 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003178 isSigned = Node->getOpcode() == ISD::SDIV;
3179 }
3180 break;
Chris Lattner48188652008-10-04 21:27:46 +00003181 case ISD::MUL:
3182 if (VT == MVT::i32)
3183 LC = RTLIB::MUL_I32;
3184 break;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003185 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00003186 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3187 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003188 break;
3189 default: break;
3190 }
3191 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003192 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003193 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003194 break;
3195 }
Mon P Wang1448aad2008-10-30 08:01:45 +00003196
Duncan Sands92c43912008-06-06 12:08:01 +00003197 assert(Node->getValueType(0).isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003198 "Cannot expand this binary operator!");
3199 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003200 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003201 break;
3202 }
3203 case TargetLowering::Promote: {
3204 switch (Node->getOpcode()) {
3205 default: assert(0 && "Do not know how to promote this BinOp!");
3206 case ISD::AND:
3207 case ISD::OR:
3208 case ISD::XOR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003209 MVT OVT = Node->getValueType(0);
3210 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3211 assert(OVT.isVector() && "Cannot promote this BinOp!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003212 // Bit convert each of the values to the new type.
3213 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3214 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3215 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3216 // Bit convert the result back the original type.
3217 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3218 break;
3219 }
3220 }
3221 }
3222 }
3223 break;
3224
Dan Gohman475cd732007-10-05 14:17:22 +00003225 case ISD::SMUL_LOHI:
3226 case ISD::UMUL_LOHI:
3227 case ISD::SDIVREM:
3228 case ISD::UDIVREM:
3229 // These nodes will only be produced by target-specific lowering, so
3230 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003231 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003232 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003233
3234 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3235 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3236 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003237 break;
3238
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003239 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3240 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3241 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3242 case Expand: assert(0 && "Not possible");
3243 case Legal:
3244 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3245 break;
3246 case Promote:
3247 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3248 break;
3249 }
3250
3251 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3252
3253 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3254 default: assert(0 && "Operation not supported");
3255 case TargetLowering::Custom:
3256 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003257 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003258 break;
3259 case TargetLowering::Legal: break;
3260 case TargetLowering::Expand: {
3261 // If this target supports fabs/fneg natively and select is cheap,
3262 // do this efficiently.
3263 if (!TLI.isSelectExpensive() &&
3264 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3265 TargetLowering::Legal &&
3266 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3267 TargetLowering::Legal) {
3268 // Get the sign bit of the RHS.
Duncan Sands92c43912008-06-06 12:08:01 +00003269 MVT IVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003270 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman8181bd12008-07-27 21:46:04 +00003271 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel502151f2008-03-10 15:42:14 +00003272 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003273 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3274 // Get the absolute value of the result.
Dan Gohman8181bd12008-07-27 21:46:04 +00003275 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003276 // Select between the nabs and abs value based on the sign bit of
3277 // the input.
3278 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3279 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3280 AbsVal),
3281 AbsVal);
3282 Result = LegalizeOp(Result);
3283 break;
3284 }
3285
3286 // Otherwise, do bitwise ops!
Duncan Sands92c43912008-06-06 12:08:01 +00003287 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003288 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3289 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3290 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3291 Result = LegalizeOp(Result);
3292 break;
3293 }
3294 }
3295 break;
3296
3297 case ISD::ADDC:
3298 case ISD::SUBC:
3299 Tmp1 = LegalizeOp(Node->getOperand(0));
3300 Tmp2 = LegalizeOp(Node->getOperand(1));
3301 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3302 // Since this produces two values, make sure to remember that we legalized
3303 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003304 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3305 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003306 return Result;
3307
3308 case ISD::ADDE:
3309 case ISD::SUBE:
3310 Tmp1 = LegalizeOp(Node->getOperand(0));
3311 Tmp2 = LegalizeOp(Node->getOperand(1));
3312 Tmp3 = LegalizeOp(Node->getOperand(2));
3313 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3314 // Since this produces two values, make sure to remember that we legalized
3315 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003316 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3317 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003318 return Result;
3319
3320 case ISD::BUILD_PAIR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003321 MVT PairTy = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003322 // TODO: handle the case where the Lo and Hi operands are not of legal type
3323 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3324 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3325 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3326 case TargetLowering::Promote:
3327 case TargetLowering::Custom:
3328 assert(0 && "Cannot promote/custom this yet!");
3329 case TargetLowering::Legal:
3330 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3331 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3332 break;
3333 case TargetLowering::Expand:
3334 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3335 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3336 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003337 DAG.getConstant(PairTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003338 TLI.getShiftAmountTy()));
3339 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3340 break;
3341 }
3342 break;
3343 }
3344
3345 case ISD::UREM:
3346 case ISD::SREM:
3347 case ISD::FREM:
3348 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3349 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3350
3351 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3352 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3353 case TargetLowering::Custom:
3354 isCustom = true;
3355 // FALLTHROUGH
3356 case TargetLowering::Legal:
3357 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3358 if (isCustom) {
3359 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003360 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003361 }
3362 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003363 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003364 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3365 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands92c43912008-06-06 12:08:01 +00003366 MVT VT = Node->getValueType(0);
Dan Gohman5a199552007-10-08 18:33:35 +00003367
3368 // See if remainder can be lowered using two-result operations.
3369 SDVTList VTs = DAG.getVTList(VT, VT);
3370 if (Node->getOpcode() == ISD::SREM &&
3371 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003372 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003373 break;
3374 }
3375 if (Node->getOpcode() == ISD::UREM &&
3376 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003377 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003378 break;
3379 }
3380
Duncan Sands92c43912008-06-06 12:08:01 +00003381 if (VT.isInteger()) {
Dan Gohman5a199552007-10-08 18:33:35 +00003382 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003383 TargetLowering::Legal) {
3384 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003385 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3386 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3387 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands92c43912008-06-06 12:08:01 +00003388 } else if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003389 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003390 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003391 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003392 "Cannot expand this binary operator!");
3393 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3394 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman8181bd12008-07-27 21:46:04 +00003395 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003396 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003397 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003398 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00003399 assert(VT.isFloatingPoint() &&
Dan Gohman59b4b102007-11-06 22:11:54 +00003400 "remainder op must have integer or floating-point type");
Duncan Sands92c43912008-06-06 12:08:01 +00003401 if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003402 Result = LegalizeOp(UnrollVectorOp(Op));
3403 } else {
3404 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003405 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3406 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003407 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003408 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003409 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003410 }
3411 break;
3412 }
Dan Gohman5a199552007-10-08 18:33:35 +00003413 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003414 break;
3415 case ISD::VAARG: {
3416 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3417 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3418
Duncan Sands92c43912008-06-06 12:08:01 +00003419 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003420 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3421 default: assert(0 && "This action is not supported yet!");
3422 case TargetLowering::Custom:
3423 isCustom = true;
3424 // FALLTHROUGH
3425 case TargetLowering::Legal:
3426 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3427 Result = Result.getValue(0);
3428 Tmp1 = Result.getValue(1);
3429
3430 if (isCustom) {
3431 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003432 if (Tmp2.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003433 Result = LegalizeOp(Tmp2);
3434 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3435 }
3436 }
3437 break;
3438 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003439 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003440 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003441 // Increment the pointer, VAList, to the next vaarg
Duncan Sands55a4c232008-11-03 11:51:11 +00003442 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3443 DAG.getConstant(TLI.getTargetData()->getABITypeSize(VT.getTypeForMVT()),
3444 TLI.getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003445 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00003446 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003447 // Load the actual argument out of the pointer VAList
3448 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3449 Tmp1 = LegalizeOp(Result.getValue(1));
3450 Result = LegalizeOp(Result);
3451 break;
3452 }
3453 }
3454 // Since VAARG produces two values, make sure to remember that we
3455 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003456 AddLegalizedOperand(SDValue(Node, 0), Result);
3457 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00003458 return Op.getResNo() ? Tmp1 : Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003459 }
3460
3461 case ISD::VACOPY:
3462 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3463 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3464 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3465
3466 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3467 default: assert(0 && "This action is not supported yet!");
3468 case TargetLowering::Custom:
3469 isCustom = true;
3470 // FALLTHROUGH
3471 case TargetLowering::Legal:
3472 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3473 Node->getOperand(3), Node->getOperand(4));
3474 if (isCustom) {
3475 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003476 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003477 }
3478 break;
3479 case TargetLowering::Expand:
3480 // This defaults to loading a pointer from the input and storing it to the
3481 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003482 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3483 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman6b9a08e2008-04-17 02:09:26 +00003484 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3485 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003486 break;
3487 }
3488 break;
3489
3490 case ISD::VAEND:
3491 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3492 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3493
3494 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3495 default: assert(0 && "This action is not supported yet!");
3496 case TargetLowering::Custom:
3497 isCustom = true;
3498 // FALLTHROUGH
3499 case TargetLowering::Legal:
3500 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3501 if (isCustom) {
3502 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003503 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003504 }
3505 break;
3506 case TargetLowering::Expand:
3507 Result = Tmp1; // Default to a no-op, return the chain
3508 break;
3509 }
3510 break;
3511
3512 case ISD::VASTART:
3513 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3514 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3515
3516 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3517
3518 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3519 default: assert(0 && "This action is not supported yet!");
3520 case TargetLowering::Legal: break;
3521 case TargetLowering::Custom:
3522 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003523 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003524 break;
3525 }
3526 break;
3527
3528 case ISD::ROTL:
3529 case ISD::ROTR:
3530 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3531 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3532 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3533 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3534 default:
3535 assert(0 && "ROTL/ROTR legalize operation not supported");
3536 break;
3537 case TargetLowering::Legal:
3538 break;
3539 case TargetLowering::Custom:
3540 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003541 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003542 break;
3543 case TargetLowering::Promote:
3544 assert(0 && "Do not know how to promote ROTL/ROTR");
3545 break;
3546 case TargetLowering::Expand:
3547 assert(0 && "Do not know how to expand ROTL/ROTR");
3548 break;
3549 }
3550 break;
3551
3552 case ISD::BSWAP:
3553 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3554 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3555 case TargetLowering::Custom:
3556 assert(0 && "Cannot custom legalize this yet!");
3557 case TargetLowering::Legal:
3558 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3559 break;
3560 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003561 MVT OVT = Tmp1.getValueType();
3562 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3563 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003564
3565 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3566 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3567 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3568 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3569 break;
3570 }
3571 case TargetLowering::Expand:
3572 Result = ExpandBSWAP(Tmp1);
3573 break;
3574 }
3575 break;
3576
3577 case ISD::CTPOP:
3578 case ISD::CTTZ:
3579 case ISD::CTLZ:
3580 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3581 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003582 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003583 case TargetLowering::Legal:
3584 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003585 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003586 TargetLowering::Custom) {
3587 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003588 if (Tmp1.getNode()) {
Scott Michelbc62b412007-08-02 02:22:46 +00003589 Result = Tmp1;
3590 }
Scott Michel48b63e62007-07-30 21:00:31 +00003591 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003592 break;
3593 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003594 MVT OVT = Tmp1.getValueType();
3595 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003596
3597 // Zero extend the argument.
3598 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3599 // Perform the larger operation, then subtract if needed.
3600 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3601 switch (Node->getOpcode()) {
3602 case ISD::CTPOP:
3603 Result = Tmp1;
3604 break;
3605 case ISD::CTTZ:
3606 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00003607 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003608 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003609 ISD::SETEQ);
3610 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003611 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003612 break;
3613 case ISD::CTLZ:
3614 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3615 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003616 DAG.getConstant(NVT.getSizeInBits() -
3617 OVT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003618 break;
3619 }
3620 break;
3621 }
3622 case TargetLowering::Expand:
3623 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3624 break;
3625 }
3626 break;
3627
3628 // Unary operators
3629 case ISD::FABS:
3630 case ISD::FNEG:
3631 case ISD::FSQRT:
3632 case ISD::FSIN:
3633 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003634 case ISD::FLOG:
3635 case ISD::FLOG2:
3636 case ISD::FLOG10:
3637 case ISD::FEXP:
3638 case ISD::FEXP2:
Dan Gohmanc8b20e22008-08-21 17:55:02 +00003639 case ISD::FTRUNC:
3640 case ISD::FFLOOR:
3641 case ISD::FCEIL:
3642 case ISD::FRINT:
3643 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003644 Tmp1 = LegalizeOp(Node->getOperand(0));
3645 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3646 case TargetLowering::Promote:
3647 case TargetLowering::Custom:
3648 isCustom = true;
3649 // FALLTHROUGH
3650 case TargetLowering::Legal:
3651 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3652 if (isCustom) {
3653 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003654 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003655 }
3656 break;
3657 case TargetLowering::Expand:
3658 switch (Node->getOpcode()) {
3659 default: assert(0 && "Unreachable!");
3660 case ISD::FNEG:
3661 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3662 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3663 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3664 break;
3665 case ISD::FABS: {
3666 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands92c43912008-06-06 12:08:01 +00003667 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003668 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel502151f2008-03-10 15:42:14 +00003669 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00003670 ISD::SETUGT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003671 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3672 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3673 break;
3674 }
Evan Cheng1fac6952008-09-09 23:35:53 +00003675 case ISD::FSQRT:
3676 case ISD::FSIN:
3677 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003678 case ISD::FLOG:
3679 case ISD::FLOG2:
3680 case ISD::FLOG10:
3681 case ISD::FEXP:
3682 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00003683 case ISD::FTRUNC:
3684 case ISD::FFLOOR:
3685 case ISD::FCEIL:
3686 case ISD::FRINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00003687 case ISD::FNEARBYINT: {
Duncan Sands92c43912008-06-06 12:08:01 +00003688 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003689
3690 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003691 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003692 Result = LegalizeOp(UnrollVectorOp(Op));
3693 break;
3694 }
3695
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003696 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3697 switch(Node->getOpcode()) {
3698 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003699 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3700 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003701 break;
3702 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003703 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3704 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003705 break;
3706 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003707 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3708 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003709 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00003710 case ISD::FLOG:
3711 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3712 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3713 break;
3714 case ISD::FLOG2:
3715 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3716 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3717 break;
3718 case ISD::FLOG10:
3719 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3720 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3721 break;
3722 case ISD::FEXP:
3723 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3724 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3725 break;
3726 case ISD::FEXP2:
3727 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3728 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3729 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00003730 case ISD::FTRUNC:
3731 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3732 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3733 break;
3734 case ISD::FFLOOR:
3735 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3736 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3737 break;
3738 case ISD::FCEIL:
3739 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3740 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3741 break;
3742 case ISD::FRINT:
3743 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3744 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3745 break;
3746 case ISD::FNEARBYINT:
3747 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3748 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3749 break;
Evan Cheng1fac6952008-09-09 23:35:53 +00003750 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003751 default: assert(0 && "Unreachable!");
3752 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003753 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003754 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003755 break;
3756 }
3757 }
3758 break;
3759 }
3760 break;
3761 case ISD::FPOWI: {
Duncan Sands92c43912008-06-06 12:08:01 +00003762 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003763
3764 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003765 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003766 Result = LegalizeOp(UnrollVectorOp(Op));
3767 break;
3768 }
3769
3770 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003771 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3772 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003773 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003774 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003775 break;
3776 }
3777 case ISD::BIT_CONVERT:
3778 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003779 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3780 Node->getValueType(0));
Duncan Sands92c43912008-06-06 12:08:01 +00003781 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003782 // The input has to be a vector type, we have to either scalarize it, pack
3783 // it, or convert it based on whether the input vector type is legal.
Gabor Greif1c80d112008-08-28 21:40:38 +00003784 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00003785 int InIx = Node->getOperand(0).getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00003786 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3787 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003788
3789 // Figure out if there is a simple type corresponding to this Vector
3790 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00003791 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003792 if (TLI.isTypeLegal(TVT)) {
3793 // Turn this into a bit convert of the vector input.
3794 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3795 LegalizeOp(Node->getOperand(0)));
3796 break;
3797 } else if (NumElems == 1) {
3798 // Turn this into a bit convert of the scalar input.
3799 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3800 ScalarizeVectorOp(Node->getOperand(0)));
3801 break;
3802 } else {
3803 // FIXME: UNIMP! Store then reload
3804 assert(0 && "Cast from unsupported vector type not implemented yet!");
3805 }
3806 } else {
3807 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3808 Node->getOperand(0).getValueType())) {
3809 default: assert(0 && "Unknown operation action!");
3810 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003811 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3812 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003813 break;
3814 case TargetLowering::Legal:
3815 Tmp1 = LegalizeOp(Node->getOperand(0));
3816 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3817 break;
3818 }
3819 }
3820 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003821 case ISD::CONVERT_RNDSAT: {
3822 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3823 switch (CvtCode) {
3824 default: assert(0 && "Unknown cvt code!");
3825 case ISD::CVT_SF:
3826 case ISD::CVT_UF:
3827 break;
3828 case ISD::CVT_FF:
3829 case ISD::CVT_FS:
3830 case ISD::CVT_FU:
3831 case ISD::CVT_SS:
3832 case ISD::CVT_SU:
3833 case ISD::CVT_US:
3834 case ISD::CVT_UU: {
3835 SDValue DTyOp = Node->getOperand(1);
3836 SDValue STyOp = Node->getOperand(2);
3837 SDValue RndOp = Node->getOperand(3);
3838 SDValue SatOp = Node->getOperand(4);
3839 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3840 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3841 case Legal:
3842 Tmp1 = LegalizeOp(Node->getOperand(0));
3843 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3844 RndOp, SatOp);
3845 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3846 TargetLowering::Custom) {
3847 Tmp1 = TLI.LowerOperation(Result, DAG);
3848 if (Tmp1.getNode()) Result = Tmp1;
3849 }
3850 break;
3851 case Promote:
3852 Result = PromoteOp(Node->getOperand(0));
3853 // For FP, make Op1 a i32
3854
3855 Result = DAG.getConvertRndSat(Result.getValueType(), Result,
3856 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3857 break;
3858 }
3859 break;
3860 }
3861 } // end switch CvtCode
3862 break;
3863 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003864 // Conversion operators. The source and destination have different types.
3865 case ISD::SINT_TO_FP:
3866 case ISD::UINT_TO_FP: {
3867 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman29c3cef2008-08-14 20:04:46 +00003868 Result = LegalizeINT_TO_FP(Result, isSigned,
3869 Node->getValueType(0), Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003870 break;
3871 }
3872 case ISD::TRUNCATE:
3873 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3874 case Legal:
3875 Tmp1 = LegalizeOp(Node->getOperand(0));
3876 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3877 break;
3878 case Expand:
3879 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3880
3881 // Since the result is legal, we should just be able to truncate the low
3882 // part of the source.
3883 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3884 break;
3885 case Promote:
3886 Result = PromoteOp(Node->getOperand(0));
3887 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3888 break;
3889 }
3890 break;
3891
3892 case ISD::FP_TO_SINT:
3893 case ISD::FP_TO_UINT:
3894 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3895 case Legal:
3896 Tmp1 = LegalizeOp(Node->getOperand(0));
3897
3898 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3899 default: assert(0 && "Unknown operation action!");
3900 case TargetLowering::Custom:
3901 isCustom = true;
3902 // FALLTHROUGH
3903 case TargetLowering::Legal:
3904 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3905 if (isCustom) {
3906 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003907 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003908 }
3909 break;
3910 case TargetLowering::Promote:
3911 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3912 Node->getOpcode() == ISD::FP_TO_SINT);
3913 break;
3914 case TargetLowering::Expand:
3915 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003916 SDValue True, False;
Duncan Sands92c43912008-06-06 12:08:01 +00003917 MVT VT = Node->getOperand(0).getValueType();
3918 MVT NVT = Node->getValueType(0);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003919 const uint64_t zero[] = {0, 0};
Duncan Sands92c43912008-06-06 12:08:01 +00003920 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3921 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohman88ae8c52008-02-29 01:44:25 +00003922 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003923 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel502151f2008-03-10 15:42:14 +00003924 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003925 Node->getOperand(0), Tmp2, ISD::SETLT);
3926 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3927 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
3928 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
3929 Tmp2));
3930 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman88ae8c52008-02-29 01:44:25 +00003931 DAG.getConstant(x, NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003932 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3933 break;
3934 } else {
3935 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3936 }
3937 break;
3938 }
3939 break;
3940 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00003941 MVT VT = Op.getValueType();
3942 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00003943 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003944 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00003945 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3946 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3947 Node->getOperand(0), DAG.getValueType(MVT::f64));
3948 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3949 DAG.getIntPtrConstant(1));
3950 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3951 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00003952 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3953 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3954 Tmp2 = DAG.getConstantFP(apf, OVT);
3955 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3956 // FIXME: generated code sucks.
3957 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3958 DAG.getNode(ISD::ADD, MVT::i32,
3959 DAG.getNode(ISD::FP_TO_SINT, VT,
3960 DAG.getNode(ISD::FSUB, OVT,
3961 Node->getOperand(0), Tmp2)),
3962 DAG.getConstant(0x80000000, MVT::i32)),
3963 DAG.getNode(ISD::FP_TO_SINT, VT,
3964 Node->getOperand(0)),
3965 DAG.getCondCode(ISD::SETGE));
3966 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003967 break;
3968 }
Dan Gohmanec51f642008-03-10 23:03:31 +00003969 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsf68dffb2008-07-17 02:36:29 +00003970 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
3971 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
3972 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman8181bd12008-07-27 21:46:04 +00003973 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003974 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003975 break;
3976 }
3977 case Promote:
3978 Tmp1 = PromoteOp(Node->getOperand(0));
3979 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3980 Result = LegalizeOp(Result);
3981 break;
3982 }
3983 break;
3984
Chris Lattner56ecde32008-01-16 06:57:07 +00003985 case ISD::FP_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00003986 MVT DstVT = Op.getValueType();
3987 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00003988 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3989 // The only other way we can lower this is to turn it into a STORE,
3990 // LOAD pair, targetting a temporary location (a stack slot).
3991 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3992 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00003993 }
3994 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3995 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3996 case Legal:
3997 Tmp1 = LegalizeOp(Node->getOperand(0));
3998 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3999 break;
4000 case Promote:
4001 Tmp1 = PromoteOp(Node->getOperand(0));
4002 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4003 break;
4004 }
4005 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004006 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004007 case ISD::FP_ROUND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004008 MVT DstVT = Op.getValueType();
4009 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004010 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4011 if (SrcVT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004012 SDValue Lo;
Dale Johannesena0d36082008-01-20 01:18:38 +00004013 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00004014 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00004015 if (DstVT!=MVT::f64)
4016 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00004017 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004018 }
Chris Lattner5872a362008-01-17 07:00:52 +00004019 // The only other way we can lower this is to turn it into a STORE,
4020 // LOAD pair, targetting a temporary location (a stack slot).
4021 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4022 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004023 }
Chris Lattner56ecde32008-01-16 06:57:07 +00004024 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4025 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4026 case Legal:
4027 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004028 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004029 break;
4030 case Promote:
4031 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004032 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4033 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004034 break;
4035 }
4036 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004037 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004038 case ISD::ANY_EXTEND:
4039 case ISD::ZERO_EXTEND:
4040 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004041 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4042 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4043 case Legal:
4044 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac54d002008-04-30 00:26:38 +00004045 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michelac7091c2008-02-15 23:05:48 +00004046 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4047 TargetLowering::Custom) {
Scott Michelac54d002008-04-30 00:26:38 +00004048 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004049 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelac7091c2008-02-15 23:05:48 +00004050 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004051 break;
4052 case Promote:
4053 switch (Node->getOpcode()) {
4054 case ISD::ANY_EXTEND:
4055 Tmp1 = PromoteOp(Node->getOperand(0));
4056 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
4057 break;
4058 case ISD::ZERO_EXTEND:
4059 Result = PromoteOp(Node->getOperand(0));
4060 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4061 Result = DAG.getZeroExtendInReg(Result,
4062 Node->getOperand(0).getValueType());
4063 break;
4064 case ISD::SIGN_EXTEND:
4065 Result = PromoteOp(Node->getOperand(0));
4066 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4067 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4068 Result,
4069 DAG.getValueType(Node->getOperand(0).getValueType()));
4070 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004071 }
4072 }
4073 break;
4074 case ISD::FP_ROUND_INREG:
4075 case ISD::SIGN_EXTEND_INREG: {
4076 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004077 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004078
4079 // If this operation is not supported, convert it to a shl/shr or load/store
4080 // pair.
4081 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4082 default: assert(0 && "This action not supported for this op yet!");
4083 case TargetLowering::Legal:
4084 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
4085 break;
4086 case TargetLowering::Expand:
4087 // If this is an integer extend and shifts are supported, do that.
4088 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
4089 // NOTE: we could fall back on load/store here too for targets without
4090 // SAR. However, it is doubtful that any exist.
Duncan Sands92c43912008-06-06 12:08:01 +00004091 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4092 ExtraVT.getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00004093 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004094 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4095 Node->getOperand(0), ShiftCst);
4096 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4097 Result, ShiftCst);
4098 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
4099 // The only way we can lower this is to turn it into a TRUNCSTORE,
4100 // EXTLOAD pair, targetting a temporary location (a stack slot).
4101
4102 // NOTE: there is a choice here between constantly creating new stack
4103 // slots and always reusing the same one. We currently always create
4104 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00004105 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4106 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004107 } else {
4108 assert(0 && "Unknown op");
4109 }
4110 break;
4111 }
4112 break;
4113 }
Duncan Sands38947cd2007-07-27 12:58:54 +00004114 case ISD::TRAMPOLINE: {
Dan Gohman8181bd12008-07-27 21:46:04 +00004115 SDValue Ops[6];
Duncan Sands38947cd2007-07-27 12:58:54 +00004116 for (unsigned i = 0; i != 6; ++i)
4117 Ops[i] = LegalizeOp(Node->getOperand(i));
4118 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4119 // The only option for this node is to custom lower it.
4120 Result = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004121 assert(Result.getNode() && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00004122
4123 // Since trampoline produces two values, make sure to remember that we
4124 // legalized both of them.
4125 Tmp1 = LegalizeOp(Result.getValue(1));
4126 Result = LegalizeOp(Result);
Dan Gohman8181bd12008-07-27 21:46:04 +00004127 AddLegalizedOperand(SDValue(Node, 0), Result);
4128 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00004129 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00004130 }
Dan Gohmane8e4a412008-05-14 00:43:10 +00004131 case ISD::FLT_ROUNDS_: {
Duncan Sands92c43912008-06-06 12:08:01 +00004132 MVT VT = Node->getValueType(0);
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004133 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4134 default: assert(0 && "This action not supported for this op yet!");
4135 case TargetLowering::Custom:
4136 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004137 if (Result.getNode()) break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004138 // Fall Thru
4139 case TargetLowering::Legal:
4140 // If this operation is not supported, lower it to constant 1
4141 Result = DAG.getConstant(1, VT);
4142 break;
4143 }
Dan Gohmane09dc8c2008-05-12 16:07:15 +00004144 break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004145 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004146 case ISD::TRAP: {
Duncan Sands92c43912008-06-06 12:08:01 +00004147 MVT VT = Node->getValueType(0);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004148 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4149 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00004150 case TargetLowering::Legal:
4151 Tmp1 = LegalizeOp(Node->getOperand(0));
4152 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4153 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004154 case TargetLowering::Custom:
4155 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004156 if (Result.getNode()) break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004157 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00004158 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004159 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00004160 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004161 TargetLowering::ArgListTy Args;
Dan Gohman8181bd12008-07-27 21:46:04 +00004162 std::pair<SDValue,SDValue> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004163 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00004164 false, false, false, false, CallingConv::C, false,
Bill Wendlingfef06052008-09-16 21:48:12 +00004165 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner88e03932008-01-15 22:09:33 +00004166 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004167 Result = CallResult.second;
4168 break;
4169 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004170 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004171 }
Bill Wendling913dcf32008-11-22 00:22:52 +00004172
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004173 case ISD::SADDO: {
4174 MVT VT = Node->getValueType(0);
4175 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4176 default: assert(0 && "This action not supported for this op yet!");
4177 case TargetLowering::Custom:
4178 Result = TLI.LowerOperation(Op, DAG);
4179 if (Result.getNode()) break;
4180 // FALLTHROUGH
4181 case TargetLowering::Legal: {
4182 SDValue LHS = LegalizeOp(Node->getOperand(0));
4183 SDValue RHS = LegalizeOp(Node->getOperand(1));
4184
4185 SDValue Sum = DAG.getNode(ISD::ADD, LHS.getValueType(), LHS, RHS);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004186 MVT OType = Node->getValueType(1);
4187
Bill Wendlingc65e6e42008-11-25 08:19:22 +00004188 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004189
Bill Wendlingcf4de122008-11-25 19:40:17 +00004190 // LHSSign -> LHS >= 0
4191 // RHSSign -> RHS >= 0
4192 // SumSign -> Sum >= 0
4193 //
4194 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
4195 //
4196 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4197 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
4198 SDValue SignsEq = DAG.getSetCC(OType, LHSSign, RHSSign, ISD::SETEQ);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004199
Bill Wendlingcf4de122008-11-25 19:40:17 +00004200 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4201 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004202
Bill Wendlingcf4de122008-11-25 19:40:17 +00004203 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsEq, SumSignNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004204
4205 MVT ValueVTs[] = { LHS.getValueType(), OType };
4206 SDValue Ops[] = { Sum, Cmp };
4207
4208 Result = DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], 2), &Ops[0], 2);
4209 SDNode *RNode = Result.getNode();
4210 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4211 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4212 break;
4213 }
4214 }
4215
4216 break;
4217 }
Bill Wendling8062b072008-11-24 01:38:29 +00004218 case ISD::UADDO: {
Bill Wendling4c134df2008-11-24 19:21:46 +00004219 MVT VT = Node->getValueType(0);
4220 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4221 default: assert(0 && "This action not supported for this op yet!");
4222 case TargetLowering::Custom:
4223 Result = TLI.LowerOperation(Op, DAG);
4224 if (Result.getNode()) break;
4225 // FALLTHROUGH
4226 case TargetLowering::Legal: {
4227 SDValue LHS = LegalizeOp(Node->getOperand(0));
4228 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling913dcf32008-11-22 00:22:52 +00004229
Bill Wendling4c134df2008-11-24 19:21:46 +00004230 SDValue Sum = DAG.getNode(ISD::ADD, LHS.getValueType(), LHS, RHS);
4231 MVT OType = Node->getValueType(1);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004232 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS, ISD::SETULT);
Bill Wendling913dcf32008-11-22 00:22:52 +00004233
Bill Wendling4c134df2008-11-24 19:21:46 +00004234 MVT ValueVTs[] = { LHS.getValueType(), OType };
4235 SDValue Ops[] = { Sum, Cmp };
Bill Wendling913dcf32008-11-22 00:22:52 +00004236
Bill Wendling4c134df2008-11-24 19:21:46 +00004237 Result = DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], 2), &Ops[0], 2);
4238 SDNode *RNode = Result.getNode();
4239 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4240 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4241 break;
4242 }
4243 }
4244
Bill Wendling913dcf32008-11-22 00:22:52 +00004245 break;
4246 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004247 }
4248
4249 assert(Result.getValueType() == Op.getValueType() &&
4250 "Bad legalization!");
4251
4252 // Make sure that the generated code is itself legal.
4253 if (Result != Op)
4254 Result = LegalizeOp(Result);
4255
4256 // Note that LegalizeOp may be reentered even from single-use nodes, which
4257 // means that we always must cache transformed nodes.
4258 AddLegalizedOperand(Op, Result);
4259 return Result;
4260}
4261
4262/// PromoteOp - Given an operation that produces a value in an invalid type,
4263/// promote it to compute the value into a larger type. The produced value will
4264/// have the correct bits for the low portion of the register, but no guarantee
4265/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +00004266SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00004267 MVT VT = Op.getValueType();
4268 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004269 assert(getTypeAction(VT) == Promote &&
4270 "Caller should expand or legalize operands that are not promotable!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004271 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004272 "Cannot promote to smaller type!");
4273
Dan Gohman8181bd12008-07-27 21:46:04 +00004274 SDValue Tmp1, Tmp2, Tmp3;
4275 SDValue Result;
Gabor Greif1c80d112008-08-28 21:40:38 +00004276 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004277
Dan Gohman8181bd12008-07-27 21:46:04 +00004278 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004279 if (I != PromotedNodes.end()) return I->second;
4280
4281 switch (Node->getOpcode()) {
4282 case ISD::CopyFromReg:
4283 assert(0 && "CopyFromReg must be legal!");
4284 default:
4285#ifndef NDEBUG
4286 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4287#endif
4288 assert(0 && "Do not know how to promote this operator!");
4289 abort();
4290 case ISD::UNDEF:
4291 Result = DAG.getNode(ISD::UNDEF, NVT);
4292 break;
4293 case ISD::Constant:
4294 if (VT != MVT::i1)
4295 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4296 else
4297 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
4298 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4299 break;
4300 case ISD::ConstantFP:
4301 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4302 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4303 break;
4304
4305 case ISD::SETCC:
Scott Michel502151f2008-03-10 15:42:14 +00004306 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004307 && "SetCC type is not legal??");
Scott Michel502151f2008-03-10 15:42:14 +00004308 Result = DAG.getNode(ISD::SETCC,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004309 TLI.getSetCCResultType(Node->getOperand(0)),
4310 Node->getOperand(0), Node->getOperand(1),
4311 Node->getOperand(2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004312 break;
4313
4314 case ISD::TRUNCATE:
4315 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4316 case Legal:
4317 Result = LegalizeOp(Node->getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00004318 assert(Result.getValueType().bitsGE(NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004319 "This truncation doesn't make sense!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004320 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004321 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4322 break;
4323 case Promote:
4324 // The truncation is not required, because we don't guarantee anything
4325 // about high bits anyway.
4326 Result = PromoteOp(Node->getOperand(0));
4327 break;
4328 case Expand:
4329 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4330 // Truncate the low part of the expanded value to the result type
4331 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4332 }
4333 break;
4334 case ISD::SIGN_EXTEND:
4335 case ISD::ZERO_EXTEND:
4336 case ISD::ANY_EXTEND:
4337 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4338 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4339 case Legal:
4340 // Input is legal? Just do extend all the way to the larger type.
4341 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4342 break;
4343 case Promote:
4344 // Promote the reg if it's smaller.
4345 Result = PromoteOp(Node->getOperand(0));
4346 // The high bits are not guaranteed to be anything. Insert an extend.
4347 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4348 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4349 DAG.getValueType(Node->getOperand(0).getValueType()));
4350 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4351 Result = DAG.getZeroExtendInReg(Result,
4352 Node->getOperand(0).getValueType());
4353 break;
4354 }
4355 break;
Mon P Wang73d31542008-11-10 20:54:11 +00004356 case ISD::CONVERT_RNDSAT: {
4357 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4358 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4359 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4360 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4361 "can only promote integers");
4362 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4363 Node->getOperand(1), Node->getOperand(2),
4364 Node->getOperand(3), Node->getOperand(4),
4365 CvtCode);
4366 break;
4367
4368 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004369 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004370 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4371 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004372 Result = PromoteOp(Result);
4373 break;
4374
4375 case ISD::FP_EXTEND:
4376 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4377 case ISD::FP_ROUND:
4378 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4379 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4380 case Promote: assert(0 && "Unreachable with 2 FP types!");
4381 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004382 if (Node->getConstantOperandVal(1) == 0) {
4383 // Input is legal? Do an FP_ROUND_INREG.
4384 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4385 DAG.getValueType(VT));
4386 } else {
4387 // Just remove the truncate, it isn't affecting the value.
4388 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4389 Node->getOperand(1));
4390 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004391 break;
4392 }
4393 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004394 case ISD::SINT_TO_FP:
4395 case ISD::UINT_TO_FP:
4396 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4397 case Legal:
4398 // No extra round required here.
4399 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4400 break;
4401
4402 case Promote:
4403 Result = PromoteOp(Node->getOperand(0));
4404 if (Node->getOpcode() == ISD::SINT_TO_FP)
4405 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4406 Result,
4407 DAG.getValueType(Node->getOperand(0).getValueType()));
4408 else
4409 Result = DAG.getZeroExtendInReg(Result,
4410 Node->getOperand(0).getValueType());
4411 // No extra round required here.
4412 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4413 break;
4414 case Expand:
4415 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4416 Node->getOperand(0));
4417 // Round if we cannot tolerate excess precision.
4418 if (NoExcessFPPrecision)
4419 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4420 DAG.getValueType(VT));
4421 break;
4422 }
4423 break;
4424
4425 case ISD::SIGN_EXTEND_INREG:
4426 Result = PromoteOp(Node->getOperand(0));
4427 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4428 Node->getOperand(1));
4429 break;
4430 case ISD::FP_TO_SINT:
4431 case ISD::FP_TO_UINT:
4432 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4433 case Legal:
4434 case Expand:
4435 Tmp1 = Node->getOperand(0);
4436 break;
4437 case Promote:
4438 // The input result is prerounded, so we don't have to do anything
4439 // special.
4440 Tmp1 = PromoteOp(Node->getOperand(0));
4441 break;
4442 }
4443 // If we're promoting a UINT to a larger size, check to see if the new node
4444 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4445 // we can use that instead. This allows us to generate better code for
4446 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4447 // legal, such as PowerPC.
4448 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4449 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4450 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4451 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4452 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4453 } else {
4454 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4455 }
4456 break;
4457
4458 case ISD::FABS:
4459 case ISD::FNEG:
4460 Tmp1 = PromoteOp(Node->getOperand(0));
4461 assert(Tmp1.getValueType() == NVT);
4462 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4463 // NOTE: we do not have to do any extra rounding here for
4464 // NoExcessFPPrecision, because we know the input will have the appropriate
4465 // precision, and these operations don't modify precision at all.
4466 break;
4467
Dale Johannesen92b33082008-09-04 00:47:13 +00004468 case ISD::FLOG:
4469 case ISD::FLOG2:
4470 case ISD::FLOG10:
4471 case ISD::FEXP:
4472 case ISD::FEXP2:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004473 case ISD::FSQRT:
4474 case ISD::FSIN:
4475 case ISD::FCOS:
Dan Gohmanb2158232008-08-21 18:38:14 +00004476 case ISD::FTRUNC:
4477 case ISD::FFLOOR:
4478 case ISD::FCEIL:
4479 case ISD::FRINT:
4480 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004481 Tmp1 = PromoteOp(Node->getOperand(0));
4482 assert(Tmp1.getValueType() == NVT);
4483 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4484 if (NoExcessFPPrecision)
4485 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4486 DAG.getValueType(VT));
4487 break;
4488
Evan Cheng1fac6952008-09-09 23:35:53 +00004489 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004490 case ISD::FPOWI: {
Evan Cheng1fac6952008-09-09 23:35:53 +00004491 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004492 // directly as well, which may be better.
4493 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng1fac6952008-09-09 23:35:53 +00004494 Tmp2 = Node->getOperand(1);
4495 if (Node->getOpcode() == ISD::FPOW)
4496 Tmp2 = PromoteOp(Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004497 assert(Tmp1.getValueType() == NVT);
Evan Cheng1fac6952008-09-09 23:35:53 +00004498 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004499 if (NoExcessFPPrecision)
4500 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4501 DAG.getValueType(VT));
4502 break;
4503 }
4504
Dale Johannesenbc187662008-08-28 02:44:49 +00004505 case ISD::ATOMIC_CMP_SWAP_8:
4506 case ISD::ATOMIC_CMP_SWAP_16:
4507 case ISD::ATOMIC_CMP_SWAP_32:
4508 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004509 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004510 Tmp2 = PromoteOp(Node->getOperand(2));
4511 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004512 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4513 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004514 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004515 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004516 // Remember that we legalized the chain.
4517 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4518 break;
4519 }
Dale Johannesenbc187662008-08-28 02:44:49 +00004520 case ISD::ATOMIC_LOAD_ADD_8:
4521 case ISD::ATOMIC_LOAD_SUB_8:
4522 case ISD::ATOMIC_LOAD_AND_8:
4523 case ISD::ATOMIC_LOAD_OR_8:
4524 case ISD::ATOMIC_LOAD_XOR_8:
4525 case ISD::ATOMIC_LOAD_NAND_8:
4526 case ISD::ATOMIC_LOAD_MIN_8:
4527 case ISD::ATOMIC_LOAD_MAX_8:
4528 case ISD::ATOMIC_LOAD_UMIN_8:
4529 case ISD::ATOMIC_LOAD_UMAX_8:
4530 case ISD::ATOMIC_SWAP_8:
4531 case ISD::ATOMIC_LOAD_ADD_16:
4532 case ISD::ATOMIC_LOAD_SUB_16:
4533 case ISD::ATOMIC_LOAD_AND_16:
4534 case ISD::ATOMIC_LOAD_OR_16:
4535 case ISD::ATOMIC_LOAD_XOR_16:
4536 case ISD::ATOMIC_LOAD_NAND_16:
4537 case ISD::ATOMIC_LOAD_MIN_16:
4538 case ISD::ATOMIC_LOAD_MAX_16:
4539 case ISD::ATOMIC_LOAD_UMIN_16:
4540 case ISD::ATOMIC_LOAD_UMAX_16:
4541 case ISD::ATOMIC_SWAP_16:
4542 case ISD::ATOMIC_LOAD_ADD_32:
4543 case ISD::ATOMIC_LOAD_SUB_32:
4544 case ISD::ATOMIC_LOAD_AND_32:
4545 case ISD::ATOMIC_LOAD_OR_32:
4546 case ISD::ATOMIC_LOAD_XOR_32:
4547 case ISD::ATOMIC_LOAD_NAND_32:
4548 case ISD::ATOMIC_LOAD_MIN_32:
4549 case ISD::ATOMIC_LOAD_MAX_32:
4550 case ISD::ATOMIC_LOAD_UMIN_32:
4551 case ISD::ATOMIC_LOAD_UMAX_32:
4552 case ISD::ATOMIC_SWAP_32:
4553 case ISD::ATOMIC_LOAD_ADD_64:
4554 case ISD::ATOMIC_LOAD_SUB_64:
4555 case ISD::ATOMIC_LOAD_AND_64:
4556 case ISD::ATOMIC_LOAD_OR_64:
4557 case ISD::ATOMIC_LOAD_XOR_64:
4558 case ISD::ATOMIC_LOAD_NAND_64:
4559 case ISD::ATOMIC_LOAD_MIN_64:
4560 case ISD::ATOMIC_LOAD_MAX_64:
4561 case ISD::ATOMIC_LOAD_UMIN_64:
4562 case ISD::ATOMIC_LOAD_UMAX_64:
4563 case ISD::ATOMIC_SWAP_64: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004564 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004565 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004566 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4567 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004568 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004569 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004570 // Remember that we legalized the chain.
4571 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4572 break;
4573 }
4574
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004575 case ISD::AND:
4576 case ISD::OR:
4577 case ISD::XOR:
4578 case ISD::ADD:
4579 case ISD::SUB:
4580 case ISD::MUL:
4581 // The input may have strange things in the top bits of the registers, but
4582 // these operations don't care. They may have weird bits going out, but
4583 // that too is okay if they are integer operations.
4584 Tmp1 = PromoteOp(Node->getOperand(0));
4585 Tmp2 = PromoteOp(Node->getOperand(1));
4586 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4587 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4588 break;
4589 case ISD::FADD:
4590 case ISD::FSUB:
4591 case ISD::FMUL:
4592 Tmp1 = PromoteOp(Node->getOperand(0));
4593 Tmp2 = PromoteOp(Node->getOperand(1));
4594 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4595 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4596
4597 // Floating point operations will give excess precision that we may not be
4598 // able to tolerate. If we DO allow excess precision, just leave it,
4599 // otherwise excise it.
4600 // FIXME: Why would we need to round FP ops more than integer ones?
4601 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4602 if (NoExcessFPPrecision)
4603 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4604 DAG.getValueType(VT));
4605 break;
4606
4607 case ISD::SDIV:
4608 case ISD::SREM:
4609 // These operators require that their input be sign extended.
4610 Tmp1 = PromoteOp(Node->getOperand(0));
4611 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004612 if (NVT.isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004613 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4614 DAG.getValueType(VT));
4615 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4616 DAG.getValueType(VT));
4617 }
4618 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4619
4620 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands92c43912008-06-06 12:08:01 +00004621 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004622 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4623 DAG.getValueType(VT));
4624 break;
4625 case ISD::FDIV:
4626 case ISD::FREM:
4627 case ISD::FCOPYSIGN:
4628 // These operators require that their input be fp extended.
4629 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004630 case Expand: assert(0 && "not implemented");
4631 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4632 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004633 }
4634 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004635 case Expand: assert(0 && "not implemented");
4636 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4637 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004638 }
4639 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4640
4641 // Perform FP_ROUND: this is probably overly pessimistic.
4642 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4643 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4644 DAG.getValueType(VT));
4645 break;
4646
4647 case ISD::UDIV:
4648 case ISD::UREM:
4649 // These operators require that their input be zero extended.
4650 Tmp1 = PromoteOp(Node->getOperand(0));
4651 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004652 assert(NVT.isInteger() && "Operators don't apply to FP!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004653 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4654 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4655 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4656 break;
4657
4658 case ISD::SHL:
4659 Tmp1 = PromoteOp(Node->getOperand(0));
4660 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4661 break;
4662 case ISD::SRA:
4663 // The input value must be properly sign extended.
4664 Tmp1 = PromoteOp(Node->getOperand(0));
4665 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4666 DAG.getValueType(VT));
4667 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4668 break;
4669 case ISD::SRL:
4670 // The input value must be properly zero extended.
4671 Tmp1 = PromoteOp(Node->getOperand(0));
4672 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4673 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4674 break;
4675
4676 case ISD::VAARG:
4677 Tmp1 = Node->getOperand(0); // Get the chain.
4678 Tmp2 = Node->getOperand(1); // Get the pointer.
4679 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4680 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sandsac496a12008-07-04 11:47:58 +00004681 Result = TLI.LowerOperation(Tmp3, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004682 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004683 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00004684 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004685 // Increment the pointer, VAList, to the next vaarg
4686 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands92c43912008-06-06 12:08:01 +00004687 DAG.getConstant(VT.getSizeInBits()/8,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004688 TLI.getPointerTy()));
4689 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00004690 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004691 // Load the actual argument out of the pointer VAList
4692 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4693 }
4694 // Remember that we legalized the chain.
4695 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4696 break;
4697
4698 case ISD::LOAD: {
4699 LoadSDNode *LD = cast<LoadSDNode>(Node);
4700 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4701 ? ISD::EXTLOAD : LD->getExtensionType();
4702 Result = DAG.getExtLoad(ExtType, NVT,
4703 LD->getChain(), LD->getBasePtr(),
4704 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004705 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004706 LD->isVolatile(),
4707 LD->getAlignment());
4708 // Remember that we legalized the chain.
4709 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4710 break;
4711 }
Scott Michel67224b22008-06-02 22:18:03 +00004712 case ISD::SELECT: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004713 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4714 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel67224b22008-06-02 22:18:03 +00004715
Duncan Sands92c43912008-06-06 12:08:01 +00004716 MVT VT2 = Tmp2.getValueType();
Scott Michel67224b22008-06-02 22:18:03 +00004717 assert(VT2 == Tmp3.getValueType()
Scott Michel7b54de02008-06-03 19:13:20 +00004718 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4719 // Ensure that the resulting node is at least the same size as the operands'
4720 // value types, because we cannot assume that TLI.getSetCCValueType() is
4721 // constant.
4722 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004723 break;
Scott Michel67224b22008-06-02 22:18:03 +00004724 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004725 case ISD::SELECT_CC:
4726 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4727 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4728 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4729 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4730 break;
4731 case ISD::BSWAP:
4732 Tmp1 = Node->getOperand(0);
4733 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4734 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4735 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004736 DAG.getConstant(NVT.getSizeInBits() -
4737 VT.getSizeInBits(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004738 TLI.getShiftAmountTy()));
4739 break;
4740 case ISD::CTPOP:
4741 case ISD::CTTZ:
4742 case ISD::CTLZ:
4743 // Zero extend the argument
4744 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4745 // Perform the larger operation, then subtract if needed.
4746 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4747 switch(Node->getOpcode()) {
4748 case ISD::CTPOP:
4749 Result = Tmp1;
4750 break;
4751 case ISD::CTTZ:
4752 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00004753 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004754 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004755 ISD::SETEQ);
4756 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00004757 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004758 break;
4759 case ISD::CTLZ:
4760 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4761 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004762 DAG.getConstant(NVT.getSizeInBits() -
4763 VT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004764 break;
4765 }
4766 break;
4767 case ISD::EXTRACT_SUBVECTOR:
4768 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4769 break;
4770 case ISD::EXTRACT_VECTOR_ELT:
4771 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4772 break;
4773 }
4774
Gabor Greif1c80d112008-08-28 21:40:38 +00004775 assert(Result.getNode() && "Didn't set a result!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004776
4777 // Make sure the result is itself legal.
4778 Result = LegalizeOp(Result);
4779
4780 // Remember that we promoted this!
4781 AddPromotedOperand(Op, Result);
4782 return Result;
4783}
4784
4785/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4786/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4787/// based on the vector type. The return type of this matches the element type
4788/// of the vector, which may not be legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00004789SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004790 // We know that operand #0 is the Vec vector. If the index is a constant
4791 // or if the invec is a supported hardware type, we can use it. Otherwise,
4792 // lower to a store then an indexed load.
Dan Gohman8181bd12008-07-27 21:46:04 +00004793 SDValue Vec = Op.getOperand(0);
4794 SDValue Idx = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004795
Duncan Sands92c43912008-06-06 12:08:01 +00004796 MVT TVT = Vec.getValueType();
4797 unsigned NumElems = TVT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004798
4799 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4800 default: assert(0 && "This action is not supported yet!");
4801 case TargetLowering::Custom: {
4802 Vec = LegalizeOp(Vec);
4803 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004804 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004805 if (Tmp3.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004806 return Tmp3;
4807 break;
4808 }
4809 case TargetLowering::Legal:
4810 if (isTypeLegal(TVT)) {
4811 Vec = LegalizeOp(Vec);
4812 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004813 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004814 }
4815 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00004816 case TargetLowering::Promote:
4817 assert(TVT.isVector() && "not vector type");
4818 // fall thru to expand since vectors are by default are promote
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004819 case TargetLowering::Expand:
4820 break;
4821 }
4822
4823 if (NumElems == 1) {
4824 // This must be an access of the only element. Return it.
4825 Op = ScalarizeVectorOp(Vec);
4826 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00004827 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004828 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004829 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004830 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004831 if (CIdx->getZExtValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004832 Vec = Lo;
4833 } else {
4834 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004835 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004836 Idx.getValueType());
4837 }
4838
4839 // It's now an extract from the appropriate high or low part. Recurse.
4840 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4841 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4842 } else {
4843 // Store the value to a temporary stack slot, then LOAD the scalar
4844 // element back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00004845 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4846 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004847
4848 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +00004849 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004850 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4851 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004852
Duncan Sandsec142ee2008-06-08 20:54:56 +00004853 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00004854 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004855 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00004856 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004857
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004858 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4859
4860 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
4861 }
4862 return Op;
4863}
4864
4865/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
4866/// we assume the operation can be split if it is not already legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00004867SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004868 // We know that operand #0 is the Vec vector. For now we assume the index
4869 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman8181bd12008-07-27 21:46:04 +00004870 SDValue Vec = Op.getOperand(0);
4871 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004872
Duncan Sands92c43912008-06-06 12:08:01 +00004873 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004874
Duncan Sands92c43912008-06-06 12:08:01 +00004875 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004876 // This must be an access of the desired vector length. Return it.
4877 return Vec;
4878 }
4879
4880 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004881 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004882 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004883 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004884 Vec = Lo;
4885 } else {
4886 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004887 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
4888 Idx.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004889 }
4890
4891 // It's now an extract from the appropriate high or low part. Recurse.
4892 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4893 return ExpandEXTRACT_SUBVECTOR(Op);
4894}
4895
4896/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4897/// with condition CC on the current target. This usually involves legalizing
4898/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4899/// there may be no choice but to create a new SetCC node to represent the
4900/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman8181bd12008-07-27 21:46:04 +00004901/// LHS, and the SDValue returned in RHS has a nil SDNode value.
4902void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
4903 SDValue &RHS,
4904 SDValue &CC) {
4905 SDValue Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004906
4907 switch (getTypeAction(LHS.getValueType())) {
4908 case Legal:
4909 Tmp1 = LegalizeOp(LHS); // LHS
4910 Tmp2 = LegalizeOp(RHS); // RHS
4911 break;
4912 case Promote:
4913 Tmp1 = PromoteOp(LHS); // LHS
4914 Tmp2 = PromoteOp(RHS); // RHS
4915
4916 // If this is an FP compare, the operands have already been extended.
Duncan Sands92c43912008-06-06 12:08:01 +00004917 if (LHS.getValueType().isInteger()) {
4918 MVT VT = LHS.getValueType();
4919 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004920
4921 // Otherwise, we have to insert explicit sign or zero extends. Note
4922 // that we could insert sign extends for ALL conditions, but zero extend
4923 // is cheaper on many machines (an AND instead of two shifts), so prefer
4924 // it.
4925 switch (cast<CondCodeSDNode>(CC)->get()) {
4926 default: assert(0 && "Unknown integer comparison!");
4927 case ISD::SETEQ:
4928 case ISD::SETNE:
4929 case ISD::SETUGE:
4930 case ISD::SETUGT:
4931 case ISD::SETULE:
4932 case ISD::SETULT:
4933 // ALL of these operations will work if we either sign or zero extend
4934 // the operands (including the unsigned comparisons!). Zero extend is
4935 // usually a simpler/cheaper operation, so prefer it.
4936 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4937 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4938 break;
4939 case ISD::SETGE:
4940 case ISD::SETGT:
4941 case ISD::SETLT:
4942 case ISD::SETLE:
4943 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4944 DAG.getValueType(VT));
4945 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4946 DAG.getValueType(VT));
Evan Chengd901b662008-10-13 18:46:18 +00004947 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
4948 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004949 break;
4950 }
4951 }
4952 break;
4953 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00004954 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004955 if (VT == MVT::f32 || VT == MVT::f64) {
4956 // Expand into one or more soft-fp libcall(s).
Evan Cheng24108632008-07-01 21:35:46 +00004957 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004958 switch (cast<CondCodeSDNode>(CC)->get()) {
4959 case ISD::SETEQ:
4960 case ISD::SETOEQ:
4961 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4962 break;
4963 case ISD::SETNE:
4964 case ISD::SETUNE:
4965 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
4966 break;
4967 case ISD::SETGE:
4968 case ISD::SETOGE:
4969 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4970 break;
4971 case ISD::SETLT:
4972 case ISD::SETOLT:
4973 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4974 break;
4975 case ISD::SETLE:
4976 case ISD::SETOLE:
4977 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4978 break;
4979 case ISD::SETGT:
4980 case ISD::SETOGT:
4981 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4982 break;
4983 case ISD::SETUO:
4984 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4985 break;
4986 case ISD::SETO:
4987 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
4988 break;
4989 default:
4990 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4991 switch (cast<CondCodeSDNode>(CC)->get()) {
4992 case ISD::SETONE:
4993 // SETONE = SETOLT | SETOGT
4994 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4995 // Fallthrough
4996 case ISD::SETUGT:
4997 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4998 break;
4999 case ISD::SETUGE:
5000 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5001 break;
5002 case ISD::SETULT:
5003 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5004 break;
5005 case ISD::SETULE:
5006 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5007 break;
5008 case ISD::SETUEQ:
5009 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5010 break;
5011 default: assert(0 && "Unsupported FP setcc!");
5012 }
5013 }
Duncan Sandsf19591c2008-06-30 10:19:09 +00005014
Dan Gohman8181bd12008-07-27 21:46:04 +00005015 SDValue Dummy;
5016 SDValue Ops[2] = { LHS, RHS };
Gabor Greif1c80d112008-08-28 21:40:38 +00005017 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005018 false /*sign irrelevant*/, Dummy);
5019 Tmp2 = DAG.getConstant(0, MVT::i32);
5020 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
5021 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel502151f2008-03-10 15:42:14 +00005022 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005023 CC);
Gabor Greif1c80d112008-08-28 21:40:38 +00005024 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005025 false /*sign irrelevant*/, Dummy);
Scott Michel502151f2008-03-10 15:42:14 +00005026 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005027 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
5028 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005029 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005030 }
Evan Cheng18a1ab12008-07-07 07:18:09 +00005031 LHS = LegalizeOp(Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005032 RHS = Tmp2;
5033 return;
5034 }
5035
Dan Gohman8181bd12008-07-27 21:46:04 +00005036 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005037 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005038 ExpandOp(RHS, RHSLo, RHSHi);
5039 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5040
5041 if (VT==MVT::ppcf128) {
5042 // FIXME: This generated code sucks. We want to generate
Dale Johannesen26317b62008-09-12 00:30:56 +00005043 // FCMPU crN, hi1, hi2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005044 // BNE crN, L:
Dale Johannesen26317b62008-09-12 00:30:56 +00005045 // FCMPU crN, lo1, lo2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005046 // The following can be improved, but not that much.
Dale Johannesen26317b62008-09-12 00:30:56 +00005047 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5048 ISD::SETOEQ);
Scott Michel502151f2008-03-10 15:42:14 +00005049 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005050 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Dale Johannesen26317b62008-09-12 00:30:56 +00005051 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5052 ISD::SETUNE);
Scott Michel502151f2008-03-10 15:42:14 +00005053 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005054 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5055 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00005056 Tmp2 = SDValue();
Dale Johannesen472d15d2007-10-06 01:24:11 +00005057 break;
5058 }
5059
5060 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005061 case ISD::SETEQ:
5062 case ISD::SETNE:
5063 if (RHSLo == RHSHi)
5064 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5065 if (RHSCST->isAllOnesValue()) {
5066 // Comparison to -1.
5067 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5068 Tmp2 = RHSLo;
5069 break;
5070 }
5071
5072 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5073 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5074 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5075 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5076 break;
5077 default:
5078 // If this is a comparison of the sign bit, just look at the top part.
5079 // X > -1, x < 0
5080 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5081 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00005082 CST->isNullValue()) || // X < 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005083 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5084 CST->isAllOnesValue())) { // X > -1
5085 Tmp1 = LHSHi;
5086 Tmp2 = RHSHi;
5087 break;
5088 }
5089
5090 // FIXME: This generated code sucks.
5091 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005092 switch (CCCode) {
5093 default: assert(0 && "Unknown integer setcc!");
5094 case ISD::SETLT:
5095 case ISD::SETULT: LowCC = ISD::SETULT; break;
5096 case ISD::SETGT:
5097 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5098 case ISD::SETLE:
5099 case ISD::SETULE: LowCC = ISD::SETULE; break;
5100 case ISD::SETGE:
5101 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5102 }
5103
5104 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5105 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5106 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5107
5108 // NOTE: on targets without efficient SELECT of bools, we can always use
5109 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
5110 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel502151f2008-03-10 15:42:14 +00005111 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005112 LowCC, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005113 if (!Tmp1.getNode())
Scott Michel502151f2008-03-10 15:42:14 +00005114 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
5115 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005116 CCCode, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005117 if (!Tmp2.getNode())
Scott Michel502151f2008-03-10 15:42:14 +00005118 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005119 RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005120
Gabor Greif1c80d112008-08-28 21:40:38 +00005121 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5122 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman9d24dc72008-03-13 22:13:53 +00005123 if ((Tmp1C && Tmp1C->isNullValue()) ||
5124 (Tmp2C && Tmp2C->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005125 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5126 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman9d24dc72008-03-13 22:13:53 +00005127 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005128 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5129 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5130 // low part is known false, returns high part.
5131 // For LE / GE, if high part is known false, ignore the low part.
5132 // For LT / GT, if high part is known true, ignore the low part.
5133 Tmp1 = Tmp2;
Dan Gohman8181bd12008-07-27 21:46:04 +00005134 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005135 } else {
Scott Michel502151f2008-03-10 15:42:14 +00005136 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005137 ISD::SETEQ, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005138 if (!Result.getNode())
Scott Michel502151f2008-03-10 15:42:14 +00005139 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005140 ISD::SETEQ);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005141 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5142 Result, Tmp1, Tmp2));
5143 Tmp1 = Result;
Dan Gohman8181bd12008-07-27 21:46:04 +00005144 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005145 }
5146 }
5147 }
5148 }
5149 LHS = Tmp1;
5150 RHS = Tmp2;
5151}
5152
Evan Cheng71343822008-10-15 02:05:31 +00005153/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5154/// condition code CC on the current target. This routine assumes LHS and rHS
5155/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5156/// illegal condition code into AND / OR of multiple SETCC values.
5157void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5158 SDValue &LHS, SDValue &RHS,
5159 SDValue &CC) {
5160 MVT OpVT = LHS.getValueType();
5161 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5162 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5163 default: assert(0 && "Unknown condition code action!");
5164 case TargetLowering::Legal:
5165 // Nothing to do.
5166 break;
5167 case TargetLowering::Expand: {
5168 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5169 unsigned Opc = 0;
5170 switch (CCCode) {
5171 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohman2b5b9ca2008-10-21 03:12:54 +00005172 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5173 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5174 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5175 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5176 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5177 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5178 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5179 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5180 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5181 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5182 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5183 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng71343822008-10-15 02:05:31 +00005184 // FIXME: Implement more expansions.
5185 }
5186
5187 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5188 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5189 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5190 RHS = SDValue();
5191 CC = SDValue();
5192 break;
5193 }
5194 }
5195}
5196
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005197/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5198/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5199/// a load from the stack slot to DestVT, extending it if needed.
5200/// The resultant code need not be legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00005201SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5202 MVT SlotVT,
5203 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005204 // Create the stack frame object.
Mon P Wang55854cc2008-07-05 20:40:31 +00005205 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5206 SrcOp.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00005207 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang55854cc2008-07-05 20:40:31 +00005208
Dan Gohman20e37962008-02-11 18:58:42 +00005209 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005210 int SPFI = StackPtrFI->getIndex();
Mon P Wang55854cc2008-07-05 20:40:31 +00005211
Duncan Sands92c43912008-06-06 12:08:01 +00005212 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5213 unsigned SlotSize = SlotVT.getSizeInBits();
5214 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang55854cc2008-07-05 20:40:31 +00005215 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5216 DestVT.getTypeForMVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005217
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005218 // Emit a store to the stack slot. Use a truncstore if the input value is
5219 // later than DestVT.
Dan Gohman8181bd12008-07-27 21:46:04 +00005220 SDValue Store;
Mon P Wang55854cc2008-07-05 20:40:31 +00005221
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005222 if (SrcSize > SlotSize)
Dan Gohman12a9c082008-02-06 22:27:42 +00005223 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005224 PseudoSourceValue::getFixedStack(SPFI), 0,
5225 SlotVT, false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005226 else {
5227 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman12a9c082008-02-06 22:27:42 +00005228 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005229 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang55854cc2008-07-05 20:40:31 +00005230 false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005231 }
5232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005233 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005234 if (SlotSize == DestSize)
Mon P Wang55854cc2008-07-05 20:40:31 +00005235 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005236
5237 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang55854cc2008-07-05 20:40:31 +00005238 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5239 false, DestAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005240}
5241
Dan Gohman8181bd12008-07-27 21:46:04 +00005242SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005243 // Create a vector sized/aligned stack slot, store the value to element #0,
5244 // then load the whole vector back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00005245 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman12a9c082008-02-06 22:27:42 +00005246
Dan Gohman20e37962008-02-11 18:58:42 +00005247 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005248 int SPFI = StackPtrFI->getIndex();
5249
Dan Gohman8181bd12008-07-27 21:46:04 +00005250 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005251 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00005252 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005253 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005254}
5255
5256
5257/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
5258/// support the operation, but do support the resultant vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00005259SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005260
5261 // If the only non-undef value is the low element, turn this into a
5262 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
5263 unsigned NumElems = Node->getNumOperands();
5264 bool isOnlyLowElement = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005265 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005266
Dan Gohman8181bd12008-07-27 21:46:04 +00005267 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerd8cee732008-03-09 00:29:42 +00005268 // and use a bitmask instead of a list of elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00005269 std::map<SDValue, std::vector<unsigned> > Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005270 Values[SplatValue].push_back(0);
5271 bool isConstant = true;
5272 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5273 SplatValue.getOpcode() != ISD::UNDEF)
5274 isConstant = false;
5275
5276 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005277 SDValue V = Node->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005278 Values[V].push_back(i);
5279 if (V.getOpcode() != ISD::UNDEF)
5280 isOnlyLowElement = false;
5281 if (SplatValue != V)
Dan Gohman8181bd12008-07-27 21:46:04 +00005282 SplatValue = SDValue(0,0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005283
5284 // If this isn't a constant element or an undef, we can't use a constant
5285 // pool load.
5286 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5287 V.getOpcode() != ISD::UNDEF)
5288 isConstant = false;
5289 }
5290
5291 if (isOnlyLowElement) {
5292 // If the low element is an undef too, then this whole things is an undef.
5293 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5294 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5295 // Otherwise, turn this into a scalar_to_vector node.
5296 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5297 Node->getOperand(0));
5298 }
5299
5300 // If all elements are constants, create a load from the constant pool.
5301 if (isConstant) {
Duncan Sands92c43912008-06-06 12:08:01 +00005302 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005303 std::vector<Constant*> CV;
5304 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5305 if (ConstantFPSDNode *V =
5306 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005307 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005308 } else if (ConstantSDNode *V =
Chris Lattner5e0610f2008-04-20 00:41:09 +00005309 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005310 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005311 } else {
5312 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner5e0610f2008-04-20 00:41:09 +00005313 const Type *OpNTy =
Duncan Sands92c43912008-06-06 12:08:01 +00005314 Node->getOperand(0).getValueType().getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005315 CV.push_back(UndefValue::get(OpNTy));
5316 }
5317 }
5318 Constant *CP = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005319 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005320 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman12a9c082008-02-06 22:27:42 +00005321 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005322 PseudoSourceValue::getConstantPool(), 0,
5323 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005324 }
5325
Gabor Greif1c80d112008-08-28 21:40:38 +00005326 if (SplatValue.getNode()) { // Splat of one value?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005327 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands92c43912008-06-06 12:08:01 +00005328 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman8181bd12008-07-27 21:46:04 +00005329 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5330 std::vector<SDValue> ZeroVec(NumElems, Zero);
5331 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005332 &ZeroVec[0], ZeroVec.size());
5333
5334 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
5335 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
5336 // Get the splatted value into the low element of a vector register.
Dan Gohman8181bd12008-07-27 21:46:04 +00005337 SDValue LowValVec =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005338 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5339
5340 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5341 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5342 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5343 SplatMask);
5344 }
5345 }
5346
5347 // If there are only two unique elements, we may be able to turn this into a
5348 // vector shuffle.
5349 if (Values.size() == 2) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005350 // Get the two values in deterministic order.
Dan Gohman8181bd12008-07-27 21:46:04 +00005351 SDValue Val1 = Node->getOperand(1);
5352 SDValue Val2;
5353 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerd8cee732008-03-09 00:29:42 +00005354 if (MI->first != Val1)
5355 Val2 = MI->first;
5356 else
5357 Val2 = (++MI)->first;
5358
5359 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5360 // vector shuffle has the undef vector on the RHS.
5361 if (Val1.getOpcode() == ISD::UNDEF)
5362 std::swap(Val1, Val2);
5363
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005364 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands92c43912008-06-06 12:08:01 +00005365 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5366 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005367 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005368
5369 // Set elements of the shuffle mask for Val1.
5370 std::vector<unsigned> &Val1Elts = Values[Val1];
5371 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5372 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5373
5374 // Set elements of the shuffle mask for Val2.
5375 std::vector<unsigned> &Val2Elts = Values[Val2];
5376 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5377 if (Val2.getOpcode() != ISD::UNDEF)
5378 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5379 else
5380 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5381
Dan Gohman8181bd12008-07-27 21:46:04 +00005382 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005383 &MaskVec[0], MaskVec.size());
5384
Chris Lattnerd8cee732008-03-09 00:29:42 +00005385 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005386 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5387 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005388 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5389 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005390 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005391
5392 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerd8cee732008-03-09 00:29:42 +00005393 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005394 }
5395 }
5396
5397 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5398 // aligned object on the stack, store each element into it, then load
5399 // the result as a vector.
Duncan Sands92c43912008-06-06 12:08:01 +00005400 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005401 // Create the stack frame object.
Dan Gohman8181bd12008-07-27 21:46:04 +00005402 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005403
5404 // Emit a store of each element to the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00005405 SmallVector<SDValue, 8> Stores;
Duncan Sands92c43912008-06-06 12:08:01 +00005406 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005407 // Store (in the right endianness) the elements to memory.
5408 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5409 // Ignore undef elements.
5410 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5411
5412 unsigned Offset = TypeByteSize*i;
5413
Dan Gohman8181bd12008-07-27 21:46:04 +00005414 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005415 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5416
5417 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
5418 NULL, 0));
5419 }
5420
Dan Gohman8181bd12008-07-27 21:46:04 +00005421 SDValue StoreChain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005422 if (!Stores.empty()) // Not all undef elements?
5423 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5424 &Stores[0], Stores.size());
5425 else
5426 StoreChain = DAG.getEntryNode();
5427
5428 // Result is a load from the stack slot.
5429 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
5430}
5431
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005432void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman8181bd12008-07-27 21:46:04 +00005433 SDValue Op, SDValue Amt,
5434 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005435 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00005436 SDValue LHSL, LHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005437 ExpandOp(Op, LHSL, LHSH);
5438
Dan Gohman8181bd12008-07-27 21:46:04 +00005439 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands92c43912008-06-06 12:08:01 +00005440 MVT VT = LHSL.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005441 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5442 Hi = Lo.getValue(1);
5443}
5444
5445
5446/// ExpandShift - Try to find a clever way to expand this shift operation out to
5447/// smaller elements. If we can't find a way that is more efficient than a
5448/// libcall on this target, return false. Otherwise, return true with the
5449/// low-parts expanded into Lo and Hi.
Dan Gohman8181bd12008-07-27 21:46:04 +00005450bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5451 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005452 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5453 "This is not a shift!");
5454
Duncan Sands92c43912008-06-06 12:08:01 +00005455 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman8181bd12008-07-27 21:46:04 +00005456 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands92c43912008-06-06 12:08:01 +00005457 MVT ShTy = ShAmt.getValueType();
5458 unsigned ShBits = ShTy.getSizeInBits();
5459 unsigned VTBits = Op.getValueType().getSizeInBits();
5460 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005461
Chris Lattner8c931452007-10-14 20:35:12 +00005462 // Handle the case when Amt is an immediate.
Gabor Greif1c80d112008-08-28 21:40:38 +00005463 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005464 unsigned Cst = CN->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005465 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005466 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005467 ExpandOp(Op, InL, InH);
5468 switch(Opc) {
5469 case ISD::SHL:
5470 if (Cst > VTBits) {
5471 Lo = DAG.getConstant(0, NVT);
5472 Hi = DAG.getConstant(0, NVT);
5473 } else if (Cst > NVTBits) {
5474 Lo = DAG.getConstant(0, NVT);
5475 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5476 } else if (Cst == NVTBits) {
5477 Lo = DAG.getConstant(0, NVT);
5478 Hi = InL;
5479 } else {
5480 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5481 Hi = DAG.getNode(ISD::OR, NVT,
5482 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5483 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5484 }
5485 return true;
5486 case ISD::SRL:
5487 if (Cst > VTBits) {
5488 Lo = DAG.getConstant(0, NVT);
5489 Hi = DAG.getConstant(0, NVT);
5490 } else if (Cst > NVTBits) {
5491 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5492 Hi = DAG.getConstant(0, NVT);
5493 } else if (Cst == NVTBits) {
5494 Lo = InH;
5495 Hi = DAG.getConstant(0, NVT);
5496 } else {
5497 Lo = DAG.getNode(ISD::OR, NVT,
5498 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5499 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5500 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5501 }
5502 return true;
5503 case ISD::SRA:
5504 if (Cst > VTBits) {
5505 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5506 DAG.getConstant(NVTBits-1, ShTy));
5507 } else if (Cst > NVTBits) {
5508 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5509 DAG.getConstant(Cst-NVTBits, ShTy));
5510 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5511 DAG.getConstant(NVTBits-1, ShTy));
5512 } else if (Cst == NVTBits) {
5513 Lo = InH;
5514 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5515 DAG.getConstant(NVTBits-1, ShTy));
5516 } else {
5517 Lo = DAG.getNode(ISD::OR, NVT,
5518 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5519 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5520 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5521 }
5522 return true;
5523 }
5524 }
5525
5526 // Okay, the shift amount isn't constant. However, if we can tell that it is
5527 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005528 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5529 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005530 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5531
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005532 // If we know that if any of the high bits of the shift amount are one, then
5533 // we can do this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005534 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005535 // Mask out the high bit, which we know is set.
5536 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005537 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005538
5539 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005540 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005541 ExpandOp(Op, InL, InH);
5542 switch(Opc) {
5543 case ISD::SHL:
5544 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5545 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5546 return true;
5547 case ISD::SRL:
5548 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5549 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5550 return true;
5551 case ISD::SRA:
5552 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5553 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5554 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5555 return true;
5556 }
5557 }
5558
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005559 // If we know that the high bits of the shift amount are all zero, then we can
5560 // do this as a couple of simple shifts.
5561 if ((KnownZero & Mask) == Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005562 // Compute 32-amt.
Dan Gohman8181bd12008-07-27 21:46:04 +00005563 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005564 DAG.getConstant(NVTBits, Amt.getValueType()),
5565 Amt);
5566
5567 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005568 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005569 ExpandOp(Op, InL, InH);
5570 switch(Opc) {
5571 case ISD::SHL:
5572 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5573 Hi = DAG.getNode(ISD::OR, NVT,
5574 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5575 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5576 return true;
5577 case ISD::SRL:
5578 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5579 Lo = DAG.getNode(ISD::OR, NVT,
5580 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5581 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5582 return true;
5583 case ISD::SRA:
5584 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5585 Lo = DAG.getNode(ISD::OR, NVT,
5586 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5587 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5588 return true;
5589 }
5590 }
5591
5592 return false;
5593}
5594
5595
5596// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5597// does not fit into a register, return the lo part and set the hi part to the
5598// by-reg argument. If it does fit into a single register, return the result
5599// and leave the Hi part unset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005600SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5601 bool isSigned, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005602 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5603 // The input chain to this libcall is the entry node of the function.
5604 // Legalizing the call will automatically add the previous call to the
5605 // dependence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005606 SDValue InChain = DAG.getEntryNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005607
5608 TargetLowering::ArgListTy Args;
5609 TargetLowering::ArgListEntry Entry;
5610 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00005611 MVT ArgVT = Node->getOperand(i).getValueType();
5612 const Type *ArgTy = ArgVT.getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005613 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5614 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005615 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005616 Args.push_back(Entry);
5617 }
Bill Wendlingfef06052008-09-16 21:48:12 +00005618 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang1448aad2008-10-30 08:01:45 +00005619 TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005620
5621 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands92c43912008-06-06 12:08:01 +00005622 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman8181bd12008-07-27 21:46:04 +00005623 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen67cc9b62008-09-26 19:31:26 +00005624 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5625 CallingConv::C, false, Callee, Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005626
5627 // Legalize the call sequence, starting with the chain. This will advance
5628 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5629 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5630 LegalizeOp(CallInfo.second);
Dan Gohman8181bd12008-07-27 21:46:04 +00005631 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005632 switch (getTypeAction(CallInfo.first.getValueType())) {
5633 default: assert(0 && "Unknown thing");
5634 case Legal:
5635 Result = CallInfo.first;
5636 break;
5637 case Expand:
5638 ExpandOp(CallInfo.first, Result, Hi);
5639 break;
5640 }
5641 return Result;
5642}
5643
Dan Gohman29c3cef2008-08-14 20:04:46 +00005644/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5645///
5646SDValue SelectionDAGLegalize::
5647LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5648 bool isCustom = false;
5649 SDValue Tmp1;
5650 switch (getTypeAction(Op.getValueType())) {
5651 case Legal:
5652 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5653 Op.getValueType())) {
5654 default: assert(0 && "Unknown operation action!");
5655 case TargetLowering::Custom:
5656 isCustom = true;
5657 // FALLTHROUGH
5658 case TargetLowering::Legal:
5659 Tmp1 = LegalizeOp(Op);
Gabor Greif1c80d112008-08-28 21:40:38 +00005660 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005661 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5662 else
5663 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5664 DestTy, Tmp1);
5665 if (isCustom) {
5666 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005667 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman29c3cef2008-08-14 20:04:46 +00005668 }
5669 break;
5670 case TargetLowering::Expand:
5671 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5672 break;
5673 case TargetLowering::Promote:
5674 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5675 break;
5676 }
5677 break;
5678 case Expand:
5679 Result = ExpandIntToFP(isSigned, DestTy, Op);
5680 break;
5681 case Promote:
5682 Tmp1 = PromoteOp(Op);
5683 if (isSigned) {
5684 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5685 Tmp1, DAG.getValueType(Op.getValueType()));
5686 } else {
5687 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5688 Op.getValueType());
5689 }
Gabor Greif1c80d112008-08-28 21:40:38 +00005690 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005691 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5692 else
5693 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5694 DestTy, Tmp1);
5695 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5696 break;
5697 }
5698 return Result;
5699}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005700
5701/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5702///
Dan Gohman8181bd12008-07-27 21:46:04 +00005703SDValue SelectionDAGLegalize::
5704ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands92c43912008-06-06 12:08:01 +00005705 MVT SourceVT = Source.getValueType();
Dan Gohman8b232ff2008-03-11 01:59:03 +00005706 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005707
Dan Gohman29c3cef2008-08-14 20:04:46 +00005708 // Expand unsupported int-to-fp vector casts by unrolling them.
5709 if (DestTy.isVector()) {
5710 if (!ExpandSource)
5711 return LegalizeOp(UnrollVectorOp(Source));
5712 MVT DestEltTy = DestTy.getVectorElementType();
5713 if (DestTy.getVectorNumElements() == 1) {
5714 SDValue Scalar = ScalarizeVectorOp(Source);
5715 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5716 DestEltTy, Scalar);
5717 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5718 }
5719 SDValue Lo, Hi;
5720 SplitVectorOp(Source, Lo, Hi);
5721 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5722 DestTy.getVectorNumElements() / 2);
5723 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5724 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengd901b662008-10-13 18:46:18 +00005725 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5726 HiResult));
Dan Gohman29c3cef2008-08-14 20:04:46 +00005727 }
5728
Evan Chengf99a7752008-04-01 02:18:22 +00005729 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5730 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmana193dba2008-03-05 02:07:31 +00005731 // The integer value loaded will be incorrectly if the 'sign bit' of the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005732 // incoming integer is set. To handle this, we dynamically test to see if
5733 // it is set, and, if so, add a fudge factor.
Dan Gohman8181bd12008-07-27 21:46:04 +00005734 SDValue Hi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005735 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005736 SDValue Lo;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005737 ExpandOp(Source, Lo, Hi);
5738 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5739 } else {
5740 // The comparison for the sign bit will use the entire operand.
5741 Hi = Source;
5742 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005743
Dale Johannesen96db7962008-11-04 20:52:49 +00005744 // Check to see if the target has a custom way to lower this. If so, use
5745 // it. (Note we've already expanded the operand in this case.)
Dale Johannesena359b8b2008-10-21 20:50:01 +00005746 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5747 default: assert(0 && "This action not implemented for this operation!");
5748 case TargetLowering::Legal:
5749 case TargetLowering::Expand:
5750 break; // This case is handled below.
5751 case TargetLowering::Custom: {
5752 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5753 Source), DAG);
5754 if (NV.getNode())
5755 return LegalizeOp(NV);
5756 break; // The target decided this was legal after all
5757 }
5758 }
5759
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005760 // If this is unsigned, and not supported, first perform the conversion to
5761 // signed, then adjust the result if the sign bit is set.
Dan Gohman8181bd12008-07-27 21:46:04 +00005762 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005763
Dan Gohman8181bd12008-07-27 21:46:04 +00005764 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005765 DAG.getConstant(0, Hi.getValueType()),
5766 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005767 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5768 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005769 SignSet, Four, Zero);
5770 uint64_t FF = 0x5f800000ULL;
5771 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmana193dba2008-03-05 02:07:31 +00005772 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005773
Dan Gohman8181bd12008-07-27 21:46:04 +00005774 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005775 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005776 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00005777 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00005778 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005779 if (DestTy == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005780 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005781 PseudoSourceValue::getConstantPool(), 0,
5782 false, Alignment);
Duncan Sandsec142ee2008-06-08 20:54:56 +00005783 else if (DestTy.bitsGT(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005784 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005785 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00005786 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005787 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00005788 MVT::f32, false, Alignment);
Dale Johannesen2fc20782007-09-14 22:26:36 +00005789 else
5790 assert(0 && "Unexpected conversion");
5791
Duncan Sands92c43912008-06-06 12:08:01 +00005792 MVT SCVT = SignedConv.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005793 if (SCVT != DestTy) {
5794 // Destination type needs to be expanded as well. The FADD now we are
5795 // constructing will be expanded into a libcall.
Duncan Sands92c43912008-06-06 12:08:01 +00005796 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5797 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmanc98645c2008-03-05 01:08:17 +00005798 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005799 SignedConv, SignedConv.getValue(1));
5800 }
5801 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5802 }
5803 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5804 }
5805
5806 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmanc98645c2008-03-05 01:08:17 +00005807 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005808 default: assert(0 && "This action not implemented for this operation!");
5809 case TargetLowering::Legal:
5810 case TargetLowering::Expand:
5811 break; // This case is handled below.
5812 case TargetLowering::Custom: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005813 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005814 Source), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005815 if (NV.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005816 return LegalizeOp(NV);
5817 break; // The target decided this was legal after all
5818 }
5819 }
5820
5821 // Expand the source, then glue it back together for the call. We must expand
5822 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman8b232ff2008-03-11 01:59:03 +00005823 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005824 SDValue SrcLo, SrcHi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005825 ExpandOp(Source, SrcLo, SrcHi);
5826 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5827 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005828
Duncan Sandsf68dffb2008-07-17 02:36:29 +00005829 RTLIB::Libcall LC = isSigned ?
5830 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5831 RTLIB::getUINTTOFP(SourceVT, DestTy);
5832 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5833
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005834 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman8181bd12008-07-27 21:46:04 +00005835 SDValue HiPart;
Gabor Greif1c80d112008-08-28 21:40:38 +00005836 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5837 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmanec51f642008-03-10 23:03:31 +00005838 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5839 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005840}
5841
5842/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5843/// INT_TO_FP operation of the specified operand when the target requests that
5844/// we expand it. At this point, we know that the result and operand types are
5845/// legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00005846SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5847 SDValue Op0,
5848 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005849 if (Op0.getValueType() == MVT::i32) {
5850 // simple 32-bit [signed|unsigned] integer to float/double expansion
5851
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005852 // Get the stack frame index of a 8 byte buffer.
Dan Gohman8181bd12008-07-27 21:46:04 +00005853 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005854
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005855 // word offset constant for Hi/Lo address computation
Dan Gohman8181bd12008-07-27 21:46:04 +00005856 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005857 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman8181bd12008-07-27 21:46:04 +00005858 SDValue Hi = StackSlot;
5859 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005860 if (TLI.isLittleEndian())
5861 std::swap(Hi, Lo);
5862
5863 // if signed map to unsigned space
Dan Gohman8181bd12008-07-27 21:46:04 +00005864 SDValue Op0Mapped;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005865 if (isSigned) {
5866 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman8181bd12008-07-27 21:46:04 +00005867 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005868 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5869 } else {
5870 Op0Mapped = Op0;
5871 }
5872 // store the lo of the constructed double - based on integer input
Dan Gohman8181bd12008-07-27 21:46:04 +00005873 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005874 Op0Mapped, Lo, NULL, 0);
5875 // initial hi portion of constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00005876 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005877 // store the hi of the constructed double - biased exponent
Dan Gohman8181bd12008-07-27 21:46:04 +00005878 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005879 // load the constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00005880 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005881 // FP constant to bias correct the final result
Dan Gohman8181bd12008-07-27 21:46:04 +00005882 SDValue Bias = DAG.getConstantFP(isSigned ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005883 BitsToDouble(0x4330000080000000ULL)
5884 : BitsToDouble(0x4330000000000000ULL),
5885 MVT::f64);
5886 // subtract the bias
Dan Gohman8181bd12008-07-27 21:46:04 +00005887 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005888 // final result
Dan Gohman8181bd12008-07-27 21:46:04 +00005889 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005890 // handle final rounding
5891 if (DestVT == MVT::f64) {
5892 // do nothing
5893 Result = Sub;
Duncan Sandsec142ee2008-06-08 20:54:56 +00005894 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner5872a362008-01-17 07:00:52 +00005895 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5896 DAG.getIntPtrConstant(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00005897 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005898 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005899 }
5900 return Result;
5901 }
5902 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman8181bd12008-07-27 21:46:04 +00005903 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005904
Dan Gohman8181bd12008-07-27 21:46:04 +00005905 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005906 DAG.getConstant(0, Op0.getValueType()),
5907 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005908 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5909 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005910 SignSet, Four, Zero);
5911
5912 // If the sign bit of the integer is set, the large number will be treated
5913 // as a negative number. To counteract this, the dynamic code adds an
5914 // offset depending on the data type.
5915 uint64_t FF;
Duncan Sands92c43912008-06-06 12:08:01 +00005916 switch (Op0.getValueType().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005917 default: assert(0 && "Unsupported integer type!");
5918 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5919 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5920 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5921 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5922 }
5923 if (TLI.isLittleEndian()) FF <<= 32;
5924 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5925
Dan Gohman8181bd12008-07-27 21:46:04 +00005926 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005927 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005928 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00005929 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00005930 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005931 if (DestVT == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005932 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005933 PseudoSourceValue::getConstantPool(), 0,
5934 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005935 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00005936 FudgeInReg =
5937 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5938 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005939 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00005940 MVT::f32, false, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005941 }
5942
5943 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5944}
5945
5946/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5947/// *INT_TO_FP operation of the specified operand when the target requests that
5948/// we promote it. At this point, we know that the result and operand types are
5949/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5950/// operation that takes a larger input.
Dan Gohman8181bd12008-07-27 21:46:04 +00005951SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
5952 MVT DestVT,
5953 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005954 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00005955 MVT NewInTy = LegalOp.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005956
5957 unsigned OpToUse = 0;
5958
5959 // Scan for the appropriate larger type to use.
5960 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00005961 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
5962 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005963
5964 // If the target supports SINT_TO_FP of this type, use it.
5965 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5966 default: break;
5967 case TargetLowering::Legal:
5968 if (!TLI.isTypeLegal(NewInTy))
5969 break; // Can't use this datatype.
5970 // FALL THROUGH.
5971 case TargetLowering::Custom:
5972 OpToUse = ISD::SINT_TO_FP;
5973 break;
5974 }
5975 if (OpToUse) break;
5976 if (isSigned) continue;
5977
5978 // If the target supports UINT_TO_FP of this type, use it.
5979 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5980 default: break;
5981 case TargetLowering::Legal:
5982 if (!TLI.isTypeLegal(NewInTy))
5983 break; // Can't use this datatype.
5984 // FALL THROUGH.
5985 case TargetLowering::Custom:
5986 OpToUse = ISD::UINT_TO_FP;
5987 break;
5988 }
5989 if (OpToUse) break;
5990
5991 // Otherwise, try a larger type.
5992 }
5993
5994 // Okay, we found the operation and type to use. Zero extend our input to the
5995 // desired type then run the operation on it.
5996 return DAG.getNode(OpToUse, DestVT,
5997 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5998 NewInTy, LegalOp));
5999}
6000
6001/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6002/// FP_TO_*INT operation of the specified operand when the target requests that
6003/// we promote it. At this point, we know that the result and operand types are
6004/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6005/// operation that returns a larger result.
Dan Gohman8181bd12008-07-27 21:46:04 +00006006SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6007 MVT DestVT,
6008 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006009 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00006010 MVT NewOutTy = DestVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006011
6012 unsigned OpToUse = 0;
6013
6014 // Scan for the appropriate larger type to use.
6015 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00006016 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6017 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006018
6019 // If the target supports FP_TO_SINT returning this type, use it.
6020 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6021 default: break;
6022 case TargetLowering::Legal:
6023 if (!TLI.isTypeLegal(NewOutTy))
6024 break; // Can't use this datatype.
6025 // FALL THROUGH.
6026 case TargetLowering::Custom:
6027 OpToUse = ISD::FP_TO_SINT;
6028 break;
6029 }
6030 if (OpToUse) break;
6031
6032 // If the target supports FP_TO_UINT of this type, use it.
6033 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6034 default: break;
6035 case TargetLowering::Legal:
6036 if (!TLI.isTypeLegal(NewOutTy))
6037 break; // Can't use this datatype.
6038 // FALL THROUGH.
6039 case TargetLowering::Custom:
6040 OpToUse = ISD::FP_TO_UINT;
6041 break;
6042 }
6043 if (OpToUse) break;
6044
6045 // Otherwise, try a larger type.
6046 }
6047
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006048
6049 // Okay, we found the operation and type to use.
Dan Gohman8181bd12008-07-27 21:46:04 +00006050 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sandsac496a12008-07-04 11:47:58 +00006051
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006052 // If the operation produces an invalid type, it must be custom lowered. Use
6053 // the target lowering hooks to expand it. Just keep the low part of the
6054 // expanded operation, we know that we're truncating anyway.
6055 if (getTypeAction(NewOutTy) == Expand) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006056 Operation = SDValue(TLI.ReplaceNodeResults(Operation.getNode(), DAG), 0);
6057 assert(Operation.getNode() && "Didn't return anything");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006058 }
Duncan Sandsac496a12008-07-04 11:47:58 +00006059
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006060 // Truncate the result of the extended FP_TO_*INT operation to the desired
6061 // size.
6062 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006063}
6064
6065/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6066///
Dan Gohman8181bd12008-07-27 21:46:04 +00006067SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00006068 MVT VT = Op.getValueType();
6069 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00006070 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands92c43912008-06-06 12:08:01 +00006071 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006072 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6073 case MVT::i16:
6074 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6075 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6076 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6077 case MVT::i32:
6078 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6079 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6080 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6081 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6082 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6083 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6084 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6085 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6086 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6087 case MVT::i64:
6088 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6089 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6090 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6091 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6092 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6093 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6094 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6095 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6096 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6097 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6098 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6099 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6100 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6101 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6102 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6103 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6104 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6105 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6106 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6107 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6108 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6109 }
6110}
6111
6112/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6113///
Dan Gohman8181bd12008-07-27 21:46:04 +00006114SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006115 switch (Opc) {
6116 default: assert(0 && "Cannot expand this yet!");
6117 case ISD::CTPOP: {
6118 static const uint64_t mask[6] = {
6119 0x5555555555555555ULL, 0x3333333333333333ULL,
6120 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6121 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6122 };
Duncan Sands92c43912008-06-06 12:08:01 +00006123 MVT VT = Op.getValueType();
6124 MVT ShVT = TLI.getShiftAmountTy();
6125 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006126 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6127 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman8181bd12008-07-27 21:46:04 +00006128 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6129 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006130 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6131 DAG.getNode(ISD::AND, VT,
6132 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6133 }
6134 return Op;
6135 }
6136 case ISD::CTLZ: {
6137 // for now, we do this:
6138 // x = x | (x >> 1);
6139 // x = x | (x >> 2);
6140 // ...
6141 // x = x | (x >>16);
6142 // x = x | (x >>32); // for 64-bit input
6143 // return popcount(~x);
6144 //
6145 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006146 MVT VT = Op.getValueType();
6147 MVT ShVT = TLI.getShiftAmountTy();
6148 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006149 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006150 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006151 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6152 }
6153 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6154 return DAG.getNode(ISD::CTPOP, VT, Op);
6155 }
6156 case ISD::CTTZ: {
6157 // for now, we use: { return popcount(~x & (x - 1)); }
6158 // unless the target has ctlz but not ctpop, in which case we use:
6159 // { return 32 - nlz(~x & (x-1)); }
6160 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006161 MVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006162 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6163 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006164 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6165 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6166 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6167 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6168 TLI.isOperationLegal(ISD::CTLZ, VT))
6169 return DAG.getNode(ISD::SUB, VT,
Duncan Sands92c43912008-06-06 12:08:01 +00006170 DAG.getConstant(VT.getSizeInBits(), VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006171 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6172 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6173 }
6174 }
6175}
6176
Dan Gohman8181bd12008-07-27 21:46:04 +00006177/// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006178/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman4fc03742008-10-01 15:07:49 +00006179/// LegalizedNodes map is filled in for any results that are not expanded, the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006180/// ExpandedNodes map is filled in for any results that are expanded, and the
6181/// Lo/Hi values are returned.
Dan Gohman8181bd12008-07-27 21:46:04 +00006182void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands92c43912008-06-06 12:08:01 +00006183 MVT VT = Op.getValueType();
6184 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greif1c80d112008-08-28 21:40:38 +00006185 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006186 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00006187 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands92c43912008-06-06 12:08:01 +00006188 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006189
6190 // See if we already expanded it.
Dan Gohman8181bd12008-07-27 21:46:04 +00006191 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006192 = ExpandedNodes.find(Op);
6193 if (I != ExpandedNodes.end()) {
6194 Lo = I->second.first;
6195 Hi = I->second.second;
6196 return;
6197 }
6198
6199 switch (Node->getOpcode()) {
6200 case ISD::CopyFromReg:
6201 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006202 case ISD::FP_ROUND_INREG:
6203 if (VT == MVT::ppcf128 &&
6204 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6205 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006206 SDValue SrcLo, SrcHi, Src;
Dale Johannesend3b6af32007-10-11 23:32:15 +00006207 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6208 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006209 SDValue Result = TLI.LowerOperation(
Dale Johannesend3b6af32007-10-11 23:32:15 +00006210 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006211 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6212 Lo = Result.getNode()->getOperand(0);
6213 Hi = Result.getNode()->getOperand(1);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006214 break;
6215 }
6216 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006217 default:
6218#ifndef NDEBUG
6219 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
6220#endif
6221 assert(0 && "Do not know how to expand this operator!");
6222 abort();
Dan Gohman550c8462008-02-27 01:52:30 +00006223 case ISD::EXTRACT_ELEMENT:
6224 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006225 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman550c8462008-02-27 01:52:30 +00006226 return ExpandOp(Hi, Lo, Hi);
Dan Gohman7e7aa2c2008-02-27 19:44:57 +00006227 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006228 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006229 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6230 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6231 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006232 case ISD::UNDEF:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006233 Lo = DAG.getNode(ISD::UNDEF, NVT);
6234 Hi = DAG.getNode(ISD::UNDEF, NVT);
6235 break;
6236 case ISD::Constant: {
Duncan Sands92c43912008-06-06 12:08:01 +00006237 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman97f1f8e2008-03-03 22:20:46 +00006238 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6239 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6240 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006241 break;
6242 }
6243 case ISD::ConstantFP: {
6244 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00006245 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00006246 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen2aef5692007-10-11 18:07:22 +00006247 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6248 MVT::f64);
6249 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6250 MVT::f64);
6251 break;
6252 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006253 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
6254 if (getTypeAction(Lo.getValueType()) == Expand)
6255 ExpandOp(Lo, Lo, Hi);
6256 break;
6257 }
6258 case ISD::BUILD_PAIR:
6259 // Return the operands.
6260 Lo = Node->getOperand(0);
6261 Hi = Node->getOperand(1);
6262 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006263
6264 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00006265 if (Node->getNumValues() == 1) {
6266 ExpandOp(Op.getOperand(0), Lo, Hi);
6267 break;
6268 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006269 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif46bf5472008-08-26 22:36:50 +00006270 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006271 Op.getValue(1).getValueType() == MVT::Other &&
6272 "unhandled MERGE_VALUES");
6273 ExpandOp(Op.getOperand(0), Lo, Hi);
6274 // Remember that we legalized the chain.
6275 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6276 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006277
6278 case ISD::SIGN_EXTEND_INREG:
6279 ExpandOp(Node->getOperand(0), Lo, Hi);
6280 // sext_inreg the low part if needed.
6281 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6282
6283 // The high part gets the sign extension from the lo-part. This handles
6284 // things like sextinreg V:i64 from i8.
6285 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands92c43912008-06-06 12:08:01 +00006286 DAG.getConstant(NVT.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006287 TLI.getShiftAmountTy()));
6288 break;
6289
6290 case ISD::BSWAP: {
6291 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006292 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006293 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6294 Lo = TempLo;
6295 break;
6296 }
6297
6298 case ISD::CTPOP:
6299 ExpandOp(Node->getOperand(0), Lo, Hi);
6300 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6301 DAG.getNode(ISD::CTPOP, NVT, Lo),
6302 DAG.getNode(ISD::CTPOP, NVT, Hi));
6303 Hi = DAG.getConstant(0, NVT);
6304 break;
6305
6306 case ISD::CTLZ: {
6307 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
6308 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006309 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6310 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
6311 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006312 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006313 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006314 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6315
6316 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6317 Hi = DAG.getConstant(0, NVT);
6318 break;
6319 }
6320
6321 case ISD::CTTZ: {
6322 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
6323 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006324 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6325 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
6326 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006327 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006328 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006329 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6330
6331 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6332 Hi = DAG.getConstant(0, NVT);
6333 break;
6334 }
6335
6336 case ISD::VAARG: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006337 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6338 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006339 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6340 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6341
6342 // Remember that we legalized the chain.
6343 Hi = LegalizeOp(Hi);
6344 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006345 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006346 std::swap(Lo, Hi);
6347 break;
6348 }
6349
6350 case ISD::LOAD: {
6351 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00006352 SDValue Ch = LD->getChain(); // Legalize the chain.
6353 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006354 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman29c3cef2008-08-14 20:04:46 +00006355 const Value *SV = LD->getSrcValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006356 int SVOffset = LD->getSrcValueOffset();
6357 unsigned Alignment = LD->getAlignment();
6358 bool isVolatile = LD->isVolatile();
6359
6360 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman29c3cef2008-08-14 20:04:46 +00006361 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006362 isVolatile, Alignment);
6363 if (VT == MVT::f32 || VT == MVT::f64) {
6364 // f32->i32 or f64->i64 one to one expansion.
6365 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006366 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006367 // Recursively expand the new load.
6368 if (getTypeAction(NVT) == Expand)
6369 ExpandOp(Lo, Lo, Hi);
6370 break;
6371 }
6372
6373 // Increment the pointer to the other half.
Duncan Sands92c43912008-06-06 12:08:01 +00006374 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006375 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006376 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006377 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006378 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00006379 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006380 isVolatile, Alignment);
6381
6382 // Build a factor node to remember that this load is independent of the
6383 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00006384 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006385 Hi.getValue(1));
6386
6387 // Remember that we legalized the chain.
6388 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006389 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006390 std::swap(Lo, Hi);
6391 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00006392 MVT EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006393
Dale Johannesen2550e3a2007-10-19 20:29:00 +00006394 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6395 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006396 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman29c3cef2008-08-14 20:04:46 +00006397 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006398 SVOffset, isVolatile, Alignment);
6399 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006400 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006401 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6402 break;
6403 }
6404
6405 if (EVT == NVT)
Dan Gohman29c3cef2008-08-14 20:04:46 +00006406 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006407 SVOffset, isVolatile, Alignment);
6408 else
Dan Gohman29c3cef2008-08-14 20:04:46 +00006409 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006410 SVOffset, EVT, isVolatile,
6411 Alignment);
6412
6413 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006414 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006415
6416 if (ExtType == ISD::SEXTLOAD) {
6417 // The high part is obtained by SRA'ing all but one of the bits of the
6418 // lo part.
Duncan Sands92c43912008-06-06 12:08:01 +00006419 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006420 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6421 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6422 } else if (ExtType == ISD::ZEXTLOAD) {
6423 // The high part is just a zero.
6424 Hi = DAG.getConstant(0, NVT);
6425 } else /* if (ExtType == ISD::EXTLOAD) */ {
6426 // The high part is undefined.
6427 Hi = DAG.getNode(ISD::UNDEF, NVT);
6428 }
6429 }
6430 break;
6431 }
6432 case ISD::AND:
6433 case ISD::OR:
6434 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +00006435 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006436 ExpandOp(Node->getOperand(0), LL, LH);
6437 ExpandOp(Node->getOperand(1), RL, RH);
6438 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6439 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6440 break;
6441 }
6442 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006443 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006444 ExpandOp(Node->getOperand(1), LL, LH);
6445 ExpandOp(Node->getOperand(2), RL, RH);
6446 if (getTypeAction(NVT) == Expand)
6447 NVT = TLI.getTypeToExpandTo(NVT);
6448 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
6449 if (VT != MVT::f32)
6450 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
6451 break;
6452 }
6453 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006454 SDValue TL, TH, FL, FH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006455 ExpandOp(Node->getOperand(2), TL, TH);
6456 ExpandOp(Node->getOperand(3), FL, FH);
6457 if (getTypeAction(NVT) == Expand)
6458 NVT = TLI.getTypeToExpandTo(NVT);
6459 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6460 Node->getOperand(1), TL, FL, Node->getOperand(4));
6461 if (VT != MVT::f32)
6462 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6463 Node->getOperand(1), TH, FH, Node->getOperand(4));
6464 break;
6465 }
6466 case ISD::ANY_EXTEND:
6467 // The low part is any extension of the input (which degenerates to a copy).
6468 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6469 // The high part is undefined.
6470 Hi = DAG.getNode(ISD::UNDEF, NVT);
6471 break;
6472 case ISD::SIGN_EXTEND: {
6473 // The low part is just a sign extension of the input (which degenerates to
6474 // a copy).
6475 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
6476
6477 // The high part is obtained by SRA'ing all but one of the bits of the lo
6478 // part.
Duncan Sands92c43912008-06-06 12:08:01 +00006479 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006480 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6481 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6482 break;
6483 }
6484 case ISD::ZERO_EXTEND:
6485 // The low part is just a zero extension of the input (which degenerates to
6486 // a copy).
6487 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
6488
6489 // The high part is just a zero.
6490 Hi = DAG.getConstant(0, NVT);
6491 break;
6492
6493 case ISD::TRUNCATE: {
6494 // The input value must be larger than this value. Expand *it*.
Dan Gohman8181bd12008-07-27 21:46:04 +00006495 SDValue NewLo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006496 ExpandOp(Node->getOperand(0), NewLo, Hi);
6497
6498 // The low part is now either the right size, or it is closer. If not the
6499 // right size, make an illegal truncate so we recursively expand it.
6500 if (NewLo.getValueType() != Node->getValueType(0))
6501 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6502 ExpandOp(NewLo, Lo, Hi);
6503 break;
6504 }
6505
6506 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006507 SDValue Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006508 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6509 // If the target wants to, allow it to lower this itself.
6510 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6511 case Expand: assert(0 && "cannot expand FP!");
6512 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6513 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6514 }
6515 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6516 }
6517
6518 // f32 / f64 must be expanded to i32 / i64.
6519 if (VT == MVT::f32 || VT == MVT::f64) {
6520 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6521 if (getTypeAction(NVT) == Expand)
6522 ExpandOp(Lo, Lo, Hi);
6523 break;
6524 }
6525
6526 // If source operand will be expanded to the same type as VT, i.e.
6527 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands92c43912008-06-06 12:08:01 +00006528 MVT VT0 = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006529 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6530 ExpandOp(Node->getOperand(0), Lo, Hi);
6531 break;
6532 }
6533
6534 // Turn this into a load/store pair by default.
Gabor Greif1c80d112008-08-28 21:40:38 +00006535 if (Tmp.getNode() == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00006536 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006537
6538 ExpandOp(Tmp, Lo, Hi);
6539 break;
6540 }
6541
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006542 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006543 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6544 TargetLowering::Custom &&
6545 "Must custom expand ReadCycleCounter");
Dan Gohman8181bd12008-07-27 21:46:04 +00006546 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006547 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006548 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006549 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006550 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006551 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006552 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006553
Dale Johannesen44eb5372008-10-03 19:41:08 +00006554 case ISD::ATOMIC_CMP_SWAP_64: {
6555 // This operation does not need a loop.
6556 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6557 assert(Tmp.getNode() && "Node must be custom expanded!");
6558 ExpandOp(Tmp.getValue(0), Lo, Hi);
6559 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6560 LegalizeOp(Tmp.getValue(1)));
6561 break;
6562 }
6563
Dale Johannesenf160d802008-10-02 18:53:47 +00006564 case ISD::ATOMIC_LOAD_ADD_64:
6565 case ISD::ATOMIC_LOAD_SUB_64:
6566 case ISD::ATOMIC_LOAD_AND_64:
6567 case ISD::ATOMIC_LOAD_OR_64:
6568 case ISD::ATOMIC_LOAD_XOR_64:
6569 case ISD::ATOMIC_LOAD_NAND_64:
Dale Johannesen44eb5372008-10-03 19:41:08 +00006570 case ISD::ATOMIC_SWAP_64: {
6571 // These operations require a loop to be generated. We can't do that yet,
6572 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesenf160d802008-10-02 18:53:47 +00006573 SDValue In2Lo, In2Hi, In2;
6574 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6575 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006576 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6577 SDValue Replace =
6578 DAG.getAtomic(Op.getOpcode(), Op.getOperand(0), Op.getOperand(1), In2,
6579 Anode->getSrcValue(), Anode->getAlignment());
6580 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006581 ExpandOp(Result.getValue(0), Lo, Hi);
6582 // Remember that we legalized the chain.
6583 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharth81580822008-03-05 01:15:49 +00006584 break;
6585 }
6586
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006587 // These operators cannot be expanded directly, emit them as calls to
6588 // library functions.
6589 case ISD::FP_TO_SINT: {
6590 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006591 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006592 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6593 case Expand: assert(0 && "cannot expand FP!");
6594 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6595 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6596 }
6597
6598 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6599
6600 // Now that the custom expander is done, expand the result, which is still
6601 // VT.
Gabor Greif1c80d112008-08-28 21:40:38 +00006602 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006603 ExpandOp(Op, Lo, Hi);
6604 break;
6605 }
6606 }
6607
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006608 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6609 VT);
6610 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6611 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006612 break;
6613 }
6614
6615 case ISD::FP_TO_UINT: {
6616 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006617 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006618 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6619 case Expand: assert(0 && "cannot expand FP!");
6620 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6621 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6622 }
6623
6624 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6625
6626 // Now that the custom expander is done, expand the result.
Gabor Greif1c80d112008-08-28 21:40:38 +00006627 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006628 ExpandOp(Op, Lo, Hi);
6629 break;
6630 }
6631 }
6632
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006633 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6634 VT);
6635 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6636 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006637 break;
6638 }
6639
6640 case ISD::SHL: {
6641 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006642 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006643 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006644 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006645 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006646 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006647 // Now that the custom expander is done, expand the result, which is
6648 // still VT.
6649 ExpandOp(Op, Lo, Hi);
6650 break;
6651 }
6652 }
6653
6654 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6655 // this X << 1 as X+X.
6656 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00006657 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006658 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006659 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006660 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6661 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6662 LoOps[1] = LoOps[0];
6663 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6664
6665 HiOps[1] = HiOps[0];
6666 HiOps[2] = Lo.getValue(1);
6667 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6668 break;
6669 }
6670 }
6671
6672 // If we can emit an efficient shift operation, do so now.
6673 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6674 break;
6675
6676 // If this target supports SHL_PARTS, use it.
6677 TargetLowering::LegalizeAction Action =
6678 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6679 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6680 Action == TargetLowering::Custom) {
6681 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6682 break;
6683 }
6684
6685 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006686 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006687 break;
6688 }
6689
6690 case ISD::SRA: {
6691 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006692 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006693 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006694 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006695 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006696 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006697 // Now that the custom expander is done, expand the result, which is
6698 // still VT.
6699 ExpandOp(Op, Lo, Hi);
6700 break;
6701 }
6702 }
6703
6704 // If we can emit an efficient shift operation, do so now.
6705 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6706 break;
6707
6708 // If this target supports SRA_PARTS, use it.
6709 TargetLowering::LegalizeAction Action =
6710 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6711 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6712 Action == TargetLowering::Custom) {
6713 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6714 break;
6715 }
6716
6717 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006718 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006719 break;
6720 }
6721
6722 case ISD::SRL: {
6723 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006724 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006725 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006726 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006727 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006728 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006729 // Now that the custom expander is done, expand the result, which is
6730 // still VT.
6731 ExpandOp(Op, Lo, Hi);
6732 break;
6733 }
6734 }
6735
6736 // If we can emit an efficient shift operation, do so now.
6737 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6738 break;
6739
6740 // If this target supports SRL_PARTS, use it.
6741 TargetLowering::LegalizeAction Action =
6742 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6743 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6744 Action == TargetLowering::Custom) {
6745 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6746 break;
6747 }
6748
6749 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006750 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006751 break;
6752 }
6753
6754 case ISD::ADD:
6755 case ISD::SUB: {
6756 // If the target wants to custom expand this, let them.
6757 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6758 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006759 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006760 if (Result.getNode()) {
Duncan Sands4c3885b2008-06-22 09:42:16 +00006761 ExpandOp(Result, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006762 break;
6763 }
6764 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006765 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006766 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006767 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6768 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6769 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006770 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006771 LoOps[0] = LHSL;
6772 LoOps[1] = RHSL;
6773 HiOps[0] = LHSH;
6774 HiOps[1] = RHSH;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006775
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006776 //cascaded check to see if any smaller size has a a carry flag.
6777 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6778 bool hasCarry = false;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006779 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6780 MVT AVT = MVT::getIntegerVT(BitSize);
6781 if (TLI.isOperationLegal(OpV, AVT)) {
6782 hasCarry = true;
6783 break;
6784 }
6785 }
6786
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006787 if(hasCarry) {
Andrew Lenharth5e814462008-10-07 14:15:42 +00006788 if (Node->getOpcode() == ISD::ADD) {
6789 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6790 HiOps[2] = Lo.getValue(1);
6791 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6792 } else {
6793 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6794 HiOps[2] = Lo.getValue(1);
6795 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6796 }
6797 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006798 } else {
Andrew Lenharth5e814462008-10-07 14:15:42 +00006799 if (Node->getOpcode() == ISD::ADD) {
6800 Lo = DAG.getNode(ISD::ADD, VTList, LoOps, 2);
6801 Hi = DAG.getNode(ISD::ADD, VTList, HiOps, 2);
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006802 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6803 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006804 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6805 DAG.getConstant(1, NVT),
6806 DAG.getConstant(0, NVT));
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006807 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6808 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006809 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6810 DAG.getConstant(1, NVT),
6811 Carry1);
6812 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6813 } else {
6814 Lo = DAG.getNode(ISD::SUB, VTList, LoOps, 2);
6815 Hi = DAG.getNode(ISD::SUB, VTList, HiOps, 2);
6816 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6817 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6818 DAG.getConstant(1, NVT),
6819 DAG.getConstant(0, NVT));
6820 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6821 }
6822 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006823 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006824 }
6825
6826 case ISD::ADDC:
6827 case ISD::SUBC: {
6828 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006829 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006830 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6831 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6832 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006833 SDValue LoOps[2] = { LHSL, RHSL };
6834 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006835
6836 if (Node->getOpcode() == ISD::ADDC) {
6837 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6838 HiOps[2] = Lo.getValue(1);
6839 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6840 } else {
6841 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6842 HiOps[2] = Lo.getValue(1);
6843 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6844 }
6845 // Remember that we legalized the flag.
6846 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6847 break;
6848 }
6849 case ISD::ADDE:
6850 case ISD::SUBE: {
6851 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006852 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006853 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6854 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6855 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006856 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6857 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006858
6859 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6860 HiOps[2] = Lo.getValue(1);
6861 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6862
6863 // Remember that we legalized the flag.
6864 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6865 break;
6866 }
6867 case ISD::MUL: {
6868 // If the target wants to custom expand this, let them.
6869 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006870 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006871 if (New.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006872 ExpandOp(New, Lo, Hi);
6873 break;
6874 }
6875 }
6876
6877 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6878 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00006879 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6880 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6881 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006882 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006883 ExpandOp(Node->getOperand(0), LL, LH);
6884 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman07961cd2008-02-25 21:11:39 +00006885 unsigned OuterBitSize = Op.getValueSizeInBits();
6886 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman5a199552007-10-08 18:33:35 +00006887 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6888 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman2594d942008-03-10 20:42:19 +00006889 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6890 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6891 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman5a199552007-10-08 18:33:35 +00006892 // The inputs are both zero-extended.
6893 if (HasUMUL_LOHI) {
6894 // We can emit a umul_lohi.
6895 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00006896 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00006897 break;
6898 }
6899 if (HasMULHU) {
6900 // We can emit a mulhu+mul.
6901 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6902 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6903 break;
6904 }
Dan Gohman5a199552007-10-08 18:33:35 +00006905 }
Dan Gohman07961cd2008-02-25 21:11:39 +00006906 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman5a199552007-10-08 18:33:35 +00006907 // The input values are both sign-extended.
6908 if (HasSMUL_LOHI) {
6909 // We can emit a smul_lohi.
6910 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00006911 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00006912 break;
6913 }
6914 if (HasMULHS) {
6915 // We can emit a mulhs+mul.
6916 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6917 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6918 break;
6919 }
6920 }
6921 if (HasUMUL_LOHI) {
6922 // Lo,Hi = umul LHS, RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00006923 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman5a199552007-10-08 18:33:35 +00006924 DAG.getVTList(NVT, NVT), LL, RL);
6925 Lo = UMulLOHI;
6926 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006927 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6928 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6929 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6930 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6931 break;
6932 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00006933 if (HasMULHU) {
6934 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6935 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6936 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6937 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6938 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6939 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6940 break;
6941 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006942 }
6943
Dan Gohman5a199552007-10-08 18:33:35 +00006944 // If nothing else, we can make a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006945 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006946 break;
6947 }
6948 case ISD::SDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006949 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006950 break;
6951 case ISD::UDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006952 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006953 break;
6954 case ISD::SREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006955 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006956 break;
6957 case ISD::UREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006958 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006959 break;
6960
6961 case ISD::FADD:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006962 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
6963 RTLIB::ADD_F64,
6964 RTLIB::ADD_F80,
6965 RTLIB::ADD_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006966 Node, false, Hi);
6967 break;
6968 case ISD::FSUB:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006969 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
6970 RTLIB::SUB_F64,
6971 RTLIB::SUB_F80,
6972 RTLIB::SUB_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006973 Node, false, Hi);
6974 break;
6975 case ISD::FMUL:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006976 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
6977 RTLIB::MUL_F64,
6978 RTLIB::MUL_F80,
6979 RTLIB::MUL_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006980 Node, false, Hi);
6981 break;
6982 case ISD::FDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006983 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
6984 RTLIB::DIV_F64,
6985 RTLIB::DIV_F80,
6986 RTLIB::DIV_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006987 Node, false, Hi);
6988 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006989 case ISD::FP_EXTEND: {
Dale Johannesen4c14d512007-10-12 01:37:08 +00006990 if (VT == MVT::ppcf128) {
6991 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6992 Node->getOperand(0).getValueType()==MVT::f64);
6993 const uint64_t zero = 0;
6994 if (Node->getOperand(0).getValueType()==MVT::f32)
6995 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6996 else
6997 Hi = Node->getOperand(0);
6998 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6999 break;
7000 }
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007001 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7002 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7003 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007004 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007005 }
7006 case ISD::FP_ROUND: {
7007 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7008 VT);
7009 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7010 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007011 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007012 }
Evan Cheng5316b392008-09-09 23:02:14 +00007013 case ISD::FSQRT:
7014 case ISD::FSIN:
7015 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007016 case ISD::FLOG:
7017 case ISD::FLOG2:
7018 case ISD::FLOG10:
7019 case ISD::FEXP:
7020 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00007021 case ISD::FTRUNC:
7022 case ISD::FFLOOR:
7023 case ISD::FCEIL:
7024 case ISD::FRINT:
7025 case ISD::FNEARBYINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00007026 case ISD::FPOW:
7027 case ISD::FPOWI: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007028 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
7029 switch(Node->getOpcode()) {
7030 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00007031 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7032 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007033 break;
7034 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00007035 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7036 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007037 break;
7038 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00007039 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7040 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007041 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00007042 case ISD::FLOG:
7043 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7044 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7045 break;
7046 case ISD::FLOG2:
7047 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7048 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7049 break;
7050 case ISD::FLOG10:
7051 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7052 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7053 break;
7054 case ISD::FEXP:
7055 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7056 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7057 break;
7058 case ISD::FEXP2:
7059 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7060 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7061 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00007062 case ISD::FTRUNC:
7063 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7064 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7065 break;
7066 case ISD::FFLOOR:
7067 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7068 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7069 break;
7070 case ISD::FCEIL:
7071 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7072 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7073 break;
7074 case ISD::FRINT:
7075 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7076 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7077 break;
7078 case ISD::FNEARBYINT:
7079 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7080 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7081 break;
Evan Cheng5316b392008-09-09 23:02:14 +00007082 case ISD::FPOW:
7083 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7084 RTLIB::POW_PPCF128);
7085 break;
7086 case ISD::FPOWI:
7087 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7088 RTLIB::POWI_PPCF128);
7089 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007090 default: assert(0 && "Unreachable!");
7091 }
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007092 Lo = ExpandLibCall(LC, Node, false, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007093 break;
7094 }
7095 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007096 if (VT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007097 SDValue Tmp;
Dale Johannesen5707ef82007-10-12 19:02:17 +00007098 ExpandOp(Node->getOperand(0), Lo, Tmp);
7099 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7100 // lo = hi==fabs(hi) ? lo : -lo;
7101 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7102 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7103 DAG.getCondCode(ISD::SETEQ));
7104 break;
7105 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007106 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007107 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7108 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7109 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7110 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7111 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7112 if (getTypeAction(NVT) == Expand)
7113 ExpandOp(Lo, Lo, Hi);
7114 break;
7115 }
7116 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007117 if (VT == MVT::ppcf128) {
7118 ExpandOp(Node->getOperand(0), Lo, Hi);
7119 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7120 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7121 break;
7122 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007123 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007124 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7125 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7126 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7127 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7128 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7129 if (getTypeAction(NVT) == Expand)
7130 ExpandOp(Lo, Lo, Hi);
7131 break;
7132 }
7133 case ISD::FCOPYSIGN: {
7134 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7135 if (getTypeAction(NVT) == Expand)
7136 ExpandOp(Lo, Lo, Hi);
7137 break;
7138 }
7139 case ISD::SINT_TO_FP:
7140 case ISD::UINT_TO_FP: {
7141 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands92c43912008-06-06 12:08:01 +00007142 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007143
7144 // Promote the operand if needed. Do this before checking for
7145 // ppcf128 so conversions of i16 and i8 work.
7146 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007147 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesen6a779c82008-03-18 17:28:38 +00007148 Tmp = isSigned
7149 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7150 DAG.getValueType(SrcVT))
7151 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greif1c80d112008-08-28 21:40:38 +00007152 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007153 SrcVT = Node->getOperand(0).getValueType();
7154 }
7155
Dan Gohmanec51f642008-03-10 23:03:31 +00007156 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman84d00962008-02-25 21:39:34 +00007157 static const uint64_t zero = 0;
Dale Johannesen4c14d512007-10-12 01:37:08 +00007158 if (isSigned) {
7159 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7160 Node->getOperand(0)));
7161 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7162 } else {
Dan Gohman84d00962008-02-25 21:39:34 +00007163 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen4c14d512007-10-12 01:37:08 +00007164 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7165 Node->getOperand(0)));
7166 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7167 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007168 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00007169 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7170 DAG.getConstant(0, MVT::i32),
7171 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7172 DAG.getConstantFP(
7173 APFloat(APInt(128, 2, TwoE32)),
7174 MVT::ppcf128)),
7175 Hi,
7176 DAG.getCondCode(ISD::SETLT)),
7177 Lo, Hi);
7178 }
7179 break;
7180 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007181 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7182 // si64->ppcf128 done by libcall, below
Dan Gohman84d00962008-02-25 21:39:34 +00007183 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007184 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7185 Lo, Hi);
7186 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7187 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7188 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7189 DAG.getConstant(0, MVT::i64),
7190 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7191 DAG.getConstantFP(
7192 APFloat(APInt(128, 2, TwoE64)),
7193 MVT::ppcf128)),
7194 Hi,
7195 DAG.getCondCode(ISD::SETLT)),
7196 Lo, Hi);
7197 break;
7198 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007199
Dan Gohmanec51f642008-03-10 23:03:31 +00007200 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7201 Node->getOperand(0));
Evan Chenga8740032008-04-01 01:50:16 +00007202 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng4a2f6df2008-04-01 01:51:26 +00007203 // float to i32 etc. can be 'expanded' to a single node.
Evan Chenga8740032008-04-01 01:50:16 +00007204 ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007205 break;
7206 }
7207 }
7208
7209 // Make sure the resultant values have been legalized themselves, unless this
7210 // is a type that requires multi-step expansion.
7211 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7212 Lo = LegalizeOp(Lo);
Gabor Greif1c80d112008-08-28 21:40:38 +00007213 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007214 // Don't legalize the high part if it is expanded to a single node.
7215 Hi = LegalizeOp(Hi);
7216 }
7217
7218 // Remember in a map if the values will be reused later.
Dan Gohman55d19662008-07-07 17:46:23 +00007219 bool isNew =
7220 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007221 assert(isNew && "Value already expanded?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007222 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007223}
7224
7225/// SplitVectorOp - Given an operand of vector type, break it down into
7226/// two smaller values, still of vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00007227void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7228 SDValue &Hi) {
Duncan Sands92c43912008-06-06 12:08:01 +00007229 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007230 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007231 unsigned NumElements = Op.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007232 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00007233
Duncan Sands92c43912008-06-06 12:08:01 +00007234 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman4a365ad2007-11-15 21:15:26 +00007235
7236 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7237 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7238
Duncan Sands92c43912008-06-06 12:08:01 +00007239 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7240 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007241
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007242 // See if we already split it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007243 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007244 = SplitNodes.find(Op);
7245 if (I != SplitNodes.end()) {
7246 Lo = I->second.first;
7247 Hi = I->second.second;
7248 return;
7249 }
7250
7251 switch (Node->getOpcode()) {
7252 default:
7253#ifndef NDEBUG
7254 Node->dump(&DAG);
7255#endif
7256 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00007257 case ISD::UNDEF:
7258 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7259 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7260 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007261 case ISD::BUILD_PAIR:
7262 Lo = Node->getOperand(0);
7263 Hi = Node->getOperand(1);
7264 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007265 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007266 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7267 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007268 unsigned Index = Idx->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007269 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007270 if (Index < NewNumElts_Lo)
7271 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7272 DAG.getIntPtrConstant(Index));
7273 else
7274 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7275 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7276 break;
7277 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007278 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007279 Node->getOperand(1),
7280 Node->getOperand(2));
7281 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007282 break;
7283 }
Chris Lattner587c46d2007-11-19 21:16:54 +00007284 case ISD::VECTOR_SHUFFLE: {
7285 // Build the low part.
Dan Gohman8181bd12008-07-27 21:46:04 +00007286 SDValue Mask = Node->getOperand(2);
7287 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +00007288 MVT PtrVT = TLI.getPointerTy();
Chris Lattner587c46d2007-11-19 21:16:54 +00007289
7290 // Insert all of the elements from the input that are needed. We use
7291 // buildvector of extractelement here because the input vectors will have
7292 // to be legalized, so this makes the code simpler.
7293 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007294 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007295 if (IdxNode.getOpcode() == ISD::UNDEF) {
7296 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7297 continue;
7298 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007299 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007300 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007301 if (Idx >= NumElements) {
7302 InVec = Node->getOperand(1);
7303 Idx -= NumElements;
7304 }
7305 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7306 DAG.getConstant(Idx, PtrVT)));
7307 }
7308 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7309 Ops.clear();
7310
7311 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007312 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007313 if (IdxNode.getOpcode() == ISD::UNDEF) {
7314 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7315 continue;
7316 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007317 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007318 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007319 if (Idx >= NumElements) {
7320 InVec = Node->getOperand(1);
7321 Idx -= NumElements;
7322 }
7323 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7324 DAG.getConstant(Idx, PtrVT)));
7325 }
Mon P Wang2e89b112008-07-25 01:30:26 +00007326 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner587c46d2007-11-19 21:16:54 +00007327 break;
7328 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007329 case ISD::BUILD_VECTOR: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007330 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00007331 Node->op_begin()+NewNumElts_Lo);
7332 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007333
Dan Gohman8181bd12008-07-27 21:46:04 +00007334 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007335 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007336 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007337 break;
7338 }
7339 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00007340 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007341 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7342 if (NewNumSubvectors == 1) {
7343 Lo = Node->getOperand(0);
7344 Hi = Node->getOperand(1);
7345 } else {
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007346 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7347 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007348 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007349
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007350 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007351 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007352 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007353 }
7354 break;
7355 }
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007356 case ISD::EXTRACT_SUBVECTOR: {
7357 SDValue Vec = Op.getOperand(0);
7358 SDValue Idx = Op.getOperand(1);
7359 MVT IdxVT = Idx.getValueType();
7360
7361 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7362 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7363 if (CIdx) {
7364 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7365 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7366 IdxVT));
7367 } else {
7368 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7369 DAG.getConstant(NewNumElts_Lo, IdxVT));
7370 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7371 }
7372 break;
7373 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00007374 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007375 SDValue Cond = Node->getOperand(0);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007376
Dan Gohman8181bd12008-07-27 21:46:04 +00007377 SDValue LL, LH, RL, RH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007378 SplitVectorOp(Node->getOperand(1), LL, LH);
7379 SplitVectorOp(Node->getOperand(2), RL, RH);
7380
Duncan Sands92c43912008-06-06 12:08:01 +00007381 if (Cond.getValueType().isVector()) {
Dan Gohmand5d4c872007-10-17 14:48:28 +00007382 // Handle a vector merge.
Dan Gohman8181bd12008-07-27 21:46:04 +00007383 SDValue CL, CH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007384 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007385 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7386 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007387 } else {
7388 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00007389 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7390 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007391 }
7392 break;
7393 }
Chris Lattnerc7471452008-06-30 02:43:01 +00007394 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007395 SDValue CondLHS = Node->getOperand(0);
7396 SDValue CondRHS = Node->getOperand(1);
7397 SDValue CondCode = Node->getOperand(4);
Chris Lattnerc7471452008-06-30 02:43:01 +00007398
Dan Gohman8181bd12008-07-27 21:46:04 +00007399 SDValue LL, LH, RL, RH;
Chris Lattnerc7471452008-06-30 02:43:01 +00007400 SplitVectorOp(Node->getOperand(2), LL, LH);
7401 SplitVectorOp(Node->getOperand(3), RL, RH);
7402
7403 // Handle a simple select with vector operands.
7404 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7405 LL, RL, CondCode);
7406 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7407 LH, RH, CondCode);
7408 break;
7409 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00007410 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007411 SDValue LL, LH, RL, RH;
Nate Begeman9a1ce152008-05-12 19:40:03 +00007412 SplitVectorOp(Node->getOperand(0), LL, LH);
7413 SplitVectorOp(Node->getOperand(1), RL, RH);
7414 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7415 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7416 break;
7417 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007418 case ISD::ADD:
7419 case ISD::SUB:
7420 case ISD::MUL:
7421 case ISD::FADD:
7422 case ISD::FSUB:
7423 case ISD::FMUL:
7424 case ISD::SDIV:
7425 case ISD::UDIV:
7426 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007427 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007428 case ISD::AND:
7429 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00007430 case ISD::XOR:
7431 case ISD::UREM:
7432 case ISD::SREM:
7433 case ISD::FREM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007434 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007435 SplitVectorOp(Node->getOperand(0), LL, LH);
7436 SplitVectorOp(Node->getOperand(1), RL, RH);
7437
Nate Begeman4a365ad2007-11-15 21:15:26 +00007438 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7439 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007440 break;
7441 }
Dan Gohman29c3cef2008-08-14 20:04:46 +00007442 case ISD::FP_ROUND:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007443 case ISD::FPOWI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007444 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007445 SplitVectorOp(Node->getOperand(0), L, H);
7446
Nate Begeman4a365ad2007-11-15 21:15:26 +00007447 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7448 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00007449 break;
7450 }
7451 case ISD::CTTZ:
7452 case ISD::CTLZ:
7453 case ISD::CTPOP:
7454 case ISD::FNEG:
7455 case ISD::FABS:
7456 case ISD::FSQRT:
7457 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00007458 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007459 case ISD::FLOG:
7460 case ISD::FLOG2:
7461 case ISD::FLOG10:
7462 case ISD::FEXP:
7463 case ISD::FEXP2:
Nate Begeman78246ca2007-11-17 03:58:34 +00007464 case ISD::FP_TO_SINT:
7465 case ISD::FP_TO_UINT:
7466 case ISD::SINT_TO_FP:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007467 case ISD::UINT_TO_FP:
7468 case ISD::TRUNCATE:
7469 case ISD::ANY_EXTEND:
7470 case ISD::SIGN_EXTEND:
7471 case ISD::ZERO_EXTEND:
7472 case ISD::FP_EXTEND: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007473 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007474 SplitVectorOp(Node->getOperand(0), L, H);
7475
Nate Begeman4a365ad2007-11-15 21:15:26 +00007476 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7477 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00007478 break;
7479 }
Mon P Wang73d31542008-11-10 20:54:11 +00007480 case ISD::CONVERT_RNDSAT: {
7481 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7482 SDValue L, H;
7483 SplitVectorOp(Node->getOperand(0), L, H);
7484 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7485 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7486 SDValue STyOpL = DAG.getValueType(L.getValueType());
7487 SDValue STyOpH = DAG.getValueType(H.getValueType());
7488
7489 SDValue RndOp = Node->getOperand(3);
7490 SDValue SatOp = Node->getOperand(4);
7491
7492 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7493 RndOp, SatOp, CvtCode);
7494 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7495 RndOp, SatOp, CvtCode);
7496 break;
7497 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007498 case ISD::LOAD: {
7499 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007500 SDValue Ch = LD->getChain();
7501 SDValue Ptr = LD->getBasePtr();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007502 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007503 const Value *SV = LD->getSrcValue();
7504 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007505 MVT MemoryVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007506 unsigned Alignment = LD->getAlignment();
7507 bool isVolatile = LD->isVolatile();
7508
Dan Gohman29c3cef2008-08-14 20:04:46 +00007509 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7510 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7511
7512 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7513 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7514 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7515
7516 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7517 NewVT_Lo, Ch, Ptr, Offset,
7518 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7519 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007520 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00007521 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007522 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00007523 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00007524 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7525 NewVT_Hi, Ch, Ptr, Offset,
7526 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007527
7528 // Build a factor node to remember that this load is independent of the
7529 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00007530 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007531 Hi.getValue(1));
7532
7533 // Remember that we legalized the chain.
7534 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
7535 break;
7536 }
7537 case ISD::BIT_CONVERT: {
7538 // We know the result is a vector. The input may be either a vector or a
7539 // scalar value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007540 SDValue InOp = Node->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007541 if (!InOp.getValueType().isVector() ||
7542 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007543 // The input is a scalar or single-element vector.
7544 // Lower to a store/load so that it can be split.
7545 // FIXME: this could be improved probably.
Mon P Wang36b59ac2008-07-15 05:28:34 +00007546 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7547 Op.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00007548 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greif1c80d112008-08-28 21:40:38 +00007549 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007550
Dan Gohman8181bd12008-07-27 21:46:04 +00007551 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00007552 InOp, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007553 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00007554 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007555 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007556 }
7557 // Split the vector and convert each of the pieces now.
7558 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007559 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7560 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007561 break;
7562 }
7563 }
7564
7565 // Remember in a map if the values will be reused later.
7566 bool isNew =
7567 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
7568 assert(isNew && "Value already split?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007569 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007570}
7571
7572
7573/// ScalarizeVectorOp - Given an operand of single-element vector type
7574/// (e.g. v1f32), convert it into the equivalent operation that returns a
7575/// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007576SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00007577 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007578 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007579 MVT NewVT = Op.getValueType().getVectorElementType();
7580 assert(Op.getValueType().getVectorNumElements() == 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007581
7582 // See if we already scalarized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007583 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007584 if (I != ScalarizedNodes.end()) return I->second;
7585
Dan Gohman8181bd12008-07-27 21:46:04 +00007586 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007587 switch (Node->getOpcode()) {
7588 default:
7589#ifndef NDEBUG
7590 Node->dump(&DAG); cerr << "\n";
7591#endif
7592 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7593 case ISD::ADD:
7594 case ISD::FADD:
7595 case ISD::SUB:
7596 case ISD::FSUB:
7597 case ISD::MUL:
7598 case ISD::FMUL:
7599 case ISD::SDIV:
7600 case ISD::UDIV:
7601 case ISD::FDIV:
7602 case ISD::SREM:
7603 case ISD::UREM:
7604 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007605 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007606 case ISD::AND:
7607 case ISD::OR:
7608 case ISD::XOR:
7609 Result = DAG.getNode(Node->getOpcode(),
7610 NewVT,
7611 ScalarizeVectorOp(Node->getOperand(0)),
7612 ScalarizeVectorOp(Node->getOperand(1)));
7613 break;
7614 case ISD::FNEG:
7615 case ISD::FABS:
7616 case ISD::FSQRT:
7617 case ISD::FSIN:
7618 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007619 case ISD::FLOG:
7620 case ISD::FLOG2:
7621 case ISD::FLOG10:
7622 case ISD::FEXP:
7623 case ISD::FEXP2:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007624 case ISD::FP_TO_SINT:
7625 case ISD::FP_TO_UINT:
7626 case ISD::SINT_TO_FP:
7627 case ISD::UINT_TO_FP:
7628 case ISD::SIGN_EXTEND:
7629 case ISD::ZERO_EXTEND:
7630 case ISD::ANY_EXTEND:
7631 case ISD::TRUNCATE:
7632 case ISD::FP_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007633 Result = DAG.getNode(Node->getOpcode(),
7634 NewVT,
7635 ScalarizeVectorOp(Node->getOperand(0)));
7636 break;
Mon P Wang73d31542008-11-10 20:54:11 +00007637 case ISD::CONVERT_RNDSAT: {
7638 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7639 Result = DAG.getConvertRndSat(NewVT, Op0,
7640 DAG.getValueType(NewVT),
7641 DAG.getValueType(Op0.getValueType()),
7642 Node->getOperand(3),
7643 Node->getOperand(4),
7644 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7645 break;
7646 }
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007647 case ISD::FPOWI:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007648 case ISD::FP_ROUND:
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007649 Result = DAG.getNode(Node->getOpcode(),
7650 NewVT,
7651 ScalarizeVectorOp(Node->getOperand(0)),
7652 Node->getOperand(1));
7653 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007654 case ISD::LOAD: {
7655 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007656 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7657 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman29c3cef2008-08-14 20:04:46 +00007658 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007659 const Value *SV = LD->getSrcValue();
7660 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007661 MVT MemoryVT = LD->getMemoryVT();
7662 unsigned Alignment = LD->getAlignment();
7663 bool isVolatile = LD->isVolatile();
7664
7665 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7666 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7667
7668 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7669 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7670 MemoryVT.getVectorElementType(),
7671 isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007672
7673 // Remember that we legalized the chain.
7674 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7675 break;
7676 }
7677 case ISD::BUILD_VECTOR:
7678 Result = Node->getOperand(0);
7679 break;
7680 case ISD::INSERT_VECTOR_ELT:
7681 // Returning the inserted scalar element.
7682 Result = Node->getOperand(1);
7683 break;
7684 case ISD::CONCAT_VECTORS:
7685 assert(Node->getOperand(0).getValueType() == NewVT &&
7686 "Concat of non-legal vectors not yet supported!");
7687 Result = Node->getOperand(0);
7688 break;
7689 case ISD::VECTOR_SHUFFLE: {
7690 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007691 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007692 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007693 Result = ScalarizeVectorOp(Node->getOperand(1));
7694 else
7695 Result = ScalarizeVectorOp(Node->getOperand(0));
7696 break;
7697 }
7698 case ISD::EXTRACT_SUBVECTOR:
Mon P Wang927daf52008-11-06 22:52:21 +00007699 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007700 Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007701 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007702 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007703 SDValue Op0 = Op.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007704 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng2cc16e72008-05-16 17:19:05 +00007705 Op0 = ScalarizeVectorOp(Op0);
7706 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007707 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007708 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007709 case ISD::SELECT:
7710 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
7711 ScalarizeVectorOp(Op.getOperand(1)),
7712 ScalarizeVectorOp(Op.getOperand(2)));
7713 break;
Chris Lattnerc7471452008-06-30 02:43:01 +00007714 case ISD::SELECT_CC:
7715 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7716 Node->getOperand(1),
7717 ScalarizeVectorOp(Op.getOperand(2)),
7718 ScalarizeVectorOp(Op.getOperand(3)),
7719 Node->getOperand(4));
7720 break;
Nate Begeman78ca4f92008-05-12 23:09:43 +00007721 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007722 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7723 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Nate Begeman78ca4f92008-05-12 23:09:43 +00007724 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7725 Op.getOperand(2));
7726 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7727 DAG.getConstant(-1ULL, NewVT),
7728 DAG.getConstant(0ULL, NewVT));
7729 break;
7730 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007731 }
7732
7733 if (TLI.isTypeLegal(NewVT))
7734 Result = LegalizeOp(Result);
7735 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7736 assert(isNew && "Value already scalarized?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007737 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007738 return Result;
7739}
7740
7741
Mon P Wang1448aad2008-10-30 08:01:45 +00007742SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7743 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7744 if (I != WidenNodes.end()) return I->second;
7745
7746 MVT VT = Op.getValueType();
7747 assert(VT.isVector() && "Cannot widen non-vector type!");
7748
7749 SDValue Result;
7750 SDNode *Node = Op.getNode();
7751 MVT EVT = VT.getVectorElementType();
7752
7753 unsigned NumElts = VT.getVectorNumElements();
7754 unsigned NewNumElts = WidenVT.getVectorNumElements();
7755 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7756 assert(NewNumElts < 17);
7757
7758 // When widen is called, it is assumed that it is more efficient to use a
7759 // wide type. The default action is to widen to operation to a wider legal
7760 // vector type and then do the operation if it is legal by calling LegalizeOp
7761 // again. If there is no vector equivalent, we will unroll the operation, do
7762 // it, and rebuild the vector. If most of the operations are vectorizible to
7763 // the legal type, the resulting code will be more efficient. If this is not
7764 // the case, the resulting code will preform badly as we end up generating
7765 // code to pack/unpack the results. It is the function that calls widen
Mon P Wanga5a239f2008-11-06 05:31:54 +00007766 // that is responsible for seeing this doesn't happen.
Mon P Wang1448aad2008-10-30 08:01:45 +00007767 switch (Node->getOpcode()) {
7768 default:
7769#ifndef NDEBUG
7770 Node->dump(&DAG);
7771#endif
7772 assert(0 && "Unexpected operation in WidenVectorOp!");
7773 break;
7774 case ISD::CopyFromReg:
Mon P Wang257e1c72008-11-15 06:05:52 +00007775 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang1448aad2008-10-30 08:01:45 +00007776 case ISD::Constant:
7777 case ISD::ConstantFP:
7778 // To build a vector of these elements, clients should call BuildVector
7779 // and with each element instead of creating a node with a vector type
7780 assert(0 && "Unexpected operation in WidenVectorOp!");
7781 case ISD::VAARG:
7782 // Variable Arguments with vector types doesn't make any sense to me
7783 assert(0 && "Unexpected operation in WidenVectorOp!");
7784 break;
Mon P Wang257e1c72008-11-15 06:05:52 +00007785 case ISD::UNDEF:
7786 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7787 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00007788 case ISD::BUILD_VECTOR: {
7789 // Build a vector with undefined for the new nodes
7790 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7791 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7792 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7793 }
7794 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7795 break;
7796 }
7797 case ISD::INSERT_VECTOR_ELT: {
7798 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7799 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7800 Node->getOperand(1), Node->getOperand(2));
7801 break;
7802 }
7803 case ISD::VECTOR_SHUFFLE: {
7804 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7805 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7806 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7807 // used as permutation array. We build the vector here instead of widening
7808 // because we don't want to legalize and have it turned to something else.
7809 SDValue PermOp = Node->getOperand(2);
7810 SDValueVector NewOps;
7811 MVT PVT = PermOp.getValueType().getVectorElementType();
7812 for (unsigned i = 0; i < NumElts; ++i) {
7813 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7814 NewOps.push_back(PermOp.getOperand(i));
7815 } else {
7816 unsigned Idx =
7817 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
7818 if (Idx < NumElts) {
7819 NewOps.push_back(PermOp.getOperand(i));
7820 }
7821 else {
7822 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7823 PermOp.getOperand(i).getValueType()));
7824 }
7825 }
7826 }
7827 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7828 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
7829 }
7830
7831 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
7832 MVT::getVectorVT(PVT, NewOps.size()),
7833 &NewOps[0], NewOps.size());
7834
7835 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
7836 break;
7837 }
7838 case ISD::LOAD: {
7839 // If the load widen returns true, we can use a single load for the
7840 // vector. Otherwise, it is returning a token factor for multiple
7841 // loads.
7842 SDValue TFOp;
7843 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
7844 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
7845 else
7846 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
7847 break;
7848 }
7849
7850 case ISD::BIT_CONVERT: {
7851 SDValue Tmp1 = Node->getOperand(0);
7852 // Converts between two different types so we need to determine
7853 // the correct widen type for the input operand.
7854 MVT TVT = Tmp1.getValueType();
7855 assert(TVT.isVector() && "can not widen non vector type");
7856 MVT TEVT = TVT.getVectorElementType();
7857 assert(WidenVT.getSizeInBits() % EVT.getSizeInBits() == 0 &&
7858 "can not widen bit bit convert that are not multiple of element type");
7859 MVT TWidenVT = MVT::getVectorVT(TEVT,
7860 WidenVT.getSizeInBits()/EVT.getSizeInBits());
7861 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7862 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
7863 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7864
7865 TargetLowering::LegalizeAction action =
7866 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7867 switch (action) {
7868 default: assert(0 && "action not supported");
7869 case TargetLowering::Legal:
7870 break;
7871 case TargetLowering::Promote:
7872 // We defer the promotion to when we legalize the op
7873 break;
7874 case TargetLowering::Expand:
7875 // Expand the operation into a bunch of nasty scalar code.
7876 Result = LegalizeOp(UnrollVectorOp(Result));
7877 break;
7878 }
7879 break;
7880 }
7881
7882 case ISD::SINT_TO_FP:
7883 case ISD::UINT_TO_FP:
7884 case ISD::FP_TO_SINT:
7885 case ISD::FP_TO_UINT: {
7886 SDValue Tmp1 = Node->getOperand(0);
7887 // Converts between two different types so we need to determine
7888 // the correct widen type for the input operand.
7889 MVT TVT = Tmp1.getValueType();
7890 assert(TVT.isVector() && "can not widen non vector type");
7891 MVT TEVT = TVT.getVectorElementType();
7892 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
7893 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7894 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
7895 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7896
7897 TargetLowering::LegalizeAction action =
7898 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7899 switch (action) {
7900 default: assert(0 && "action not supported");
7901 case TargetLowering::Legal:
7902 break;
7903 case TargetLowering::Promote:
7904 // We defer the promotion to when we legalize the op
7905 break;
7906 case TargetLowering::Expand:
7907 // Expand the operation into a bunch of nasty scalar code.
7908 Result = LegalizeOp(UnrollVectorOp(Result));
7909 break;
7910 }
7911 break;
7912 }
7913
7914 case ISD::FP_EXTEND:
7915 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
7916 case ISD::TRUNCATE:
7917 case ISD::SIGN_EXTEND:
7918 case ISD::ZERO_EXTEND:
7919 case ISD::ANY_EXTEND:
7920 case ISD::FP_ROUND:
7921 case ISD::SIGN_EXTEND_INREG:
7922 case ISD::FABS:
7923 case ISD::FNEG:
7924 case ISD::FSQRT:
7925 case ISD::FSIN:
Mon P Wang257e1c72008-11-15 06:05:52 +00007926 case ISD::FCOS:
7927 case ISD::CTPOP:
7928 case ISD::CTTZ:
7929 case ISD::CTLZ: {
Mon P Wang1448aad2008-10-30 08:01:45 +00007930 // Unary op widening
7931 SDValue Tmp1;
7932 TargetLowering::LegalizeAction action =
7933 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7934
7935 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7936 assert(Tmp1.getValueType() == WidenVT);
7937 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7938 switch (action) {
7939 default: assert(0 && "action not supported");
7940 case TargetLowering::Legal:
7941 break;
7942 case TargetLowering::Promote:
7943 // We defer the promotion to when we legalize the op
7944 break;
7945 case TargetLowering::Expand:
7946 // Expand the operation into a bunch of nasty scalar code.
7947 Result = LegalizeOp(UnrollVectorOp(Result));
7948 break;
7949 }
7950 break;
7951 }
Mon P Wang73d31542008-11-10 20:54:11 +00007952 case ISD::CONVERT_RNDSAT: {
7953 SDValue RndOp = Node->getOperand(3);
7954 SDValue SatOp = Node->getOperand(4);
7955
7956 TargetLowering::LegalizeAction action =
7957 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7958
7959 SDValue SrcOp = Node->getOperand(0);
7960
7961 // Converts between two different types so we need to determine
7962 // the correct widen type for the input operand.
7963 MVT SVT = SrcOp.getValueType();
7964 assert(SVT.isVector() && "can not widen non vector type");
7965 MVT SEVT = SVT.getVectorElementType();
7966 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
7967
7968 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
7969 assert(SrcOp.getValueType() == WidenVT);
7970 SDValue DTyOp = DAG.getValueType(WidenVT);
7971 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
7972 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7973
7974 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
7975 RndOp, SatOp, CvtCode);
7976 switch (action) {
7977 default: assert(0 && "action not supported");
7978 case TargetLowering::Legal:
7979 break;
7980 case TargetLowering::Promote:
7981 // We defer the promotion to when we legalize the op
7982 break;
7983 case TargetLowering::Expand:
7984 // Expand the operation into a bunch of nasty scalar code.
7985 Result = LegalizeOp(UnrollVectorOp(Result));
7986 break;
7987 }
7988 break;
7989 }
Mon P Wang1448aad2008-10-30 08:01:45 +00007990 case ISD::FPOW:
7991 case ISD::FPOWI:
7992 case ISD::ADD:
7993 case ISD::SUB:
7994 case ISD::MUL:
7995 case ISD::MULHS:
7996 case ISD::MULHU:
7997 case ISD::AND:
7998 case ISD::OR:
7999 case ISD::XOR:
8000 case ISD::FADD:
8001 case ISD::FSUB:
8002 case ISD::FMUL:
8003 case ISD::SDIV:
8004 case ISD::SREM:
8005 case ISD::FDIV:
8006 case ISD::FREM:
8007 case ISD::FCOPYSIGN:
8008 case ISD::UDIV:
8009 case ISD::UREM:
8010 case ISD::BSWAP: {
8011 // Binary op widening
8012 TargetLowering::LegalizeAction action =
8013 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8014
8015 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8016 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8017 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8018 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
8019 switch (action) {
8020 default: assert(0 && "action not supported");
8021 case TargetLowering::Legal:
8022 break;
8023 case TargetLowering::Promote:
8024 // We defer the promotion to when we legalize the op
8025 break;
8026 case TargetLowering::Expand:
8027 // Expand the operation into a bunch of nasty scalar code by first
8028 // Widening to the right type and then unroll the beast.
8029 Result = LegalizeOp(UnrollVectorOp(Result));
8030 break;
8031 }
8032 break;
8033 }
8034
8035 case ISD::SHL:
8036 case ISD::SRA:
8037 case ISD::SRL: {
8038 // Binary op with one non vector operand
8039 TargetLowering::LegalizeAction action =
8040 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8041
8042 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8043 assert(Tmp1.getValueType() == WidenVT);
8044 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Node->getOperand(1));
8045 switch (action) {
8046 default: assert(0 && "action not supported");
8047 case TargetLowering::Legal:
8048 break;
8049 case TargetLowering::Promote:
8050 // We defer the promotion to when we legalize the op
8051 break;
8052 case TargetLowering::Expand:
8053 // Expand the operation into a bunch of nasty scalar code.
8054 Result = LegalizeOp(UnrollVectorOp(Result));
8055 break;
8056 }
8057 break;
8058 }
8059 case ISD::EXTRACT_VECTOR_ELT: {
8060 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8061 assert(Tmp1.getValueType() == WidenVT);
8062 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8063 break;
8064 }
8065 case ISD::CONCAT_VECTORS: {
8066 // We concurrently support only widen on a multiple of the incoming vector.
8067 // We could widen on a multiple of the incoming operand if necessary.
8068 unsigned NumConcat = NewNumElts / NumElts;
8069 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
8070 std::vector<SDValue> UnOps(NumElts, DAG.getNode(ISD::UNDEF,
8071 VT.getVectorElementType()));
8072 SDValue UndefVal = DAG.getNode(ISD::BUILD_VECTOR, VT,
8073 &UnOps[0], UnOps.size());
8074 SmallVector<SDValue, 8> MOps;
8075 MOps.push_back(Op);
8076 for (unsigned i = 1; i != NumConcat; ++i) {
8077 MOps.push_back(UndefVal);
8078 }
8079 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8080 &MOps[0], MOps.size()));
8081 break;
8082 }
8083 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang257e1c72008-11-15 06:05:52 +00008084 SDValue Tmp1 = Node->getOperand(0);
8085 SDValue Idx = Node->getOperand(1);
8086 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8087 if (CIdx && CIdx->getZExtValue() == 0) {
8088 // Since we are access the start of the vector, the incoming
8089 // vector type might be the proper.
8090 MVT Tmp1VT = Tmp1.getValueType();
8091 if (Tmp1VT == WidenVT)
8092 return Tmp1;
8093 else {
8094 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8095 if (Tmp1VTNumElts < NewNumElts)
8096 Result = WidenVectorOp(Tmp1, WidenVT);
8097 else
8098 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8099 }
8100 } else if (NewNumElts % NumElts == 0) {
8101 // Widen the extracted subvector.
8102 unsigned NumConcat = NewNumElts / NumElts;
8103 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8104 SmallVector<SDValue, 8> MOps;
8105 MOps.push_back(Op);
8106 for (unsigned i = 1; i != NumConcat; ++i) {
8107 MOps.push_back(UndefVal);
8108 }
8109 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8110 &MOps[0], MOps.size()));
8111 } else {
8112 assert(0 && "can not widen extract subvector");
8113 // This could be implemented using insert and build vector but I would
8114 // like to see when this happens.
8115 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008116 break;
8117 }
8118
8119 case ISD::SELECT: {
8120 TargetLowering::LegalizeAction action =
8121 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8122
8123 // Determine new condition widen type and widen
8124 SDValue Cond1 = Node->getOperand(0);
8125 MVT CondVT = Cond1.getValueType();
8126 assert(CondVT.isVector() && "can not widen non vector type");
8127 MVT CondEVT = CondVT.getVectorElementType();
8128 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8129 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8130 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8131
8132 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8133 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8134 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8135 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
8136 switch (action) {
8137 default: assert(0 && "action not supported");
8138 case TargetLowering::Legal:
8139 break;
8140 case TargetLowering::Promote:
8141 // We defer the promotion to when we legalize the op
8142 break;
8143 case TargetLowering::Expand:
8144 // Expand the operation into a bunch of nasty scalar code by first
8145 // Widening to the right type and then unroll the beast.
8146 Result = LegalizeOp(UnrollVectorOp(Result));
8147 break;
8148 }
8149 break;
8150 }
8151
8152 case ISD::SELECT_CC: {
8153 TargetLowering::LegalizeAction action =
8154 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8155
8156 // Determine new condition widen type and widen
8157 SDValue Cond1 = Node->getOperand(0);
8158 SDValue Cond2 = Node->getOperand(1);
8159 MVT CondVT = Cond1.getValueType();
8160 assert(CondVT.isVector() && "can not widen non vector type");
8161 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8162 MVT CondEVT = CondVT.getVectorElementType();
8163 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8164 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8165 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8166 assert(Cond1.getValueType() == CondWidenVT &&
8167 Cond2.getValueType() == CondWidenVT && "condition not widen");
8168
8169 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8170 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8171 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8172 "operands not widen");
8173 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8174 Tmp2, Node->getOperand(4));
8175 switch (action) {
8176 default: assert(0 && "action not supported");
8177 case TargetLowering::Legal:
8178 break;
8179 case TargetLowering::Promote:
8180 // We defer the promotion to when we legalize the op
8181 break;
8182 case TargetLowering::Expand:
8183 // Expand the operation into a bunch of nasty scalar code by first
8184 // Widening to the right type and then unroll the beast.
8185 Result = LegalizeOp(UnrollVectorOp(Result));
8186 break;
8187 }
8188 break;
Mon P Wang42ac14e2008-10-30 18:21:52 +00008189 }
8190 case ISD::VSETCC: {
8191 // Determine widen for the operand
8192 SDValue Tmp1 = Node->getOperand(0);
8193 MVT TmpVT = Tmp1.getValueType();
8194 assert(TmpVT.isVector() && "can not widen non vector type");
8195 MVT TmpEVT = TmpVT.getVectorElementType();
8196 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8197 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8198 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
8199 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
8200 Node->getOperand(2));
Mon P Wang1448aad2008-10-30 08:01:45 +00008201 break;
8202 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008203 case ISD::ATOMIC_CMP_SWAP_8:
8204 case ISD::ATOMIC_CMP_SWAP_16:
8205 case ISD::ATOMIC_CMP_SWAP_32:
8206 case ISD::ATOMIC_CMP_SWAP_64:
8207 case ISD::ATOMIC_LOAD_ADD_8:
8208 case ISD::ATOMIC_LOAD_SUB_8:
8209 case ISD::ATOMIC_LOAD_AND_8:
8210 case ISD::ATOMIC_LOAD_OR_8:
8211 case ISD::ATOMIC_LOAD_XOR_8:
8212 case ISD::ATOMIC_LOAD_NAND_8:
8213 case ISD::ATOMIC_LOAD_MIN_8:
8214 case ISD::ATOMIC_LOAD_MAX_8:
8215 case ISD::ATOMIC_LOAD_UMIN_8:
8216 case ISD::ATOMIC_LOAD_UMAX_8:
8217 case ISD::ATOMIC_SWAP_8:
8218 case ISD::ATOMIC_LOAD_ADD_16:
8219 case ISD::ATOMIC_LOAD_SUB_16:
8220 case ISD::ATOMIC_LOAD_AND_16:
8221 case ISD::ATOMIC_LOAD_OR_16:
8222 case ISD::ATOMIC_LOAD_XOR_16:
8223 case ISD::ATOMIC_LOAD_NAND_16:
8224 case ISD::ATOMIC_LOAD_MIN_16:
8225 case ISD::ATOMIC_LOAD_MAX_16:
8226 case ISD::ATOMIC_LOAD_UMIN_16:
8227 case ISD::ATOMIC_LOAD_UMAX_16:
8228 case ISD::ATOMIC_SWAP_16:
8229 case ISD::ATOMIC_LOAD_ADD_32:
8230 case ISD::ATOMIC_LOAD_SUB_32:
8231 case ISD::ATOMIC_LOAD_AND_32:
8232 case ISD::ATOMIC_LOAD_OR_32:
8233 case ISD::ATOMIC_LOAD_XOR_32:
8234 case ISD::ATOMIC_LOAD_NAND_32:
8235 case ISD::ATOMIC_LOAD_MIN_32:
8236 case ISD::ATOMIC_LOAD_MAX_32:
8237 case ISD::ATOMIC_LOAD_UMIN_32:
8238 case ISD::ATOMIC_LOAD_UMAX_32:
8239 case ISD::ATOMIC_SWAP_32:
8240 case ISD::ATOMIC_LOAD_ADD_64:
8241 case ISD::ATOMIC_LOAD_SUB_64:
8242 case ISD::ATOMIC_LOAD_AND_64:
8243 case ISD::ATOMIC_LOAD_OR_64:
8244 case ISD::ATOMIC_LOAD_XOR_64:
8245 case ISD::ATOMIC_LOAD_NAND_64:
8246 case ISD::ATOMIC_LOAD_MIN_64:
8247 case ISD::ATOMIC_LOAD_MAX_64:
8248 case ISD::ATOMIC_LOAD_UMIN_64:
8249 case ISD::ATOMIC_LOAD_UMAX_64:
8250 case ISD::ATOMIC_SWAP_64: {
8251 // For now, we assume that using vectors for these operations don't make
8252 // much sense so we just split it. We return an empty result
8253 SDValue X, Y;
8254 SplitVectorOp(Op, X, Y);
8255 return Result;
8256 break;
8257 }
8258
8259 } // end switch (Node->getOpcode())
8260
8261 assert(Result.getNode() && "Didn't set a result!");
8262 if (Result != Op)
8263 Result = LegalizeOp(Result);
8264
Mon P Wanga5a239f2008-11-06 05:31:54 +00008265 AddWidenedOperand(Op, Result);
Mon P Wang1448aad2008-10-30 08:01:45 +00008266 return Result;
8267}
8268
8269// Utility function to find a legal vector type and its associated element
8270// type from a preferred width and whose vector type must be the same size
8271// as the VVT.
8272// TLI: Target lowering used to determine legal types
8273// Width: Preferred width of element type
8274// VVT: Vector value type whose size we must match.
8275// Returns VecEVT and EVT - the vector type and its associated element type
8276static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8277 MVT& EVT, MVT& VecEVT) {
8278 // We start with the preferred width, make it a power of 2 and see if
8279 // we can find a vector type of that width. If not, we reduce it by
8280 // another power of 2. If we have widen the type, a vector of bytes should
8281 // always be legal.
8282 assert(TLI.isTypeLegal(VVT));
8283 unsigned EWidth = Width + 1;
8284 do {
8285 assert(EWidth > 0);
8286 EWidth = (1 << Log2_32(EWidth-1));
8287 EVT = MVT::getIntegerVT(EWidth);
8288 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8289 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8290 } while (!TLI.isTypeLegal(VecEVT) ||
8291 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8292}
8293
8294SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8295 SDValue Chain,
8296 SDValue BasePtr,
8297 const Value *SV,
8298 int SVOffset,
8299 unsigned Alignment,
8300 bool isVolatile,
8301 unsigned LdWidth,
8302 MVT ResType) {
8303 // We assume that we have good rules to handle loading power of two loads so
8304 // we break down the operations to power of 2 loads. The strategy is to
8305 // load the largest power of 2 that we can easily transform to a legal vector
8306 // and then insert into that vector, and the cast the result into the legal
8307 // vector that we want. This avoids unnecessary stack converts.
8308 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8309 // the load is nonvolatile, we an use a wider load for the value.
8310 // Find a vector length we can load a large chunk
8311 MVT EVT, VecEVT;
8312 unsigned EVTWidth;
8313 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8314 EVTWidth = EVT.getSizeInBits();
8315
8316 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8317 isVolatile, Alignment);
8318 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8319 LdChain.push_back(LdOp.getValue(1));
8320
8321 // Check if we can load the element with one instruction
8322 if (LdWidth == EVTWidth) {
8323 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8324 }
8325
8326 // The vector element order is endianness dependent.
8327 unsigned Idx = 1;
8328 LdWidth -= EVTWidth;
8329 unsigned Offset = 0;
8330
8331 while (LdWidth > 0) {
8332 unsigned Increment = EVTWidth / 8;
8333 Offset += Increment;
8334 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8335 DAG.getIntPtrConstant(Increment));
8336
8337 if (LdWidth < EVTWidth) {
8338 // Our current type we are using is too large, use a smaller size by
8339 // using a smaller power of 2
8340 unsigned oEVTWidth = EVTWidth;
8341 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8342 EVTWidth = EVT.getSizeInBits();
8343 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008344 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang1448aad2008-10-30 08:01:45 +00008345 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8346 }
8347
8348 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8349 SVOffset+Offset, isVolatile,
8350 MinAlign(Alignment, Offset));
8351 LdChain.push_back(LdOp.getValue(1));
8352 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8353 DAG.getIntPtrConstant(Idx++));
8354
8355 LdWidth -= EVTWidth;
8356 }
8357
8358 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8359}
8360
8361bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8362 SDValue& TFOp,
8363 SDValue Op,
8364 MVT NVT) {
8365 // TODO: Add support for ConcatVec and the ability to load many vector
8366 // types (e.g., v4i8). This will not work when a vector register
8367 // to memory mapping is strange (e.g., vector elements are not
8368 // stored in some sequential order).
8369
8370 // It must be true that the widen vector type is bigger than where
8371 // we need to load from.
8372 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8373 MVT LdVT = LD->getMemoryVT();
8374 assert(LdVT.isVector() && NVT.isVector());
8375 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8376
8377 // Load information
8378 SDValue Chain = LD->getChain();
8379 SDValue BasePtr = LD->getBasePtr();
8380 int SVOffset = LD->getSrcValueOffset();
8381 unsigned Alignment = LD->getAlignment();
8382 bool isVolatile = LD->isVolatile();
8383 const Value *SV = LD->getSrcValue();
8384 unsigned int LdWidth = LdVT.getSizeInBits();
8385
8386 // Load value as a large register
8387 SDValueVector LdChain;
8388 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8389 Alignment, isVolatile, LdWidth, NVT);
8390
8391 if (LdChain.size() == 1) {
8392 TFOp = LdChain[0];
8393 return true;
8394 }
8395 else {
8396 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8397 return false;
8398 }
8399}
8400
8401
8402void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8403 SDValue Chain,
8404 SDValue BasePtr,
8405 const Value *SV,
8406 int SVOffset,
8407 unsigned Alignment,
8408 bool isVolatile,
Mon P Wang257e1c72008-11-15 06:05:52 +00008409 SDValue ValOp,
Mon P Wang1448aad2008-10-30 08:01:45 +00008410 unsigned StWidth) {
8411 // Breaks the stores into a series of power of 2 width stores. For any
8412 // width, we convert the vector to the vector of element size that we
8413 // want to store. This avoids requiring a stack convert.
8414
8415 // Find a width of the element type we can store with
8416 MVT VVT = ValOp.getValueType();
8417 MVT EVT, VecEVT;
8418 unsigned EVTWidth;
8419 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8420 EVTWidth = EVT.getSizeInBits();
8421
8422 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8423 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang927daf52008-11-06 22:52:21 +00008424 DAG.getIntPtrConstant(0));
Mon P Wang1448aad2008-10-30 08:01:45 +00008425 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8426 isVolatile, Alignment);
8427 StChain.push_back(StOp);
8428
8429 // Check if we are done
8430 if (StWidth == EVTWidth) {
8431 return;
8432 }
8433
8434 unsigned Idx = 1;
8435 StWidth -= EVTWidth;
8436 unsigned Offset = 0;
8437
8438 while (StWidth > 0) {
8439 unsigned Increment = EVTWidth / 8;
8440 Offset += Increment;
8441 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8442 DAG.getIntPtrConstant(Increment));
8443
8444 if (StWidth < EVTWidth) {
8445 // Our current type we are using is too large, use a smaller size by
8446 // using a smaller power of 2
8447 unsigned oEVTWidth = EVTWidth;
8448 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8449 EVTWidth = EVT.getSizeInBits();
8450 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008451 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang1448aad2008-10-30 08:01:45 +00008452 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8453 }
8454
8455 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang257e1c72008-11-15 06:05:52 +00008456 DAG.getIntPtrConstant(Idx++));
Mon P Wang1448aad2008-10-30 08:01:45 +00008457 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8458 SVOffset + Offset, isVolatile,
8459 MinAlign(Alignment, Offset)));
8460 StWidth -= EVTWidth;
8461 }
8462}
8463
8464
8465SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8466 SDValue Chain,
8467 SDValue BasePtr) {
8468 // TODO: It might be cleaner if we can use SplitVector and have more legal
8469 // vector types that can be stored into memory (e.g., v4xi8 can
8470 // be stored as a word). This will not work when a vector register
8471 // to memory mapping is strange (e.g., vector elements are not
8472 // stored in some sequential order).
8473
8474 MVT StVT = ST->getMemoryVT();
8475 SDValue ValOp = ST->getValue();
8476
8477 // Check if we have widen this node with another value
8478 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8479 if (I != WidenNodes.end())
8480 ValOp = I->second;
8481
8482 MVT VVT = ValOp.getValueType();
8483
8484 // It must be true that we the widen vector type is bigger than where
8485 // we need to store.
8486 assert(StVT.isVector() && VVT.isVector());
8487 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8488 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8489
8490 // Store value
8491 SDValueVector StChain;
8492 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8493 ST->getSrcValueOffset(), ST->getAlignment(),
8494 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8495 if (StChain.size() == 1)
8496 return StChain[0];
8497 else
8498 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8499}
8500
8501
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008502// SelectionDAG::Legalize - This is the entry point for the file.
8503//
8504void SelectionDAG::Legalize() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008505 /// run - This is the main entry point to this class.
8506 ///
8507 SelectionDAGLegalize(*this).LegalizeDAG();
8508}
8509