blob: 2108abf302f2f9496501385772b9065a8a8cd83b [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);
Mon P Wang9901e732008-12-09 05:46:39 +0000300
301 // Returns the legalized (truncated or extended) shift amount.
302 SDValue LegalizeShiftAmount(SDValue ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303};
304}
305
306/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
307/// specified mask and type. Targets can specify exactly which masks they
308/// support and the code generator is tasked with not creating illegal masks.
309///
310/// Note that this will also return true for shuffles that are promoted to a
311/// different type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000312SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
314 default: return 0;
315 case TargetLowering::Legal:
316 case TargetLowering::Custom:
317 break;
318 case TargetLowering::Promote: {
319 // If this is promoted to a different type, convert the shuffle mask and
320 // ask if it is legal in the promoted type!
Duncan Sands92c43912008-06-06 12:08:01 +0000321 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd3ace282008-07-21 10:20:31 +0000322 MVT EltVT = NVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323
324 // If we changed # elements, change the shuffle mask.
325 unsigned NumEltsGrowth =
Duncan Sands92c43912008-06-06 12:08:01 +0000326 NVT.getVectorNumElements() / VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
328 if (NumEltsGrowth > 1) {
329 // Renumber the elements.
Dan Gohman8181bd12008-07-27 21:46:04 +0000330 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000332 SDValue InOp = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
334 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd3ace282008-07-21 10:20:31 +0000335 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000337 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd3ace282008-07-21 10:20:31 +0000338 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 }
340 }
341 }
342 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
343 }
344 VT = NVT;
345 break;
346 }
347 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000348 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349}
350
351SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
352 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
353 ValueTypeActions(TLI.getValueTypeActions()) {
354 assert(MVT::LAST_VALUETYPE <= 32 &&
355 "Too many value types for ValueTypeActions to hold!");
356}
357
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358void SelectionDAGLegalize::LegalizeDAG() {
359 LastCALLSEQ_END = DAG.getEntryNode();
360 IsLegalizingCall = false;
361
362 // The legalize process is inherently a bottom-up recursive process (users
363 // legalize their uses before themselves). Given infinite stack space, we
364 // could just start legalizing on the root and traverse the whole graph. In
365 // practice however, this causes us to run out of stack space on large basic
366 // blocks. To avoid this problem, compute an ordering of the nodes where each
367 // node is only legalized after all of its operands are legalized.
Dan Gohman2d2a7a32008-09-30 18:30:35 +0000368 DAG.AssignTopologicalOrder();
369 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
370 E = prior(DAG.allnodes_end()); I != next(E); ++I)
371 HandleOp(SDValue(I, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372
373 // Finally, it's possible the root changed. Get the new root.
Dan Gohman8181bd12008-07-27 21:46:04 +0000374 SDValue OldRoot = DAG.getRoot();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
376 DAG.setRoot(LegalizedNodes[OldRoot]);
377
378 ExpandedNodes.clear();
379 LegalizedNodes.clear();
380 PromotedNodes.clear();
381 SplitNodes.clear();
382 ScalarizedNodes.clear();
Mon P Wang1448aad2008-10-30 08:01:45 +0000383 WidenNodes.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384
385 // Remove dead nodes now.
386 DAG.RemoveDeadNodes();
387}
388
389
390/// FindCallEndFromCallStart - Given a chained node that is part of a call
391/// sequence, find the CALLSEQ_END node that terminates the call sequence.
392static SDNode *FindCallEndFromCallStart(SDNode *Node) {
393 if (Node->getOpcode() == ISD::CALLSEQ_END)
394 return Node;
395 if (Node->use_empty())
396 return 0; // No CallSeqEnd
397
398 // The chain is usually at the end.
Dan Gohman8181bd12008-07-27 21:46:04 +0000399 SDValue TheChain(Node, Node->getNumValues()-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 if (TheChain.getValueType() != MVT::Other) {
401 // Sometimes it's at the beginning.
Dan Gohman8181bd12008-07-27 21:46:04 +0000402 TheChain = SDValue(Node, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 if (TheChain.getValueType() != MVT::Other) {
404 // Otherwise, hunt for it.
405 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
406 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000407 TheChain = SDValue(Node, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408 break;
409 }
410
411 // Otherwise, we walked into a node without a chain.
412 if (TheChain.getValueType() != MVT::Other)
413 return 0;
414 }
415 }
416
417 for (SDNode::use_iterator UI = Node->use_begin(),
418 E = Node->use_end(); UI != E; ++UI) {
419
420 // Make sure to only follow users of our token chain.
Dan Gohman0c97f1d2008-07-27 20:43:25 +0000421 SDNode *User = *UI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
423 if (User->getOperand(i) == TheChain)
424 if (SDNode *Result = FindCallEndFromCallStart(User))
425 return Result;
426 }
427 return 0;
428}
429
430/// FindCallStartFromCallEnd - Given a chained node that is part of a call
431/// sequence, find the CALLSEQ_START node that initiates the call sequence.
432static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
433 assert(Node && "Didn't find callseq_start for a call??");
434 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
435
436 assert(Node->getOperand(0).getValueType() == MVT::Other &&
437 "Node doesn't have a token chain argument!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000438 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439}
440
441/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
442/// see if any uses can reach Dest. If no dest operands can get to dest,
443/// legalize them, legalize ourself, and return false, otherwise, return true.
444///
445/// Keep track of the nodes we fine that actually do lead to Dest in
446/// NodesLeadingTo. This avoids retraversing them exponential number of times.
447///
448bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
449 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
450 if (N == Dest) return true; // N certainly leads to Dest :)
451
452 // If we've already processed this node and it does lead to Dest, there is no
453 // need to reprocess it.
454 if (NodesLeadingTo.count(N)) return true;
455
456 // If the first result of this node has been already legalized, then it cannot
457 // reach N.
458 switch (getTypeAction(N->getValueType(0))) {
459 case Legal:
Dan Gohman8181bd12008-07-27 21:46:04 +0000460 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 break;
462 case Promote:
Dan Gohman8181bd12008-07-27 21:46:04 +0000463 if (PromotedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 break;
465 case Expand:
Dan Gohman8181bd12008-07-27 21:46:04 +0000466 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 break;
468 }
469
470 // Okay, this node has not already been legalized. Check and legalize all
471 // operands. If none lead to Dest, then we can legalize this node.
472 bool OperandsLeadToDest = false;
473 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
474 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greif1c80d112008-08-28 21:40:38 +0000475 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476
477 if (OperandsLeadToDest) {
478 NodesLeadingTo.insert(N);
479 return true;
480 }
481
482 // Okay, this node looks safe, legalize it and return false.
Dan Gohman8181bd12008-07-27 21:46:04 +0000483 HandleOp(SDValue(N, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 return false;
485}
486
Mon P Wang1448aad2008-10-30 08:01:45 +0000487/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488/// appropriate for its type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000489void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000490 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491 switch (getTypeAction(VT)) {
492 default: assert(0 && "Bad type action!");
493 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang1448aad2008-10-30 08:01:45 +0000494 case Promote:
495 if (!VT.isVector()) {
496 (void)PromoteOp(Op);
497 break;
498 }
499 else {
500 // See if we can widen otherwise use Expand to either scalarize or split
501 MVT WidenVT = TLI.getWidenVectorType(VT);
502 if (WidenVT != MVT::Other) {
503 (void) WidenVectorOp(Op, WidenVT);
504 break;
505 }
506 // else fall thru to expand since we can't widen the vector
507 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +0000509 if (!VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 // If this is an illegal scalar, expand it into its two component
511 // pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +0000512 SDValue X, Y;
Chris Lattnerdad577b2007-08-25 01:00:22 +0000513 if (Op.getOpcode() == ISD::TargetConstant)
514 break; // Allow illegal target nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 ExpandOp(Op, X, Y);
Duncan Sands92c43912008-06-06 12:08:01 +0000516 } else if (VT.getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 // If this is an illegal single element vector, convert it to a
518 // scalar operation.
519 (void)ScalarizeVectorOp(Op);
520 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +0000521 // This is an illegal multiple element vector.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 // Split it in half and legalize both parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000523 SDValue X, Y;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 SplitVectorOp(Op, X, Y);
525 }
526 break;
527 }
528}
529
530/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
531/// a load from the constant pool.
Dan Gohman8181bd12008-07-27 21:46:04 +0000532static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 SelectionDAG &DAG, TargetLowering &TLI) {
534 bool Extend = false;
535
536 // If a FP immediate is precise when represented as a float and if the
537 // target can do an extending load from float to double, we put it into
538 // the constant pool as a float, even if it's is statically typed as a
Chris Lattnere718cc52008-03-05 06:46:58 +0000539 // double. This shrinks FP constants and canonicalizes them for targets where
540 // an FP extending load is the same cost as a normal load (such as on the x87
541 // fp stack or PPC FP unit).
Duncan Sands92c43912008-06-06 12:08:01 +0000542 MVT VT = CFP->getValueType(0);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000543 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544 if (!UseCP) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000545 if (VT!=MVT::f64 && VT!=MVT::f32)
546 assert(0 && "Invalid type expansion");
Dale Johannesen49cc7ce2008-10-09 18:53:47 +0000547 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Cheng354be062008-03-04 08:05:30 +0000548 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 }
550
Duncan Sands92c43912008-06-06 12:08:01 +0000551 MVT OrigVT = VT;
552 MVT SVT = VT;
Evan Cheng354be062008-03-04 08:05:30 +0000553 while (SVT != MVT::f32) {
Duncan Sands92c43912008-06-06 12:08:01 +0000554 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Cheng354be062008-03-04 08:05:30 +0000555 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
556 // Only do this if the target has a native EXTLOAD instruction from
557 // smaller type.
Evan Cheng08c171a2008-10-14 21:26:46 +0000558 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattnere718cc52008-03-05 06:46:58 +0000559 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000560 const Type *SType = SVT.getTypeForMVT();
Evan Cheng354be062008-03-04 08:05:30 +0000561 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
562 VT = SVT;
563 Extend = true;
564 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 }
566
Dan Gohman8181bd12008-07-27 21:46:04 +0000567 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +0000568 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Cheng354be062008-03-04 08:05:30 +0000569 if (Extend)
570 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohmanfb020b62008-02-07 18:41:25 +0000571 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman04637d12008-09-16 22:05:41 +0000572 0, VT, false, Alignment);
Evan Cheng354be062008-03-04 08:05:30 +0000573 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +0000574 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575}
576
577
578/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
579/// operations.
580static
Dan Gohman8181bd12008-07-27 21:46:04 +0000581SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
582 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +0000583 MVT VT = Node->getValueType(0);
584 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
586 "fcopysign expansion only supported for f32 and f64");
Duncan Sands92c43912008-06-06 12:08:01 +0000587 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588
589 // First get the sign bit of second operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000590 SDValue Mask1 = (SrcVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
592 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
593 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000594 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
596 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands92c43912008-06-06 12:08:01 +0000597 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 if (SizeDiff > 0) {
599 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
600 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
601 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnere6fa1452008-07-10 23:46:13 +0000602 } else if (SizeDiff < 0) {
603 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
604 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
605 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
606 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607
608 // Clear the sign bit of first operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000609 SDValue Mask2 = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
611 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
612 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman8181bd12008-07-27 21:46:04 +0000613 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
615
616 // Or the value with the sign bit.
617 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
618 return Result;
619}
620
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000621/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
622static
Dan Gohman8181bd12008-07-27 21:46:04 +0000623SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
624 TargetLowering &TLI) {
625 SDValue Chain = ST->getChain();
626 SDValue Ptr = ST->getBasePtr();
627 SDValue Val = ST->getValue();
Duncan Sands92c43912008-06-06 12:08:01 +0000628 MVT VT = Val.getValueType();
Dale Johannesen08275382007-09-08 19:29:23 +0000629 int Alignment = ST->getAlignment();
630 int SVOffset = ST->getSrcValueOffset();
Duncan Sands92c43912008-06-06 12:08:01 +0000631 if (ST->getMemoryVT().isFloatingPoint() ||
632 ST->getMemoryVT().isVector()) {
Dale Johannesen08275382007-09-08 19:29:23 +0000633 // Expand to a bitconvert of the value to the integer type of the
634 // same size, then a (misaligned) int store.
Duncan Sands92c43912008-06-06 12:08:01 +0000635 MVT intVT;
636 if (VT.is128BitVector() || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesendc0ee192008-02-27 22:36:00 +0000637 intVT = MVT::i128;
Duncan Sands92c43912008-06-06 12:08:01 +0000638 else if (VT.is64BitVector() || VT==MVT::f64)
Dale Johannesen08275382007-09-08 19:29:23 +0000639 intVT = MVT::i64;
640 else if (VT==MVT::f32)
641 intVT = MVT::i32;
642 else
Dale Johannesenb1d1ab92008-02-28 18:36:51 +0000643 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen08275382007-09-08 19:29:23 +0000644
Dan Gohman8181bd12008-07-27 21:46:04 +0000645 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
Dale Johannesen08275382007-09-08 19:29:23 +0000646 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
647 SVOffset, ST->isVolatile(), Alignment);
648 }
Duncan Sands92c43912008-06-06 12:08:01 +0000649 assert(ST->getMemoryVT().isInteger() &&
650 !ST->getMemoryVT().isVector() &&
Dale Johannesen08275382007-09-08 19:29:23 +0000651 "Unaligned store of unknown type.");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000652 // Get the half-size VT
Duncan Sands92c43912008-06-06 12:08:01 +0000653 MVT NewStoredVT =
654 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
655 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000656 int IncrementSize = NumBits / 8;
657
658 // Divide the stored value in two parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000659 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
660 SDValue Lo = Val;
661 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000662
663 // Store the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000664 SDValue Store1, Store2;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000665 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
666 ST->getSrcValue(), SVOffset, NewStoredVT,
667 ST->isVolatile(), Alignment);
668 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
669 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsa3691432007-10-28 12:59:45 +0000670 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000671 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
672 ST->getSrcValue(), SVOffset + IncrementSize,
673 NewStoredVT, ST->isVolatile(), Alignment);
674
675 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
676}
677
678/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
679static
Dan Gohman8181bd12008-07-27 21:46:04 +0000680SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
681 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000682 int SVOffset = LD->getSrcValueOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +0000683 SDValue Chain = LD->getChain();
684 SDValue Ptr = LD->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +0000685 MVT VT = LD->getValueType(0);
686 MVT LoadedVT = LD->getMemoryVT();
687 if (VT.isFloatingPoint() || VT.isVector()) {
Dale Johannesen08275382007-09-08 19:29:23 +0000688 // Expand to a (misaligned) integer load of the same size,
Dale Johannesendc0ee192008-02-27 22:36:00 +0000689 // then bitconvert to floating point or vector.
Duncan Sands92c43912008-06-06 12:08:01 +0000690 MVT intVT;
691 if (LoadedVT.is128BitVector() ||
Dale Johannesenf8c1e852008-03-01 03:40:57 +0000692 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesendc0ee192008-02-27 22:36:00 +0000693 intVT = MVT::i128;
Duncan Sands92c43912008-06-06 12:08:01 +0000694 else if (LoadedVT.is64BitVector() || LoadedVT == MVT::f64)
Dale Johannesen08275382007-09-08 19:29:23 +0000695 intVT = MVT::i64;
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000696 else if (LoadedVT == MVT::f32)
Dale Johannesen08275382007-09-08 19:29:23 +0000697 intVT = MVT::i32;
698 else
Dale Johannesendc0ee192008-02-27 22:36:00 +0000699 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen08275382007-09-08 19:29:23 +0000700
Dan Gohman8181bd12008-07-27 21:46:04 +0000701 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
Dale Johannesen08275382007-09-08 19:29:23 +0000702 SVOffset, LD->isVolatile(),
703 LD->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +0000704 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Duncan Sands92c43912008-06-06 12:08:01 +0000705 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesen08275382007-09-08 19:29:23 +0000706 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
707
Dan Gohman8181bd12008-07-27 21:46:04 +0000708 SDValue Ops[] = { Result, Chain };
Duncan Sands698842f2008-07-02 17:40:58 +0000709 return DAG.getMergeValues(Ops, 2);
Dale Johannesen08275382007-09-08 19:29:23 +0000710 }
Duncan Sands92c43912008-06-06 12:08:01 +0000711 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000712 "Unaligned load of unsupported type.");
713
Dale Johannesendc0ee192008-02-27 22:36:00 +0000714 // Compute the new VT that is half the size of the old one. This is an
715 // integer MVT.
Duncan Sands92c43912008-06-06 12:08:01 +0000716 unsigned NumBits = LoadedVT.getSizeInBits();
717 MVT NewLoadedVT;
718 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000719 NumBits >>= 1;
720
721 unsigned Alignment = LD->getAlignment();
722 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000723 ISD::LoadExtType HiExtType = LD->getExtensionType();
724
725 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
726 if (HiExtType == ISD::NON_EXTLOAD)
727 HiExtType = ISD::ZEXTLOAD;
728
729 // Load the value in two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000730 SDValue Lo, Hi;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000731 if (TLI.isLittleEndian()) {
732 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
733 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
734 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
735 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
736 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
737 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000738 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000739 } else {
740 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
741 NewLoadedVT,LD->isVolatile(), Alignment);
742 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
743 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
744 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
745 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000746 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000747 }
748
749 // aggregate the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000750 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
751 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000752 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
753
Dan Gohman8181bd12008-07-27 21:46:04 +0000754 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000755 Hi.getValue(1));
756
Dan Gohman8181bd12008-07-27 21:46:04 +0000757 SDValue Ops[] = { Result, TF };
Duncan Sands698842f2008-07-02 17:40:58 +0000758 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000759}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760
Dan Gohman6d05cac2007-10-11 23:57:53 +0000761/// UnrollVectorOp - We know that the given vector has a legal type, however
762/// the operation it performs is not legal and is an operation that we have
763/// no way of lowering. "Unroll" the vector, splitting out the scalars and
764/// operating on each element individually.
Dan Gohman8181bd12008-07-27 21:46:04 +0000765SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000766 MVT VT = Op.getValueType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000767 assert(isTypeLegal(VT) &&
768 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000769 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman6d05cac2007-10-11 23:57:53 +0000770 "Can't unroll a vector with multiple results!");
Duncan Sands92c43912008-06-06 12:08:01 +0000771 unsigned NE = VT.getVectorNumElements();
772 MVT EltVT = VT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000773
Dan Gohman8181bd12008-07-27 21:46:04 +0000774 SmallVector<SDValue, 8> Scalars;
775 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman6d05cac2007-10-11 23:57:53 +0000776 for (unsigned i = 0; i != NE; ++i) {
777 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000778 SDValue Operand = Op.getOperand(j);
Duncan Sands92c43912008-06-06 12:08:01 +0000779 MVT OperandVT = Operand.getValueType();
780 if (OperandVT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +0000781 // A vector operand; extract a single element.
Duncan Sands92c43912008-06-06 12:08:01 +0000782 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000783 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
784 OperandEltVT,
785 Operand,
786 DAG.getConstant(i, MVT::i32));
787 } else {
788 // A scalar operand; just use it as is.
789 Operands[j] = Operand;
790 }
791 }
Mon P Wang9901e732008-12-09 05:46:39 +0000792
793 switch (Op.getOpcode()) {
794 default:
795 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
796 &Operands[0], Operands.size()));
797 break;
798 case ISD::SHL:
799 case ISD::SRA:
800 case ISD::SRL:
801 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT, Operands[0],
802 LegalizeShiftAmount(Operands[1])));
803 break;
804 }
Dan Gohman6d05cac2007-10-11 23:57:53 +0000805 }
806
807 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
808}
809
Duncan Sands37a3f472008-01-10 10:28:30 +0000810/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands92c43912008-06-06 12:08:01 +0000811static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands37a3f472008-01-10 10:28:30 +0000812 RTLIB::Libcall Call_F32,
813 RTLIB::Libcall Call_F64,
814 RTLIB::Libcall Call_F80,
815 RTLIB::Libcall Call_PPCF128) {
816 return
817 VT == MVT::f32 ? Call_F32 :
818 VT == MVT::f64 ? Call_F64 :
819 VT == MVT::f80 ? Call_F80 :
820 VT == MVT::ppcf128 ? Call_PPCF128 :
821 RTLIB::UNKNOWN_LIBCALL;
822}
823
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000824/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
825/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
826/// is necessary to spill the vector being inserted into to memory, perform
827/// the insert there, and then read the result back.
Dan Gohman8181bd12008-07-27 21:46:04 +0000828SDValue SelectionDAGLegalize::
829PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
830 SDValue Tmp1 = Vec;
831 SDValue Tmp2 = Val;
832 SDValue Tmp3 = Idx;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000833
834 // If the target doesn't support this, we have to spill the input vector
835 // to a temporary stack slot, update the element, then reload it. This is
836 // badness. We could also load the value into a vector register (either
837 // with a "move to register" or "extload into register" instruction, then
838 // permute it into place, if the idx is a constant and if the idx is
839 // supported by the target.
Duncan Sands92c43912008-06-06 12:08:01 +0000840 MVT VT = Tmp1.getValueType();
841 MVT EltVT = VT.getVectorElementType();
842 MVT IdxVT = Tmp3.getValueType();
843 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +0000844 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000845
Gabor Greif1c80d112008-08-28 21:40:38 +0000846 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000847
848 // Store the vector.
Dan Gohman8181bd12008-07-27 21:46:04 +0000849 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang1448aad2008-10-30 08:01:45 +0000850 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000851
852 // Truncate or zero extend offset to target pointer type.
Duncan Sandsec142ee2008-06-08 20:54:56 +0000853 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000854 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
855 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +0000856 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000857 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman8181bd12008-07-27 21:46:04 +0000858 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000859 // Store the scalar value.
860 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000861 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000862 // Load the updated vector.
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000863 return DAG.getLoad(VT, Ch, StackPtr,
864 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000865}
866
Mon P Wang9901e732008-12-09 05:46:39 +0000867SDValue SelectionDAGLegalize::LegalizeShiftAmount(SDValue ShiftAmt) {
868 if (TLI.getShiftAmountTy().bitsLT(ShiftAmt.getValueType()))
869 return DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), ShiftAmt);
870
871 if (TLI.getShiftAmountTy().bitsGT(ShiftAmt.getValueType()))
872 return DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), ShiftAmt);
873
874 return ShiftAmt;
875}
876
877
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878/// LegalizeOp - We know that the specified value has a legal type, and
879/// that its operands are legal. Now ensure that the operation itself
880/// is legal, recursively ensuring that the operands' operations remain
881/// legal.
Dan Gohman8181bd12008-07-27 21:46:04 +0000882SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattnerdad577b2007-08-25 01:00:22 +0000883 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
884 return Op;
885
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886 assert(isTypeLegal(Op.getValueType()) &&
887 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000888 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889
890 // If this operation defines any values that cannot be represented in a
891 // register on this target, make sure to expand or promote them.
892 if (Node->getNumValues() > 1) {
893 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
894 if (getTypeAction(Node->getValueType(i)) != Legal) {
895 HandleOp(Op.getValue(i));
896 assert(LegalizedNodes.count(Op) &&
897 "Handling didn't add legal operands!");
898 return LegalizedNodes[Op];
899 }
900 }
901
902 // Note that LegalizeOp may be reentered even from single-use nodes, which
903 // means that we always must cache transformed nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +0000904 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000905 if (I != LegalizedNodes.end()) return I->second;
906
Dan Gohman8181bd12008-07-27 21:46:04 +0000907 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
908 SDValue Result = Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909 bool isCustom = false;
910
911 switch (Node->getOpcode()) {
912 case ISD::FrameIndex:
913 case ISD::EntryToken:
914 case ISD::Register:
915 case ISD::BasicBlock:
916 case ISD::TargetFrameIndex:
917 case ISD::TargetJumpTable:
918 case ISD::TargetConstant:
919 case ISD::TargetConstantFP:
920 case ISD::TargetConstantPool:
921 case ISD::TargetGlobalAddress:
922 case ISD::TargetGlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +0000923 case ISD::TargetExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000924 case ISD::VALUETYPE:
925 case ISD::SRCVALUE:
Dan Gohman12a9c082008-02-06 22:27:42 +0000926 case ISD::MEMOPERAND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000927 case ISD::CONDCODE:
Duncan Sandsc93fae32008-03-21 09:14:45 +0000928 case ISD::ARG_FLAGS:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +0000930 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000931 "This must be legal!");
932 break;
933 default:
934 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
935 // If this is a target node, legalize it by legalizing the operands then
936 // passing it through.
Dan Gohman8181bd12008-07-27 21:46:04 +0000937 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000938 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
939 Ops.push_back(LegalizeOp(Node->getOperand(i)));
940
941 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
942
943 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
944 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +0000945 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946 }
947 // Otherwise this is an unhandled builtin node. splat.
948#ifndef NDEBUG
949 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
950#endif
951 assert(0 && "Do not know how to legalize this operator!");
952 abort();
953 case ISD::GLOBAL_OFFSET_TABLE:
954 case ISD::GlobalAddress:
955 case ISD::GlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +0000956 case ISD::ExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000957 case ISD::ConstantPool:
958 case ISD::JumpTable: // Nothing to do.
959 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
960 default: assert(0 && "This action is not supported yet!");
961 case TargetLowering::Custom:
962 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +0000963 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000964 // FALLTHROUGH if the target doesn't want to lower this op after all.
965 case TargetLowering::Legal:
966 break;
967 }
968 break;
969 case ISD::FRAMEADDR:
970 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971 // The only option for these nodes is to custom lower them. If the target
972 // does not custom lower them, then return zero.
973 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +0000974 if (Tmp1.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000975 Result = Tmp1;
976 else
977 Result = DAG.getConstant(0, TLI.getPointerTy());
978 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000979 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands92c43912008-06-06 12:08:01 +0000980 MVT VT = Node->getValueType(0);
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000981 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
982 default: assert(0 && "This action is not supported yet!");
983 case TargetLowering::Custom:
984 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +0000985 if (Result.getNode()) break;
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000986 // Fall Thru
987 case TargetLowering::Legal:
988 Result = DAG.getConstant(0, VT);
989 break;
990 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000991 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000992 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993 case ISD::EXCEPTIONADDR: {
994 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +0000995 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000996 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
997 default: assert(0 && "This action is not supported yet!");
998 case TargetLowering::Expand: {
999 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001000 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001001 }
1002 break;
1003 case TargetLowering::Custom:
1004 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001005 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006 // Fall Thru
1007 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001008 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands698842f2008-07-02 17:40:58 +00001009 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 break;
1011 }
1012 }
1013 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001014 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001015
Gabor Greif1c80d112008-08-28 21:40:38 +00001016 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001017 "Cannot return more than two values!");
1018
1019 // Since we produced two values, make sure to remember that we
1020 // legalized both of them.
1021 Tmp1 = LegalizeOp(Result);
1022 Tmp2 = LegalizeOp(Result.getValue(1));
1023 AddLegalizedOperand(Op.getValue(0), Tmp1);
1024 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001025 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026 case ISD::EHSELECTION: {
1027 Tmp1 = LegalizeOp(Node->getOperand(0));
1028 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00001029 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1031 default: assert(0 && "This action is not supported yet!");
1032 case TargetLowering::Expand: {
1033 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001034 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 }
1036 break;
1037 case TargetLowering::Custom:
1038 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001039 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001040 // Fall Thru
1041 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001042 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands698842f2008-07-02 17:40:58 +00001043 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 break;
1045 }
1046 }
1047 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001048 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001049
Gabor Greif1c80d112008-08-28 21:40:38 +00001050 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001051 "Cannot return more than two values!");
1052
1053 // Since we produced two values, make sure to remember that we
1054 // legalized both of them.
1055 Tmp1 = LegalizeOp(Result);
1056 Tmp2 = LegalizeOp(Result.getValue(1));
1057 AddLegalizedOperand(Op.getValue(0), Tmp1);
1058 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001059 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001060 case ISD::EH_RETURN: {
Duncan Sands92c43912008-06-06 12:08:01 +00001061 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062 // The only "good" option for this node is to custom lower it.
1063 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1064 default: assert(0 && "This action is not supported at all!");
1065 case TargetLowering::Custom:
1066 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001067 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068 // Fall Thru
1069 case TargetLowering::Legal:
1070 // Target does not know, how to lower this, lower to noop
1071 Result = LegalizeOp(Node->getOperand(0));
1072 break;
1073 }
1074 }
1075 break;
1076 case ISD::AssertSext:
1077 case ISD::AssertZext:
1078 Tmp1 = LegalizeOp(Node->getOperand(0));
1079 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1080 break;
1081 case ISD::MERGE_VALUES:
1082 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif46bf5472008-08-26 22:36:50 +00001083 Result = Node->getOperand(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001084 break;
1085 case ISD::CopyFromReg:
1086 Tmp1 = LegalizeOp(Node->getOperand(0));
1087 Result = Op.getValue(0);
1088 if (Node->getNumValues() == 2) {
1089 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1090 } else {
1091 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
1092 if (Node->getNumOperands() == 3) {
1093 Tmp2 = LegalizeOp(Node->getOperand(2));
1094 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1095 } else {
1096 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1097 }
1098 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1099 }
1100 // Since CopyFromReg produces two values, make sure to remember that we
1101 // legalized both of them.
1102 AddLegalizedOperand(Op.getValue(0), Result);
1103 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001104 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001105 case ISD::UNDEF: {
Duncan Sands92c43912008-06-06 12:08:01 +00001106 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001107 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
1108 default: assert(0 && "This action is not supported yet!");
1109 case TargetLowering::Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00001110 if (VT.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001111 Result = DAG.getConstant(0, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00001112 else if (VT.isFloatingPoint())
1113 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesen20b76352007-09-26 17:26:49 +00001114 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001115 else
1116 assert(0 && "Unknown value type!");
1117 break;
1118 case TargetLowering::Legal:
1119 break;
1120 }
1121 break;
1122 }
1123
1124 case ISD::INTRINSIC_W_CHAIN:
1125 case ISD::INTRINSIC_WO_CHAIN:
1126 case ISD::INTRINSIC_VOID: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001127 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001128 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1129 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1130 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1131
1132 // Allow the target to custom lower its intrinsics if it wants to.
1133 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1134 TargetLowering::Custom) {
1135 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001136 if (Tmp3.getNode()) Result = Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001137 }
1138
Gabor Greif1c80d112008-08-28 21:40:38 +00001139 if (Result.getNode()->getNumValues() == 1) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001140
1141 // Must have return value and chain result.
Gabor Greif1c80d112008-08-28 21:40:38 +00001142 assert(Result.getNode()->getNumValues() == 2 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143 "Cannot return more than two values!");
1144
1145 // Since loads produce two values, make sure to remember that we
1146 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001147 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1148 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001149 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001150 }
1151
Dan Gohman472d12c2008-06-30 20:59:49 +00001152 case ISD::DBG_STOPPOINT:
1153 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001154 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1155
Dan Gohman472d12c2008-06-30 20:59:49 +00001156 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001157 case TargetLowering::Promote:
1158 default: assert(0 && "This action is not supported yet!");
1159 case TargetLowering::Expand: {
1160 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1161 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001162 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001163
Dan Gohman472d12c2008-06-30 20:59:49 +00001164 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman472d12c2008-06-30 20:59:49 +00001166 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1167 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001168
Dan Gohman472d12c2008-06-30 20:59:49 +00001169 unsigned Line = DSP->getLine();
1170 unsigned Col = DSP->getColumn();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001171
1172 if (useDEBUG_LOC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001173 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Chengd6f57682008-07-08 20:06:39 +00001174 DAG.getConstant(Col, MVT::i32),
1175 DAG.getConstant(SrcFile, MVT::i32) };
1176 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001177 } else {
Evan Cheng69eda822008-02-01 02:05:57 +00001178 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001179 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001180 }
1181 } else {
1182 Result = Tmp1; // chain
1183 }
1184 break;
1185 }
Evan Chengd6f57682008-07-08 20:06:39 +00001186 case TargetLowering::Legal: {
1187 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1188 if (Action == Legal && Tmp1 == Node->getOperand(0))
1189 break;
1190
Dan Gohman8181bd12008-07-27 21:46:04 +00001191 SmallVector<SDValue, 8> Ops;
Evan Chengd6f57682008-07-08 20:06:39 +00001192 Ops.push_back(Tmp1);
1193 if (Action == Legal) {
1194 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1195 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1196 } else {
1197 // Otherwise promote them.
1198 Ops.push_back(PromoteOp(Node->getOperand(1)));
1199 Ops.push_back(PromoteOp(Node->getOperand(2)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001200 }
Evan Chengd6f57682008-07-08 20:06:39 +00001201 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1202 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1203 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204 break;
1205 }
Evan Chengd6f57682008-07-08 20:06:39 +00001206 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001207 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001208
1209 case ISD::DECLARE:
1210 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1211 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1212 default: assert(0 && "This action is not supported yet!");
1213 case TargetLowering::Legal:
1214 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1215 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1216 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1217 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1218 break;
Chris Lattner203cd052008-02-28 05:53:40 +00001219 case TargetLowering::Expand:
1220 Result = LegalizeOp(Node->getOperand(0));
1221 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001222 }
1223 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001224
1225 case ISD::DEBUG_LOC:
1226 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1227 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1228 default: assert(0 && "This action is not supported yet!");
Evan Chengd6f57682008-07-08 20:06:39 +00001229 case TargetLowering::Legal: {
1230 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001231 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Chengd6f57682008-07-08 20:06:39 +00001232 if (Action == Legal && Tmp1 == Node->getOperand(0))
1233 break;
1234 if (Action == Legal) {
1235 Tmp2 = Node->getOperand(1);
1236 Tmp3 = Node->getOperand(2);
1237 Tmp4 = Node->getOperand(3);
1238 } else {
1239 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1240 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1241 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1242 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001243 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1244 break;
1245 }
Evan Chengd6f57682008-07-08 20:06:39 +00001246 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 break;
1248
Dan Gohmanfa607c92008-07-01 00:05:16 +00001249 case ISD::DBG_LABEL:
1250 case ISD::EH_LABEL:
1251 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1252 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001253 default: assert(0 && "This action is not supported yet!");
1254 case TargetLowering::Legal:
1255 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohmanfa607c92008-07-01 00:05:16 +00001256 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001257 break;
1258 case TargetLowering::Expand:
1259 Result = LegalizeOp(Node->getOperand(0));
1260 break;
1261 }
1262 break;
1263
Evan Chengd1d68072008-03-08 00:58:38 +00001264 case ISD::PREFETCH:
1265 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1266 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1267 default: assert(0 && "This action is not supported yet!");
1268 case TargetLowering::Legal:
1269 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1270 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1271 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1272 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1273 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1274 break;
1275 case TargetLowering::Expand:
1276 // It's a noop.
1277 Result = LegalizeOp(Node->getOperand(0));
1278 break;
1279 }
1280 break;
1281
Andrew Lenharth785610d2008-02-16 01:24:58 +00001282 case ISD::MEMBARRIER: {
1283 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001284 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1285 default: assert(0 && "This action is not supported yet!");
1286 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001287 SDValue Ops[6];
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001288 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands3ee041a2008-02-27 08:53:44 +00001289 for (int x = 1; x < 6; ++x) {
1290 Ops[x] = Node->getOperand(x);
1291 if (!isTypeLegal(Ops[x].getValueType()))
1292 Ops[x] = PromoteOp(Ops[x]);
1293 }
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001294 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1295 break;
1296 }
1297 case TargetLowering::Expand:
1298 //There is no libgcc call for this op
1299 Result = Node->getOperand(0); // Noop
1300 break;
1301 }
Andrew Lenharth785610d2008-02-16 01:24:58 +00001302 break;
1303 }
1304
Dale Johannesenbc187662008-08-28 02:44:49 +00001305 case ISD::ATOMIC_CMP_SWAP_8:
1306 case ISD::ATOMIC_CMP_SWAP_16:
1307 case ISD::ATOMIC_CMP_SWAP_32:
1308 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001309 unsigned int num_operands = 4;
1310 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001311 SDValue Ops[4];
Mon P Wang078a62d2008-05-05 19:05:59 +00001312 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001313 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang078a62d2008-05-05 19:05:59 +00001314 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1315
1316 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1317 default: assert(0 && "This action is not supported yet!");
1318 case TargetLowering::Custom:
1319 Result = TLI.LowerOperation(Result, DAG);
1320 break;
1321 case TargetLowering::Legal:
1322 break;
1323 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001324 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1325 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001326 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001327 }
Dale Johannesenbc187662008-08-28 02:44:49 +00001328 case ISD::ATOMIC_LOAD_ADD_8:
1329 case ISD::ATOMIC_LOAD_SUB_8:
1330 case ISD::ATOMIC_LOAD_AND_8:
1331 case ISD::ATOMIC_LOAD_OR_8:
1332 case ISD::ATOMIC_LOAD_XOR_8:
1333 case ISD::ATOMIC_LOAD_NAND_8:
1334 case ISD::ATOMIC_LOAD_MIN_8:
1335 case ISD::ATOMIC_LOAD_MAX_8:
1336 case ISD::ATOMIC_LOAD_UMIN_8:
1337 case ISD::ATOMIC_LOAD_UMAX_8:
1338 case ISD::ATOMIC_SWAP_8:
1339 case ISD::ATOMIC_LOAD_ADD_16:
1340 case ISD::ATOMIC_LOAD_SUB_16:
1341 case ISD::ATOMIC_LOAD_AND_16:
1342 case ISD::ATOMIC_LOAD_OR_16:
1343 case ISD::ATOMIC_LOAD_XOR_16:
1344 case ISD::ATOMIC_LOAD_NAND_16:
1345 case ISD::ATOMIC_LOAD_MIN_16:
1346 case ISD::ATOMIC_LOAD_MAX_16:
1347 case ISD::ATOMIC_LOAD_UMIN_16:
1348 case ISD::ATOMIC_LOAD_UMAX_16:
1349 case ISD::ATOMIC_SWAP_16:
1350 case ISD::ATOMIC_LOAD_ADD_32:
1351 case ISD::ATOMIC_LOAD_SUB_32:
1352 case ISD::ATOMIC_LOAD_AND_32:
1353 case ISD::ATOMIC_LOAD_OR_32:
1354 case ISD::ATOMIC_LOAD_XOR_32:
1355 case ISD::ATOMIC_LOAD_NAND_32:
1356 case ISD::ATOMIC_LOAD_MIN_32:
1357 case ISD::ATOMIC_LOAD_MAX_32:
1358 case ISD::ATOMIC_LOAD_UMIN_32:
1359 case ISD::ATOMIC_LOAD_UMAX_32:
1360 case ISD::ATOMIC_SWAP_32:
1361 case ISD::ATOMIC_LOAD_ADD_64:
1362 case ISD::ATOMIC_LOAD_SUB_64:
1363 case ISD::ATOMIC_LOAD_AND_64:
1364 case ISD::ATOMIC_LOAD_OR_64:
1365 case ISD::ATOMIC_LOAD_XOR_64:
1366 case ISD::ATOMIC_LOAD_NAND_64:
1367 case ISD::ATOMIC_LOAD_MIN_64:
1368 case ISD::ATOMIC_LOAD_MAX_64:
1369 case ISD::ATOMIC_LOAD_UMIN_64:
1370 case ISD::ATOMIC_LOAD_UMAX_64:
1371 case ISD::ATOMIC_SWAP_64: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001372 unsigned int num_operands = 3;
1373 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001374 SDValue Ops[3];
Mon P Wang078a62d2008-05-05 19:05:59 +00001375 for (unsigned int x = 0; x < num_operands; ++x)
1376 Ops[x] = LegalizeOp(Node->getOperand(x));
1377 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sandsac496a12008-07-04 11:47:58 +00001378
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001379 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001380 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001381 case TargetLowering::Custom:
1382 Result = TLI.LowerOperation(Result, DAG);
1383 break;
1384 case TargetLowering::Legal:
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001385 break;
1386 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001387 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1388 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001389 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001390 }
Scott Michelf2e2b702007-08-08 23:23:31 +00001391 case ISD::Constant: {
1392 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1393 unsigned opAction =
1394 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1395
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001396 // We know we don't need to expand constants here, constants only have one
1397 // value and we check that it is fine above.
1398
Scott Michelf2e2b702007-08-08 23:23:31 +00001399 if (opAction == TargetLowering::Custom) {
1400 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001401 if (Tmp1.getNode())
Scott Michelf2e2b702007-08-08 23:23:31 +00001402 Result = Tmp1;
1403 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001404 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001405 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001406 case ISD::ConstantFP: {
1407 // Spill FP immediates to the constant pool if the target cannot directly
1408 // codegen them. Targets often have some immediate values that can be
1409 // efficiently generated into an FP register without a load. We explicitly
1410 // leave these constants as ConstantFP nodes for the target to deal with.
1411 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1412
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1414 default: assert(0 && "This action is not supported yet!");
Nate Begemane2ba64f2008-02-14 08:57:00 +00001415 case TargetLowering::Legal:
1416 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417 case TargetLowering::Custom:
1418 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001419 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001420 Result = Tmp3;
1421 break;
1422 }
1423 // FALLTHROUGH
Nate Begemane2ba64f2008-02-14 08:57:00 +00001424 case TargetLowering::Expand: {
1425 // Check to see if this FP immediate is already legal.
1426 bool isLegal = false;
1427 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1428 E = TLI.legal_fpimm_end(); I != E; ++I) {
1429 if (CFP->isExactlyValue(*I)) {
1430 isLegal = true;
1431 break;
1432 }
1433 }
1434 // If this is a legal constant, turn it into a TargetConstantFP node.
1435 if (isLegal)
1436 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001437 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1438 }
Nate Begemane2ba64f2008-02-14 08:57:00 +00001439 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001440 break;
1441 }
1442 case ISD::TokenFactor:
1443 if (Node->getNumOperands() == 2) {
1444 Tmp1 = LegalizeOp(Node->getOperand(0));
1445 Tmp2 = LegalizeOp(Node->getOperand(1));
1446 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1447 } else if (Node->getNumOperands() == 3) {
1448 Tmp1 = LegalizeOp(Node->getOperand(0));
1449 Tmp2 = LegalizeOp(Node->getOperand(1));
1450 Tmp3 = LegalizeOp(Node->getOperand(2));
1451 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1452 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00001453 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001454 // Legalize the operands.
1455 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1456 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1457 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1458 }
1459 break;
1460
1461 case ISD::FORMAL_ARGUMENTS:
1462 case ISD::CALL:
1463 // The only option for this is to custom lower it.
1464 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001465 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesenac246272008-03-05 19:14:03 +00001466 // A call within a calling sequence must be legalized to something
1467 // other than the normal CALLSEQ_END. Violating this gets Legalize
1468 // into an infinite loop.
1469 assert ((!IsLegalizingCall ||
1470 Node->getOpcode() != ISD::CALL ||
Gabor Greif1c80d112008-08-28 21:40:38 +00001471 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesenac246272008-03-05 19:14:03 +00001472 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001473
1474 // The number of incoming and outgoing values should match; unless the final
1475 // outgoing value is a flag.
Gabor Greif1c80d112008-08-28 21:40:38 +00001476 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1477 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1478 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling22f8deb2007-11-13 00:44:25 +00001479 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001480 "Lowering call/formal_arguments produced unexpected # results!");
1481
1482 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1483 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greif1c80d112008-08-28 21:40:38 +00001484 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1485 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling22f8deb2007-11-13 00:44:25 +00001486 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001487 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00001488 if (Op.getResNo() == i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001489 Tmp2 = Tmp1;
Dan Gohman8181bd12008-07-27 21:46:04 +00001490 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491 }
1492 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001493 case ISD::EXTRACT_SUBREG: {
1494 Tmp1 = LegalizeOp(Node->getOperand(0));
1495 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1496 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001497 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001498 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1499 }
1500 break;
1501 case ISD::INSERT_SUBREG: {
1502 Tmp1 = LegalizeOp(Node->getOperand(0));
1503 Tmp2 = LegalizeOp(Node->getOperand(1));
1504 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1505 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001506 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001507 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1508 }
1509 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001510 case ISD::BUILD_VECTOR:
1511 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1512 default: assert(0 && "This action is not supported yet!");
1513 case TargetLowering::Custom:
1514 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001515 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001516 Result = Tmp3;
1517 break;
1518 }
1519 // FALLTHROUGH
1520 case TargetLowering::Expand:
Gabor Greif1c80d112008-08-28 21:40:38 +00001521 Result = ExpandBUILD_VECTOR(Result.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001522 break;
1523 }
1524 break;
1525 case ISD::INSERT_VECTOR_ELT:
1526 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001527 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001528
1529 // The type of the value to insert may not be legal, even though the vector
1530 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1531 // here.
1532 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1533 default: assert(0 && "Cannot expand insert element operand");
1534 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1535 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang1448aad2008-10-30 08:01:45 +00001536 case Expand:
1537 // FIXME: An alternative would be to check to see if the target is not
1538 // going to custom lower this operation, we could bitcast to half elt
1539 // width and perform two inserts at that width, if that is legal.
1540 Tmp2 = Node->getOperand(1);
1541 break;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001542 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001543 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1544
1545 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1546 Node->getValueType(0))) {
1547 default: assert(0 && "This action is not supported yet!");
1548 case TargetLowering::Legal:
1549 break;
1550 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001551 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001552 if (Tmp4.getNode()) {
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001553 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001554 break;
1555 }
1556 // FALLTHROUGH
Mon P Wang1448aad2008-10-30 08:01:45 +00001557 case TargetLowering::Promote:
1558 // Fall thru for vector case
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001559 case TargetLowering::Expand: {
1560 // If the insert index is a constant, codegen this as a scalar_to_vector,
1561 // then a shuffle that inserts it into the right position in the vector.
1562 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001563 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1564 // match the element type of the vector being created.
1565 if (Tmp2.getValueType() ==
Duncan Sands92c43912008-06-06 12:08:01 +00001566 Op.getValueType().getVectorElementType()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001567 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001568 Tmp1.getValueType(), Tmp2);
1569
Duncan Sands92c43912008-06-06 12:08:01 +00001570 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1571 MVT ShufMaskVT =
1572 MVT::getIntVectorWithNumElements(NumElts);
1573 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001574
1575 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1576 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1577 // elt 0 of the RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00001578 SmallVector<SDValue, 8> ShufOps;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001579 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001580 if (i != InsertPos->getZExtValue())
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001581 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1582 else
1583 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1584 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001585 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001586 &ShufOps[0], ShufOps.size());
1587
1588 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1589 Tmp1, ScVec, ShufMask);
1590 Result = LegalizeOp(Result);
1591 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001592 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001593 }
Nate Begeman7c9e4b72008-04-25 18:07:40 +00001594 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001595 break;
1596 }
1597 }
1598 break;
1599 case ISD::SCALAR_TO_VECTOR:
1600 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1601 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1602 break;
1603 }
1604
1605 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1606 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1607 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1608 Node->getValueType(0))) {
1609 default: assert(0 && "This action is not supported yet!");
1610 case TargetLowering::Legal:
1611 break;
1612 case TargetLowering::Custom:
1613 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001614 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001615 Result = Tmp3;
1616 break;
1617 }
1618 // FALLTHROUGH
1619 case TargetLowering::Expand:
1620 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1621 break;
1622 }
1623 break;
1624 case ISD::VECTOR_SHUFFLE:
1625 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1626 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1627 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1628
1629 // Allow targets to custom lower the SHUFFLEs they support.
1630 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1631 default: assert(0 && "Unknown operation action!");
1632 case TargetLowering::Legal:
1633 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1634 "vector shuffle should not be created if not legal!");
1635 break;
1636 case TargetLowering::Custom:
1637 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001638 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001639 Result = Tmp3;
1640 break;
1641 }
1642 // FALLTHROUGH
1643 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00001644 MVT VT = Node->getValueType(0);
1645 MVT EltVT = VT.getVectorElementType();
1646 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00001647 SDValue Mask = Node->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001648 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00001649 SmallVector<SDValue,8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001650 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001651 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001652 if (Arg.getOpcode() == ISD::UNDEF) {
1653 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1654 } else {
1655 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001656 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001657 if (Idx < NumElems)
1658 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1659 DAG.getConstant(Idx, PtrVT)));
1660 else
1661 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1662 DAG.getConstant(Idx - NumElems, PtrVT)));
1663 }
1664 }
1665 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1666 break;
1667 }
1668 case TargetLowering::Promote: {
1669 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00001670 MVT OVT = Node->getValueType(0);
1671 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001672
1673 // Cast the two input vectors.
1674 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1675 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1676
1677 // Convert the shuffle mask to the right # elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00001678 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001679 assert(Tmp3.getNode() && "Shuffle not legal?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001680 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1681 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1682 break;
1683 }
1684 }
1685 break;
1686
1687 case ISD::EXTRACT_VECTOR_ELT:
1688 Tmp1 = Node->getOperand(0);
1689 Tmp2 = LegalizeOp(Node->getOperand(1));
1690 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1691 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1692 break;
1693
1694 case ISD::EXTRACT_SUBVECTOR:
1695 Tmp1 = Node->getOperand(0);
1696 Tmp2 = LegalizeOp(Node->getOperand(1));
1697 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1698 Result = ExpandEXTRACT_SUBVECTOR(Result);
1699 break;
1700
Mon P Wang1448aad2008-10-30 08:01:45 +00001701 case ISD::CONCAT_VECTORS: {
1702 // Use extract/insert/build vector for now. We might try to be
1703 // more clever later.
1704 MVT PtrVT = TLI.getPointerTy();
1705 SmallVector<SDValue, 8> Ops;
1706 unsigned NumOperands = Node->getNumOperands();
1707 for (unsigned i=0; i < NumOperands; ++i) {
1708 SDValue SubOp = Node->getOperand(i);
1709 MVT VVT = SubOp.getNode()->getValueType(0);
1710 MVT EltVT = VVT.getVectorElementType();
1711 unsigned NumSubElem = VVT.getVectorNumElements();
1712 for (unsigned j=0; j < NumSubElem; ++j) {
1713 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1714 DAG.getConstant(j, PtrVT)));
1715 }
1716 }
1717 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1718 &Ops[0], Ops.size()));
1719 }
1720
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001721 case ISD::CALLSEQ_START: {
1722 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1723
1724 // Recursively Legalize all of the inputs of the call end that do not lead
1725 // to this call start. This ensures that any libcalls that need be inserted
1726 // are inserted *before* the CALLSEQ_START.
1727 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1728 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greif1c80d112008-08-28 21:40:38 +00001729 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001730 NodesLeadingTo);
1731 }
1732
1733 // Now that we legalized all of the inputs (which may have inserted
1734 // libcalls) create the new CALLSEQ_START node.
1735 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1736
1737 // Merge in the last call, to ensure that this call start after the last
1738 // call ended.
1739 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1740 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1741 Tmp1 = LegalizeOp(Tmp1);
1742 }
1743
1744 // Do not try to legalize the target-specific arguments (#1+).
1745 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001746 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001747 Ops[0] = Tmp1;
1748 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1749 }
1750
1751 // Remember that the CALLSEQ_START is legalized.
1752 AddLegalizedOperand(Op.getValue(0), Result);
1753 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1754 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1755
1756 // Now that the callseq_start and all of the non-call nodes above this call
1757 // sequence have been legalized, legalize the call itself. During this
1758 // process, no libcalls can/will be inserted, guaranteeing that no calls
1759 // can overlap.
1760 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001761 // Note that we are selecting this call!
Dan Gohman8181bd12008-07-27 21:46:04 +00001762 LastCALLSEQ_END = SDValue(CallEnd, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001763 IsLegalizingCall = true;
1764
1765 // Legalize the call, starting from the CALLSEQ_END.
1766 LegalizeOp(LastCALLSEQ_END);
1767 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1768 return Result;
1769 }
1770 case ISD::CALLSEQ_END:
1771 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1772 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greif1c80d112008-08-28 21:40:38 +00001773 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001774 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1775 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001776 assert(I != LegalizedNodes.end() &&
1777 "Legalizing the call start should have legalized this node!");
1778 return I->second;
1779 }
1780
1781 // Otherwise, the call start has been legalized and everything is going
1782 // according to plan. Just legalize ourselves normally here.
1783 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1784 // Do not try to legalize the target-specific arguments (#1+), except for
1785 // an optional flag input.
1786 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1787 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001788 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001789 Ops[0] = Tmp1;
1790 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1791 }
1792 } else {
1793 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1794 if (Tmp1 != Node->getOperand(0) ||
1795 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001796 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001797 Ops[0] = Tmp1;
1798 Ops.back() = Tmp2;
1799 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1800 }
1801 }
1802 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1803 // This finishes up call legalization.
1804 IsLegalizingCall = false;
1805
1806 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001807 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001808 if (Node->getNumValues() == 2)
Dan Gohman8181bd12008-07-27 21:46:04 +00001809 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001810 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001811 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands92c43912008-06-06 12:08:01 +00001812 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001813 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1814 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1815 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1816 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1817
1818 Tmp1 = Result.getValue(0);
1819 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001820 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001821 default: assert(0 && "This action is not supported yet!");
1822 case TargetLowering::Expand: {
1823 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1824 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1825 " not tell us which reg is the stack pointer!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001826 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001827
1828 // Chain the dynamic stack allocation so that it doesn't modify the stack
1829 // pointer when other instructions are using the stack.
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001830 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling22f8deb2007-11-13 00:44:25 +00001831
Dan Gohman8181bd12008-07-27 21:46:04 +00001832 SDValue Size = Tmp2.getOperand(1);
1833 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Chenga448bc42007-08-16 23:50:06 +00001834 Chain = SP.getValue(1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001835 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Chenga448bc42007-08-16 23:50:06 +00001836 unsigned StackAlign =
1837 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1838 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001839 SP = DAG.getNode(ISD::AND, VT, SP,
1840 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001841 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling22f8deb2007-11-13 00:44:25 +00001842 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1843
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001844 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1845 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling22f8deb2007-11-13 00:44:25 +00001846
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001847 Tmp1 = LegalizeOp(Tmp1);
1848 Tmp2 = LegalizeOp(Tmp2);
1849 break;
1850 }
1851 case TargetLowering::Custom:
1852 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001853 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001854 Tmp1 = LegalizeOp(Tmp3);
1855 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1856 }
1857 break;
1858 case TargetLowering::Legal:
1859 break;
1860 }
1861 // Since this op produce two values, make sure to remember that we
1862 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001863 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1864 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001865 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001866 }
1867 case ISD::INLINEASM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001868 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001869 bool Changed = false;
1870 // Legalize all of the operands of the inline asm, in case they are nodes
1871 // that need to be expanded or something. Note we skip the asm string and
1872 // all of the TargetConstant flags.
Dan Gohman8181bd12008-07-27 21:46:04 +00001873 SDValue Op = LegalizeOp(Ops[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001874 Changed = Op != Ops[0];
1875 Ops[0] = Op;
1876
1877 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1878 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001879 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001880 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001881 SDValue Op = LegalizeOp(Ops[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001882 if (Op != Ops[i]) {
1883 Changed = true;
1884 Ops[i] = Op;
1885 }
1886 }
1887 }
1888
1889 if (HasInFlag) {
1890 Op = LegalizeOp(Ops.back());
1891 Changed |= Op != Ops.back();
1892 Ops.back() = Op;
1893 }
1894
1895 if (Changed)
1896 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1897
1898 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman8181bd12008-07-27 21:46:04 +00001899 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1900 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001901 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001902 }
1903 case ISD::BR:
1904 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1905 // Ensure that libcalls are emitted before a branch.
1906 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1907 Tmp1 = LegalizeOp(Tmp1);
1908 LastCALLSEQ_END = DAG.getEntryNode();
1909
1910 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1911 break;
1912 case ISD::BRIND:
1913 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1914 // Ensure that libcalls are emitted before a branch.
1915 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1916 Tmp1 = LegalizeOp(Tmp1);
1917 LastCALLSEQ_END = DAG.getEntryNode();
1918
1919 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1920 default: assert(0 && "Indirect target must be legal type (pointer)!");
1921 case Legal:
1922 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1923 break;
1924 }
1925 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1926 break;
1927 case ISD::BR_JT:
1928 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1929 // Ensure that libcalls are emitted before a branch.
1930 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1931 Tmp1 = LegalizeOp(Tmp1);
1932 LastCALLSEQ_END = DAG.getEntryNode();
1933
1934 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1935 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1936
1937 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1938 default: assert(0 && "This action is not supported yet!");
1939 case TargetLowering::Legal: break;
1940 case TargetLowering::Custom:
1941 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001942 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001943 break;
1944 case TargetLowering::Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001945 SDValue Chain = Result.getOperand(0);
1946 SDValue Table = Result.getOperand(1);
1947 SDValue Index = Result.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001948
Duncan Sands92c43912008-06-06 12:08:01 +00001949 MVT PTy = TLI.getPointerTy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001950 MachineFunction &MF = DAG.getMachineFunction();
1951 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
1952 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman8181bd12008-07-27 21:46:04 +00001953 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001954
Dan Gohman8181bd12008-07-27 21:46:04 +00001955 SDValue LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001956 switch (EntrySize) {
1957 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001958 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001959 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001960 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001961 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001962 }
1963
Evan Cheng6fb06762007-11-09 01:32:10 +00001964 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001965 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1966 // For PIC, the sequence is:
1967 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00001968 // RelocBase can be JumpTable, GOT or some sort of global base.
1969 if (PTy != MVT::i32)
1970 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1971 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1972 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001973 }
Evan Cheng6fb06762007-11-09 01:32:10 +00001974 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001975 }
1976 }
1977 break;
1978 case ISD::BRCOND:
1979 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1980 // Ensure that libcalls are emitted before a return.
1981 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1982 Tmp1 = LegalizeOp(Tmp1);
1983 LastCALLSEQ_END = DAG.getEntryNode();
1984
1985 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1986 case Expand: assert(0 && "It's impossible to expand bools");
1987 case Legal:
1988 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1989 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00001990 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001991 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1992
1993 // The top bits of the promoted condition are not necessarily zero, ensure
1994 // that the value is properly zero extended.
Dan Gohman07961cd2008-02-25 21:11:39 +00001995 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001996 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman07961cd2008-02-25 21:11:39 +00001997 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001998 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1999 break;
2000 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002001 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002002
2003 // Basic block destination (Op#2) is always legal.
2004 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2005
2006 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
2007 default: assert(0 && "This action is not supported yet!");
2008 case TargetLowering::Legal: break;
2009 case TargetLowering::Custom:
2010 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002011 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002012 break;
2013 case TargetLowering::Expand:
2014 // Expand brcond's setcc into its constituent parts and create a BR_CC
2015 // Node.
2016 if (Tmp2.getOpcode() == ISD::SETCC) {
2017 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
2018 Tmp2.getOperand(0), Tmp2.getOperand(1),
2019 Node->getOperand(2));
2020 } else {
2021 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
2022 DAG.getCondCode(ISD::SETNE), Tmp2,
2023 DAG.getConstant(0, Tmp2.getValueType()),
2024 Node->getOperand(2));
2025 }
2026 break;
2027 }
2028 break;
2029 case ISD::BR_CC:
2030 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2031 // Ensure that libcalls are emitted before a branch.
2032 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2033 Tmp1 = LegalizeOp(Tmp1);
2034 Tmp2 = Node->getOperand(2); // LHS
2035 Tmp3 = Node->getOperand(3); // RHS
2036 Tmp4 = Node->getOperand(1); // CC
2037
Dale Johannesen32100b22008-11-07 22:54:33 +00002038 LegalizeSetCC(TLI.getSetCCResultType(Tmp2), Tmp2, Tmp3, Tmp4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002039 LastCALLSEQ_END = DAG.getEntryNode();
2040
Evan Cheng71343822008-10-15 02:05:31 +00002041 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002042 // the LHS is a legal SETCC itself. In this case, we need to compare
2043 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00002044 if (Tmp3.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002045 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2046 Tmp4 = DAG.getCondCode(ISD::SETNE);
2047 }
2048
2049 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2050 Node->getOperand(4));
2051
2052 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2053 default: assert(0 && "Unexpected action for BR_CC!");
2054 case TargetLowering::Legal: break;
2055 case TargetLowering::Custom:
2056 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002057 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002058 break;
2059 }
2060 break;
2061 case ISD::LOAD: {
2062 LoadSDNode *LD = cast<LoadSDNode>(Node);
2063 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2064 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
2065
2066 ISD::LoadExtType ExtType = LD->getExtensionType();
2067 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands92c43912008-06-06 12:08:01 +00002068 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002069 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2070 Tmp3 = Result.getValue(0);
2071 Tmp4 = Result.getValue(1);
2072
2073 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2074 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002075 case TargetLowering::Legal:
2076 // If this is an unaligned load and the target doesn't support it,
2077 // expand it.
2078 if (!TLI.allowsUnalignedMemoryAccesses()) {
2079 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002080 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002081 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002082 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002083 TLI);
2084 Tmp3 = Result.getOperand(0);
2085 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00002086 Tmp3 = LegalizeOp(Tmp3);
2087 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002088 }
2089 }
2090 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002091 case TargetLowering::Custom:
2092 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002093 if (Tmp1.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002094 Tmp3 = LegalizeOp(Tmp1);
2095 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2096 }
2097 break;
2098 case TargetLowering::Promote: {
2099 // Only promote a load of vector type to another.
Duncan Sands92c43912008-06-06 12:08:01 +00002100 assert(VT.isVector() && "Cannot promote this load!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002101 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002102 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002103
2104 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
2105 LD->getSrcValueOffset(),
2106 LD->isVolatile(), LD->getAlignment());
2107 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2108 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2109 break;
2110 }
2111 }
2112 // Since loads produce two values, make sure to remember that we
2113 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002114 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2115 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif46bf5472008-08-26 22:36:50 +00002116 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002117 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00002118 MVT SrcVT = LD->getMemoryVT();
2119 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sands082524c2008-01-23 20:39:46 +00002120 int SVOffset = LD->getSrcValueOffset();
2121 unsigned Alignment = LD->getAlignment();
2122 bool isVolatile = LD->isVolatile();
2123
Duncan Sands92c43912008-06-06 12:08:01 +00002124 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002125 // Some targets pretend to have an i1 loading operation, and actually
2126 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2127 // bits are guaranteed to be zero; it helps the optimizers understand
2128 // that these bits are zero. It is also useful for EXTLOAD, since it
2129 // tells the optimizers that those bits are undefined. It would be
2130 // nice to have an effective generic way of getting these benefits...
2131 // Until such a way is found, don't insist on promoting i1 here.
2132 (SrcVT != MVT::i1 ||
Evan Cheng08c171a2008-10-14 21:26:46 +00002133 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002134 // Promote to a byte-sized load if not loading an integral number of
2135 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands92c43912008-06-06 12:08:01 +00002136 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2137 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002138 SDValue Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002139
2140 // The extra bits are guaranteed to be zero, since we stored them that
2141 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2142
2143 ISD::LoadExtType NewExtType =
2144 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2145
2146 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2147 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2148 NVT, isVolatile, Alignment);
2149
2150 Ch = Result.getValue(1); // The chain.
2151
2152 if (ExtType == ISD::SEXTLOAD)
2153 // Having the top bits zero doesn't help when sign extending.
2154 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2155 Result, DAG.getValueType(SrcVT));
2156 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2157 // All the top bits are guaranteed to be zero - inform the optimizers.
2158 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2159 DAG.getValueType(SrcVT));
2160
2161 Tmp1 = LegalizeOp(Result);
2162 Tmp2 = LegalizeOp(Ch);
2163 } else if (SrcWidth & (SrcWidth - 1)) {
2164 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands92c43912008-06-06 12:08:01 +00002165 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002166 "Unsupported extload!");
2167 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2168 assert(RoundWidth < SrcWidth);
2169 unsigned ExtraWidth = SrcWidth - RoundWidth;
2170 assert(ExtraWidth < RoundWidth);
2171 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2172 "Load size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002173 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2174 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002175 SDValue Lo, Hi, Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002176 unsigned IncrementSize;
2177
2178 if (TLI.isLittleEndian()) {
2179 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2180 // Load the bottom RoundWidth bits.
2181 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2182 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2183 Alignment);
2184
2185 // Load the remaining ExtraWidth bits.
2186 IncrementSize = RoundWidth / 8;
2187 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2188 DAG.getIntPtrConstant(IncrementSize));
2189 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2190 LD->getSrcValue(), SVOffset + IncrementSize,
2191 ExtraVT, isVolatile,
2192 MinAlign(Alignment, IncrementSize));
2193
2194 // Build a factor node to remember that this load is independent of the
2195 // other one.
2196 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2197 Hi.getValue(1));
2198
2199 // Move the top bits to the right place.
2200 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2201 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2202
2203 // Join the hi and lo parts.
2204 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002205 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00002206 // Big endian - avoid unaligned loads.
2207 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2208 // Load the top RoundWidth bits.
2209 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2210 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2211 Alignment);
2212
2213 // Load the remaining ExtraWidth bits.
2214 IncrementSize = RoundWidth / 8;
2215 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2216 DAG.getIntPtrConstant(IncrementSize));
2217 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2218 LD->getSrcValue(), SVOffset + IncrementSize,
2219 ExtraVT, isVolatile,
2220 MinAlign(Alignment, IncrementSize));
2221
2222 // Build a factor node to remember that this load is independent of the
2223 // other one.
2224 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2225 Hi.getValue(1));
2226
2227 // Move the top bits to the right place.
2228 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2229 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2230
2231 // Join the hi and lo parts.
2232 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2233 }
2234
2235 Tmp1 = LegalizeOp(Result);
2236 Tmp2 = LegalizeOp(Ch);
2237 } else {
Evan Cheng08c171a2008-10-14 21:26:46 +00002238 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002239 default: assert(0 && "This action is not supported yet!");
2240 case TargetLowering::Custom:
2241 isCustom = true;
2242 // FALLTHROUGH
2243 case TargetLowering::Legal:
2244 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2245 Tmp1 = Result.getValue(0);
2246 Tmp2 = Result.getValue(1);
2247
2248 if (isCustom) {
2249 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002250 if (Tmp3.getNode()) {
Duncan Sands082524c2008-01-23 20:39:46 +00002251 Tmp1 = LegalizeOp(Tmp3);
2252 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2253 }
2254 } else {
2255 // If this is an unaligned load and the target doesn't support it,
2256 // expand it.
2257 if (!TLI.allowsUnalignedMemoryAccesses()) {
2258 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002259 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sands082524c2008-01-23 20:39:46 +00002260 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002261 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sands082524c2008-01-23 20:39:46 +00002262 TLI);
2263 Tmp1 = Result.getOperand(0);
2264 Tmp2 = Result.getOperand(1);
2265 Tmp1 = LegalizeOp(Tmp1);
2266 Tmp2 = LegalizeOp(Tmp2);
2267 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002268 }
2269 }
Duncan Sands082524c2008-01-23 20:39:46 +00002270 break;
2271 case TargetLowering::Expand:
2272 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2273 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002274 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sands082524c2008-01-23 20:39:46 +00002275 LD->getSrcValueOffset(),
2276 LD->isVolatile(), LD->getAlignment());
2277 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2278 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2279 Tmp2 = LegalizeOp(Load.getValue(1));
2280 break;
2281 }
2282 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2283 // Turn the unsupported load into an EXTLOAD followed by an explicit
2284 // zero/sign extend inreg.
2285 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2286 Tmp1, Tmp2, LD->getSrcValue(),
2287 LD->getSrcValueOffset(), SrcVT,
2288 LD->isVolatile(), LD->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00002289 SDValue ValRes;
Duncan Sands082524c2008-01-23 20:39:46 +00002290 if (ExtType == ISD::SEXTLOAD)
2291 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2292 Result, DAG.getValueType(SrcVT));
2293 else
2294 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2295 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2296 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002297 break;
2298 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002299 }
Duncan Sands082524c2008-01-23 20:39:46 +00002300
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002301 // Since loads produce two values, make sure to remember that we legalized
2302 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002303 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2304 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002305 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002306 }
2307 }
2308 case ISD::EXTRACT_ELEMENT: {
Duncan Sands92c43912008-06-06 12:08:01 +00002309 MVT OpTy = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002310 switch (getTypeAction(OpTy)) {
2311 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2312 case Legal:
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002313 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002314 // 1 -> Hi
2315 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands92c43912008-06-06 12:08:01 +00002316 DAG.getConstant(OpTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002317 TLI.getShiftAmountTy()));
2318 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2319 } else {
2320 // 0 -> Lo
2321 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2322 Node->getOperand(0));
2323 }
2324 break;
2325 case Expand:
2326 // Get both the low and high parts.
2327 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002328 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002329 Result = Tmp2; // 1 -> Hi
2330 else
2331 Result = Tmp1; // 0 -> Lo
2332 break;
2333 }
2334 break;
2335 }
2336
2337 case ISD::CopyToReg:
2338 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2339
2340 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2341 "Register type must be legal!");
2342 // Legalize the incoming value (must be a legal type).
2343 Tmp2 = LegalizeOp(Node->getOperand(2));
2344 if (Node->getNumValues() == 1) {
2345 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2346 } else {
2347 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2348 if (Node->getNumOperands() == 4) {
2349 Tmp3 = LegalizeOp(Node->getOperand(3));
2350 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2351 Tmp3);
2352 } else {
2353 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2354 }
2355
2356 // Since this produces two values, make sure to remember that we legalized
2357 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002358 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2359 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002360 return Result;
2361 }
2362 break;
2363
2364 case ISD::RET:
2365 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2366
2367 // Ensure that libcalls are emitted before a return.
2368 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2369 Tmp1 = LegalizeOp(Tmp1);
2370 LastCALLSEQ_END = DAG.getEntryNode();
2371
2372 switch (Node->getNumOperands()) {
2373 case 3: // ret val
2374 Tmp2 = Node->getOperand(1);
2375 Tmp3 = Node->getOperand(2); // Signness
2376 switch (getTypeAction(Tmp2.getValueType())) {
2377 case Legal:
2378 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2379 break;
2380 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00002381 if (!Tmp2.getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002382 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002383 ExpandOp(Tmp2, Lo, Hi);
2384
2385 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002386 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002387 std::swap(Lo, Hi);
2388
Gabor Greif1c80d112008-08-28 21:40:38 +00002389 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002390 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2391 else
2392 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2393 Result = LegalizeOp(Result);
2394 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +00002395 SDNode *InVal = Tmp2.getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002396 int InIx = Tmp2.getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002397 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2398 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002399
2400 // Figure out if there is a simple type corresponding to this Vector
2401 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002402 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002403 if (TLI.isTypeLegal(TVT)) {
2404 // Turn this into a return of the vector type.
2405 Tmp2 = LegalizeOp(Tmp2);
2406 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2407 } else if (NumElems == 1) {
2408 // Turn this into a return of the scalar type.
2409 Tmp2 = ScalarizeVectorOp(Tmp2);
2410 Tmp2 = LegalizeOp(Tmp2);
2411 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2412
2413 // FIXME: Returns of gcc generic vectors smaller than a legal type
2414 // should be returned in integer registers!
2415
2416 // The scalarized value type may not be legal, e.g. it might require
2417 // promotion or expansion. Relegalize the return.
2418 Result = LegalizeOp(Result);
2419 } else {
2420 // FIXME: Returns of gcc generic vectors larger than a legal vector
2421 // type should be returned by reference!
Dan Gohman8181bd12008-07-27 21:46:04 +00002422 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002423 SplitVectorOp(Tmp2, Lo, Hi);
2424 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2425 Result = LegalizeOp(Result);
2426 }
2427 }
2428 break;
2429 case Promote:
2430 Tmp2 = PromoteOp(Node->getOperand(1));
2431 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2432 Result = LegalizeOp(Result);
2433 break;
2434 }
2435 break;
2436 case 1: // ret void
2437 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2438 break;
2439 default: { // ret <values>
Dan Gohman8181bd12008-07-27 21:46:04 +00002440 SmallVector<SDValue, 8> NewValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002441 NewValues.push_back(Tmp1);
2442 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2443 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2444 case Legal:
2445 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2446 NewValues.push_back(Node->getOperand(i+1));
2447 break;
2448 case Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002449 SDValue Lo, Hi;
Duncan Sands92c43912008-06-06 12:08:01 +00002450 assert(!Node->getOperand(i).getValueType().isExtended() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002451 "FIXME: TODO: implement returning non-legal vector types!");
2452 ExpandOp(Node->getOperand(i), Lo, Hi);
2453 NewValues.push_back(Lo);
2454 NewValues.push_back(Node->getOperand(i+1));
Gabor Greif1c80d112008-08-28 21:40:38 +00002455 if (Hi.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002456 NewValues.push_back(Hi);
2457 NewValues.push_back(Node->getOperand(i+1));
2458 }
2459 break;
2460 }
2461 case Promote:
2462 assert(0 && "Can't promote multiple return value yet!");
2463 }
2464
2465 if (NewValues.size() == Node->getNumOperands())
2466 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2467 else
2468 Result = DAG.getNode(ISD::RET, MVT::Other,
2469 &NewValues[0], NewValues.size());
2470 break;
2471 }
2472 }
2473
2474 if (Result.getOpcode() == ISD::RET) {
2475 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2476 default: assert(0 && "This action is not supported yet!");
2477 case TargetLowering::Legal: break;
2478 case TargetLowering::Custom:
2479 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002480 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002481 break;
2482 }
2483 }
2484 break;
2485 case ISD::STORE: {
2486 StoreSDNode *ST = cast<StoreSDNode>(Node);
2487 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2488 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2489 int SVOffset = ST->getSrcValueOffset();
2490 unsigned Alignment = ST->getAlignment();
2491 bool isVolatile = ST->isVolatile();
2492
2493 if (!ST->isTruncatingStore()) {
2494 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2495 // FIXME: We shouldn't do this for TargetConstantFP's.
2496 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2497 // to phase ordering between legalized code and the dag combiner. This
2498 // probably means that we need to integrate dag combiner and legalizer
2499 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002500 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002501 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002502 if (CFP->getValueType(0) == MVT::f32 &&
2503 getTypeAction(MVT::i32) == Legal) {
Dan Gohman39509762008-03-11 00:11:06 +00002504 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002505 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen1616e902007-09-11 18:32:33 +00002506 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002507 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2508 SVOffset, isVolatile, Alignment);
2509 break;
2510 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002511 // If this target supports 64-bit registers, do a single 64-bit store.
2512 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002513 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman39509762008-03-11 00:11:06 +00002514 zextOrTrunc(64), MVT::i64);
Chris Lattner19f229a2007-10-15 05:46:06 +00002515 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2516 SVOffset, isVolatile, Alignment);
2517 break;
Duncan Sands2418bec2008-06-13 19:07:40 +00002518 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002519 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2520 // stores. If the target supports neither 32- nor 64-bits, this
2521 // xform is certainly not worth it.
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002522 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman8181bd12008-07-27 21:46:04 +00002523 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2524 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002525 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002526
2527 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2528 SVOffset, isVolatile, Alignment);
2529 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002530 DAG.getIntPtrConstant(4));
Chris Lattner19f229a2007-10-15 05:46:06 +00002531 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002532 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002533
2534 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2535 break;
2536 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002537 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002538 }
2539
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002540 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002541 case Legal: {
2542 Tmp3 = LegalizeOp(ST->getValue());
2543 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2544 ST->getOffset());
2545
Duncan Sands92c43912008-06-06 12:08:01 +00002546 MVT VT = Tmp3.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002547 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2548 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002549 case TargetLowering::Legal:
2550 // If this is an unaligned store and the target doesn't support it,
2551 // expand it.
2552 if (!TLI.allowsUnalignedMemoryAccesses()) {
2553 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002554 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002555 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002556 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002557 TLI);
2558 }
2559 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002560 case TargetLowering::Custom:
2561 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002562 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002563 break;
2564 case TargetLowering::Promote:
Duncan Sands92c43912008-06-06 12:08:01 +00002565 assert(VT.isVector() && "Unknown legal promote case!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002566 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2567 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2568 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2569 ST->getSrcValue(), SVOffset, isVolatile,
2570 Alignment);
2571 break;
2572 }
2573 break;
2574 }
2575 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002576 if (!ST->getMemoryVT().isVector()) {
2577 // Truncate the value and store the result.
2578 Tmp3 = PromoteOp(ST->getValue());
2579 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2580 SVOffset, ST->getMemoryVT(),
2581 isVolatile, Alignment);
2582 break;
2583 }
2584 // Fall thru to expand for vector
2585 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002586 unsigned IncrementSize = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002587 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002588
2589 // If this is a vector type, then we have to calculate the increment as
2590 // the product of the element size in bytes, and the number of elements
2591 // in the high half of the vector.
Duncan Sands92c43912008-06-06 12:08:01 +00002592 if (ST->getValue().getValueType().isVector()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002593 SDNode *InVal = ST->getValue().getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002594 int InIx = ST->getValue().getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002595 MVT InVT = InVal->getValueType(InIx);
2596 unsigned NumElems = InVT.getVectorNumElements();
2597 MVT EVT = InVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002598
2599 // Figure out if there is a simple type corresponding to this Vector
2600 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002601 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002602 if (TLI.isTypeLegal(TVT)) {
2603 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002604 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002605 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2606 SVOffset, isVolatile, Alignment);
2607 Result = LegalizeOp(Result);
2608 break;
2609 } else if (NumElems == 1) {
2610 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002611 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002612 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2613 SVOffset, isVolatile, Alignment);
2614 // The scalarized value type may not be legal, e.g. it might require
2615 // promotion or expansion. Relegalize the scalar store.
2616 Result = LegalizeOp(Result);
2617 break;
2618 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +00002619 // Check if we have widen this node with another value
2620 std::map<SDValue, SDValue>::iterator I =
2621 WidenNodes.find(ST->getValue());
2622 if (I != WidenNodes.end()) {
2623 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2624 break;
2625 }
2626 else {
2627 SplitVectorOp(ST->getValue(), Lo, Hi);
2628 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2629 EVT.getSizeInBits()/8;
2630 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002631 }
2632 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002633 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greif1c80d112008-08-28 21:40:38 +00002634 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002635
Richard Pennington73ae9e42008-09-25 16:15:10 +00002636 if (Hi.getNode() && TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002637 std::swap(Lo, Hi);
2638 }
2639
2640 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2641 SVOffset, isVolatile, Alignment);
2642
Gabor Greif1c80d112008-08-28 21:40:38 +00002643 if (Hi.getNode() == NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002644 // Must be int <-> float one-to-one expansion.
2645 Result = Lo;
2646 break;
2647 }
2648
2649 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002650 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002651 assert(isTypeLegal(Tmp2.getValueType()) &&
2652 "Pointers must be legal!");
2653 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002654 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002655 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2656 SVOffset, isVolatile, Alignment);
2657 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2658 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00002659 } // case Expand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002660 }
2661 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002662 switch (getTypeAction(ST->getValue().getValueType())) {
2663 case Legal:
2664 Tmp3 = LegalizeOp(ST->getValue());
2665 break;
2666 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002667 if (!ST->getValue().getValueType().isVector()) {
2668 // We can promote the value, the truncstore will still take care of it.
2669 Tmp3 = PromoteOp(ST->getValue());
2670 break;
2671 }
2672 // Vector case falls through to expand
Chris Lattner3bc08502008-01-17 19:59:44 +00002673 case Expand:
2674 // Just store the low part. This may become a non-trunc store, so make
2675 // sure to use getTruncStore, not UpdateNodeOperands below.
2676 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2677 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2678 SVOffset, MVT::i8, isVolatile, Alignment);
2679 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002680
Duncan Sands92c43912008-06-06 12:08:01 +00002681 MVT StVT = ST->getMemoryVT();
2682 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands40676662008-01-22 07:17:34 +00002683
Duncan Sands92c43912008-06-06 12:08:01 +00002684 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands40676662008-01-22 07:17:34 +00002685 // Promote to a byte-sized store with upper bits zero if not
2686 // storing an integral number of bytes. For example, promote
2687 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands92c43912008-06-06 12:08:01 +00002688 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands40676662008-01-22 07:17:34 +00002689 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2690 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2691 SVOffset, NVT, isVolatile, Alignment);
2692 } else if (StWidth & (StWidth - 1)) {
2693 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands92c43912008-06-06 12:08:01 +00002694 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands40676662008-01-22 07:17:34 +00002695 "Unsupported truncstore!");
2696 unsigned RoundWidth = 1 << Log2_32(StWidth);
2697 assert(RoundWidth < StWidth);
2698 unsigned ExtraWidth = StWidth - RoundWidth;
2699 assert(ExtraWidth < RoundWidth);
2700 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2701 "Store size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002702 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2703 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002704 SDValue Lo, Hi;
Duncan Sands40676662008-01-22 07:17:34 +00002705 unsigned IncrementSize;
2706
2707 if (TLI.isLittleEndian()) {
2708 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2709 // Store the bottom RoundWidth bits.
2710 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2711 SVOffset, RoundVT,
2712 isVolatile, Alignment);
2713
2714 // Store the remaining ExtraWidth bits.
2715 IncrementSize = RoundWidth / 8;
2716 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2717 DAG.getIntPtrConstant(IncrementSize));
2718 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2719 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2720 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2721 SVOffset + IncrementSize, ExtraVT, isVolatile,
2722 MinAlign(Alignment, IncrementSize));
2723 } else {
2724 // Big endian - avoid unaligned stores.
2725 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2726 // Store the top RoundWidth bits.
2727 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2728 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2729 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2730 RoundVT, isVolatile, Alignment);
2731
2732 // Store the remaining ExtraWidth bits.
2733 IncrementSize = RoundWidth / 8;
2734 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2735 DAG.getIntPtrConstant(IncrementSize));
2736 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2737 SVOffset + IncrementSize, ExtraVT, isVolatile,
2738 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002739 }
Duncan Sands40676662008-01-22 07:17:34 +00002740
2741 // The order of the stores doesn't matter.
2742 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2743 } else {
2744 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2745 Tmp2 != ST->getBasePtr())
2746 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2747 ST->getOffset());
2748
2749 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2750 default: assert(0 && "This action is not supported yet!");
2751 case TargetLowering::Legal:
2752 // If this is an unaligned store and the target doesn't support it,
2753 // expand it.
2754 if (!TLI.allowsUnalignedMemoryAccesses()) {
2755 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002756 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands40676662008-01-22 07:17:34 +00002757 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002758 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands40676662008-01-22 07:17:34 +00002759 TLI);
2760 }
2761 break;
2762 case TargetLowering::Custom:
2763 Result = TLI.LowerOperation(Result, DAG);
2764 break;
2765 case Expand:
2766 // TRUNCSTORE:i16 i32 -> STORE i16
2767 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2768 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2769 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2770 isVolatile, Alignment);
2771 break;
2772 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002773 }
2774 }
2775 break;
2776 }
2777 case ISD::PCMARKER:
2778 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2779 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2780 break;
2781 case ISD::STACKSAVE:
2782 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2783 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2784 Tmp1 = Result.getValue(0);
2785 Tmp2 = Result.getValue(1);
2786
2787 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2788 default: assert(0 && "This action is not supported yet!");
2789 case TargetLowering::Legal: break;
2790 case TargetLowering::Custom:
2791 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002792 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002793 Tmp1 = LegalizeOp(Tmp3);
2794 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2795 }
2796 break;
2797 case TargetLowering::Expand:
2798 // Expand to CopyFromReg if the target set
2799 // StackPointerRegisterToSaveRestore.
2800 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2801 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2802 Node->getValueType(0));
2803 Tmp2 = Tmp1.getValue(1);
2804 } else {
2805 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2806 Tmp2 = Node->getOperand(0);
2807 }
2808 break;
2809 }
2810
2811 // Since stacksave produce two values, make sure to remember that we
2812 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002813 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2814 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002815 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002816
2817 case ISD::STACKRESTORE:
2818 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2819 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2820 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2821
2822 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2823 default: assert(0 && "This action is not supported yet!");
2824 case TargetLowering::Legal: break;
2825 case TargetLowering::Custom:
2826 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002827 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002828 break;
2829 case TargetLowering::Expand:
2830 // Expand to CopyToReg if the target set
2831 // StackPointerRegisterToSaveRestore.
2832 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2833 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2834 } else {
2835 Result = Tmp1;
2836 }
2837 break;
2838 }
2839 break;
2840
2841 case ISD::READCYCLECOUNTER:
2842 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2843 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2844 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2845 Node->getValueType(0))) {
2846 default: assert(0 && "This action is not supported yet!");
2847 case TargetLowering::Legal:
2848 Tmp1 = Result.getValue(0);
2849 Tmp2 = Result.getValue(1);
2850 break;
2851 case TargetLowering::Custom:
2852 Result = TLI.LowerOperation(Result, DAG);
2853 Tmp1 = LegalizeOp(Result.getValue(0));
2854 Tmp2 = LegalizeOp(Result.getValue(1));
2855 break;
2856 }
2857
2858 // Since rdcc produce two values, make sure to remember that we legalized
2859 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002860 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2861 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002862 return Result;
2863
2864 case ISD::SELECT:
2865 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2866 case Expand: assert(0 && "It's impossible to expand bools");
2867 case Legal:
2868 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2869 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002870 case Promote: {
Mon P Wang1448aad2008-10-30 08:01:45 +00002871 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002872 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2873 // Make sure the condition is either zero or one.
Dan Gohman07961cd2008-02-25 21:11:39 +00002874 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002875 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman07961cd2008-02-25 21:11:39 +00002876 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002877 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2878 break;
2879 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002880 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002881 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2882 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2883
2884 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2885
2886 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2887 default: assert(0 && "This action is not supported yet!");
2888 case TargetLowering::Legal: break;
2889 case TargetLowering::Custom: {
2890 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002891 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002892 break;
2893 }
2894 case TargetLowering::Expand:
2895 if (Tmp1.getOpcode() == ISD::SETCC) {
2896 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2897 Tmp2, Tmp3,
2898 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2899 } else {
2900 Result = DAG.getSelectCC(Tmp1,
2901 DAG.getConstant(0, Tmp1.getValueType()),
2902 Tmp2, Tmp3, ISD::SETNE);
2903 }
2904 break;
2905 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00002906 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002907 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2908 unsigned ExtOp, TruncOp;
Duncan Sands92c43912008-06-06 12:08:01 +00002909 if (Tmp2.getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002910 ExtOp = ISD::BIT_CONVERT;
2911 TruncOp = ISD::BIT_CONVERT;
Duncan Sands92c43912008-06-06 12:08:01 +00002912 } else if (Tmp2.getValueType().isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002913 ExtOp = ISD::ANY_EXTEND;
2914 TruncOp = ISD::TRUNCATE;
2915 } else {
2916 ExtOp = ISD::FP_EXTEND;
2917 TruncOp = ISD::FP_ROUND;
2918 }
2919 // Promote each of the values to the new type.
2920 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2921 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2922 // Perform the larger operation, then round down.
2923 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00002924 if (TruncOp != ISD::FP_ROUND)
2925 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2926 else
2927 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2928 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002929 break;
2930 }
2931 }
2932 break;
2933 case ISD::SELECT_CC: {
2934 Tmp1 = Node->getOperand(0); // LHS
2935 Tmp2 = Node->getOperand(1); // RHS
2936 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2937 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman8181bd12008-07-27 21:46:04 +00002938 SDValue CC = Node->getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002939
Dale Johannesen32100b22008-11-07 22:54:33 +00002940 LegalizeSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2, CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002941
Evan Cheng71343822008-10-15 02:05:31 +00002942 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943 // the LHS is a legal SETCC itself. In this case, we need to compare
2944 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00002945 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002946 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2947 CC = DAG.getCondCode(ISD::SETNE);
2948 }
2949 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2950
2951 // Everything is legal, see if we should expand this op or something.
2952 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2953 default: assert(0 && "This action is not supported yet!");
2954 case TargetLowering::Legal: break;
2955 case TargetLowering::Custom:
2956 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002957 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002958 break;
2959 }
2960 break;
2961 }
2962 case ISD::SETCC:
2963 Tmp1 = Node->getOperand(0);
2964 Tmp2 = Node->getOperand(1);
2965 Tmp3 = Node->getOperand(2);
Evan Cheng71343822008-10-15 02:05:31 +00002966 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002967
2968 // If we had to Expand the SetCC operands into a SELECT node, then it may
2969 // not always be possible to return a true LHS & RHS. In this case, just
2970 // return the value we legalized, returned in the LHS
Gabor Greif1c80d112008-08-28 21:40:38 +00002971 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002972 Result = Tmp1;
2973 break;
2974 }
2975
2976 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
2977 default: assert(0 && "Cannot handle this action for SETCC yet!");
2978 case TargetLowering::Custom:
2979 isCustom = true;
2980 // FALLTHROUGH.
2981 case TargetLowering::Legal:
2982 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2983 if (isCustom) {
2984 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002985 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002986 }
2987 break;
2988 case TargetLowering::Promote: {
2989 // First step, figure out the appropriate operation to use.
2990 // Allow SETCC to not be supported for all legal data types
2991 // Mostly this targets FP
Duncan Sands92c43912008-06-06 12:08:01 +00002992 MVT NewInTy = Node->getOperand(0).getValueType();
2993 MVT OldVT = NewInTy; OldVT = OldVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002994
2995 // Scan for the appropriate larger type to use.
2996 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00002997 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002998
Duncan Sands92c43912008-06-06 12:08:01 +00002999 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003000 "Fell off of the edge of the integer world");
Duncan Sands92c43912008-06-06 12:08:01 +00003001 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003002 "Fell off of the edge of the floating point world");
3003
3004 // If the target supports SETCC of this type, use it.
3005 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
3006 break;
3007 }
Duncan Sands92c43912008-06-06 12:08:01 +00003008 if (NewInTy.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003009 assert(0 && "Cannot promote Legal Integer SETCC yet");
3010 else {
3011 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
3012 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
3013 }
3014 Tmp1 = LegalizeOp(Tmp1);
3015 Tmp2 = LegalizeOp(Tmp2);
3016 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3017 Result = LegalizeOp(Result);
3018 break;
3019 }
3020 case TargetLowering::Expand:
3021 // Expand a setcc node into a select_cc of the same condition, lhs, and
3022 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands92c43912008-06-06 12:08:01 +00003023 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003024 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3025 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3026 Tmp3);
3027 break;
3028 }
3029 break;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003030 case ISD::VSETCC: {
3031 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3032 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman8181bd12008-07-27 21:46:04 +00003033 SDValue CC = Node->getOperand(2);
Nate Begeman9a1ce152008-05-12 19:40:03 +00003034
3035 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3036
3037 // Everything is legal, see if we should expand this op or something.
3038 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3039 default: assert(0 && "This action is not supported yet!");
3040 case TargetLowering::Legal: break;
3041 case TargetLowering::Custom:
3042 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003043 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003044 break;
3045 }
3046 break;
3047 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003048
3049 case ISD::SHL_PARTS:
3050 case ISD::SRA_PARTS:
3051 case ISD::SRL_PARTS: {
Dan Gohman8181bd12008-07-27 21:46:04 +00003052 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003053 bool Changed = false;
3054 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3055 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3056 Changed |= Ops.back() != Node->getOperand(i);
3057 }
3058 if (Changed)
3059 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
3060
3061 switch (TLI.getOperationAction(Node->getOpcode(),
3062 Node->getValueType(0))) {
3063 default: assert(0 && "This action is not supported yet!");
3064 case TargetLowering::Legal: break;
3065 case TargetLowering::Custom:
3066 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003067 if (Tmp1.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003068 SDValue Tmp2, RetVal(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003069 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
3070 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman8181bd12008-07-27 21:46:04 +00003071 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00003072 if (i == Op.getResNo())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003073 RetVal = Tmp2;
3074 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003075 assert(RetVal.getNode() && "Illegal result number");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003076 return RetVal;
3077 }
3078 break;
3079 }
3080
3081 // Since these produce multiple values, make sure to remember that we
3082 // legalized all of them.
3083 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman8181bd12008-07-27 21:46:04 +00003084 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00003085 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003086 }
3087
3088 // Binary operators
3089 case ISD::ADD:
3090 case ISD::SUB:
3091 case ISD::MUL:
3092 case ISD::MULHS:
3093 case ISD::MULHU:
3094 case ISD::UDIV:
3095 case ISD::SDIV:
3096 case ISD::AND:
3097 case ISD::OR:
3098 case ISD::XOR:
3099 case ISD::SHL:
3100 case ISD::SRL:
3101 case ISD::SRA:
3102 case ISD::FADD:
3103 case ISD::FSUB:
3104 case ISD::FMUL:
3105 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00003106 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003107 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3108 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3109 case Expand: assert(0 && "Not possible");
3110 case Legal:
3111 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3112 break;
3113 case Promote:
3114 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3115 break;
3116 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003117
3118 if ((Node->getOpcode() == ISD::SHL ||
3119 Node->getOpcode() == ISD::SRL ||
3120 Node->getOpcode() == ISD::SRA) &&
3121 !Node->getValueType(0).isVector()) {
Mon P Wang9901e732008-12-09 05:46:39 +00003122 Tmp2 = LegalizeShiftAmount(Tmp2);
3123 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003124
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003125 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangbff5d9c2008-11-10 04:46:22 +00003126
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003127 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3128 default: assert(0 && "BinOp legalize operation not supported");
3129 case TargetLowering::Legal: break;
3130 case TargetLowering::Custom:
3131 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003132 if (Tmp1.getNode()) {
Nate Begemanbb1ce942008-07-29 15:49:41 +00003133 Result = Tmp1;
3134 break;
Nate Begeman7569e762008-07-29 19:07:27 +00003135 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003136 // Fall through if the custom lower can't deal with the operation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003137 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00003138 MVT VT = Op.getValueType();
Mon P Wang1448aad2008-10-30 08:01:45 +00003139
Dan Gohman5a199552007-10-08 18:33:35 +00003140 // See if multiply or divide can be lowered using two-result operations.
3141 SDVTList VTs = DAG.getVTList(VT, VT);
3142 if (Node->getOpcode() == ISD::MUL) {
3143 // We just need the low half of the multiply; try both the signed
3144 // and unsigned forms. If the target supports both SMUL_LOHI and
3145 // UMUL_LOHI, form a preference by checking which forms of plain
3146 // MULH it supports.
3147 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3148 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3149 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3150 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3151 unsigned OpToUse = 0;
3152 if (HasSMUL_LOHI && !HasMULHS) {
3153 OpToUse = ISD::SMUL_LOHI;
3154 } else if (HasUMUL_LOHI && !HasMULHU) {
3155 OpToUse = ISD::UMUL_LOHI;
3156 } else if (HasSMUL_LOHI) {
3157 OpToUse = ISD::SMUL_LOHI;
3158 } else if (HasUMUL_LOHI) {
3159 OpToUse = ISD::UMUL_LOHI;
3160 }
3161 if (OpToUse) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003162 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003163 break;
3164 }
3165 }
3166 if (Node->getOpcode() == ISD::MULHS &&
3167 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003168 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3169 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003170 break;
3171 }
3172 if (Node->getOpcode() == ISD::MULHU &&
3173 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003174 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3175 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003176 break;
3177 }
3178 if (Node->getOpcode() == ISD::SDIV &&
3179 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003180 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3181 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003182 break;
3183 }
3184 if (Node->getOpcode() == ISD::UDIV &&
3185 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003186 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3187 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003188 break;
3189 }
Mon P Wang1448aad2008-10-30 08:01:45 +00003190
Dan Gohman6d05cac2007-10-11 23:57:53 +00003191 // Check to see if we have a libcall for this operator.
3192 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3193 bool isSigned = false;
3194 switch (Node->getOpcode()) {
3195 case ISD::UDIV:
3196 case ISD::SDIV:
3197 if (VT == MVT::i32) {
3198 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang1448aad2008-10-30 08:01:45 +00003199 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003200 isSigned = Node->getOpcode() == ISD::SDIV;
3201 }
3202 break;
Chris Lattner48188652008-10-04 21:27:46 +00003203 case ISD::MUL:
3204 if (VT == MVT::i32)
3205 LC = RTLIB::MUL_I32;
3206 break;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003207 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00003208 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3209 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003210 break;
3211 default: break;
3212 }
3213 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003214 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003215 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003216 break;
3217 }
Mon P Wang1448aad2008-10-30 08:01:45 +00003218
Duncan Sands92c43912008-06-06 12:08:01 +00003219 assert(Node->getValueType(0).isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003220 "Cannot expand this binary operator!");
3221 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003222 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003223 break;
3224 }
3225 case TargetLowering::Promote: {
3226 switch (Node->getOpcode()) {
3227 default: assert(0 && "Do not know how to promote this BinOp!");
3228 case ISD::AND:
3229 case ISD::OR:
3230 case ISD::XOR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003231 MVT OVT = Node->getValueType(0);
3232 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3233 assert(OVT.isVector() && "Cannot promote this BinOp!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003234 // Bit convert each of the values to the new type.
3235 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3236 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3237 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3238 // Bit convert the result back the original type.
3239 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3240 break;
3241 }
3242 }
3243 }
3244 }
3245 break;
3246
Dan Gohman475cd732007-10-05 14:17:22 +00003247 case ISD::SMUL_LOHI:
3248 case ISD::UMUL_LOHI:
3249 case ISD::SDIVREM:
3250 case ISD::UDIVREM:
3251 // These nodes will only be produced by target-specific lowering, so
3252 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003253 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003254 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003255
3256 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3257 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3258 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003259 break;
3260
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003261 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3262 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3263 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3264 case Expand: assert(0 && "Not possible");
3265 case Legal:
3266 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3267 break;
3268 case Promote:
3269 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3270 break;
3271 }
3272
3273 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3274
3275 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3276 default: assert(0 && "Operation not supported");
3277 case TargetLowering::Custom:
3278 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003279 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003280 break;
3281 case TargetLowering::Legal: break;
3282 case TargetLowering::Expand: {
3283 // If this target supports fabs/fneg natively and select is cheap,
3284 // do this efficiently.
3285 if (!TLI.isSelectExpensive() &&
3286 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3287 TargetLowering::Legal &&
3288 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3289 TargetLowering::Legal) {
3290 // Get the sign bit of the RHS.
Duncan Sands92c43912008-06-06 12:08:01 +00003291 MVT IVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003292 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman8181bd12008-07-27 21:46:04 +00003293 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel502151f2008-03-10 15:42:14 +00003294 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003295 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3296 // Get the absolute value of the result.
Dan Gohman8181bd12008-07-27 21:46:04 +00003297 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003298 // Select between the nabs and abs value based on the sign bit of
3299 // the input.
3300 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3301 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3302 AbsVal),
3303 AbsVal);
3304 Result = LegalizeOp(Result);
3305 break;
3306 }
3307
3308 // Otherwise, do bitwise ops!
Duncan Sands92c43912008-06-06 12:08:01 +00003309 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003310 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3311 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3312 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3313 Result = LegalizeOp(Result);
3314 break;
3315 }
3316 }
3317 break;
3318
3319 case ISD::ADDC:
3320 case ISD::SUBC:
3321 Tmp1 = LegalizeOp(Node->getOperand(0));
3322 Tmp2 = LegalizeOp(Node->getOperand(1));
3323 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003324 Tmp3 = Result.getValue(0);
3325 Tmp4 = Result.getValue(1);
3326
3327 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3328 default: assert(0 && "This action is not supported yet!");
3329 case TargetLowering::Legal:
3330 break;
3331 case TargetLowering::Custom:
3332 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3333 if (Tmp1.getNode() != NULL) {
Sanjiv Guptad57f2e12008-11-27 05:58:04 +00003334 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003335 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3336 }
3337 break;
3338 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003339 // Since this produces two values, make sure to remember that we legalized
3340 // both of them.
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003341 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3342 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3343 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003344
3345 case ISD::ADDE:
3346 case ISD::SUBE:
3347 Tmp1 = LegalizeOp(Node->getOperand(0));
3348 Tmp2 = LegalizeOp(Node->getOperand(1));
3349 Tmp3 = LegalizeOp(Node->getOperand(2));
3350 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003351 Tmp3 = Result.getValue(0);
3352 Tmp4 = Result.getValue(1);
3353
3354 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3355 default: assert(0 && "This action is not supported yet!");
3356 case TargetLowering::Legal:
3357 break;
3358 case TargetLowering::Custom:
3359 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3360 if (Tmp1.getNode() != NULL) {
Sanjiv Guptad57f2e12008-11-27 05:58:04 +00003361 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003362 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3363 }
3364 break;
3365 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003366 // Since this produces two values, make sure to remember that we legalized
3367 // both of them.
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003368 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3369 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3370 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003371
3372 case ISD::BUILD_PAIR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003373 MVT PairTy = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003374 // TODO: handle the case where the Lo and Hi operands are not of legal type
3375 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3376 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3377 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3378 case TargetLowering::Promote:
3379 case TargetLowering::Custom:
3380 assert(0 && "Cannot promote/custom this yet!");
3381 case TargetLowering::Legal:
3382 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3383 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3384 break;
3385 case TargetLowering::Expand:
3386 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3387 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3388 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003389 DAG.getConstant(PairTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003390 TLI.getShiftAmountTy()));
3391 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3392 break;
3393 }
3394 break;
3395 }
3396
3397 case ISD::UREM:
3398 case ISD::SREM:
3399 case ISD::FREM:
3400 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3401 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3402
3403 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3404 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3405 case TargetLowering::Custom:
3406 isCustom = true;
3407 // FALLTHROUGH
3408 case TargetLowering::Legal:
3409 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3410 if (isCustom) {
3411 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003412 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003413 }
3414 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003415 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003416 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3417 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands92c43912008-06-06 12:08:01 +00003418 MVT VT = Node->getValueType(0);
Dan Gohman5a199552007-10-08 18:33:35 +00003419
3420 // See if remainder can be lowered using two-result operations.
3421 SDVTList VTs = DAG.getVTList(VT, VT);
3422 if (Node->getOpcode() == ISD::SREM &&
3423 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003424 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003425 break;
3426 }
3427 if (Node->getOpcode() == ISD::UREM &&
3428 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003429 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003430 break;
3431 }
3432
Duncan Sands92c43912008-06-06 12:08:01 +00003433 if (VT.isInteger()) {
Dan Gohman5a199552007-10-08 18:33:35 +00003434 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003435 TargetLowering::Legal) {
3436 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003437 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3438 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3439 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands92c43912008-06-06 12:08:01 +00003440 } else if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003441 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003442 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003443 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003444 "Cannot expand this binary operator!");
3445 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3446 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman8181bd12008-07-27 21:46:04 +00003447 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003448 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003449 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003450 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00003451 assert(VT.isFloatingPoint() &&
Dan Gohman59b4b102007-11-06 22:11:54 +00003452 "remainder op must have integer or floating-point type");
Duncan Sands92c43912008-06-06 12:08:01 +00003453 if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003454 Result = LegalizeOp(UnrollVectorOp(Op));
3455 } else {
3456 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003457 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3458 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003459 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003460 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003461 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003462 }
3463 break;
3464 }
Dan Gohman5a199552007-10-08 18:33:35 +00003465 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003466 break;
3467 case ISD::VAARG: {
3468 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3469 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3470
Duncan Sands92c43912008-06-06 12:08:01 +00003471 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003472 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3473 default: assert(0 && "This action is not supported yet!");
3474 case TargetLowering::Custom:
3475 isCustom = true;
3476 // FALLTHROUGH
3477 case TargetLowering::Legal:
3478 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3479 Result = Result.getValue(0);
3480 Tmp1 = Result.getValue(1);
3481
3482 if (isCustom) {
3483 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003484 if (Tmp2.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003485 Result = LegalizeOp(Tmp2);
3486 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3487 }
3488 }
3489 break;
3490 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003491 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003492 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003493 // Increment the pointer, VAList, to the next vaarg
Duncan Sands55a4c232008-11-03 11:51:11 +00003494 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3495 DAG.getConstant(TLI.getTargetData()->getABITypeSize(VT.getTypeForMVT()),
3496 TLI.getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003497 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00003498 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003499 // Load the actual argument out of the pointer VAList
3500 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3501 Tmp1 = LegalizeOp(Result.getValue(1));
3502 Result = LegalizeOp(Result);
3503 break;
3504 }
3505 }
3506 // Since VAARG produces two values, make sure to remember that we
3507 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003508 AddLegalizedOperand(SDValue(Node, 0), Result);
3509 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00003510 return Op.getResNo() ? Tmp1 : Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003511 }
3512
3513 case ISD::VACOPY:
3514 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3515 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3516 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3517
3518 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3519 default: assert(0 && "This action is not supported yet!");
3520 case TargetLowering::Custom:
3521 isCustom = true;
3522 // FALLTHROUGH
3523 case TargetLowering::Legal:
3524 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3525 Node->getOperand(3), Node->getOperand(4));
3526 if (isCustom) {
3527 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003528 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003529 }
3530 break;
3531 case TargetLowering::Expand:
3532 // This defaults to loading a pointer from the input and storing it to the
3533 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003534 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3535 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman6b9a08e2008-04-17 02:09:26 +00003536 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3537 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003538 break;
3539 }
3540 break;
3541
3542 case ISD::VAEND:
3543 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3544 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3545
3546 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3547 default: assert(0 && "This action is not supported yet!");
3548 case TargetLowering::Custom:
3549 isCustom = true;
3550 // FALLTHROUGH
3551 case TargetLowering::Legal:
3552 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3553 if (isCustom) {
3554 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003555 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003556 }
3557 break;
3558 case TargetLowering::Expand:
3559 Result = Tmp1; // Default to a no-op, return the chain
3560 break;
3561 }
3562 break;
3563
3564 case ISD::VASTART:
3565 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3566 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3567
3568 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3569
3570 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3571 default: assert(0 && "This action is not supported yet!");
3572 case TargetLowering::Legal: break;
3573 case TargetLowering::Custom:
3574 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003575 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003576 break;
3577 }
3578 break;
3579
3580 case ISD::ROTL:
3581 case ISD::ROTR:
3582 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3583 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3584 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3585 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3586 default:
3587 assert(0 && "ROTL/ROTR legalize operation not supported");
3588 break;
3589 case TargetLowering::Legal:
3590 break;
3591 case TargetLowering::Custom:
3592 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003593 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003594 break;
3595 case TargetLowering::Promote:
3596 assert(0 && "Do not know how to promote ROTL/ROTR");
3597 break;
3598 case TargetLowering::Expand:
3599 assert(0 && "Do not know how to expand ROTL/ROTR");
3600 break;
3601 }
3602 break;
3603
3604 case ISD::BSWAP:
3605 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3606 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3607 case TargetLowering::Custom:
3608 assert(0 && "Cannot custom legalize this yet!");
3609 case TargetLowering::Legal:
3610 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3611 break;
3612 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003613 MVT OVT = Tmp1.getValueType();
3614 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3615 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003616
3617 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3618 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3619 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3620 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3621 break;
3622 }
3623 case TargetLowering::Expand:
3624 Result = ExpandBSWAP(Tmp1);
3625 break;
3626 }
3627 break;
3628
3629 case ISD::CTPOP:
3630 case ISD::CTTZ:
3631 case ISD::CTLZ:
3632 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3633 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003634 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003635 case TargetLowering::Legal:
3636 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003637 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003638 TargetLowering::Custom) {
3639 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003640 if (Tmp1.getNode()) {
Scott Michelbc62b412007-08-02 02:22:46 +00003641 Result = Tmp1;
3642 }
Scott Michel48b63e62007-07-30 21:00:31 +00003643 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003644 break;
3645 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003646 MVT OVT = Tmp1.getValueType();
3647 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003648
3649 // Zero extend the argument.
3650 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3651 // Perform the larger operation, then subtract if needed.
3652 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3653 switch (Node->getOpcode()) {
3654 case ISD::CTPOP:
3655 Result = Tmp1;
3656 break;
3657 case ISD::CTTZ:
3658 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00003659 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003660 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003661 ISD::SETEQ);
3662 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003663 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003664 break;
3665 case ISD::CTLZ:
3666 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3667 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003668 DAG.getConstant(NVT.getSizeInBits() -
3669 OVT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003670 break;
3671 }
3672 break;
3673 }
3674 case TargetLowering::Expand:
3675 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3676 break;
3677 }
3678 break;
3679
3680 // Unary operators
3681 case ISD::FABS:
3682 case ISD::FNEG:
3683 case ISD::FSQRT:
3684 case ISD::FSIN:
3685 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003686 case ISD::FLOG:
3687 case ISD::FLOG2:
3688 case ISD::FLOG10:
3689 case ISD::FEXP:
3690 case ISD::FEXP2:
Dan Gohmanc8b20e22008-08-21 17:55:02 +00003691 case ISD::FTRUNC:
3692 case ISD::FFLOOR:
3693 case ISD::FCEIL:
3694 case ISD::FRINT:
3695 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003696 Tmp1 = LegalizeOp(Node->getOperand(0));
3697 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3698 case TargetLowering::Promote:
3699 case TargetLowering::Custom:
3700 isCustom = true;
3701 // FALLTHROUGH
3702 case TargetLowering::Legal:
3703 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3704 if (isCustom) {
3705 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003706 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003707 }
3708 break;
3709 case TargetLowering::Expand:
3710 switch (Node->getOpcode()) {
3711 default: assert(0 && "Unreachable!");
3712 case ISD::FNEG:
3713 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3714 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3715 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3716 break;
3717 case ISD::FABS: {
3718 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands92c43912008-06-06 12:08:01 +00003719 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003720 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel502151f2008-03-10 15:42:14 +00003721 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00003722 ISD::SETUGT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003723 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3724 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3725 break;
3726 }
Evan Cheng1fac6952008-09-09 23:35:53 +00003727 case ISD::FSQRT:
3728 case ISD::FSIN:
3729 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003730 case ISD::FLOG:
3731 case ISD::FLOG2:
3732 case ISD::FLOG10:
3733 case ISD::FEXP:
3734 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00003735 case ISD::FTRUNC:
3736 case ISD::FFLOOR:
3737 case ISD::FCEIL:
3738 case ISD::FRINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00003739 case ISD::FNEARBYINT: {
Duncan Sands92c43912008-06-06 12:08:01 +00003740 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003741
3742 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003743 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003744 Result = LegalizeOp(UnrollVectorOp(Op));
3745 break;
3746 }
3747
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003748 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3749 switch(Node->getOpcode()) {
3750 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003751 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3752 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003753 break;
3754 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003755 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3756 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003757 break;
3758 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003759 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3760 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003761 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00003762 case ISD::FLOG:
3763 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3764 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3765 break;
3766 case ISD::FLOG2:
3767 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3768 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3769 break;
3770 case ISD::FLOG10:
3771 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3772 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3773 break;
3774 case ISD::FEXP:
3775 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3776 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3777 break;
3778 case ISD::FEXP2:
3779 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3780 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3781 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00003782 case ISD::FTRUNC:
3783 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3784 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3785 break;
3786 case ISD::FFLOOR:
3787 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3788 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3789 break;
3790 case ISD::FCEIL:
3791 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3792 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3793 break;
3794 case ISD::FRINT:
3795 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3796 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3797 break;
3798 case ISD::FNEARBYINT:
3799 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3800 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3801 break;
Evan Cheng1fac6952008-09-09 23:35:53 +00003802 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003803 default: assert(0 && "Unreachable!");
3804 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003805 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003806 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003807 break;
3808 }
3809 }
3810 break;
3811 }
3812 break;
3813 case ISD::FPOWI: {
Duncan Sands92c43912008-06-06 12:08:01 +00003814 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003815
3816 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003817 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003818 Result = LegalizeOp(UnrollVectorOp(Op));
3819 break;
3820 }
3821
3822 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003823 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3824 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003825 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003826 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003827 break;
3828 }
3829 case ISD::BIT_CONVERT:
3830 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003831 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3832 Node->getValueType(0));
Duncan Sands92c43912008-06-06 12:08:01 +00003833 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003834 // The input has to be a vector type, we have to either scalarize it, pack
3835 // it, or convert it based on whether the input vector type is legal.
Gabor Greif1c80d112008-08-28 21:40:38 +00003836 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00003837 int InIx = Node->getOperand(0).getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00003838 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3839 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003840
3841 // Figure out if there is a simple type corresponding to this Vector
3842 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00003843 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003844 if (TLI.isTypeLegal(TVT)) {
3845 // Turn this into a bit convert of the vector input.
3846 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3847 LegalizeOp(Node->getOperand(0)));
3848 break;
3849 } else if (NumElems == 1) {
3850 // Turn this into a bit convert of the scalar input.
3851 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3852 ScalarizeVectorOp(Node->getOperand(0)));
3853 break;
3854 } else {
3855 // FIXME: UNIMP! Store then reload
3856 assert(0 && "Cast from unsupported vector type not implemented yet!");
3857 }
3858 } else {
3859 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3860 Node->getOperand(0).getValueType())) {
3861 default: assert(0 && "Unknown operation action!");
3862 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003863 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3864 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003865 break;
3866 case TargetLowering::Legal:
3867 Tmp1 = LegalizeOp(Node->getOperand(0));
3868 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3869 break;
3870 }
3871 }
3872 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003873 case ISD::CONVERT_RNDSAT: {
3874 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3875 switch (CvtCode) {
3876 default: assert(0 && "Unknown cvt code!");
3877 case ISD::CVT_SF:
3878 case ISD::CVT_UF:
Mon P Wang73d31542008-11-10 20:54:11 +00003879 case ISD::CVT_FF:
Mon P Wang2fc3f9e2008-12-09 07:27:39 +00003880 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003881 case ISD::CVT_FS:
3882 case ISD::CVT_FU:
3883 case ISD::CVT_SS:
3884 case ISD::CVT_SU:
3885 case ISD::CVT_US:
3886 case ISD::CVT_UU: {
3887 SDValue DTyOp = Node->getOperand(1);
3888 SDValue STyOp = Node->getOperand(2);
3889 SDValue RndOp = Node->getOperand(3);
3890 SDValue SatOp = Node->getOperand(4);
3891 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3892 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3893 case Legal:
3894 Tmp1 = LegalizeOp(Node->getOperand(0));
3895 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3896 RndOp, SatOp);
3897 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3898 TargetLowering::Custom) {
3899 Tmp1 = TLI.LowerOperation(Result, DAG);
3900 if (Tmp1.getNode()) Result = Tmp1;
3901 }
3902 break;
3903 case Promote:
3904 Result = PromoteOp(Node->getOperand(0));
3905 // For FP, make Op1 a i32
3906
Mon P Wang2fc3f9e2008-12-09 07:27:39 +00003907 Result = DAG.getConvertRndSat(Op.getValueType(), Result,
Mon P Wang73d31542008-11-10 20:54:11 +00003908 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3909 break;
3910 }
3911 break;
3912 }
3913 } // end switch CvtCode
3914 break;
3915 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003916 // Conversion operators. The source and destination have different types.
3917 case ISD::SINT_TO_FP:
3918 case ISD::UINT_TO_FP: {
3919 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman29c3cef2008-08-14 20:04:46 +00003920 Result = LegalizeINT_TO_FP(Result, isSigned,
3921 Node->getValueType(0), Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003922 break;
3923 }
3924 case ISD::TRUNCATE:
3925 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3926 case Legal:
3927 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michele9b8a402008-12-02 19:55:08 +00003928 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3929 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
3930 case TargetLowering::Custom:
Mon P Wang72fe5462008-12-11 00:44:22 +00003931 isCustom = true;
3932 // FALLTHROUGH
Scott Michele9b8a402008-12-02 19:55:08 +00003933 case TargetLowering::Legal:
Mon P Wang72fe5462008-12-11 00:44:22 +00003934 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3935 if (isCustom) {
3936 Tmp1 = TLI.LowerOperation(Result, DAG);
3937 if (Tmp1.getNode()) Result = Tmp1;
3938 }
3939 break;
Mon P Wang83edba52008-12-12 01:25:51 +00003940 case TargetLowering::Expand:
3941 assert(Result.getValueType().isVector() && "must be vector type");
3942 // Unroll the truncate. We should do better.
3943 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Schellerbfc55ee2008-12-02 12:12:25 +00003944 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003945 break;
3946 case Expand:
3947 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3948
3949 // Since the result is legal, we should just be able to truncate the low
3950 // part of the source.
3951 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3952 break;
3953 case Promote:
3954 Result = PromoteOp(Node->getOperand(0));
3955 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3956 break;
3957 }
3958 break;
3959
3960 case ISD::FP_TO_SINT:
3961 case ISD::FP_TO_UINT:
3962 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3963 case Legal:
3964 Tmp1 = LegalizeOp(Node->getOperand(0));
3965
3966 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3967 default: assert(0 && "Unknown operation action!");
3968 case TargetLowering::Custom:
3969 isCustom = true;
3970 // FALLTHROUGH
3971 case TargetLowering::Legal:
3972 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3973 if (isCustom) {
3974 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003975 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003976 }
3977 break;
3978 case TargetLowering::Promote:
3979 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3980 Node->getOpcode() == ISD::FP_TO_SINT);
3981 break;
3982 case TargetLowering::Expand:
3983 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003984 SDValue True, False;
Duncan Sands92c43912008-06-06 12:08:01 +00003985 MVT VT = Node->getOperand(0).getValueType();
3986 MVT NVT = Node->getValueType(0);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003987 const uint64_t zero[] = {0, 0};
Duncan Sands92c43912008-06-06 12:08:01 +00003988 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3989 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohman88ae8c52008-02-29 01:44:25 +00003990 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003991 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel502151f2008-03-10 15:42:14 +00003992 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003993 Node->getOperand(0), Tmp2, ISD::SETLT);
3994 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3995 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
3996 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
3997 Tmp2));
3998 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman88ae8c52008-02-29 01:44:25 +00003999 DAG.getConstant(x, NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004000 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
4001 break;
4002 } else {
4003 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
4004 }
4005 break;
4006 }
4007 break;
4008 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00004009 MVT VT = Op.getValueType();
4010 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00004011 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004012 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00004013 if (Node->getOpcode() == ISD::FP_TO_SINT) {
4014 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
4015 Node->getOperand(0), DAG.getValueType(MVT::f64));
4016 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
4017 DAG.getIntPtrConstant(1));
4018 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
4019 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00004020 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4021 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4022 Tmp2 = DAG.getConstantFP(apf, OVT);
4023 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4024 // FIXME: generated code sucks.
4025 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
4026 DAG.getNode(ISD::ADD, MVT::i32,
4027 DAG.getNode(ISD::FP_TO_SINT, VT,
4028 DAG.getNode(ISD::FSUB, OVT,
4029 Node->getOperand(0), Tmp2)),
4030 DAG.getConstant(0x80000000, MVT::i32)),
4031 DAG.getNode(ISD::FP_TO_SINT, VT,
4032 Node->getOperand(0)),
4033 DAG.getCondCode(ISD::SETGE));
4034 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004035 break;
4036 }
Dan Gohmanec51f642008-03-10 23:03:31 +00004037 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsf68dffb2008-07-17 02:36:29 +00004038 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4039 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4040 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman8181bd12008-07-27 21:46:04 +00004041 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00004042 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004043 break;
4044 }
4045 case Promote:
4046 Tmp1 = PromoteOp(Node->getOperand(0));
4047 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4048 Result = LegalizeOp(Result);
4049 break;
4050 }
4051 break;
4052
Chris Lattner56ecde32008-01-16 06:57:07 +00004053 case ISD::FP_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004054 MVT DstVT = Op.getValueType();
4055 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004056 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4057 // The only other way we can lower this is to turn it into a STORE,
4058 // LOAD pair, targetting a temporary location (a stack slot).
4059 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
4060 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00004061 }
4062 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4063 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4064 case Legal:
4065 Tmp1 = LegalizeOp(Node->getOperand(0));
4066 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4067 break;
4068 case Promote:
4069 Tmp1 = PromoteOp(Node->getOperand(0));
4070 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4071 break;
4072 }
4073 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004074 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004075 case ISD::FP_ROUND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004076 MVT DstVT = Op.getValueType();
4077 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004078 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4079 if (SrcVT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004080 SDValue Lo;
Dale Johannesena0d36082008-01-20 01:18:38 +00004081 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00004082 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00004083 if (DstVT!=MVT::f64)
4084 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00004085 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004086 }
Chris Lattner5872a362008-01-17 07:00:52 +00004087 // The only other way we can lower this is to turn it into a STORE,
4088 // LOAD pair, targetting a temporary location (a stack slot).
4089 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4090 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004091 }
Chris Lattner56ecde32008-01-16 06:57:07 +00004092 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4093 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4094 case Legal:
4095 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004096 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004097 break;
4098 case Promote:
4099 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004100 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4101 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004102 break;
4103 }
4104 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004105 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004106 case ISD::ANY_EXTEND:
4107 case ISD::ZERO_EXTEND:
4108 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004109 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4110 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4111 case Legal:
4112 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac54d002008-04-30 00:26:38 +00004113 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michelac7091c2008-02-15 23:05:48 +00004114 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4115 TargetLowering::Custom) {
Scott Michelac54d002008-04-30 00:26:38 +00004116 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004117 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelac7091c2008-02-15 23:05:48 +00004118 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004119 break;
4120 case Promote:
4121 switch (Node->getOpcode()) {
4122 case ISD::ANY_EXTEND:
4123 Tmp1 = PromoteOp(Node->getOperand(0));
4124 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
4125 break;
4126 case ISD::ZERO_EXTEND:
4127 Result = PromoteOp(Node->getOperand(0));
4128 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4129 Result = DAG.getZeroExtendInReg(Result,
4130 Node->getOperand(0).getValueType());
4131 break;
4132 case ISD::SIGN_EXTEND:
4133 Result = PromoteOp(Node->getOperand(0));
4134 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4135 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4136 Result,
4137 DAG.getValueType(Node->getOperand(0).getValueType()));
4138 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004139 }
4140 }
4141 break;
4142 case ISD::FP_ROUND_INREG:
4143 case ISD::SIGN_EXTEND_INREG: {
4144 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004145 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004146
4147 // If this operation is not supported, convert it to a shl/shr or load/store
4148 // pair.
4149 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4150 default: assert(0 && "This action not supported for this op yet!");
4151 case TargetLowering::Legal:
4152 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
4153 break;
4154 case TargetLowering::Expand:
4155 // If this is an integer extend and shifts are supported, do that.
4156 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
4157 // NOTE: we could fall back on load/store here too for targets without
4158 // SAR. However, it is doubtful that any exist.
Duncan Sands92c43912008-06-06 12:08:01 +00004159 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4160 ExtraVT.getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00004161 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004162 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4163 Node->getOperand(0), ShiftCst);
4164 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4165 Result, ShiftCst);
4166 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
4167 // The only way we can lower this is to turn it into a TRUNCSTORE,
4168 // EXTLOAD pair, targetting a temporary location (a stack slot).
4169
4170 // NOTE: there is a choice here between constantly creating new stack
4171 // slots and always reusing the same one. We currently always create
4172 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00004173 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4174 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004175 } else {
4176 assert(0 && "Unknown op");
4177 }
4178 break;
4179 }
4180 break;
4181 }
Duncan Sands38947cd2007-07-27 12:58:54 +00004182 case ISD::TRAMPOLINE: {
Dan Gohman8181bd12008-07-27 21:46:04 +00004183 SDValue Ops[6];
Duncan Sands38947cd2007-07-27 12:58:54 +00004184 for (unsigned i = 0; i != 6; ++i)
4185 Ops[i] = LegalizeOp(Node->getOperand(i));
4186 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4187 // The only option for this node is to custom lower it.
4188 Result = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004189 assert(Result.getNode() && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00004190
4191 // Since trampoline produces two values, make sure to remember that we
4192 // legalized both of them.
4193 Tmp1 = LegalizeOp(Result.getValue(1));
4194 Result = LegalizeOp(Result);
Dan Gohman8181bd12008-07-27 21:46:04 +00004195 AddLegalizedOperand(SDValue(Node, 0), Result);
4196 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00004197 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00004198 }
Dan Gohmane8e4a412008-05-14 00:43:10 +00004199 case ISD::FLT_ROUNDS_: {
Duncan Sands92c43912008-06-06 12:08:01 +00004200 MVT VT = Node->getValueType(0);
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004201 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4202 default: assert(0 && "This action not supported for this op yet!");
4203 case TargetLowering::Custom:
4204 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004205 if (Result.getNode()) break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004206 // Fall Thru
4207 case TargetLowering::Legal:
4208 // If this operation is not supported, lower it to constant 1
4209 Result = DAG.getConstant(1, VT);
4210 break;
4211 }
Dan Gohmane09dc8c2008-05-12 16:07:15 +00004212 break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004213 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004214 case ISD::TRAP: {
Duncan Sands92c43912008-06-06 12:08:01 +00004215 MVT VT = Node->getValueType(0);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004216 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4217 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00004218 case TargetLowering::Legal:
4219 Tmp1 = LegalizeOp(Node->getOperand(0));
4220 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4221 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004222 case TargetLowering::Custom:
4223 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004224 if (Result.getNode()) break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004225 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00004226 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004227 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00004228 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004229 TargetLowering::ArgListTy Args;
Dan Gohman8181bd12008-07-27 21:46:04 +00004230 std::pair<SDValue,SDValue> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004231 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00004232 false, false, false, false, CallingConv::C, false,
Bill Wendlingfef06052008-09-16 21:48:12 +00004233 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner88e03932008-01-15 22:09:33 +00004234 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004235 Result = CallResult.second;
4236 break;
4237 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004238 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004239 }
Bill Wendling913dcf32008-11-22 00:22:52 +00004240
Bill Wendling7e04be62008-12-09 22:08:41 +00004241 case ISD::SADDO:
4242 case ISD::SSUBO: {
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004243 MVT VT = Node->getValueType(0);
4244 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4245 default: assert(0 && "This action not supported for this op yet!");
4246 case TargetLowering::Custom:
4247 Result = TLI.LowerOperation(Op, DAG);
4248 if (Result.getNode()) break;
4249 // FALLTHROUGH
4250 case TargetLowering::Legal: {
4251 SDValue LHS = LegalizeOp(Node->getOperand(0));
4252 SDValue RHS = LegalizeOp(Node->getOperand(1));
4253
Bill Wendling7e04be62008-12-09 22:08:41 +00004254 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
4255 ISD::ADD : ISD::SUB, LHS.getValueType(),
4256 LHS, RHS);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004257 MVT OType = Node->getValueType(1);
4258
Bill Wendlingc65e6e42008-11-25 08:19:22 +00004259 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004260
Bill Wendlingcf4de122008-11-25 19:40:17 +00004261 // LHSSign -> LHS >= 0
4262 // RHSSign -> RHS >= 0
4263 // SumSign -> Sum >= 0
4264 //
Bill Wendling7e04be62008-12-09 22:08:41 +00004265 // Add:
Bill Wendlingcf4de122008-11-25 19:40:17 +00004266 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling7e04be62008-12-09 22:08:41 +00004267 // Sub:
4268 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendlingcf4de122008-11-25 19:40:17 +00004269 //
4270 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4271 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
Bill Wendling7e04be62008-12-09 22:08:41 +00004272 SDValue SignsMatch = DAG.getSetCC(OType, LHSSign, RHSSign,
4273 Node->getOpcode() == ISD::SADDO ?
4274 ISD::SETEQ : ISD::SETNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004275
Bill Wendlingcf4de122008-11-25 19:40:17 +00004276 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4277 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004278
Bill Wendling7e04be62008-12-09 22:08:41 +00004279 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsMatch, SumSignNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004280
4281 MVT ValueVTs[] = { LHS.getValueType(), OType };
4282 SDValue Ops[] = { Sum, Cmp };
4283
Duncan Sands42d7bb82008-12-01 11:41:29 +00004284 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4285 &Ops[0], 2);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004286 SDNode *RNode = Result.getNode();
4287 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4288 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4289 break;
4290 }
4291 }
4292
4293 break;
4294 }
Bill Wendling7e04be62008-12-09 22:08:41 +00004295 case ISD::UADDO:
4296 case ISD::USUBO: {
Bill Wendling4c134df2008-11-24 19:21:46 +00004297 MVT VT = Node->getValueType(0);
4298 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4299 default: assert(0 && "This action not supported for this op yet!");
4300 case TargetLowering::Custom:
4301 Result = TLI.LowerOperation(Op, DAG);
4302 if (Result.getNode()) break;
4303 // FALLTHROUGH
4304 case TargetLowering::Legal: {
4305 SDValue LHS = LegalizeOp(Node->getOperand(0));
4306 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling913dcf32008-11-22 00:22:52 +00004307
Bill Wendling7e04be62008-12-09 22:08:41 +00004308 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
4309 ISD::ADD : ISD::SUB, LHS.getValueType(),
4310 LHS, RHS);
Bill Wendling4c134df2008-11-24 19:21:46 +00004311 MVT OType = Node->getValueType(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00004312 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS,
4313 Node->getOpcode () == ISD::UADDO ?
4314 ISD::SETULT : ISD::SETUGT);
Bill Wendling913dcf32008-11-22 00:22:52 +00004315
Bill Wendling4c134df2008-11-24 19:21:46 +00004316 MVT ValueVTs[] = { LHS.getValueType(), OType };
4317 SDValue Ops[] = { Sum, Cmp };
Bill Wendling913dcf32008-11-22 00:22:52 +00004318
Duncan Sands42d7bb82008-12-01 11:41:29 +00004319 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4320 &Ops[0], 2);
Bill Wendling4c134df2008-11-24 19:21:46 +00004321 SDNode *RNode = Result.getNode();
4322 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4323 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4324 break;
4325 }
4326 }
4327
Bill Wendling913dcf32008-11-22 00:22:52 +00004328 break;
4329 }
Bill Wendling7e04be62008-12-09 22:08:41 +00004330 case ISD::SMULO:
4331 case ISD::UMULO: {
4332 MVT VT = Node->getValueType(0);
4333 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4334 default: assert(0 && "This action is not supported at all!");
4335 case TargetLowering::Custom:
4336 Result = TLI.LowerOperation(Op, DAG);
4337 if (Result.getNode()) break;
4338 // Fall Thru
4339 case TargetLowering::Legal:
4340 // FIXME: According to Hacker's Delight, this can be implemented in
4341 // target independent lowering, but it would be inefficient, since it
Bill Wendling35f1a9d2008-12-10 02:01:32 +00004342 // requires a division + a branch.
Bill Wendling7e04be62008-12-09 22:08:41 +00004343 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4344 break;
4345 }
4346 break;
4347 }
4348
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004349 }
4350
4351 assert(Result.getValueType() == Op.getValueType() &&
4352 "Bad legalization!");
4353
4354 // Make sure that the generated code is itself legal.
4355 if (Result != Op)
4356 Result = LegalizeOp(Result);
4357
4358 // Note that LegalizeOp may be reentered even from single-use nodes, which
4359 // means that we always must cache transformed nodes.
4360 AddLegalizedOperand(Op, Result);
4361 return Result;
4362}
4363
4364/// PromoteOp - Given an operation that produces a value in an invalid type,
4365/// promote it to compute the value into a larger type. The produced value will
4366/// have the correct bits for the low portion of the register, but no guarantee
4367/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +00004368SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00004369 MVT VT = Op.getValueType();
4370 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004371 assert(getTypeAction(VT) == Promote &&
4372 "Caller should expand or legalize operands that are not promotable!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004373 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004374 "Cannot promote to smaller type!");
4375
Dan Gohman8181bd12008-07-27 21:46:04 +00004376 SDValue Tmp1, Tmp2, Tmp3;
4377 SDValue Result;
Gabor Greif1c80d112008-08-28 21:40:38 +00004378 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004379
Dan Gohman8181bd12008-07-27 21:46:04 +00004380 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004381 if (I != PromotedNodes.end()) return I->second;
4382
4383 switch (Node->getOpcode()) {
4384 case ISD::CopyFromReg:
4385 assert(0 && "CopyFromReg must be legal!");
4386 default:
4387#ifndef NDEBUG
4388 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4389#endif
4390 assert(0 && "Do not know how to promote this operator!");
4391 abort();
4392 case ISD::UNDEF:
4393 Result = DAG.getNode(ISD::UNDEF, NVT);
4394 break;
4395 case ISD::Constant:
4396 if (VT != MVT::i1)
4397 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4398 else
4399 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
4400 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4401 break;
4402 case ISD::ConstantFP:
4403 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4404 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4405 break;
4406
4407 case ISD::SETCC:
Scott Michel502151f2008-03-10 15:42:14 +00004408 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004409 && "SetCC type is not legal??");
Scott Michel502151f2008-03-10 15:42:14 +00004410 Result = DAG.getNode(ISD::SETCC,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004411 TLI.getSetCCResultType(Node->getOperand(0)),
4412 Node->getOperand(0), Node->getOperand(1),
4413 Node->getOperand(2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004414 break;
4415
4416 case ISD::TRUNCATE:
4417 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4418 case Legal:
4419 Result = LegalizeOp(Node->getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00004420 assert(Result.getValueType().bitsGE(NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004421 "This truncation doesn't make sense!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004422 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4424 break;
4425 case Promote:
4426 // The truncation is not required, because we don't guarantee anything
4427 // about high bits anyway.
4428 Result = PromoteOp(Node->getOperand(0));
4429 break;
4430 case Expand:
4431 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4432 // Truncate the low part of the expanded value to the result type
4433 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4434 }
4435 break;
4436 case ISD::SIGN_EXTEND:
4437 case ISD::ZERO_EXTEND:
4438 case ISD::ANY_EXTEND:
4439 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4440 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4441 case Legal:
4442 // Input is legal? Just do extend all the way to the larger type.
4443 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4444 break;
4445 case Promote:
4446 // Promote the reg if it's smaller.
4447 Result = PromoteOp(Node->getOperand(0));
4448 // The high bits are not guaranteed to be anything. Insert an extend.
4449 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4450 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4451 DAG.getValueType(Node->getOperand(0).getValueType()));
4452 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4453 Result = DAG.getZeroExtendInReg(Result,
4454 Node->getOperand(0).getValueType());
4455 break;
4456 }
4457 break;
Mon P Wang73d31542008-11-10 20:54:11 +00004458 case ISD::CONVERT_RNDSAT: {
4459 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4460 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4461 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4462 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4463 "can only promote integers");
4464 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4465 Node->getOperand(1), Node->getOperand(2),
4466 Node->getOperand(3), Node->getOperand(4),
4467 CvtCode);
4468 break;
4469
4470 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004471 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004472 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4473 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004474 Result = PromoteOp(Result);
4475 break;
4476
4477 case ISD::FP_EXTEND:
4478 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4479 case ISD::FP_ROUND:
4480 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4481 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4482 case Promote: assert(0 && "Unreachable with 2 FP types!");
4483 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004484 if (Node->getConstantOperandVal(1) == 0) {
4485 // Input is legal? Do an FP_ROUND_INREG.
4486 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4487 DAG.getValueType(VT));
4488 } else {
4489 // Just remove the truncate, it isn't affecting the value.
4490 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4491 Node->getOperand(1));
4492 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004493 break;
4494 }
4495 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004496 case ISD::SINT_TO_FP:
4497 case ISD::UINT_TO_FP:
4498 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4499 case Legal:
4500 // No extra round required here.
4501 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4502 break;
4503
4504 case Promote:
4505 Result = PromoteOp(Node->getOperand(0));
4506 if (Node->getOpcode() == ISD::SINT_TO_FP)
4507 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4508 Result,
4509 DAG.getValueType(Node->getOperand(0).getValueType()));
4510 else
4511 Result = DAG.getZeroExtendInReg(Result,
4512 Node->getOperand(0).getValueType());
4513 // No extra round required here.
4514 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4515 break;
4516 case Expand:
4517 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4518 Node->getOperand(0));
4519 // Round if we cannot tolerate excess precision.
4520 if (NoExcessFPPrecision)
4521 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4522 DAG.getValueType(VT));
4523 break;
4524 }
4525 break;
4526
4527 case ISD::SIGN_EXTEND_INREG:
4528 Result = PromoteOp(Node->getOperand(0));
4529 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4530 Node->getOperand(1));
4531 break;
4532 case ISD::FP_TO_SINT:
4533 case ISD::FP_TO_UINT:
4534 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4535 case Legal:
4536 case Expand:
4537 Tmp1 = Node->getOperand(0);
4538 break;
4539 case Promote:
4540 // The input result is prerounded, so we don't have to do anything
4541 // special.
4542 Tmp1 = PromoteOp(Node->getOperand(0));
4543 break;
4544 }
4545 // If we're promoting a UINT to a larger size, check to see if the new node
4546 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4547 // we can use that instead. This allows us to generate better code for
4548 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4549 // legal, such as PowerPC.
4550 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4551 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4552 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4553 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4554 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4555 } else {
4556 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4557 }
4558 break;
4559
4560 case ISD::FABS:
4561 case ISD::FNEG:
4562 Tmp1 = PromoteOp(Node->getOperand(0));
4563 assert(Tmp1.getValueType() == NVT);
4564 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4565 // NOTE: we do not have to do any extra rounding here for
4566 // NoExcessFPPrecision, because we know the input will have the appropriate
4567 // precision, and these operations don't modify precision at all.
4568 break;
4569
Dale Johannesen92b33082008-09-04 00:47:13 +00004570 case ISD::FLOG:
4571 case ISD::FLOG2:
4572 case ISD::FLOG10:
4573 case ISD::FEXP:
4574 case ISD::FEXP2:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004575 case ISD::FSQRT:
4576 case ISD::FSIN:
4577 case ISD::FCOS:
Dan Gohmanb2158232008-08-21 18:38:14 +00004578 case ISD::FTRUNC:
4579 case ISD::FFLOOR:
4580 case ISD::FCEIL:
4581 case ISD::FRINT:
4582 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004583 Tmp1 = PromoteOp(Node->getOperand(0));
4584 assert(Tmp1.getValueType() == NVT);
4585 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4586 if (NoExcessFPPrecision)
4587 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4588 DAG.getValueType(VT));
4589 break;
4590
Evan Cheng1fac6952008-09-09 23:35:53 +00004591 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004592 case ISD::FPOWI: {
Evan Cheng1fac6952008-09-09 23:35:53 +00004593 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004594 // directly as well, which may be better.
4595 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng1fac6952008-09-09 23:35:53 +00004596 Tmp2 = Node->getOperand(1);
4597 if (Node->getOpcode() == ISD::FPOW)
4598 Tmp2 = PromoteOp(Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004599 assert(Tmp1.getValueType() == NVT);
Evan Cheng1fac6952008-09-09 23:35:53 +00004600 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004601 if (NoExcessFPPrecision)
4602 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4603 DAG.getValueType(VT));
4604 break;
4605 }
4606
Dale Johannesenbc187662008-08-28 02:44:49 +00004607 case ISD::ATOMIC_CMP_SWAP_8:
4608 case ISD::ATOMIC_CMP_SWAP_16:
4609 case ISD::ATOMIC_CMP_SWAP_32:
4610 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004611 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004612 Tmp2 = PromoteOp(Node->getOperand(2));
4613 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004614 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4615 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004616 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004617 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004618 // Remember that we legalized the chain.
4619 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4620 break;
4621 }
Dale Johannesenbc187662008-08-28 02:44:49 +00004622 case ISD::ATOMIC_LOAD_ADD_8:
4623 case ISD::ATOMIC_LOAD_SUB_8:
4624 case ISD::ATOMIC_LOAD_AND_8:
4625 case ISD::ATOMIC_LOAD_OR_8:
4626 case ISD::ATOMIC_LOAD_XOR_8:
4627 case ISD::ATOMIC_LOAD_NAND_8:
4628 case ISD::ATOMIC_LOAD_MIN_8:
4629 case ISD::ATOMIC_LOAD_MAX_8:
4630 case ISD::ATOMIC_LOAD_UMIN_8:
4631 case ISD::ATOMIC_LOAD_UMAX_8:
4632 case ISD::ATOMIC_SWAP_8:
4633 case ISD::ATOMIC_LOAD_ADD_16:
4634 case ISD::ATOMIC_LOAD_SUB_16:
4635 case ISD::ATOMIC_LOAD_AND_16:
4636 case ISD::ATOMIC_LOAD_OR_16:
4637 case ISD::ATOMIC_LOAD_XOR_16:
4638 case ISD::ATOMIC_LOAD_NAND_16:
4639 case ISD::ATOMIC_LOAD_MIN_16:
4640 case ISD::ATOMIC_LOAD_MAX_16:
4641 case ISD::ATOMIC_LOAD_UMIN_16:
4642 case ISD::ATOMIC_LOAD_UMAX_16:
4643 case ISD::ATOMIC_SWAP_16:
4644 case ISD::ATOMIC_LOAD_ADD_32:
4645 case ISD::ATOMIC_LOAD_SUB_32:
4646 case ISD::ATOMIC_LOAD_AND_32:
4647 case ISD::ATOMIC_LOAD_OR_32:
4648 case ISD::ATOMIC_LOAD_XOR_32:
4649 case ISD::ATOMIC_LOAD_NAND_32:
4650 case ISD::ATOMIC_LOAD_MIN_32:
4651 case ISD::ATOMIC_LOAD_MAX_32:
4652 case ISD::ATOMIC_LOAD_UMIN_32:
4653 case ISD::ATOMIC_LOAD_UMAX_32:
4654 case ISD::ATOMIC_SWAP_32:
4655 case ISD::ATOMIC_LOAD_ADD_64:
4656 case ISD::ATOMIC_LOAD_SUB_64:
4657 case ISD::ATOMIC_LOAD_AND_64:
4658 case ISD::ATOMIC_LOAD_OR_64:
4659 case ISD::ATOMIC_LOAD_XOR_64:
4660 case ISD::ATOMIC_LOAD_NAND_64:
4661 case ISD::ATOMIC_LOAD_MIN_64:
4662 case ISD::ATOMIC_LOAD_MAX_64:
4663 case ISD::ATOMIC_LOAD_UMIN_64:
4664 case ISD::ATOMIC_LOAD_UMAX_64:
4665 case ISD::ATOMIC_SWAP_64: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004666 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004667 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004668 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4669 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004670 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004671 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004672 // Remember that we legalized the chain.
4673 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4674 break;
4675 }
4676
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004677 case ISD::AND:
4678 case ISD::OR:
4679 case ISD::XOR:
4680 case ISD::ADD:
4681 case ISD::SUB:
4682 case ISD::MUL:
4683 // The input may have strange things in the top bits of the registers, but
4684 // these operations don't care. They may have weird bits going out, but
4685 // that too is okay if they are integer operations.
4686 Tmp1 = PromoteOp(Node->getOperand(0));
4687 Tmp2 = PromoteOp(Node->getOperand(1));
4688 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4689 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4690 break;
4691 case ISD::FADD:
4692 case ISD::FSUB:
4693 case ISD::FMUL:
4694 Tmp1 = PromoteOp(Node->getOperand(0));
4695 Tmp2 = PromoteOp(Node->getOperand(1));
4696 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4697 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4698
4699 // Floating point operations will give excess precision that we may not be
4700 // able to tolerate. If we DO allow excess precision, just leave it,
4701 // otherwise excise it.
4702 // FIXME: Why would we need to round FP ops more than integer ones?
4703 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4704 if (NoExcessFPPrecision)
4705 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4706 DAG.getValueType(VT));
4707 break;
4708
4709 case ISD::SDIV:
4710 case ISD::SREM:
4711 // These operators require that their input be sign extended.
4712 Tmp1 = PromoteOp(Node->getOperand(0));
4713 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004714 if (NVT.isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004715 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4716 DAG.getValueType(VT));
4717 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4718 DAG.getValueType(VT));
4719 }
4720 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4721
4722 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands92c43912008-06-06 12:08:01 +00004723 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004724 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4725 DAG.getValueType(VT));
4726 break;
4727 case ISD::FDIV:
4728 case ISD::FREM:
4729 case ISD::FCOPYSIGN:
4730 // These operators require that their input be fp extended.
4731 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004732 case Expand: assert(0 && "not implemented");
4733 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4734 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004735 }
4736 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004737 case Expand: assert(0 && "not implemented");
4738 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4739 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004740 }
4741 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4742
4743 // Perform FP_ROUND: this is probably overly pessimistic.
4744 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4745 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4746 DAG.getValueType(VT));
4747 break;
4748
4749 case ISD::UDIV:
4750 case ISD::UREM:
4751 // These operators require that their input be zero extended.
4752 Tmp1 = PromoteOp(Node->getOperand(0));
4753 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004754 assert(NVT.isInteger() && "Operators don't apply to FP!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004755 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4756 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4757 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4758 break;
4759
4760 case ISD::SHL:
4761 Tmp1 = PromoteOp(Node->getOperand(0));
4762 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4763 break;
4764 case ISD::SRA:
4765 // The input value must be properly sign extended.
4766 Tmp1 = PromoteOp(Node->getOperand(0));
4767 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4768 DAG.getValueType(VT));
4769 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4770 break;
4771 case ISD::SRL:
4772 // The input value must be properly zero extended.
4773 Tmp1 = PromoteOp(Node->getOperand(0));
4774 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4775 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4776 break;
4777
4778 case ISD::VAARG:
4779 Tmp1 = Node->getOperand(0); // Get the chain.
4780 Tmp2 = Node->getOperand(1); // Get the pointer.
4781 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4782 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sandsac496a12008-07-04 11:47:58 +00004783 Result = TLI.LowerOperation(Tmp3, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004784 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004785 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00004786 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004787 // Increment the pointer, VAList, to the next vaarg
4788 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands92c43912008-06-06 12:08:01 +00004789 DAG.getConstant(VT.getSizeInBits()/8,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004790 TLI.getPointerTy()));
4791 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00004792 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004793 // Load the actual argument out of the pointer VAList
4794 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4795 }
4796 // Remember that we legalized the chain.
4797 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4798 break;
4799
4800 case ISD::LOAD: {
4801 LoadSDNode *LD = cast<LoadSDNode>(Node);
4802 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4803 ? ISD::EXTLOAD : LD->getExtensionType();
4804 Result = DAG.getExtLoad(ExtType, NVT,
4805 LD->getChain(), LD->getBasePtr(),
4806 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004807 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004808 LD->isVolatile(),
4809 LD->getAlignment());
4810 // Remember that we legalized the chain.
4811 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4812 break;
4813 }
Scott Michel67224b22008-06-02 22:18:03 +00004814 case ISD::SELECT: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004815 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4816 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel67224b22008-06-02 22:18:03 +00004817
Duncan Sands92c43912008-06-06 12:08:01 +00004818 MVT VT2 = Tmp2.getValueType();
Scott Michel67224b22008-06-02 22:18:03 +00004819 assert(VT2 == Tmp3.getValueType()
Scott Michel7b54de02008-06-03 19:13:20 +00004820 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4821 // Ensure that the resulting node is at least the same size as the operands'
4822 // value types, because we cannot assume that TLI.getSetCCValueType() is
4823 // constant.
4824 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004825 break;
Scott Michel67224b22008-06-02 22:18:03 +00004826 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004827 case ISD::SELECT_CC:
4828 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4829 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4830 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4831 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4832 break;
4833 case ISD::BSWAP:
4834 Tmp1 = Node->getOperand(0);
4835 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4836 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4837 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004838 DAG.getConstant(NVT.getSizeInBits() -
4839 VT.getSizeInBits(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004840 TLI.getShiftAmountTy()));
4841 break;
4842 case ISD::CTPOP:
4843 case ISD::CTTZ:
4844 case ISD::CTLZ:
4845 // Zero extend the argument
4846 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4847 // Perform the larger operation, then subtract if needed.
4848 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4849 switch(Node->getOpcode()) {
4850 case ISD::CTPOP:
4851 Result = Tmp1;
4852 break;
4853 case ISD::CTTZ:
4854 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00004855 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004856 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004857 ISD::SETEQ);
4858 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00004859 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004860 break;
4861 case ISD::CTLZ:
4862 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4863 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004864 DAG.getConstant(NVT.getSizeInBits() -
4865 VT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004866 break;
4867 }
4868 break;
4869 case ISD::EXTRACT_SUBVECTOR:
4870 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4871 break;
4872 case ISD::EXTRACT_VECTOR_ELT:
4873 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4874 break;
4875 }
4876
Gabor Greif1c80d112008-08-28 21:40:38 +00004877 assert(Result.getNode() && "Didn't set a result!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004878
4879 // Make sure the result is itself legal.
4880 Result = LegalizeOp(Result);
4881
4882 // Remember that we promoted this!
4883 AddPromotedOperand(Op, Result);
4884 return Result;
4885}
4886
4887/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4888/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4889/// based on the vector type. The return type of this matches the element type
4890/// of the vector, which may not be legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00004891SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004892 // We know that operand #0 is the Vec vector. If the index is a constant
4893 // or if the invec is a supported hardware type, we can use it. Otherwise,
4894 // lower to a store then an indexed load.
Dan Gohman8181bd12008-07-27 21:46:04 +00004895 SDValue Vec = Op.getOperand(0);
4896 SDValue Idx = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004897
Duncan Sands92c43912008-06-06 12:08:01 +00004898 MVT TVT = Vec.getValueType();
4899 unsigned NumElems = TVT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004900
4901 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4902 default: assert(0 && "This action is not supported yet!");
4903 case TargetLowering::Custom: {
4904 Vec = LegalizeOp(Vec);
4905 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004906 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004907 if (Tmp3.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004908 return Tmp3;
4909 break;
4910 }
4911 case TargetLowering::Legal:
4912 if (isTypeLegal(TVT)) {
4913 Vec = LegalizeOp(Vec);
4914 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004915 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004916 }
4917 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00004918 case TargetLowering::Promote:
4919 assert(TVT.isVector() && "not vector type");
4920 // fall thru to expand since vectors are by default are promote
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004921 case TargetLowering::Expand:
4922 break;
4923 }
4924
4925 if (NumElems == 1) {
4926 // This must be an access of the only element. Return it.
4927 Op = ScalarizeVectorOp(Vec);
4928 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00004929 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004930 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004931 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004932 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004933 if (CIdx->getZExtValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004934 Vec = Lo;
4935 } else {
4936 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004937 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004938 Idx.getValueType());
4939 }
4940
4941 // It's now an extract from the appropriate high or low part. Recurse.
4942 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4943 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4944 } else {
4945 // Store the value to a temporary stack slot, then LOAD the scalar
4946 // element back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00004947 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4948 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004949
4950 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +00004951 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004952 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4953 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004954
Duncan Sandsec142ee2008-06-08 20:54:56 +00004955 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00004956 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004957 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00004958 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004959
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004960 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4961
4962 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
4963 }
4964 return Op;
4965}
4966
4967/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
4968/// we assume the operation can be split if it is not already legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00004969SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004970 // We know that operand #0 is the Vec vector. For now we assume the index
4971 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman8181bd12008-07-27 21:46:04 +00004972 SDValue Vec = Op.getOperand(0);
4973 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004974
Duncan Sands92c43912008-06-06 12:08:01 +00004975 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004976
Duncan Sands92c43912008-06-06 12:08:01 +00004977 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004978 // This must be an access of the desired vector length. Return it.
4979 return Vec;
4980 }
4981
4982 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004983 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004984 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004985 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004986 Vec = Lo;
4987 } else {
4988 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004989 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
4990 Idx.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004991 }
4992
4993 // It's now an extract from the appropriate high or low part. Recurse.
4994 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4995 return ExpandEXTRACT_SUBVECTOR(Op);
4996}
4997
4998/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4999/// with condition CC on the current target. This usually involves legalizing
5000/// or promoting the arguments. In the case where LHS and RHS must be expanded,
5001/// there may be no choice but to create a new SetCC node to represent the
5002/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman8181bd12008-07-27 21:46:04 +00005003/// LHS, and the SDValue returned in RHS has a nil SDNode value.
5004void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
5005 SDValue &RHS,
5006 SDValue &CC) {
5007 SDValue Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005008
5009 switch (getTypeAction(LHS.getValueType())) {
5010 case Legal:
5011 Tmp1 = LegalizeOp(LHS); // LHS
5012 Tmp2 = LegalizeOp(RHS); // RHS
5013 break;
5014 case Promote:
5015 Tmp1 = PromoteOp(LHS); // LHS
5016 Tmp2 = PromoteOp(RHS); // RHS
5017
5018 // If this is an FP compare, the operands have already been extended.
Duncan Sands92c43912008-06-06 12:08:01 +00005019 if (LHS.getValueType().isInteger()) {
5020 MVT VT = LHS.getValueType();
5021 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005022
5023 // Otherwise, we have to insert explicit sign or zero extends. Note
5024 // that we could insert sign extends for ALL conditions, but zero extend
5025 // is cheaper on many machines (an AND instead of two shifts), so prefer
5026 // it.
5027 switch (cast<CondCodeSDNode>(CC)->get()) {
5028 default: assert(0 && "Unknown integer comparison!");
5029 case ISD::SETEQ:
5030 case ISD::SETNE:
5031 case ISD::SETUGE:
5032 case ISD::SETUGT:
5033 case ISD::SETULE:
5034 case ISD::SETULT:
5035 // ALL of these operations will work if we either sign or zero extend
5036 // the operands (including the unsigned comparisons!). Zero extend is
5037 // usually a simpler/cheaper operation, so prefer it.
5038 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
5039 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
5040 break;
5041 case ISD::SETGE:
5042 case ISD::SETGT:
5043 case ISD::SETLT:
5044 case ISD::SETLE:
5045 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
5046 DAG.getValueType(VT));
5047 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
5048 DAG.getValueType(VT));
Evan Chengd901b662008-10-13 18:46:18 +00005049 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5050 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005051 break;
5052 }
5053 }
5054 break;
5055 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00005056 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005057 if (VT == MVT::f32 || VT == MVT::f64) {
5058 // Expand into one or more soft-fp libcall(s).
Evan Cheng24108632008-07-01 21:35:46 +00005059 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005060 switch (cast<CondCodeSDNode>(CC)->get()) {
5061 case ISD::SETEQ:
5062 case ISD::SETOEQ:
5063 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5064 break;
5065 case ISD::SETNE:
5066 case ISD::SETUNE:
5067 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
5068 break;
5069 case ISD::SETGE:
5070 case ISD::SETOGE:
5071 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5072 break;
5073 case ISD::SETLT:
5074 case ISD::SETOLT:
5075 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5076 break;
5077 case ISD::SETLE:
5078 case ISD::SETOLE:
5079 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5080 break;
5081 case ISD::SETGT:
5082 case ISD::SETOGT:
5083 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
5084 break;
5085 case ISD::SETUO:
5086 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5087 break;
5088 case ISD::SETO:
5089 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
5090 break;
5091 default:
5092 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5093 switch (cast<CondCodeSDNode>(CC)->get()) {
5094 case ISD::SETONE:
5095 // SETONE = SETOLT | SETOGT
5096 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5097 // Fallthrough
5098 case ISD::SETUGT:
5099 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
5100 break;
5101 case ISD::SETUGE:
5102 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5103 break;
5104 case ISD::SETULT:
5105 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5106 break;
5107 case ISD::SETULE:
5108 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5109 break;
5110 case ISD::SETUEQ:
5111 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5112 break;
5113 default: assert(0 && "Unsupported FP setcc!");
5114 }
5115 }
Duncan Sandsf19591c2008-06-30 10:19:09 +00005116
Dan Gohman8181bd12008-07-27 21:46:04 +00005117 SDValue Dummy;
5118 SDValue Ops[2] = { LHS, RHS };
Gabor Greif1c80d112008-08-28 21:40:38 +00005119 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005120 false /*sign irrelevant*/, Dummy);
5121 Tmp2 = DAG.getConstant(0, MVT::i32);
5122 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
5123 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel502151f2008-03-10 15:42:14 +00005124 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005125 CC);
Gabor Greif1c80d112008-08-28 21:40:38 +00005126 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005127 false /*sign irrelevant*/, Dummy);
Scott Michel502151f2008-03-10 15:42:14 +00005128 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005129 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
5130 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005131 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005132 }
Evan Cheng18a1ab12008-07-07 07:18:09 +00005133 LHS = LegalizeOp(Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005134 RHS = Tmp2;
5135 return;
5136 }
5137
Dan Gohman8181bd12008-07-27 21:46:04 +00005138 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005139 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005140 ExpandOp(RHS, RHSLo, RHSHi);
5141 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5142
5143 if (VT==MVT::ppcf128) {
5144 // FIXME: This generated code sucks. We want to generate
Dale Johannesen26317b62008-09-12 00:30:56 +00005145 // FCMPU crN, hi1, hi2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005146 // BNE crN, L:
Dale Johannesen26317b62008-09-12 00:30:56 +00005147 // FCMPU crN, lo1, lo2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005148 // The following can be improved, but not that much.
Dale Johannesen26317b62008-09-12 00:30:56 +00005149 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5150 ISD::SETOEQ);
Scott Michel502151f2008-03-10 15:42:14 +00005151 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005152 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Dale Johannesen26317b62008-09-12 00:30:56 +00005153 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5154 ISD::SETUNE);
Scott Michel502151f2008-03-10 15:42:14 +00005155 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005156 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5157 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00005158 Tmp2 = SDValue();
Dale Johannesen472d15d2007-10-06 01:24:11 +00005159 break;
5160 }
5161
5162 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005163 case ISD::SETEQ:
5164 case ISD::SETNE:
5165 if (RHSLo == RHSHi)
5166 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5167 if (RHSCST->isAllOnesValue()) {
5168 // Comparison to -1.
5169 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5170 Tmp2 = RHSLo;
5171 break;
5172 }
5173
5174 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5175 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5176 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5177 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5178 break;
5179 default:
5180 // If this is a comparison of the sign bit, just look at the top part.
5181 // X > -1, x < 0
5182 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5183 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00005184 CST->isNullValue()) || // X < 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005185 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5186 CST->isAllOnesValue())) { // X > -1
5187 Tmp1 = LHSHi;
5188 Tmp2 = RHSHi;
5189 break;
5190 }
5191
5192 // FIXME: This generated code sucks.
5193 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005194 switch (CCCode) {
5195 default: assert(0 && "Unknown integer setcc!");
5196 case ISD::SETLT:
5197 case ISD::SETULT: LowCC = ISD::SETULT; break;
5198 case ISD::SETGT:
5199 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5200 case ISD::SETLE:
5201 case ISD::SETULE: LowCC = ISD::SETULE; break;
5202 case ISD::SETGE:
5203 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5204 }
5205
5206 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5207 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5208 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5209
5210 // NOTE: on targets without efficient SELECT of bools, we can always use
5211 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
5212 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel502151f2008-03-10 15:42:14 +00005213 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005214 LowCC, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005215 if (!Tmp1.getNode())
Scott Michel502151f2008-03-10 15:42:14 +00005216 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
5217 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005218 CCCode, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005219 if (!Tmp2.getNode())
Scott Michel502151f2008-03-10 15:42:14 +00005220 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005221 RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005222
Gabor Greif1c80d112008-08-28 21:40:38 +00005223 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5224 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman9d24dc72008-03-13 22:13:53 +00005225 if ((Tmp1C && Tmp1C->isNullValue()) ||
5226 (Tmp2C && Tmp2C->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005227 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5228 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman9d24dc72008-03-13 22:13:53 +00005229 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005230 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5231 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5232 // low part is known false, returns high part.
5233 // For LE / GE, if high part is known false, ignore the low part.
5234 // For LT / GT, if high part is known true, ignore the low part.
5235 Tmp1 = Tmp2;
Dan Gohman8181bd12008-07-27 21:46:04 +00005236 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005237 } else {
Scott Michel502151f2008-03-10 15:42:14 +00005238 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005239 ISD::SETEQ, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005240 if (!Result.getNode())
Scott Michel502151f2008-03-10 15:42:14 +00005241 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005242 ISD::SETEQ);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005243 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5244 Result, Tmp1, Tmp2));
5245 Tmp1 = Result;
Dan Gohman8181bd12008-07-27 21:46:04 +00005246 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005247 }
5248 }
5249 }
5250 }
5251 LHS = Tmp1;
5252 RHS = Tmp2;
5253}
5254
Evan Cheng71343822008-10-15 02:05:31 +00005255/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5256/// condition code CC on the current target. This routine assumes LHS and rHS
5257/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5258/// illegal condition code into AND / OR of multiple SETCC values.
5259void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5260 SDValue &LHS, SDValue &RHS,
5261 SDValue &CC) {
5262 MVT OpVT = LHS.getValueType();
5263 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5264 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5265 default: assert(0 && "Unknown condition code action!");
5266 case TargetLowering::Legal:
5267 // Nothing to do.
5268 break;
5269 case TargetLowering::Expand: {
5270 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5271 unsigned Opc = 0;
5272 switch (CCCode) {
5273 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohman2b5b9ca2008-10-21 03:12:54 +00005274 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5275 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5276 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5277 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5278 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5279 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5280 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5281 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5282 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5283 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5284 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5285 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng71343822008-10-15 02:05:31 +00005286 // FIXME: Implement more expansions.
5287 }
5288
5289 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5290 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5291 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5292 RHS = SDValue();
5293 CC = SDValue();
5294 break;
5295 }
5296 }
5297}
5298
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005299/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5300/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5301/// a load from the stack slot to DestVT, extending it if needed.
5302/// The resultant code need not be legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00005303SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5304 MVT SlotVT,
5305 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005306 // Create the stack frame object.
Mon P Wang55854cc2008-07-05 20:40:31 +00005307 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5308 SrcOp.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00005309 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang55854cc2008-07-05 20:40:31 +00005310
Dan Gohman20e37962008-02-11 18:58:42 +00005311 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005312 int SPFI = StackPtrFI->getIndex();
Mon P Wang55854cc2008-07-05 20:40:31 +00005313
Duncan Sands92c43912008-06-06 12:08:01 +00005314 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5315 unsigned SlotSize = SlotVT.getSizeInBits();
5316 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang55854cc2008-07-05 20:40:31 +00005317 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5318 DestVT.getTypeForMVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005319
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005320 // Emit a store to the stack slot. Use a truncstore if the input value is
5321 // later than DestVT.
Dan Gohman8181bd12008-07-27 21:46:04 +00005322 SDValue Store;
Mon P Wang55854cc2008-07-05 20:40:31 +00005323
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005324 if (SrcSize > SlotSize)
Dan Gohman12a9c082008-02-06 22:27:42 +00005325 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005326 PseudoSourceValue::getFixedStack(SPFI), 0,
5327 SlotVT, false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005328 else {
5329 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman12a9c082008-02-06 22:27:42 +00005330 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005331 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang55854cc2008-07-05 20:40:31 +00005332 false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005333 }
5334
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005335 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005336 if (SlotSize == DestSize)
Mon P Wang55854cc2008-07-05 20:40:31 +00005337 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005338
5339 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang55854cc2008-07-05 20:40:31 +00005340 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5341 false, DestAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005342}
5343
Dan Gohman8181bd12008-07-27 21:46:04 +00005344SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005345 // Create a vector sized/aligned stack slot, store the value to element #0,
5346 // then load the whole vector back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00005347 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman12a9c082008-02-06 22:27:42 +00005348
Dan Gohman20e37962008-02-11 18:58:42 +00005349 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005350 int SPFI = StackPtrFI->getIndex();
5351
Dan Gohman8181bd12008-07-27 21:46:04 +00005352 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005353 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00005354 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005355 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005356}
5357
5358
5359/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
5360/// support the operation, but do support the resultant vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00005361SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005362
5363 // If the only non-undef value is the low element, turn this into a
5364 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
5365 unsigned NumElems = Node->getNumOperands();
5366 bool isOnlyLowElement = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005367 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005368
Dan Gohman8181bd12008-07-27 21:46:04 +00005369 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerd8cee732008-03-09 00:29:42 +00005370 // and use a bitmask instead of a list of elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00005371 std::map<SDValue, std::vector<unsigned> > Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005372 Values[SplatValue].push_back(0);
5373 bool isConstant = true;
5374 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5375 SplatValue.getOpcode() != ISD::UNDEF)
5376 isConstant = false;
5377
5378 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005379 SDValue V = Node->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005380 Values[V].push_back(i);
5381 if (V.getOpcode() != ISD::UNDEF)
5382 isOnlyLowElement = false;
5383 if (SplatValue != V)
Dan Gohman8181bd12008-07-27 21:46:04 +00005384 SplatValue = SDValue(0,0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005385
5386 // If this isn't a constant element or an undef, we can't use a constant
5387 // pool load.
5388 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5389 V.getOpcode() != ISD::UNDEF)
5390 isConstant = false;
5391 }
5392
5393 if (isOnlyLowElement) {
5394 // If the low element is an undef too, then this whole things is an undef.
5395 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5396 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5397 // Otherwise, turn this into a scalar_to_vector node.
5398 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5399 Node->getOperand(0));
5400 }
5401
5402 // If all elements are constants, create a load from the constant pool.
5403 if (isConstant) {
Duncan Sands92c43912008-06-06 12:08:01 +00005404 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005405 std::vector<Constant*> CV;
5406 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5407 if (ConstantFPSDNode *V =
5408 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005409 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005410 } else if (ConstantSDNode *V =
Chris Lattner5e0610f2008-04-20 00:41:09 +00005411 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005412 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005413 } else {
5414 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner5e0610f2008-04-20 00:41:09 +00005415 const Type *OpNTy =
Duncan Sands92c43912008-06-06 12:08:01 +00005416 Node->getOperand(0).getValueType().getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005417 CV.push_back(UndefValue::get(OpNTy));
5418 }
5419 }
5420 Constant *CP = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005421 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005422 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman12a9c082008-02-06 22:27:42 +00005423 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005424 PseudoSourceValue::getConstantPool(), 0,
5425 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005426 }
5427
Gabor Greif1c80d112008-08-28 21:40:38 +00005428 if (SplatValue.getNode()) { // Splat of one value?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005429 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands92c43912008-06-06 12:08:01 +00005430 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman8181bd12008-07-27 21:46:04 +00005431 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5432 std::vector<SDValue> ZeroVec(NumElems, Zero);
5433 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005434 &ZeroVec[0], ZeroVec.size());
5435
5436 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
5437 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
5438 // Get the splatted value into the low element of a vector register.
Dan Gohman8181bd12008-07-27 21:46:04 +00005439 SDValue LowValVec =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005440 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5441
5442 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5443 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5444 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5445 SplatMask);
5446 }
5447 }
5448
5449 // If there are only two unique elements, we may be able to turn this into a
5450 // vector shuffle.
5451 if (Values.size() == 2) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005452 // Get the two values in deterministic order.
Dan Gohman8181bd12008-07-27 21:46:04 +00005453 SDValue Val1 = Node->getOperand(1);
5454 SDValue Val2;
5455 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerd8cee732008-03-09 00:29:42 +00005456 if (MI->first != Val1)
5457 Val2 = MI->first;
5458 else
5459 Val2 = (++MI)->first;
5460
5461 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5462 // vector shuffle has the undef vector on the RHS.
5463 if (Val1.getOpcode() == ISD::UNDEF)
5464 std::swap(Val1, Val2);
5465
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005466 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands92c43912008-06-06 12:08:01 +00005467 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5468 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005469 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005470
5471 // Set elements of the shuffle mask for Val1.
5472 std::vector<unsigned> &Val1Elts = Values[Val1];
5473 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5474 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5475
5476 // Set elements of the shuffle mask for Val2.
5477 std::vector<unsigned> &Val2Elts = Values[Val2];
5478 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5479 if (Val2.getOpcode() != ISD::UNDEF)
5480 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5481 else
5482 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5483
Dan Gohman8181bd12008-07-27 21:46:04 +00005484 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005485 &MaskVec[0], MaskVec.size());
5486
Chris Lattnerd8cee732008-03-09 00:29:42 +00005487 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005488 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5489 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005490 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5491 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005492 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005493
5494 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerd8cee732008-03-09 00:29:42 +00005495 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005496 }
5497 }
5498
5499 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5500 // aligned object on the stack, store each element into it, then load
5501 // the result as a vector.
Duncan Sands92c43912008-06-06 12:08:01 +00005502 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005503 // Create the stack frame object.
Dan Gohman8181bd12008-07-27 21:46:04 +00005504 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005505
5506 // Emit a store of each element to the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00005507 SmallVector<SDValue, 8> Stores;
Duncan Sands92c43912008-06-06 12:08:01 +00005508 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005509 // Store (in the right endianness) the elements to memory.
5510 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5511 // Ignore undef elements.
5512 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5513
5514 unsigned Offset = TypeByteSize*i;
5515
Dan Gohman8181bd12008-07-27 21:46:04 +00005516 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005517 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5518
5519 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
5520 NULL, 0));
5521 }
5522
Dan Gohman8181bd12008-07-27 21:46:04 +00005523 SDValue StoreChain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005524 if (!Stores.empty()) // Not all undef elements?
5525 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5526 &Stores[0], Stores.size());
5527 else
5528 StoreChain = DAG.getEntryNode();
5529
5530 // Result is a load from the stack slot.
5531 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
5532}
5533
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005534void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman8181bd12008-07-27 21:46:04 +00005535 SDValue Op, SDValue Amt,
5536 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005537 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00005538 SDValue LHSL, LHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005539 ExpandOp(Op, LHSL, LHSH);
5540
Dan Gohman8181bd12008-07-27 21:46:04 +00005541 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands92c43912008-06-06 12:08:01 +00005542 MVT VT = LHSL.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005543 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5544 Hi = Lo.getValue(1);
5545}
5546
5547
5548/// ExpandShift - Try to find a clever way to expand this shift operation out to
5549/// smaller elements. If we can't find a way that is more efficient than a
5550/// libcall on this target, return false. Otherwise, return true with the
5551/// low-parts expanded into Lo and Hi.
Dan Gohman8181bd12008-07-27 21:46:04 +00005552bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5553 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005554 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5555 "This is not a shift!");
5556
Duncan Sands92c43912008-06-06 12:08:01 +00005557 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman8181bd12008-07-27 21:46:04 +00005558 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands92c43912008-06-06 12:08:01 +00005559 MVT ShTy = ShAmt.getValueType();
5560 unsigned ShBits = ShTy.getSizeInBits();
5561 unsigned VTBits = Op.getValueType().getSizeInBits();
5562 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005563
Chris Lattner8c931452007-10-14 20:35:12 +00005564 // Handle the case when Amt is an immediate.
Gabor Greif1c80d112008-08-28 21:40:38 +00005565 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005566 unsigned Cst = CN->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005567 // 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 if (Cst > VTBits) {
5573 Lo = DAG.getConstant(0, NVT);
5574 Hi = DAG.getConstant(0, NVT);
5575 } else if (Cst > NVTBits) {
5576 Lo = DAG.getConstant(0, NVT);
5577 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5578 } else if (Cst == NVTBits) {
5579 Lo = DAG.getConstant(0, NVT);
5580 Hi = InL;
5581 } else {
5582 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5583 Hi = DAG.getNode(ISD::OR, NVT,
5584 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5585 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5586 }
5587 return true;
5588 case ISD::SRL:
5589 if (Cst > VTBits) {
5590 Lo = DAG.getConstant(0, NVT);
5591 Hi = DAG.getConstant(0, NVT);
5592 } else if (Cst > NVTBits) {
5593 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5594 Hi = DAG.getConstant(0, NVT);
5595 } else if (Cst == NVTBits) {
5596 Lo = InH;
5597 Hi = DAG.getConstant(0, NVT);
5598 } else {
5599 Lo = DAG.getNode(ISD::OR, NVT,
5600 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5601 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5602 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5603 }
5604 return true;
5605 case ISD::SRA:
5606 if (Cst > VTBits) {
5607 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5608 DAG.getConstant(NVTBits-1, ShTy));
5609 } else if (Cst > NVTBits) {
5610 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5611 DAG.getConstant(Cst-NVTBits, ShTy));
5612 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5613 DAG.getConstant(NVTBits-1, ShTy));
5614 } else if (Cst == NVTBits) {
5615 Lo = InH;
5616 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5617 DAG.getConstant(NVTBits-1, ShTy));
5618 } else {
5619 Lo = DAG.getNode(ISD::OR, NVT,
5620 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5621 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5622 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5623 }
5624 return true;
5625 }
5626 }
5627
5628 // Okay, the shift amount isn't constant. However, if we can tell that it is
5629 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005630 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5631 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005632 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5633
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005634 // If we know that if any of the high bits of the shift amount are one, then
5635 // we can do this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005636 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005637 // Mask out the high bit, which we know is set.
5638 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005639 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005640
5641 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005642 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005643 ExpandOp(Op, InL, InH);
5644 switch(Opc) {
5645 case ISD::SHL:
5646 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5647 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5648 return true;
5649 case ISD::SRL:
5650 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5651 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5652 return true;
5653 case ISD::SRA:
5654 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5655 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5656 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5657 return true;
5658 }
5659 }
5660
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005661 // If we know that the high bits of the shift amount are all zero, then we can
5662 // do this as a couple of simple shifts.
5663 if ((KnownZero & Mask) == Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005664 // Compute 32-amt.
Dan Gohman8181bd12008-07-27 21:46:04 +00005665 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005666 DAG.getConstant(NVTBits, Amt.getValueType()),
5667 Amt);
5668
5669 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005670 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005671 ExpandOp(Op, InL, InH);
5672 switch(Opc) {
5673 case ISD::SHL:
5674 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5675 Hi = DAG.getNode(ISD::OR, NVT,
5676 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5677 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5678 return true;
5679 case ISD::SRL:
5680 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5681 Lo = DAG.getNode(ISD::OR, NVT,
5682 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5683 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5684 return true;
5685 case ISD::SRA:
5686 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5687 Lo = DAG.getNode(ISD::OR, NVT,
5688 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5689 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5690 return true;
5691 }
5692 }
5693
5694 return false;
5695}
5696
5697
5698// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5699// does not fit into a register, return the lo part and set the hi part to the
5700// by-reg argument. If it does fit into a single register, return the result
5701// and leave the Hi part unset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005702SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5703 bool isSigned, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005704 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5705 // The input chain to this libcall is the entry node of the function.
5706 // Legalizing the call will automatically add the previous call to the
5707 // dependence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005708 SDValue InChain = DAG.getEntryNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005709
5710 TargetLowering::ArgListTy Args;
5711 TargetLowering::ArgListEntry Entry;
5712 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00005713 MVT ArgVT = Node->getOperand(i).getValueType();
5714 const Type *ArgTy = ArgVT.getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005715 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5716 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005717 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005718 Args.push_back(Entry);
5719 }
Bill Wendlingfef06052008-09-16 21:48:12 +00005720 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang1448aad2008-10-30 08:01:45 +00005721 TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005722
5723 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands92c43912008-06-06 12:08:01 +00005724 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman8181bd12008-07-27 21:46:04 +00005725 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen67cc9b62008-09-26 19:31:26 +00005726 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5727 CallingConv::C, false, Callee, Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005728
5729 // Legalize the call sequence, starting with the chain. This will advance
5730 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5731 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5732 LegalizeOp(CallInfo.second);
Dan Gohman8181bd12008-07-27 21:46:04 +00005733 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005734 switch (getTypeAction(CallInfo.first.getValueType())) {
5735 default: assert(0 && "Unknown thing");
5736 case Legal:
5737 Result = CallInfo.first;
5738 break;
5739 case Expand:
5740 ExpandOp(CallInfo.first, Result, Hi);
5741 break;
5742 }
5743 return Result;
5744}
5745
Dan Gohman29c3cef2008-08-14 20:04:46 +00005746/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5747///
5748SDValue SelectionDAGLegalize::
5749LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5750 bool isCustom = false;
5751 SDValue Tmp1;
5752 switch (getTypeAction(Op.getValueType())) {
5753 case Legal:
5754 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5755 Op.getValueType())) {
5756 default: assert(0 && "Unknown operation action!");
5757 case TargetLowering::Custom:
5758 isCustom = true;
5759 // FALLTHROUGH
5760 case TargetLowering::Legal:
5761 Tmp1 = LegalizeOp(Op);
Gabor Greif1c80d112008-08-28 21:40:38 +00005762 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005763 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5764 else
5765 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5766 DestTy, Tmp1);
5767 if (isCustom) {
5768 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005769 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman29c3cef2008-08-14 20:04:46 +00005770 }
5771 break;
5772 case TargetLowering::Expand:
5773 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5774 break;
5775 case TargetLowering::Promote:
5776 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5777 break;
5778 }
5779 break;
5780 case Expand:
5781 Result = ExpandIntToFP(isSigned, DestTy, Op);
5782 break;
5783 case Promote:
5784 Tmp1 = PromoteOp(Op);
5785 if (isSigned) {
5786 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5787 Tmp1, DAG.getValueType(Op.getValueType()));
5788 } else {
5789 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5790 Op.getValueType());
5791 }
Gabor Greif1c80d112008-08-28 21:40:38 +00005792 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005793 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5794 else
5795 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5796 DestTy, Tmp1);
5797 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5798 break;
5799 }
5800 return Result;
5801}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005802
5803/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5804///
Dan Gohman8181bd12008-07-27 21:46:04 +00005805SDValue SelectionDAGLegalize::
5806ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands92c43912008-06-06 12:08:01 +00005807 MVT SourceVT = Source.getValueType();
Dan Gohman8b232ff2008-03-11 01:59:03 +00005808 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005809
Dan Gohman29c3cef2008-08-14 20:04:46 +00005810 // Expand unsupported int-to-fp vector casts by unrolling them.
5811 if (DestTy.isVector()) {
5812 if (!ExpandSource)
5813 return LegalizeOp(UnrollVectorOp(Source));
5814 MVT DestEltTy = DestTy.getVectorElementType();
5815 if (DestTy.getVectorNumElements() == 1) {
5816 SDValue Scalar = ScalarizeVectorOp(Source);
5817 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5818 DestEltTy, Scalar);
5819 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5820 }
5821 SDValue Lo, Hi;
5822 SplitVectorOp(Source, Lo, Hi);
5823 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5824 DestTy.getVectorNumElements() / 2);
5825 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5826 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengd901b662008-10-13 18:46:18 +00005827 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5828 HiResult));
Dan Gohman29c3cef2008-08-14 20:04:46 +00005829 }
5830
Evan Chengf99a7752008-04-01 02:18:22 +00005831 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5832 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmana193dba2008-03-05 02:07:31 +00005833 // The integer value loaded will be incorrectly if the 'sign bit' of the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005834 // incoming integer is set. To handle this, we dynamically test to see if
5835 // it is set, and, if so, add a fudge factor.
Dan Gohman8181bd12008-07-27 21:46:04 +00005836 SDValue Hi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005837 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005838 SDValue Lo;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005839 ExpandOp(Source, Lo, Hi);
5840 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5841 } else {
5842 // The comparison for the sign bit will use the entire operand.
5843 Hi = Source;
5844 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005845
Dale Johannesen96db7962008-11-04 20:52:49 +00005846 // Check to see if the target has a custom way to lower this. If so, use
5847 // it. (Note we've already expanded the operand in this case.)
Dale Johannesena359b8b2008-10-21 20:50:01 +00005848 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5849 default: assert(0 && "This action not implemented for this operation!");
5850 case TargetLowering::Legal:
5851 case TargetLowering::Expand:
5852 break; // This case is handled below.
5853 case TargetLowering::Custom: {
5854 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5855 Source), DAG);
5856 if (NV.getNode())
5857 return LegalizeOp(NV);
5858 break; // The target decided this was legal after all
5859 }
5860 }
5861
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005862 // If this is unsigned, and not supported, first perform the conversion to
5863 // signed, then adjust the result if the sign bit is set.
Dan Gohman8181bd12008-07-27 21:46:04 +00005864 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005865
Dan Gohman8181bd12008-07-27 21:46:04 +00005866 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005867 DAG.getConstant(0, Hi.getValueType()),
5868 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005869 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5870 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005871 SignSet, Four, Zero);
5872 uint64_t FF = 0x5f800000ULL;
5873 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmana193dba2008-03-05 02:07:31 +00005874 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005875
Dan Gohman8181bd12008-07-27 21:46:04 +00005876 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005877 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005878 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00005879 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00005880 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005881 if (DestTy == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005882 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005883 PseudoSourceValue::getConstantPool(), 0,
5884 false, Alignment);
Duncan Sandsec142ee2008-06-08 20:54:56 +00005885 else if (DestTy.bitsGT(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005886 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005887 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00005888 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005889 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00005890 MVT::f32, false, Alignment);
Dale Johannesen2fc20782007-09-14 22:26:36 +00005891 else
5892 assert(0 && "Unexpected conversion");
5893
Duncan Sands92c43912008-06-06 12:08:01 +00005894 MVT SCVT = SignedConv.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005895 if (SCVT != DestTy) {
5896 // Destination type needs to be expanded as well. The FADD now we are
5897 // constructing will be expanded into a libcall.
Duncan Sands92c43912008-06-06 12:08:01 +00005898 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5899 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmanc98645c2008-03-05 01:08:17 +00005900 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005901 SignedConv, SignedConv.getValue(1));
5902 }
5903 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5904 }
5905 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5906 }
5907
5908 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmanc98645c2008-03-05 01:08:17 +00005909 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005910 default: assert(0 && "This action not implemented for this operation!");
5911 case TargetLowering::Legal:
5912 case TargetLowering::Expand:
5913 break; // This case is handled below.
5914 case TargetLowering::Custom: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005915 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005916 Source), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005917 if (NV.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005918 return LegalizeOp(NV);
5919 break; // The target decided this was legal after all
5920 }
5921 }
5922
5923 // Expand the source, then glue it back together for the call. We must expand
5924 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman8b232ff2008-03-11 01:59:03 +00005925 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005926 SDValue SrcLo, SrcHi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005927 ExpandOp(Source, SrcLo, SrcHi);
5928 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5929 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005930
Duncan Sandsf68dffb2008-07-17 02:36:29 +00005931 RTLIB::Libcall LC = isSigned ?
5932 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5933 RTLIB::getUINTTOFP(SourceVT, DestTy);
5934 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5935
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005936 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman8181bd12008-07-27 21:46:04 +00005937 SDValue HiPart;
Gabor Greif1c80d112008-08-28 21:40:38 +00005938 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5939 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmanec51f642008-03-10 23:03:31 +00005940 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5941 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005942}
5943
5944/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5945/// INT_TO_FP operation of the specified operand when the target requests that
5946/// we expand it. At this point, we know that the result and operand types are
5947/// legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00005948SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5949 SDValue Op0,
5950 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005951 if (Op0.getValueType() == MVT::i32) {
5952 // simple 32-bit [signed|unsigned] integer to float/double expansion
5953
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005954 // Get the stack frame index of a 8 byte buffer.
Dan Gohman8181bd12008-07-27 21:46:04 +00005955 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005956
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005957 // word offset constant for Hi/Lo address computation
Dan Gohman8181bd12008-07-27 21:46:04 +00005958 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005959 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman8181bd12008-07-27 21:46:04 +00005960 SDValue Hi = StackSlot;
5961 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005962 if (TLI.isLittleEndian())
5963 std::swap(Hi, Lo);
5964
5965 // if signed map to unsigned space
Dan Gohman8181bd12008-07-27 21:46:04 +00005966 SDValue Op0Mapped;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005967 if (isSigned) {
5968 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman8181bd12008-07-27 21:46:04 +00005969 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005970 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5971 } else {
5972 Op0Mapped = Op0;
5973 }
5974 // store the lo of the constructed double - based on integer input
Dan Gohman8181bd12008-07-27 21:46:04 +00005975 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005976 Op0Mapped, Lo, NULL, 0);
5977 // initial hi portion of constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00005978 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005979 // store the hi of the constructed double - biased exponent
Dan Gohman8181bd12008-07-27 21:46:04 +00005980 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005981 // load the constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00005982 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005983 // FP constant to bias correct the final result
Dan Gohman8181bd12008-07-27 21:46:04 +00005984 SDValue Bias = DAG.getConstantFP(isSigned ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005985 BitsToDouble(0x4330000080000000ULL)
5986 : BitsToDouble(0x4330000000000000ULL),
5987 MVT::f64);
5988 // subtract the bias
Dan Gohman8181bd12008-07-27 21:46:04 +00005989 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005990 // final result
Dan Gohman8181bd12008-07-27 21:46:04 +00005991 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005992 // handle final rounding
5993 if (DestVT == MVT::f64) {
5994 // do nothing
5995 Result = Sub;
Duncan Sandsec142ee2008-06-08 20:54:56 +00005996 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner5872a362008-01-17 07:00:52 +00005997 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5998 DAG.getIntPtrConstant(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00005999 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenb17a7a22007-09-16 16:51:49 +00006000 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006001 }
6002 return Result;
6003 }
6004 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman8181bd12008-07-27 21:46:04 +00006005 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006006
Dan Gohman8181bd12008-07-27 21:46:04 +00006007 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006008 DAG.getConstant(0, Op0.getValueType()),
6009 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00006010 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
6011 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006012 SignSet, Four, Zero);
6013
6014 // If the sign bit of the integer is set, the large number will be treated
6015 // as a negative number. To counteract this, the dynamic code adds an
6016 // offset depending on the data type.
6017 uint64_t FF;
Duncan Sands92c43912008-06-06 12:08:01 +00006018 switch (Op0.getValueType().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006019 default: assert(0 && "Unsupported integer type!");
6020 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6021 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6022 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6023 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6024 }
6025 if (TLI.isLittleEndian()) FF <<= 32;
6026 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
6027
Dan Gohman8181bd12008-07-27 21:46:04 +00006028 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00006029 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006030 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00006031 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00006032 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006033 if (DestVT == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00006034 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00006035 PseudoSourceValue::getConstantPool(), 0,
6036 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006037 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00006038 FudgeInReg =
6039 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
6040 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006041 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00006042 MVT::f32, false, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006043 }
6044
6045 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
6046}
6047
6048/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6049/// *INT_TO_FP operation of the specified operand when the target requests that
6050/// we promote it. At this point, we know that the result and operand types are
6051/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6052/// operation that takes a larger input.
Dan Gohman8181bd12008-07-27 21:46:04 +00006053SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6054 MVT DestVT,
6055 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006056 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00006057 MVT NewInTy = LegalOp.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006058
6059 unsigned OpToUse = 0;
6060
6061 // Scan for the appropriate larger type to use.
6062 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00006063 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6064 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006065
6066 // If the target supports SINT_TO_FP of this type, use it.
6067 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6068 default: break;
6069 case TargetLowering::Legal:
6070 if (!TLI.isTypeLegal(NewInTy))
6071 break; // Can't use this datatype.
6072 // FALL THROUGH.
6073 case TargetLowering::Custom:
6074 OpToUse = ISD::SINT_TO_FP;
6075 break;
6076 }
6077 if (OpToUse) break;
6078 if (isSigned) continue;
6079
6080 // If the target supports UINT_TO_FP of this type, use it.
6081 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6082 default: break;
6083 case TargetLowering::Legal:
6084 if (!TLI.isTypeLegal(NewInTy))
6085 break; // Can't use this datatype.
6086 // FALL THROUGH.
6087 case TargetLowering::Custom:
6088 OpToUse = ISD::UINT_TO_FP;
6089 break;
6090 }
6091 if (OpToUse) break;
6092
6093 // Otherwise, try a larger type.
6094 }
6095
6096 // Okay, we found the operation and type to use. Zero extend our input to the
6097 // desired type then run the operation on it.
6098 return DAG.getNode(OpToUse, DestVT,
6099 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
6100 NewInTy, LegalOp));
6101}
6102
6103/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6104/// FP_TO_*INT operation of the specified operand when the target requests that
6105/// we promote it. At this point, we know that the result and operand types are
6106/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6107/// operation that returns a larger result.
Dan Gohman8181bd12008-07-27 21:46:04 +00006108SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6109 MVT DestVT,
6110 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006111 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00006112 MVT NewOutTy = DestVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006113
6114 unsigned OpToUse = 0;
6115
6116 // Scan for the appropriate larger type to use.
6117 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00006118 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6119 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006120
6121 // If the target supports FP_TO_SINT returning this type, use it.
6122 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6123 default: break;
6124 case TargetLowering::Legal:
6125 if (!TLI.isTypeLegal(NewOutTy))
6126 break; // Can't use this datatype.
6127 // FALL THROUGH.
6128 case TargetLowering::Custom:
6129 OpToUse = ISD::FP_TO_SINT;
6130 break;
6131 }
6132 if (OpToUse) break;
6133
6134 // If the target supports FP_TO_UINT of this type, use it.
6135 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6136 default: break;
6137 case TargetLowering::Legal:
6138 if (!TLI.isTypeLegal(NewOutTy))
6139 break; // Can't use this datatype.
6140 // FALL THROUGH.
6141 case TargetLowering::Custom:
6142 OpToUse = ISD::FP_TO_UINT;
6143 break;
6144 }
6145 if (OpToUse) break;
6146
6147 // Otherwise, try a larger type.
6148 }
6149
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006150
6151 // Okay, we found the operation and type to use.
Dan Gohman8181bd12008-07-27 21:46:04 +00006152 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sandsac496a12008-07-04 11:47:58 +00006153
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006154 // If the operation produces an invalid type, it must be custom lowered. Use
6155 // the target lowering hooks to expand it. Just keep the low part of the
6156 // expanded operation, we know that we're truncating anyway.
6157 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006158 SmallVector<SDValue, 2> Results;
6159 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6160 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6161 Operation = Results[0];
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006162 }
Duncan Sandsac496a12008-07-04 11:47:58 +00006163
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006164 // Truncate the result of the extended FP_TO_*INT operation to the desired
6165 // size.
6166 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006167}
6168
6169/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6170///
Dan Gohman8181bd12008-07-27 21:46:04 +00006171SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00006172 MVT VT = Op.getValueType();
6173 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00006174 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands92c43912008-06-06 12:08:01 +00006175 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006176 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6177 case MVT::i16:
6178 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6179 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6180 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6181 case MVT::i32:
6182 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6183 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6184 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6185 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6186 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6187 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6188 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6189 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6190 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6191 case MVT::i64:
6192 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6193 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6194 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6195 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6196 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6197 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6198 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6199 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6200 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6201 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6202 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6203 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6204 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6205 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6206 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6207 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6208 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6209 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6210 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6211 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6212 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6213 }
6214}
6215
6216/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6217///
Dan Gohman8181bd12008-07-27 21:46:04 +00006218SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006219 switch (Opc) {
6220 default: assert(0 && "Cannot expand this yet!");
6221 case ISD::CTPOP: {
6222 static const uint64_t mask[6] = {
6223 0x5555555555555555ULL, 0x3333333333333333ULL,
6224 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6225 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6226 };
Duncan Sands92c43912008-06-06 12:08:01 +00006227 MVT VT = Op.getValueType();
6228 MVT ShVT = TLI.getShiftAmountTy();
6229 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006230 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6231 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman8181bd12008-07-27 21:46:04 +00006232 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6233 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006234 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6235 DAG.getNode(ISD::AND, VT,
6236 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6237 }
6238 return Op;
6239 }
6240 case ISD::CTLZ: {
6241 // for now, we do this:
6242 // x = x | (x >> 1);
6243 // x = x | (x >> 2);
6244 // ...
6245 // x = x | (x >>16);
6246 // x = x | (x >>32); // for 64-bit input
6247 // return popcount(~x);
6248 //
6249 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006250 MVT VT = Op.getValueType();
6251 MVT ShVT = TLI.getShiftAmountTy();
6252 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006253 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006254 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006255 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6256 }
6257 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6258 return DAG.getNode(ISD::CTPOP, VT, Op);
6259 }
6260 case ISD::CTTZ: {
6261 // for now, we use: { return popcount(~x & (x - 1)); }
6262 // unless the target has ctlz but not ctpop, in which case we use:
6263 // { return 32 - nlz(~x & (x-1)); }
6264 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006265 MVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006266 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6267 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006268 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6269 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6270 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6271 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6272 TLI.isOperationLegal(ISD::CTLZ, VT))
6273 return DAG.getNode(ISD::SUB, VT,
Duncan Sands92c43912008-06-06 12:08:01 +00006274 DAG.getConstant(VT.getSizeInBits(), VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006275 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6276 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6277 }
6278 }
6279}
6280
Dan Gohman8181bd12008-07-27 21:46:04 +00006281/// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006282/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman4fc03742008-10-01 15:07:49 +00006283/// LegalizedNodes map is filled in for any results that are not expanded, the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006284/// ExpandedNodes map is filled in for any results that are expanded, and the
6285/// Lo/Hi values are returned.
Dan Gohman8181bd12008-07-27 21:46:04 +00006286void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands92c43912008-06-06 12:08:01 +00006287 MVT VT = Op.getValueType();
6288 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greif1c80d112008-08-28 21:40:38 +00006289 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006290 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00006291 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands92c43912008-06-06 12:08:01 +00006292 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006293
6294 // See if we already expanded it.
Dan Gohman8181bd12008-07-27 21:46:04 +00006295 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006296 = ExpandedNodes.find(Op);
6297 if (I != ExpandedNodes.end()) {
6298 Lo = I->second.first;
6299 Hi = I->second.second;
6300 return;
6301 }
6302
6303 switch (Node->getOpcode()) {
6304 case ISD::CopyFromReg:
6305 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006306 case ISD::FP_ROUND_INREG:
6307 if (VT == MVT::ppcf128 &&
6308 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6309 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006310 SDValue SrcLo, SrcHi, Src;
Dale Johannesend3b6af32007-10-11 23:32:15 +00006311 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6312 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006313 SDValue Result = TLI.LowerOperation(
Dale Johannesend3b6af32007-10-11 23:32:15 +00006314 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006315 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6316 Lo = Result.getNode()->getOperand(0);
6317 Hi = Result.getNode()->getOperand(1);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006318 break;
6319 }
6320 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006321 default:
6322#ifndef NDEBUG
6323 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
6324#endif
6325 assert(0 && "Do not know how to expand this operator!");
6326 abort();
Dan Gohman550c8462008-02-27 01:52:30 +00006327 case ISD::EXTRACT_ELEMENT:
6328 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006329 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman550c8462008-02-27 01:52:30 +00006330 return ExpandOp(Hi, Lo, Hi);
Dan Gohman7e7aa2c2008-02-27 19:44:57 +00006331 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006332 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006333 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6334 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6335 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006336 case ISD::UNDEF:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006337 Lo = DAG.getNode(ISD::UNDEF, NVT);
6338 Hi = DAG.getNode(ISD::UNDEF, NVT);
6339 break;
6340 case ISD::Constant: {
Duncan Sands92c43912008-06-06 12:08:01 +00006341 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman97f1f8e2008-03-03 22:20:46 +00006342 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6343 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6344 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006345 break;
6346 }
6347 case ISD::ConstantFP: {
6348 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00006349 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00006350 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen2aef5692007-10-11 18:07:22 +00006351 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6352 MVT::f64);
6353 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6354 MVT::f64);
6355 break;
6356 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006357 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
6358 if (getTypeAction(Lo.getValueType()) == Expand)
6359 ExpandOp(Lo, Lo, Hi);
6360 break;
6361 }
6362 case ISD::BUILD_PAIR:
6363 // Return the operands.
6364 Lo = Node->getOperand(0);
6365 Hi = Node->getOperand(1);
6366 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006367
6368 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00006369 if (Node->getNumValues() == 1) {
6370 ExpandOp(Op.getOperand(0), Lo, Hi);
6371 break;
6372 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006373 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif46bf5472008-08-26 22:36:50 +00006374 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006375 Op.getValue(1).getValueType() == MVT::Other &&
6376 "unhandled MERGE_VALUES");
6377 ExpandOp(Op.getOperand(0), Lo, Hi);
6378 // Remember that we legalized the chain.
6379 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6380 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006381
6382 case ISD::SIGN_EXTEND_INREG:
6383 ExpandOp(Node->getOperand(0), Lo, Hi);
6384 // sext_inreg the low part if needed.
6385 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6386
6387 // The high part gets the sign extension from the lo-part. This handles
6388 // things like sextinreg V:i64 from i8.
6389 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands92c43912008-06-06 12:08:01 +00006390 DAG.getConstant(NVT.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006391 TLI.getShiftAmountTy()));
6392 break;
6393
6394 case ISD::BSWAP: {
6395 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006396 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006397 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6398 Lo = TempLo;
6399 break;
6400 }
6401
6402 case ISD::CTPOP:
6403 ExpandOp(Node->getOperand(0), Lo, Hi);
6404 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6405 DAG.getNode(ISD::CTPOP, NVT, Lo),
6406 DAG.getNode(ISD::CTPOP, NVT, Hi));
6407 Hi = DAG.getConstant(0, NVT);
6408 break;
6409
6410 case ISD::CTLZ: {
6411 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
6412 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006413 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6414 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
6415 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006416 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006417 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006418 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6419
6420 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6421 Hi = DAG.getConstant(0, NVT);
6422 break;
6423 }
6424
6425 case ISD::CTTZ: {
6426 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
6427 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006428 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6429 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
6430 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006431 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006432 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006433 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6434
6435 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6436 Hi = DAG.getConstant(0, NVT);
6437 break;
6438 }
6439
6440 case ISD::VAARG: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006441 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6442 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006443 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6444 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6445
6446 // Remember that we legalized the chain.
6447 Hi = LegalizeOp(Hi);
6448 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006449 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006450 std::swap(Lo, Hi);
6451 break;
6452 }
6453
6454 case ISD::LOAD: {
6455 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00006456 SDValue Ch = LD->getChain(); // Legalize the chain.
6457 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006458 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman29c3cef2008-08-14 20:04:46 +00006459 const Value *SV = LD->getSrcValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006460 int SVOffset = LD->getSrcValueOffset();
6461 unsigned Alignment = LD->getAlignment();
6462 bool isVolatile = LD->isVolatile();
6463
6464 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman29c3cef2008-08-14 20:04:46 +00006465 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006466 isVolatile, Alignment);
6467 if (VT == MVT::f32 || VT == MVT::f64) {
6468 // f32->i32 or f64->i64 one to one expansion.
6469 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006470 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006471 // Recursively expand the new load.
6472 if (getTypeAction(NVT) == Expand)
6473 ExpandOp(Lo, Lo, Hi);
6474 break;
6475 }
6476
6477 // Increment the pointer to the other half.
Duncan Sands92c43912008-06-06 12:08:01 +00006478 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006479 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006480 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006481 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006482 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00006483 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006484 isVolatile, Alignment);
6485
6486 // Build a factor node to remember that this load is independent of the
6487 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00006488 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006489 Hi.getValue(1));
6490
6491 // Remember that we legalized the chain.
6492 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006493 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006494 std::swap(Lo, Hi);
6495 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00006496 MVT EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006497
Dale Johannesen2550e3a2007-10-19 20:29:00 +00006498 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6499 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006500 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman29c3cef2008-08-14 20:04:46 +00006501 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006502 SVOffset, isVolatile, Alignment);
6503 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006504 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006505 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6506 break;
6507 }
6508
6509 if (EVT == NVT)
Dan Gohman29c3cef2008-08-14 20:04:46 +00006510 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006511 SVOffset, isVolatile, Alignment);
6512 else
Dan Gohman29c3cef2008-08-14 20:04:46 +00006513 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006514 SVOffset, EVT, isVolatile,
6515 Alignment);
6516
6517 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006518 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006519
6520 if (ExtType == ISD::SEXTLOAD) {
6521 // The high part is obtained by SRA'ing all but one of the bits of the
6522 // lo part.
Duncan Sands92c43912008-06-06 12:08:01 +00006523 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006524 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6525 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6526 } else if (ExtType == ISD::ZEXTLOAD) {
6527 // The high part is just a zero.
6528 Hi = DAG.getConstant(0, NVT);
6529 } else /* if (ExtType == ISD::EXTLOAD) */ {
6530 // The high part is undefined.
6531 Hi = DAG.getNode(ISD::UNDEF, NVT);
6532 }
6533 }
6534 break;
6535 }
6536 case ISD::AND:
6537 case ISD::OR:
6538 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +00006539 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006540 ExpandOp(Node->getOperand(0), LL, LH);
6541 ExpandOp(Node->getOperand(1), RL, RH);
6542 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6543 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6544 break;
6545 }
6546 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006547 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006548 ExpandOp(Node->getOperand(1), LL, LH);
6549 ExpandOp(Node->getOperand(2), RL, RH);
6550 if (getTypeAction(NVT) == Expand)
6551 NVT = TLI.getTypeToExpandTo(NVT);
6552 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
6553 if (VT != MVT::f32)
6554 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
6555 break;
6556 }
6557 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006558 SDValue TL, TH, FL, FH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006559 ExpandOp(Node->getOperand(2), TL, TH);
6560 ExpandOp(Node->getOperand(3), FL, FH);
6561 if (getTypeAction(NVT) == Expand)
6562 NVT = TLI.getTypeToExpandTo(NVT);
6563 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6564 Node->getOperand(1), TL, FL, Node->getOperand(4));
6565 if (VT != MVT::f32)
6566 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6567 Node->getOperand(1), TH, FH, Node->getOperand(4));
6568 break;
6569 }
6570 case ISD::ANY_EXTEND:
6571 // The low part is any extension of the input (which degenerates to a copy).
6572 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6573 // The high part is undefined.
6574 Hi = DAG.getNode(ISD::UNDEF, NVT);
6575 break;
6576 case ISD::SIGN_EXTEND: {
6577 // The low part is just a sign extension of the input (which degenerates to
6578 // a copy).
6579 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
6580
6581 // The high part is obtained by SRA'ing all but one of the bits of the lo
6582 // part.
Duncan Sands92c43912008-06-06 12:08:01 +00006583 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006584 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6585 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6586 break;
6587 }
6588 case ISD::ZERO_EXTEND:
6589 // The low part is just a zero extension of the input (which degenerates to
6590 // a copy).
6591 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
6592
6593 // The high part is just a zero.
6594 Hi = DAG.getConstant(0, NVT);
6595 break;
6596
6597 case ISD::TRUNCATE: {
6598 // The input value must be larger than this value. Expand *it*.
Dan Gohman8181bd12008-07-27 21:46:04 +00006599 SDValue NewLo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006600 ExpandOp(Node->getOperand(0), NewLo, Hi);
6601
6602 // The low part is now either the right size, or it is closer. If not the
6603 // right size, make an illegal truncate so we recursively expand it.
6604 if (NewLo.getValueType() != Node->getValueType(0))
6605 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6606 ExpandOp(NewLo, Lo, Hi);
6607 break;
6608 }
6609
6610 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006611 SDValue Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006612 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6613 // If the target wants to, allow it to lower this itself.
6614 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6615 case Expand: assert(0 && "cannot expand FP!");
6616 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6617 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6618 }
6619 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6620 }
6621
6622 // f32 / f64 must be expanded to i32 / i64.
6623 if (VT == MVT::f32 || VT == MVT::f64) {
6624 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6625 if (getTypeAction(NVT) == Expand)
6626 ExpandOp(Lo, Lo, Hi);
6627 break;
6628 }
6629
6630 // If source operand will be expanded to the same type as VT, i.e.
6631 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands92c43912008-06-06 12:08:01 +00006632 MVT VT0 = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006633 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6634 ExpandOp(Node->getOperand(0), Lo, Hi);
6635 break;
6636 }
6637
6638 // Turn this into a load/store pair by default.
Gabor Greif1c80d112008-08-28 21:40:38 +00006639 if (Tmp.getNode() == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00006640 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006641
6642 ExpandOp(Tmp, Lo, Hi);
6643 break;
6644 }
6645
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006646 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006647 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6648 TargetLowering::Custom &&
6649 "Must custom expand ReadCycleCounter");
Dan Gohman8181bd12008-07-27 21:46:04 +00006650 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006651 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006652 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006653 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006654 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006655 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006656 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006657
Dale Johannesen44eb5372008-10-03 19:41:08 +00006658 case ISD::ATOMIC_CMP_SWAP_64: {
6659 // This operation does not need a loop.
6660 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6661 assert(Tmp.getNode() && "Node must be custom expanded!");
6662 ExpandOp(Tmp.getValue(0), Lo, Hi);
6663 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6664 LegalizeOp(Tmp.getValue(1)));
6665 break;
6666 }
6667
Dale Johannesenf160d802008-10-02 18:53:47 +00006668 case ISD::ATOMIC_LOAD_ADD_64:
6669 case ISD::ATOMIC_LOAD_SUB_64:
6670 case ISD::ATOMIC_LOAD_AND_64:
6671 case ISD::ATOMIC_LOAD_OR_64:
6672 case ISD::ATOMIC_LOAD_XOR_64:
6673 case ISD::ATOMIC_LOAD_NAND_64:
Dale Johannesen44eb5372008-10-03 19:41:08 +00006674 case ISD::ATOMIC_SWAP_64: {
6675 // These operations require a loop to be generated. We can't do that yet,
6676 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesenf160d802008-10-02 18:53:47 +00006677 SDValue In2Lo, In2Hi, In2;
6678 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6679 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006680 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6681 SDValue Replace =
6682 DAG.getAtomic(Op.getOpcode(), Op.getOperand(0), Op.getOperand(1), In2,
6683 Anode->getSrcValue(), Anode->getAlignment());
6684 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006685 ExpandOp(Result.getValue(0), Lo, Hi);
6686 // Remember that we legalized the chain.
6687 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharth81580822008-03-05 01:15:49 +00006688 break;
6689 }
6690
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006691 // These operators cannot be expanded directly, emit them as calls to
6692 // library functions.
6693 case ISD::FP_TO_SINT: {
6694 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006695 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006696 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6697 case Expand: assert(0 && "cannot expand FP!");
6698 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6699 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6700 }
6701
6702 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6703
6704 // Now that the custom expander is done, expand the result, which is still
6705 // VT.
Gabor Greif1c80d112008-08-28 21:40:38 +00006706 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006707 ExpandOp(Op, Lo, Hi);
6708 break;
6709 }
6710 }
6711
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006712 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6713 VT);
6714 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6715 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006716 break;
6717 }
6718
6719 case ISD::FP_TO_UINT: {
6720 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006721 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006722 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6723 case Expand: assert(0 && "cannot expand FP!");
6724 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6725 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6726 }
6727
6728 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6729
6730 // Now that the custom expander is done, expand the result.
Gabor Greif1c80d112008-08-28 21:40:38 +00006731 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006732 ExpandOp(Op, Lo, Hi);
6733 break;
6734 }
6735 }
6736
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006737 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6738 VT);
6739 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6740 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006741 break;
6742 }
6743
6744 case ISD::SHL: {
6745 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006746 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006747 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006748 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006749 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006750 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006751 // Now that the custom expander is done, expand the result, which is
6752 // still VT.
6753 ExpandOp(Op, Lo, Hi);
6754 break;
6755 }
6756 }
6757
6758 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6759 // this X << 1 as X+X.
6760 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00006761 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006762 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006763 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006764 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6765 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6766 LoOps[1] = LoOps[0];
6767 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6768
6769 HiOps[1] = HiOps[0];
6770 HiOps[2] = Lo.getValue(1);
6771 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6772 break;
6773 }
6774 }
6775
6776 // If we can emit an efficient shift operation, do so now.
6777 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6778 break;
6779
6780 // If this target supports SHL_PARTS, use it.
6781 TargetLowering::LegalizeAction Action =
6782 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6783 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6784 Action == TargetLowering::Custom) {
6785 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6786 break;
6787 }
6788
6789 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006790 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006791 break;
6792 }
6793
6794 case ISD::SRA: {
6795 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006796 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006797 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006798 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006799 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006800 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006801 // Now that the custom expander is done, expand the result, which is
6802 // still VT.
6803 ExpandOp(Op, Lo, Hi);
6804 break;
6805 }
6806 }
6807
6808 // If we can emit an efficient shift operation, do so now.
6809 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6810 break;
6811
6812 // If this target supports SRA_PARTS, use it.
6813 TargetLowering::LegalizeAction Action =
6814 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6815 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6816 Action == TargetLowering::Custom) {
6817 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6818 break;
6819 }
6820
6821 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006822 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006823 break;
6824 }
6825
6826 case ISD::SRL: {
6827 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006828 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006829 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006830 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006831 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006832 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006833 // Now that the custom expander is done, expand the result, which is
6834 // still VT.
6835 ExpandOp(Op, Lo, Hi);
6836 break;
6837 }
6838 }
6839
6840 // If we can emit an efficient shift operation, do so now.
6841 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6842 break;
6843
6844 // If this target supports SRL_PARTS, use it.
6845 TargetLowering::LegalizeAction Action =
6846 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6847 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6848 Action == TargetLowering::Custom) {
6849 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6850 break;
6851 }
6852
6853 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006854 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006855 break;
6856 }
6857
6858 case ISD::ADD:
6859 case ISD::SUB: {
6860 // If the target wants to custom expand this, let them.
6861 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6862 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006863 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006864 if (Result.getNode()) {
Duncan Sands4c3885b2008-06-22 09:42:16 +00006865 ExpandOp(Result, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006866 break;
6867 }
6868 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006869 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006870 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006871 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6872 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6873 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006874 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006875 LoOps[0] = LHSL;
6876 LoOps[1] = RHSL;
6877 HiOps[0] = LHSH;
6878 HiOps[1] = RHSH;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006879
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006880 //cascaded check to see if any smaller size has a a carry flag.
6881 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6882 bool hasCarry = false;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006883 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6884 MVT AVT = MVT::getIntegerVT(BitSize);
6885 if (TLI.isOperationLegal(OpV, AVT)) {
6886 hasCarry = true;
6887 break;
6888 }
6889 }
6890
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006891 if(hasCarry) {
Andrew Lenharth5e814462008-10-07 14:15:42 +00006892 if (Node->getOpcode() == ISD::ADD) {
6893 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6894 HiOps[2] = Lo.getValue(1);
6895 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6896 } else {
6897 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6898 HiOps[2] = Lo.getValue(1);
6899 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6900 }
6901 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006902 } else {
Andrew Lenharth5e814462008-10-07 14:15:42 +00006903 if (Node->getOpcode() == ISD::ADD) {
6904 Lo = DAG.getNode(ISD::ADD, VTList, LoOps, 2);
6905 Hi = DAG.getNode(ISD::ADD, VTList, HiOps, 2);
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006906 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6907 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006908 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6909 DAG.getConstant(1, NVT),
6910 DAG.getConstant(0, NVT));
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006911 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6912 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006913 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6914 DAG.getConstant(1, NVT),
6915 Carry1);
6916 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6917 } else {
6918 Lo = DAG.getNode(ISD::SUB, VTList, LoOps, 2);
6919 Hi = DAG.getNode(ISD::SUB, VTList, HiOps, 2);
6920 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6921 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6922 DAG.getConstant(1, NVT),
6923 DAG.getConstant(0, NVT));
6924 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6925 }
6926 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006927 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006928 }
6929
6930 case ISD::ADDC:
6931 case ISD::SUBC: {
6932 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006933 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006934 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6935 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6936 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006937 SDValue LoOps[2] = { LHSL, RHSL };
6938 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006939
6940 if (Node->getOpcode() == ISD::ADDC) {
6941 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6942 HiOps[2] = Lo.getValue(1);
6943 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6944 } else {
6945 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6946 HiOps[2] = Lo.getValue(1);
6947 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6948 }
6949 // Remember that we legalized the flag.
6950 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6951 break;
6952 }
6953 case ISD::ADDE:
6954 case ISD::SUBE: {
6955 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006956 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006957 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6958 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6959 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006960 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6961 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006962
6963 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6964 HiOps[2] = Lo.getValue(1);
6965 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6966
6967 // Remember that we legalized the flag.
6968 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6969 break;
6970 }
6971 case ISD::MUL: {
6972 // If the target wants to custom expand this, let them.
6973 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006974 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006975 if (New.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006976 ExpandOp(New, Lo, Hi);
6977 break;
6978 }
6979 }
6980
6981 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6982 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00006983 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6984 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6985 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006986 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006987 ExpandOp(Node->getOperand(0), LL, LH);
6988 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman07961cd2008-02-25 21:11:39 +00006989 unsigned OuterBitSize = Op.getValueSizeInBits();
6990 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman5a199552007-10-08 18:33:35 +00006991 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6992 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman2594d942008-03-10 20:42:19 +00006993 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6994 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6995 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman5a199552007-10-08 18:33:35 +00006996 // The inputs are both zero-extended.
6997 if (HasUMUL_LOHI) {
6998 // We can emit a umul_lohi.
6999 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00007000 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00007001 break;
7002 }
7003 if (HasMULHU) {
7004 // We can emit a mulhu+mul.
7005 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7006 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7007 break;
7008 }
Dan Gohman5a199552007-10-08 18:33:35 +00007009 }
Dan Gohman07961cd2008-02-25 21:11:39 +00007010 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman5a199552007-10-08 18:33:35 +00007011 // The input values are both sign-extended.
7012 if (HasSMUL_LOHI) {
7013 // We can emit a smul_lohi.
7014 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00007015 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00007016 break;
7017 }
7018 if (HasMULHS) {
7019 // We can emit a mulhs+mul.
7020 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7021 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
7022 break;
7023 }
7024 }
7025 if (HasUMUL_LOHI) {
7026 // Lo,Hi = umul LHS, RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00007027 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman5a199552007-10-08 18:33:35 +00007028 DAG.getVTList(NVT, NVT), LL, RL);
7029 Lo = UMulLOHI;
7030 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007031 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7032 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7033 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7034 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7035 break;
7036 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00007037 if (HasMULHU) {
7038 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7039 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7040 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7041 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7042 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7043 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7044 break;
7045 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007046 }
7047
Dan Gohman5a199552007-10-08 18:33:35 +00007048 // If nothing else, we can make a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007049 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007050 break;
7051 }
7052 case ISD::SDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007053 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007054 break;
7055 case ISD::UDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007056 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007057 break;
7058 case ISD::SREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007059 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007060 break;
7061 case ISD::UREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007062 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007063 break;
7064
7065 case ISD::FADD:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007066 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7067 RTLIB::ADD_F64,
7068 RTLIB::ADD_F80,
7069 RTLIB::ADD_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007070 Node, false, Hi);
7071 break;
7072 case ISD::FSUB:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007073 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7074 RTLIB::SUB_F64,
7075 RTLIB::SUB_F80,
7076 RTLIB::SUB_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007077 Node, false, Hi);
7078 break;
7079 case ISD::FMUL:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007080 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7081 RTLIB::MUL_F64,
7082 RTLIB::MUL_F80,
7083 RTLIB::MUL_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007084 Node, false, Hi);
7085 break;
7086 case ISD::FDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007087 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7088 RTLIB::DIV_F64,
7089 RTLIB::DIV_F80,
7090 RTLIB::DIV_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007091 Node, false, Hi);
7092 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007093 case ISD::FP_EXTEND: {
Dale Johannesen4c14d512007-10-12 01:37:08 +00007094 if (VT == MVT::ppcf128) {
7095 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7096 Node->getOperand(0).getValueType()==MVT::f64);
7097 const uint64_t zero = 0;
7098 if (Node->getOperand(0).getValueType()==MVT::f32)
7099 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
7100 else
7101 Hi = Node->getOperand(0);
7102 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7103 break;
7104 }
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007105 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7106 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7107 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007108 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007109 }
7110 case ISD::FP_ROUND: {
7111 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7112 VT);
7113 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7114 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007115 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007116 }
Evan Cheng5316b392008-09-09 23:02:14 +00007117 case ISD::FSQRT:
7118 case ISD::FSIN:
7119 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007120 case ISD::FLOG:
7121 case ISD::FLOG2:
7122 case ISD::FLOG10:
7123 case ISD::FEXP:
7124 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00007125 case ISD::FTRUNC:
7126 case ISD::FFLOOR:
7127 case ISD::FCEIL:
7128 case ISD::FRINT:
7129 case ISD::FNEARBYINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00007130 case ISD::FPOW:
7131 case ISD::FPOWI: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007132 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
7133 switch(Node->getOpcode()) {
7134 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00007135 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7136 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007137 break;
7138 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00007139 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7140 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007141 break;
7142 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00007143 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7144 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007145 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00007146 case ISD::FLOG:
7147 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7148 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7149 break;
7150 case ISD::FLOG2:
7151 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7152 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7153 break;
7154 case ISD::FLOG10:
7155 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7156 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7157 break;
7158 case ISD::FEXP:
7159 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7160 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7161 break;
7162 case ISD::FEXP2:
7163 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7164 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7165 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00007166 case ISD::FTRUNC:
7167 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7168 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7169 break;
7170 case ISD::FFLOOR:
7171 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7172 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7173 break;
7174 case ISD::FCEIL:
7175 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7176 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7177 break;
7178 case ISD::FRINT:
7179 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7180 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7181 break;
7182 case ISD::FNEARBYINT:
7183 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7184 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7185 break;
Evan Cheng5316b392008-09-09 23:02:14 +00007186 case ISD::FPOW:
7187 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7188 RTLIB::POW_PPCF128);
7189 break;
7190 case ISD::FPOWI:
7191 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7192 RTLIB::POWI_PPCF128);
7193 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007194 default: assert(0 && "Unreachable!");
7195 }
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007196 Lo = ExpandLibCall(LC, Node, false, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007197 break;
7198 }
7199 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007200 if (VT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007201 SDValue Tmp;
Dale Johannesen5707ef82007-10-12 19:02:17 +00007202 ExpandOp(Node->getOperand(0), Lo, Tmp);
7203 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7204 // lo = hi==fabs(hi) ? lo : -lo;
7205 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7206 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7207 DAG.getCondCode(ISD::SETEQ));
7208 break;
7209 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007210 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007211 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7212 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7213 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7214 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7215 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7216 if (getTypeAction(NVT) == Expand)
7217 ExpandOp(Lo, Lo, Hi);
7218 break;
7219 }
7220 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007221 if (VT == MVT::ppcf128) {
7222 ExpandOp(Node->getOperand(0), Lo, Hi);
7223 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7224 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7225 break;
7226 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007227 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007228 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7229 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7230 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7231 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7232 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7233 if (getTypeAction(NVT) == Expand)
7234 ExpandOp(Lo, Lo, Hi);
7235 break;
7236 }
7237 case ISD::FCOPYSIGN: {
7238 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7239 if (getTypeAction(NVT) == Expand)
7240 ExpandOp(Lo, Lo, Hi);
7241 break;
7242 }
7243 case ISD::SINT_TO_FP:
7244 case ISD::UINT_TO_FP: {
7245 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands92c43912008-06-06 12:08:01 +00007246 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007247
7248 // Promote the operand if needed. Do this before checking for
7249 // ppcf128 so conversions of i16 and i8 work.
7250 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007251 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesen6a779c82008-03-18 17:28:38 +00007252 Tmp = isSigned
7253 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7254 DAG.getValueType(SrcVT))
7255 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greif1c80d112008-08-28 21:40:38 +00007256 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007257 SrcVT = Node->getOperand(0).getValueType();
7258 }
7259
Dan Gohmanec51f642008-03-10 23:03:31 +00007260 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman84d00962008-02-25 21:39:34 +00007261 static const uint64_t zero = 0;
Dale Johannesen4c14d512007-10-12 01:37:08 +00007262 if (isSigned) {
7263 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7264 Node->getOperand(0)));
7265 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7266 } else {
Dan Gohman84d00962008-02-25 21:39:34 +00007267 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen4c14d512007-10-12 01:37:08 +00007268 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7269 Node->getOperand(0)));
7270 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7271 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007272 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00007273 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7274 DAG.getConstant(0, MVT::i32),
7275 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7276 DAG.getConstantFP(
7277 APFloat(APInt(128, 2, TwoE32)),
7278 MVT::ppcf128)),
7279 Hi,
7280 DAG.getCondCode(ISD::SETLT)),
7281 Lo, Hi);
7282 }
7283 break;
7284 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007285 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7286 // si64->ppcf128 done by libcall, below
Dan Gohman84d00962008-02-25 21:39:34 +00007287 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007288 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7289 Lo, Hi);
7290 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7291 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7292 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7293 DAG.getConstant(0, MVT::i64),
7294 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7295 DAG.getConstantFP(
7296 APFloat(APInt(128, 2, TwoE64)),
7297 MVT::ppcf128)),
7298 Hi,
7299 DAG.getCondCode(ISD::SETLT)),
7300 Lo, Hi);
7301 break;
7302 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007303
Dan Gohmanec51f642008-03-10 23:03:31 +00007304 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7305 Node->getOperand(0));
Evan Chenga8740032008-04-01 01:50:16 +00007306 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng4a2f6df2008-04-01 01:51:26 +00007307 // float to i32 etc. can be 'expanded' to a single node.
Evan Chenga8740032008-04-01 01:50:16 +00007308 ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007309 break;
7310 }
7311 }
7312
7313 // Make sure the resultant values have been legalized themselves, unless this
7314 // is a type that requires multi-step expansion.
7315 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7316 Lo = LegalizeOp(Lo);
Gabor Greif1c80d112008-08-28 21:40:38 +00007317 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007318 // Don't legalize the high part if it is expanded to a single node.
7319 Hi = LegalizeOp(Hi);
7320 }
7321
7322 // Remember in a map if the values will be reused later.
Dan Gohman55d19662008-07-07 17:46:23 +00007323 bool isNew =
7324 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007325 assert(isNew && "Value already expanded?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007326 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007327}
7328
7329/// SplitVectorOp - Given an operand of vector type, break it down into
7330/// two smaller values, still of vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00007331void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7332 SDValue &Hi) {
Duncan Sands92c43912008-06-06 12:08:01 +00007333 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007334 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007335 unsigned NumElements = Op.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007336 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00007337
Duncan Sands92c43912008-06-06 12:08:01 +00007338 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman4a365ad2007-11-15 21:15:26 +00007339
7340 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7341 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7342
Duncan Sands92c43912008-06-06 12:08:01 +00007343 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7344 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007345
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007346 // See if we already split it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007347 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007348 = SplitNodes.find(Op);
7349 if (I != SplitNodes.end()) {
7350 Lo = I->second.first;
7351 Hi = I->second.second;
7352 return;
7353 }
7354
7355 switch (Node->getOpcode()) {
7356 default:
7357#ifndef NDEBUG
7358 Node->dump(&DAG);
7359#endif
7360 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00007361 case ISD::UNDEF:
7362 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7363 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7364 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007365 case ISD::BUILD_PAIR:
7366 Lo = Node->getOperand(0);
7367 Hi = Node->getOperand(1);
7368 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007369 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007370 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7371 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007372 unsigned Index = Idx->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007373 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007374 if (Index < NewNumElts_Lo)
7375 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7376 DAG.getIntPtrConstant(Index));
7377 else
7378 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7379 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7380 break;
7381 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007382 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007383 Node->getOperand(1),
7384 Node->getOperand(2));
7385 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007386 break;
7387 }
Chris Lattner587c46d2007-11-19 21:16:54 +00007388 case ISD::VECTOR_SHUFFLE: {
7389 // Build the low part.
Dan Gohman8181bd12008-07-27 21:46:04 +00007390 SDValue Mask = Node->getOperand(2);
7391 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +00007392 MVT PtrVT = TLI.getPointerTy();
Chris Lattner587c46d2007-11-19 21:16:54 +00007393
7394 // Insert all of the elements from the input that are needed. We use
7395 // buildvector of extractelement here because the input vectors will have
7396 // to be legalized, so this makes the code simpler.
7397 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007398 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007399 if (IdxNode.getOpcode() == ISD::UNDEF) {
7400 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7401 continue;
7402 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007403 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007404 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007405 if (Idx >= NumElements) {
7406 InVec = Node->getOperand(1);
7407 Idx -= NumElements;
7408 }
7409 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7410 DAG.getConstant(Idx, PtrVT)));
7411 }
7412 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7413 Ops.clear();
7414
7415 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007416 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007417 if (IdxNode.getOpcode() == ISD::UNDEF) {
7418 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7419 continue;
7420 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007421 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007422 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007423 if (Idx >= NumElements) {
7424 InVec = Node->getOperand(1);
7425 Idx -= NumElements;
7426 }
7427 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7428 DAG.getConstant(Idx, PtrVT)));
7429 }
Mon P Wang2e89b112008-07-25 01:30:26 +00007430 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner587c46d2007-11-19 21:16:54 +00007431 break;
7432 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007433 case ISD::BUILD_VECTOR: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007434 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00007435 Node->op_begin()+NewNumElts_Lo);
7436 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007437
Dan Gohman8181bd12008-07-27 21:46:04 +00007438 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007439 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007440 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007441 break;
7442 }
7443 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00007444 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007445 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7446 if (NewNumSubvectors == 1) {
7447 Lo = Node->getOperand(0);
7448 Hi = Node->getOperand(1);
7449 } else {
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007450 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7451 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007452 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007453
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007454 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007455 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007456 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007457 }
7458 break;
7459 }
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007460 case ISD::EXTRACT_SUBVECTOR: {
7461 SDValue Vec = Op.getOperand(0);
7462 SDValue Idx = Op.getOperand(1);
7463 MVT IdxVT = Idx.getValueType();
7464
7465 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7466 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7467 if (CIdx) {
7468 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7469 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7470 IdxVT));
7471 } else {
7472 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7473 DAG.getConstant(NewNumElts_Lo, IdxVT));
7474 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7475 }
7476 break;
7477 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00007478 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007479 SDValue Cond = Node->getOperand(0);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007480
Dan Gohman8181bd12008-07-27 21:46:04 +00007481 SDValue LL, LH, RL, RH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007482 SplitVectorOp(Node->getOperand(1), LL, LH);
7483 SplitVectorOp(Node->getOperand(2), RL, RH);
7484
Duncan Sands92c43912008-06-06 12:08:01 +00007485 if (Cond.getValueType().isVector()) {
Dan Gohmand5d4c872007-10-17 14:48:28 +00007486 // Handle a vector merge.
Dan Gohman8181bd12008-07-27 21:46:04 +00007487 SDValue CL, CH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007488 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007489 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7490 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007491 } else {
7492 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00007493 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7494 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007495 }
7496 break;
7497 }
Chris Lattnerc7471452008-06-30 02:43:01 +00007498 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007499 SDValue CondLHS = Node->getOperand(0);
7500 SDValue CondRHS = Node->getOperand(1);
7501 SDValue CondCode = Node->getOperand(4);
Chris Lattnerc7471452008-06-30 02:43:01 +00007502
Dan Gohman8181bd12008-07-27 21:46:04 +00007503 SDValue LL, LH, RL, RH;
Chris Lattnerc7471452008-06-30 02:43:01 +00007504 SplitVectorOp(Node->getOperand(2), LL, LH);
7505 SplitVectorOp(Node->getOperand(3), RL, RH);
7506
7507 // Handle a simple select with vector operands.
7508 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7509 LL, RL, CondCode);
7510 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7511 LH, RH, CondCode);
7512 break;
7513 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00007514 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007515 SDValue LL, LH, RL, RH;
Nate Begeman9a1ce152008-05-12 19:40:03 +00007516 SplitVectorOp(Node->getOperand(0), LL, LH);
7517 SplitVectorOp(Node->getOperand(1), RL, RH);
7518 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7519 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7520 break;
7521 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007522 case ISD::ADD:
7523 case ISD::SUB:
7524 case ISD::MUL:
7525 case ISD::FADD:
7526 case ISD::FSUB:
7527 case ISD::FMUL:
7528 case ISD::SDIV:
7529 case ISD::UDIV:
7530 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007531 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007532 case ISD::AND:
7533 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00007534 case ISD::XOR:
7535 case ISD::UREM:
7536 case ISD::SREM:
7537 case ISD::FREM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007538 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007539 SplitVectorOp(Node->getOperand(0), LL, LH);
7540 SplitVectorOp(Node->getOperand(1), RL, RH);
7541
Nate Begeman4a365ad2007-11-15 21:15:26 +00007542 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7543 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007544 break;
7545 }
Dan Gohman29c3cef2008-08-14 20:04:46 +00007546 case ISD::FP_ROUND:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007547 case ISD::FPOWI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007548 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007549 SplitVectorOp(Node->getOperand(0), L, H);
7550
Nate Begeman4a365ad2007-11-15 21:15:26 +00007551 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7552 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00007553 break;
7554 }
7555 case ISD::CTTZ:
7556 case ISD::CTLZ:
7557 case ISD::CTPOP:
7558 case ISD::FNEG:
7559 case ISD::FABS:
7560 case ISD::FSQRT:
7561 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00007562 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007563 case ISD::FLOG:
7564 case ISD::FLOG2:
7565 case ISD::FLOG10:
7566 case ISD::FEXP:
7567 case ISD::FEXP2:
Nate Begeman78246ca2007-11-17 03:58:34 +00007568 case ISD::FP_TO_SINT:
7569 case ISD::FP_TO_UINT:
7570 case ISD::SINT_TO_FP:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007571 case ISD::UINT_TO_FP:
7572 case ISD::TRUNCATE:
7573 case ISD::ANY_EXTEND:
7574 case ISD::SIGN_EXTEND:
7575 case ISD::ZERO_EXTEND:
7576 case ISD::FP_EXTEND: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007577 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007578 SplitVectorOp(Node->getOperand(0), L, H);
7579
Nate Begeman4a365ad2007-11-15 21:15:26 +00007580 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7581 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00007582 break;
7583 }
Mon P Wang73d31542008-11-10 20:54:11 +00007584 case ISD::CONVERT_RNDSAT: {
7585 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7586 SDValue L, H;
7587 SplitVectorOp(Node->getOperand(0), L, H);
7588 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7589 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7590 SDValue STyOpL = DAG.getValueType(L.getValueType());
7591 SDValue STyOpH = DAG.getValueType(H.getValueType());
7592
7593 SDValue RndOp = Node->getOperand(3);
7594 SDValue SatOp = Node->getOperand(4);
7595
7596 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7597 RndOp, SatOp, CvtCode);
7598 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7599 RndOp, SatOp, CvtCode);
7600 break;
7601 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007602 case ISD::LOAD: {
7603 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007604 SDValue Ch = LD->getChain();
7605 SDValue Ptr = LD->getBasePtr();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007606 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007607 const Value *SV = LD->getSrcValue();
7608 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007609 MVT MemoryVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007610 unsigned Alignment = LD->getAlignment();
7611 bool isVolatile = LD->isVolatile();
7612
Dan Gohman29c3cef2008-08-14 20:04:46 +00007613 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7614 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7615
7616 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7617 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7618 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7619
7620 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7621 NewVT_Lo, Ch, Ptr, Offset,
7622 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7623 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007624 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00007625 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007626 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00007627 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00007628 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7629 NewVT_Hi, Ch, Ptr, Offset,
7630 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007631
7632 // Build a factor node to remember that this load is independent of the
7633 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00007634 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007635 Hi.getValue(1));
7636
7637 // Remember that we legalized the chain.
7638 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
7639 break;
7640 }
7641 case ISD::BIT_CONVERT: {
7642 // We know the result is a vector. The input may be either a vector or a
7643 // scalar value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007644 SDValue InOp = Node->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007645 if (!InOp.getValueType().isVector() ||
7646 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007647 // The input is a scalar or single-element vector.
7648 // Lower to a store/load so that it can be split.
7649 // FIXME: this could be improved probably.
Mon P Wang36b59ac2008-07-15 05:28:34 +00007650 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7651 Op.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00007652 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greif1c80d112008-08-28 21:40:38 +00007653 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007654
Dan Gohman8181bd12008-07-27 21:46:04 +00007655 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00007656 InOp, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007657 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00007658 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007659 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007660 }
7661 // Split the vector and convert each of the pieces now.
7662 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007663 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7664 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007665 break;
7666 }
7667 }
7668
7669 // Remember in a map if the values will be reused later.
7670 bool isNew =
7671 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
7672 assert(isNew && "Value already split?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007673 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007674}
7675
7676
7677/// ScalarizeVectorOp - Given an operand of single-element vector type
7678/// (e.g. v1f32), convert it into the equivalent operation that returns a
7679/// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007680SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00007681 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007682 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007683 MVT NewVT = Op.getValueType().getVectorElementType();
7684 assert(Op.getValueType().getVectorNumElements() == 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007685
7686 // See if we already scalarized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007687 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007688 if (I != ScalarizedNodes.end()) return I->second;
7689
Dan Gohman8181bd12008-07-27 21:46:04 +00007690 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007691 switch (Node->getOpcode()) {
7692 default:
7693#ifndef NDEBUG
7694 Node->dump(&DAG); cerr << "\n";
7695#endif
7696 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7697 case ISD::ADD:
7698 case ISD::FADD:
7699 case ISD::SUB:
7700 case ISD::FSUB:
7701 case ISD::MUL:
7702 case ISD::FMUL:
7703 case ISD::SDIV:
7704 case ISD::UDIV:
7705 case ISD::FDIV:
7706 case ISD::SREM:
7707 case ISD::UREM:
7708 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007709 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007710 case ISD::AND:
7711 case ISD::OR:
7712 case ISD::XOR:
7713 Result = DAG.getNode(Node->getOpcode(),
7714 NewVT,
7715 ScalarizeVectorOp(Node->getOperand(0)),
7716 ScalarizeVectorOp(Node->getOperand(1)));
7717 break;
7718 case ISD::FNEG:
7719 case ISD::FABS:
7720 case ISD::FSQRT:
7721 case ISD::FSIN:
7722 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007723 case ISD::FLOG:
7724 case ISD::FLOG2:
7725 case ISD::FLOG10:
7726 case ISD::FEXP:
7727 case ISD::FEXP2:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007728 case ISD::FP_TO_SINT:
7729 case ISD::FP_TO_UINT:
7730 case ISD::SINT_TO_FP:
7731 case ISD::UINT_TO_FP:
7732 case ISD::SIGN_EXTEND:
7733 case ISD::ZERO_EXTEND:
7734 case ISD::ANY_EXTEND:
7735 case ISD::TRUNCATE:
7736 case ISD::FP_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007737 Result = DAG.getNode(Node->getOpcode(),
7738 NewVT,
7739 ScalarizeVectorOp(Node->getOperand(0)));
7740 break;
Mon P Wang73d31542008-11-10 20:54:11 +00007741 case ISD::CONVERT_RNDSAT: {
7742 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7743 Result = DAG.getConvertRndSat(NewVT, Op0,
7744 DAG.getValueType(NewVT),
7745 DAG.getValueType(Op0.getValueType()),
7746 Node->getOperand(3),
7747 Node->getOperand(4),
7748 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7749 break;
7750 }
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007751 case ISD::FPOWI:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007752 case ISD::FP_ROUND:
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007753 Result = DAG.getNode(Node->getOpcode(),
7754 NewVT,
7755 ScalarizeVectorOp(Node->getOperand(0)),
7756 Node->getOperand(1));
7757 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007758 case ISD::LOAD: {
7759 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007760 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7761 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman29c3cef2008-08-14 20:04:46 +00007762 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007763 const Value *SV = LD->getSrcValue();
7764 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007765 MVT MemoryVT = LD->getMemoryVT();
7766 unsigned Alignment = LD->getAlignment();
7767 bool isVolatile = LD->isVolatile();
7768
7769 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7770 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7771
7772 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7773 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7774 MemoryVT.getVectorElementType(),
7775 isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007776
7777 // Remember that we legalized the chain.
7778 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7779 break;
7780 }
7781 case ISD::BUILD_VECTOR:
7782 Result = Node->getOperand(0);
7783 break;
7784 case ISD::INSERT_VECTOR_ELT:
7785 // Returning the inserted scalar element.
7786 Result = Node->getOperand(1);
7787 break;
7788 case ISD::CONCAT_VECTORS:
7789 assert(Node->getOperand(0).getValueType() == NewVT &&
7790 "Concat of non-legal vectors not yet supported!");
7791 Result = Node->getOperand(0);
7792 break;
7793 case ISD::VECTOR_SHUFFLE: {
7794 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007795 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007796 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007797 Result = ScalarizeVectorOp(Node->getOperand(1));
7798 else
7799 Result = ScalarizeVectorOp(Node->getOperand(0));
7800 break;
7801 }
7802 case ISD::EXTRACT_SUBVECTOR:
Mon P Wang927daf52008-11-06 22:52:21 +00007803 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007804 Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007805 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007806 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007807 SDValue Op0 = Op.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007808 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng2cc16e72008-05-16 17:19:05 +00007809 Op0 = ScalarizeVectorOp(Op0);
7810 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007811 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007812 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007813 case ISD::SELECT:
7814 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
7815 ScalarizeVectorOp(Op.getOperand(1)),
7816 ScalarizeVectorOp(Op.getOperand(2)));
7817 break;
Chris Lattnerc7471452008-06-30 02:43:01 +00007818 case ISD::SELECT_CC:
7819 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7820 Node->getOperand(1),
7821 ScalarizeVectorOp(Op.getOperand(2)),
7822 ScalarizeVectorOp(Op.getOperand(3)),
7823 Node->getOperand(4));
7824 break;
Nate Begeman78ca4f92008-05-12 23:09:43 +00007825 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007826 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7827 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Nate Begeman78ca4f92008-05-12 23:09:43 +00007828 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7829 Op.getOperand(2));
7830 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7831 DAG.getConstant(-1ULL, NewVT),
7832 DAG.getConstant(0ULL, NewVT));
7833 break;
7834 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007835 }
7836
7837 if (TLI.isTypeLegal(NewVT))
7838 Result = LegalizeOp(Result);
7839 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7840 assert(isNew && "Value already scalarized?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007841 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007842 return Result;
7843}
7844
7845
Mon P Wang1448aad2008-10-30 08:01:45 +00007846SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7847 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7848 if (I != WidenNodes.end()) return I->second;
7849
7850 MVT VT = Op.getValueType();
7851 assert(VT.isVector() && "Cannot widen non-vector type!");
7852
7853 SDValue Result;
7854 SDNode *Node = Op.getNode();
7855 MVT EVT = VT.getVectorElementType();
7856
7857 unsigned NumElts = VT.getVectorNumElements();
7858 unsigned NewNumElts = WidenVT.getVectorNumElements();
7859 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7860 assert(NewNumElts < 17);
7861
7862 // When widen is called, it is assumed that it is more efficient to use a
7863 // wide type. The default action is to widen to operation to a wider legal
7864 // vector type and then do the operation if it is legal by calling LegalizeOp
7865 // again. If there is no vector equivalent, we will unroll the operation, do
7866 // it, and rebuild the vector. If most of the operations are vectorizible to
7867 // the legal type, the resulting code will be more efficient. If this is not
7868 // the case, the resulting code will preform badly as we end up generating
7869 // code to pack/unpack the results. It is the function that calls widen
Mon P Wanga5a239f2008-11-06 05:31:54 +00007870 // that is responsible for seeing this doesn't happen.
Mon P Wang1448aad2008-10-30 08:01:45 +00007871 switch (Node->getOpcode()) {
7872 default:
7873#ifndef NDEBUG
7874 Node->dump(&DAG);
7875#endif
7876 assert(0 && "Unexpected operation in WidenVectorOp!");
7877 break;
7878 case ISD::CopyFromReg:
Mon P Wang257e1c72008-11-15 06:05:52 +00007879 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang1448aad2008-10-30 08:01:45 +00007880 case ISD::Constant:
7881 case ISD::ConstantFP:
7882 // To build a vector of these elements, clients should call BuildVector
7883 // and with each element instead of creating a node with a vector type
7884 assert(0 && "Unexpected operation in WidenVectorOp!");
7885 case ISD::VAARG:
7886 // Variable Arguments with vector types doesn't make any sense to me
7887 assert(0 && "Unexpected operation in WidenVectorOp!");
7888 break;
Mon P Wang257e1c72008-11-15 06:05:52 +00007889 case ISD::UNDEF:
7890 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7891 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00007892 case ISD::BUILD_VECTOR: {
7893 // Build a vector with undefined for the new nodes
7894 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7895 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7896 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7897 }
7898 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7899 break;
7900 }
7901 case ISD::INSERT_VECTOR_ELT: {
7902 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7903 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7904 Node->getOperand(1), Node->getOperand(2));
7905 break;
7906 }
7907 case ISD::VECTOR_SHUFFLE: {
7908 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7909 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7910 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7911 // used as permutation array. We build the vector here instead of widening
7912 // because we don't want to legalize and have it turned to something else.
7913 SDValue PermOp = Node->getOperand(2);
7914 SDValueVector NewOps;
7915 MVT PVT = PermOp.getValueType().getVectorElementType();
7916 for (unsigned i = 0; i < NumElts; ++i) {
7917 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7918 NewOps.push_back(PermOp.getOperand(i));
7919 } else {
7920 unsigned Idx =
7921 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
7922 if (Idx < NumElts) {
7923 NewOps.push_back(PermOp.getOperand(i));
7924 }
7925 else {
7926 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7927 PermOp.getOperand(i).getValueType()));
7928 }
7929 }
7930 }
7931 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7932 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
7933 }
7934
7935 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
7936 MVT::getVectorVT(PVT, NewOps.size()),
7937 &NewOps[0], NewOps.size());
7938
7939 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
7940 break;
7941 }
7942 case ISD::LOAD: {
7943 // If the load widen returns true, we can use a single load for the
7944 // vector. Otherwise, it is returning a token factor for multiple
7945 // loads.
7946 SDValue TFOp;
7947 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
7948 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
7949 else
7950 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
7951 break;
7952 }
7953
7954 case ISD::BIT_CONVERT: {
7955 SDValue Tmp1 = Node->getOperand(0);
7956 // Converts between two different types so we need to determine
7957 // the correct widen type for the input operand.
7958 MVT TVT = Tmp1.getValueType();
7959 assert(TVT.isVector() && "can not widen non vector type");
7960 MVT TEVT = TVT.getVectorElementType();
7961 assert(WidenVT.getSizeInBits() % EVT.getSizeInBits() == 0 &&
7962 "can not widen bit bit convert that are not multiple of element type");
7963 MVT TWidenVT = MVT::getVectorVT(TEVT,
7964 WidenVT.getSizeInBits()/EVT.getSizeInBits());
7965 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7966 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
7967 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7968
7969 TargetLowering::LegalizeAction action =
7970 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7971 switch (action) {
7972 default: assert(0 && "action not supported");
7973 case TargetLowering::Legal:
7974 break;
7975 case TargetLowering::Promote:
7976 // We defer the promotion to when we legalize the op
7977 break;
7978 case TargetLowering::Expand:
7979 // Expand the operation into a bunch of nasty scalar code.
7980 Result = LegalizeOp(UnrollVectorOp(Result));
7981 break;
7982 }
7983 break;
7984 }
7985
7986 case ISD::SINT_TO_FP:
7987 case ISD::UINT_TO_FP:
7988 case ISD::FP_TO_SINT:
7989 case ISD::FP_TO_UINT: {
7990 SDValue Tmp1 = Node->getOperand(0);
7991 // Converts between two different types so we need to determine
7992 // the correct widen type for the input operand.
7993 MVT TVT = Tmp1.getValueType();
7994 assert(TVT.isVector() && "can not widen non vector type");
7995 MVT TEVT = TVT.getVectorElementType();
7996 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
7997 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7998 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
7999 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang1448aad2008-10-30 08:01:45 +00008000 break;
8001 }
8002
8003 case ISD::FP_EXTEND:
8004 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
8005 case ISD::TRUNCATE:
8006 case ISD::SIGN_EXTEND:
8007 case ISD::ZERO_EXTEND:
8008 case ISD::ANY_EXTEND:
8009 case ISD::FP_ROUND:
8010 case ISD::SIGN_EXTEND_INREG:
8011 case ISD::FABS:
8012 case ISD::FNEG:
8013 case ISD::FSQRT:
8014 case ISD::FSIN:
Mon P Wang257e1c72008-11-15 06:05:52 +00008015 case ISD::FCOS:
8016 case ISD::CTPOP:
8017 case ISD::CTTZ:
8018 case ISD::CTLZ: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008019 // Unary op widening
8020 SDValue Tmp1;
Mon P Wang1448aad2008-10-30 08:01:45 +00008021 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8022 assert(Tmp1.getValueType() == WidenVT);
8023 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang1448aad2008-10-30 08:01:45 +00008024 break;
8025 }
Mon P Wang73d31542008-11-10 20:54:11 +00008026 case ISD::CONVERT_RNDSAT: {
8027 SDValue RndOp = Node->getOperand(3);
8028 SDValue SatOp = Node->getOperand(4);
Mon P Wang73d31542008-11-10 20:54:11 +00008029 SDValue SrcOp = Node->getOperand(0);
8030
8031 // Converts between two different types so we need to determine
8032 // the correct widen type for the input operand.
8033 MVT SVT = SrcOp.getValueType();
8034 assert(SVT.isVector() && "can not widen non vector type");
8035 MVT SEVT = SVT.getVectorElementType();
8036 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8037
8038 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8039 assert(SrcOp.getValueType() == WidenVT);
8040 SDValue DTyOp = DAG.getValueType(WidenVT);
8041 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8042 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8043
8044 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
8045 RndOp, SatOp, CvtCode);
Mon P Wang73d31542008-11-10 20:54:11 +00008046 break;
8047 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008048 case ISD::FPOW:
8049 case ISD::FPOWI:
8050 case ISD::ADD:
8051 case ISD::SUB:
8052 case ISD::MUL:
8053 case ISD::MULHS:
8054 case ISD::MULHU:
8055 case ISD::AND:
8056 case ISD::OR:
8057 case ISD::XOR:
8058 case ISD::FADD:
8059 case ISD::FSUB:
8060 case ISD::FMUL:
8061 case ISD::SDIV:
8062 case ISD::SREM:
8063 case ISD::FDIV:
8064 case ISD::FREM:
8065 case ISD::FCOPYSIGN:
8066 case ISD::UDIV:
8067 case ISD::UREM:
8068 case ISD::BSWAP: {
8069 // Binary op widening
Mon P Wang1448aad2008-10-30 08:01:45 +00008070 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8071 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8072 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8073 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
Mon P Wang1448aad2008-10-30 08:01:45 +00008074 break;
8075 }
8076
8077 case ISD::SHL:
8078 case ISD::SRA:
8079 case ISD::SRL: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008080 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8081 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangd5638262008-12-02 07:35:08 +00008082 SDValue ShOp = Node->getOperand(1);
8083 MVT ShVT = ShOp.getValueType();
8084 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8085 WidenVT.getVectorNumElements());
8086 ShOp = WidenVectorOp(ShOp, NewShVT);
8087 assert(ShOp.getValueType() == NewShVT);
8088 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, ShOp);
Mon P Wang1448aad2008-10-30 08:01:45 +00008089 break;
8090 }
Mon P Wangd5638262008-12-02 07:35:08 +00008091
Mon P Wang1448aad2008-10-30 08:01:45 +00008092 case ISD::EXTRACT_VECTOR_ELT: {
8093 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8094 assert(Tmp1.getValueType() == WidenVT);
8095 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8096 break;
8097 }
8098 case ISD::CONCAT_VECTORS: {
8099 // We concurrently support only widen on a multiple of the incoming vector.
8100 // We could widen on a multiple of the incoming operand if necessary.
8101 unsigned NumConcat = NewNumElts / NumElts;
8102 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Mon P Wangd5638262008-12-02 07:35:08 +00008103 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
Mon P Wang1448aad2008-10-30 08:01:45 +00008104 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 break;
8112 }
8113 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang257e1c72008-11-15 06:05:52 +00008114 SDValue Tmp1 = Node->getOperand(0);
8115 SDValue Idx = Node->getOperand(1);
8116 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8117 if (CIdx && CIdx->getZExtValue() == 0) {
8118 // Since we are access the start of the vector, the incoming
8119 // vector type might be the proper.
8120 MVT Tmp1VT = Tmp1.getValueType();
8121 if (Tmp1VT == WidenVT)
8122 return Tmp1;
8123 else {
8124 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8125 if (Tmp1VTNumElts < NewNumElts)
8126 Result = WidenVectorOp(Tmp1, WidenVT);
8127 else
8128 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8129 }
8130 } else if (NewNumElts % NumElts == 0) {
8131 // Widen the extracted subvector.
8132 unsigned NumConcat = NewNumElts / NumElts;
8133 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8134 SmallVector<SDValue, 8> MOps;
8135 MOps.push_back(Op);
8136 for (unsigned i = 1; i != NumConcat; ++i) {
8137 MOps.push_back(UndefVal);
8138 }
8139 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8140 &MOps[0], MOps.size()));
8141 } else {
8142 assert(0 && "can not widen extract subvector");
8143 // This could be implemented using insert and build vector but I would
8144 // like to see when this happens.
8145 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008146 break;
8147 }
8148
8149 case ISD::SELECT: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008150 // Determine new condition widen type and widen
8151 SDValue Cond1 = Node->getOperand(0);
8152 MVT CondVT = Cond1.getValueType();
8153 assert(CondVT.isVector() && "can not widen non vector type");
8154 MVT CondEVT = CondVT.getVectorElementType();
8155 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8156 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8157 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8158
8159 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8160 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8161 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8162 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang1448aad2008-10-30 08:01:45 +00008163 break;
8164 }
8165
8166 case ISD::SELECT_CC: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008167 // Determine new condition widen type and widen
8168 SDValue Cond1 = Node->getOperand(0);
8169 SDValue Cond2 = Node->getOperand(1);
8170 MVT CondVT = Cond1.getValueType();
8171 assert(CondVT.isVector() && "can not widen non vector type");
8172 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8173 MVT CondEVT = CondVT.getVectorElementType();
8174 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8175 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8176 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8177 assert(Cond1.getValueType() == CondWidenVT &&
8178 Cond2.getValueType() == CondWidenVT && "condition not widen");
8179
8180 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8181 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8182 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8183 "operands not widen");
8184 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8185 Tmp2, Node->getOperand(4));
Mon P Wang1448aad2008-10-30 08:01:45 +00008186 break;
Mon P Wang42ac14e2008-10-30 18:21:52 +00008187 }
8188 case ISD::VSETCC: {
8189 // Determine widen for the operand
8190 SDValue Tmp1 = Node->getOperand(0);
8191 MVT TmpVT = Tmp1.getValueType();
8192 assert(TmpVT.isVector() && "can not widen non vector type");
8193 MVT TmpEVT = TmpVT.getVectorElementType();
8194 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8195 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8196 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
8197 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
8198 Node->getOperand(2));
Mon P Wang1448aad2008-10-30 08:01:45 +00008199 break;
8200 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008201 case ISD::ATOMIC_CMP_SWAP_8:
8202 case ISD::ATOMIC_CMP_SWAP_16:
8203 case ISD::ATOMIC_CMP_SWAP_32:
8204 case ISD::ATOMIC_CMP_SWAP_64:
8205 case ISD::ATOMIC_LOAD_ADD_8:
8206 case ISD::ATOMIC_LOAD_SUB_8:
8207 case ISD::ATOMIC_LOAD_AND_8:
8208 case ISD::ATOMIC_LOAD_OR_8:
8209 case ISD::ATOMIC_LOAD_XOR_8:
8210 case ISD::ATOMIC_LOAD_NAND_8:
8211 case ISD::ATOMIC_LOAD_MIN_8:
8212 case ISD::ATOMIC_LOAD_MAX_8:
8213 case ISD::ATOMIC_LOAD_UMIN_8:
8214 case ISD::ATOMIC_LOAD_UMAX_8:
8215 case ISD::ATOMIC_SWAP_8:
8216 case ISD::ATOMIC_LOAD_ADD_16:
8217 case ISD::ATOMIC_LOAD_SUB_16:
8218 case ISD::ATOMIC_LOAD_AND_16:
8219 case ISD::ATOMIC_LOAD_OR_16:
8220 case ISD::ATOMIC_LOAD_XOR_16:
8221 case ISD::ATOMIC_LOAD_NAND_16:
8222 case ISD::ATOMIC_LOAD_MIN_16:
8223 case ISD::ATOMIC_LOAD_MAX_16:
8224 case ISD::ATOMIC_LOAD_UMIN_16:
8225 case ISD::ATOMIC_LOAD_UMAX_16:
8226 case ISD::ATOMIC_SWAP_16:
8227 case ISD::ATOMIC_LOAD_ADD_32:
8228 case ISD::ATOMIC_LOAD_SUB_32:
8229 case ISD::ATOMIC_LOAD_AND_32:
8230 case ISD::ATOMIC_LOAD_OR_32:
8231 case ISD::ATOMIC_LOAD_XOR_32:
8232 case ISD::ATOMIC_LOAD_NAND_32:
8233 case ISD::ATOMIC_LOAD_MIN_32:
8234 case ISD::ATOMIC_LOAD_MAX_32:
8235 case ISD::ATOMIC_LOAD_UMIN_32:
8236 case ISD::ATOMIC_LOAD_UMAX_32:
8237 case ISD::ATOMIC_SWAP_32:
8238 case ISD::ATOMIC_LOAD_ADD_64:
8239 case ISD::ATOMIC_LOAD_SUB_64:
8240 case ISD::ATOMIC_LOAD_AND_64:
8241 case ISD::ATOMIC_LOAD_OR_64:
8242 case ISD::ATOMIC_LOAD_XOR_64:
8243 case ISD::ATOMIC_LOAD_NAND_64:
8244 case ISD::ATOMIC_LOAD_MIN_64:
8245 case ISD::ATOMIC_LOAD_MAX_64:
8246 case ISD::ATOMIC_LOAD_UMIN_64:
8247 case ISD::ATOMIC_LOAD_UMAX_64:
8248 case ISD::ATOMIC_SWAP_64: {
8249 // For now, we assume that using vectors for these operations don't make
8250 // much sense so we just split it. We return an empty result
8251 SDValue X, Y;
8252 SplitVectorOp(Op, X, Y);
8253 return Result;
8254 break;
8255 }
8256
8257 } // end switch (Node->getOpcode())
8258
8259 assert(Result.getNode() && "Didn't set a result!");
8260 if (Result != Op)
8261 Result = LegalizeOp(Result);
8262
Mon P Wanga5a239f2008-11-06 05:31:54 +00008263 AddWidenedOperand(Op, Result);
Mon P Wang1448aad2008-10-30 08:01:45 +00008264 return Result;
8265}
8266
8267// Utility function to find a legal vector type and its associated element
8268// type from a preferred width and whose vector type must be the same size
8269// as the VVT.
8270// TLI: Target lowering used to determine legal types
8271// Width: Preferred width of element type
8272// VVT: Vector value type whose size we must match.
8273// Returns VecEVT and EVT - the vector type and its associated element type
8274static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8275 MVT& EVT, MVT& VecEVT) {
8276 // We start with the preferred width, make it a power of 2 and see if
8277 // we can find a vector type of that width. If not, we reduce it by
8278 // another power of 2. If we have widen the type, a vector of bytes should
8279 // always be legal.
8280 assert(TLI.isTypeLegal(VVT));
8281 unsigned EWidth = Width + 1;
8282 do {
8283 assert(EWidth > 0);
8284 EWidth = (1 << Log2_32(EWidth-1));
8285 EVT = MVT::getIntegerVT(EWidth);
8286 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8287 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8288 } while (!TLI.isTypeLegal(VecEVT) ||
8289 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8290}
8291
8292SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8293 SDValue Chain,
8294 SDValue BasePtr,
8295 const Value *SV,
8296 int SVOffset,
8297 unsigned Alignment,
8298 bool isVolatile,
8299 unsigned LdWidth,
8300 MVT ResType) {
8301 // We assume that we have good rules to handle loading power of two loads so
8302 // we break down the operations to power of 2 loads. The strategy is to
8303 // load the largest power of 2 that we can easily transform to a legal vector
8304 // and then insert into that vector, and the cast the result into the legal
8305 // vector that we want. This avoids unnecessary stack converts.
8306 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8307 // the load is nonvolatile, we an use a wider load for the value.
8308 // Find a vector length we can load a large chunk
8309 MVT EVT, VecEVT;
8310 unsigned EVTWidth;
8311 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8312 EVTWidth = EVT.getSizeInBits();
8313
8314 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8315 isVolatile, Alignment);
8316 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8317 LdChain.push_back(LdOp.getValue(1));
8318
8319 // Check if we can load the element with one instruction
8320 if (LdWidth == EVTWidth) {
8321 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8322 }
8323
8324 // The vector element order is endianness dependent.
8325 unsigned Idx = 1;
8326 LdWidth -= EVTWidth;
8327 unsigned Offset = 0;
8328
8329 while (LdWidth > 0) {
8330 unsigned Increment = EVTWidth / 8;
8331 Offset += Increment;
8332 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8333 DAG.getIntPtrConstant(Increment));
8334
8335 if (LdWidth < EVTWidth) {
8336 // Our current type we are using is too large, use a smaller size by
8337 // using a smaller power of 2
8338 unsigned oEVTWidth = EVTWidth;
8339 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8340 EVTWidth = EVT.getSizeInBits();
8341 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008342 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang1448aad2008-10-30 08:01:45 +00008343 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8344 }
8345
8346 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8347 SVOffset+Offset, isVolatile,
8348 MinAlign(Alignment, Offset));
8349 LdChain.push_back(LdOp.getValue(1));
8350 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8351 DAG.getIntPtrConstant(Idx++));
8352
8353 LdWidth -= EVTWidth;
8354 }
8355
8356 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8357}
8358
8359bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8360 SDValue& TFOp,
8361 SDValue Op,
8362 MVT NVT) {
8363 // TODO: Add support for ConcatVec and the ability to load many vector
8364 // types (e.g., v4i8). This will not work when a vector register
8365 // to memory mapping is strange (e.g., vector elements are not
8366 // stored in some sequential order).
8367
8368 // It must be true that the widen vector type is bigger than where
8369 // we need to load from.
8370 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8371 MVT LdVT = LD->getMemoryVT();
8372 assert(LdVT.isVector() && NVT.isVector());
8373 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8374
8375 // Load information
8376 SDValue Chain = LD->getChain();
8377 SDValue BasePtr = LD->getBasePtr();
8378 int SVOffset = LD->getSrcValueOffset();
8379 unsigned Alignment = LD->getAlignment();
8380 bool isVolatile = LD->isVolatile();
8381 const Value *SV = LD->getSrcValue();
8382 unsigned int LdWidth = LdVT.getSizeInBits();
8383
8384 // Load value as a large register
8385 SDValueVector LdChain;
8386 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8387 Alignment, isVolatile, LdWidth, NVT);
8388
8389 if (LdChain.size() == 1) {
8390 TFOp = LdChain[0];
8391 return true;
8392 }
8393 else {
8394 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8395 return false;
8396 }
8397}
8398
8399
8400void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8401 SDValue Chain,
8402 SDValue BasePtr,
8403 const Value *SV,
8404 int SVOffset,
8405 unsigned Alignment,
8406 bool isVolatile,
Mon P Wang257e1c72008-11-15 06:05:52 +00008407 SDValue ValOp,
Mon P Wang1448aad2008-10-30 08:01:45 +00008408 unsigned StWidth) {
8409 // Breaks the stores into a series of power of 2 width stores. For any
8410 // width, we convert the vector to the vector of element size that we
8411 // want to store. This avoids requiring a stack convert.
8412
8413 // Find a width of the element type we can store with
8414 MVT VVT = ValOp.getValueType();
8415 MVT EVT, VecEVT;
8416 unsigned EVTWidth;
8417 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8418 EVTWidth = EVT.getSizeInBits();
8419
8420 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8421 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang927daf52008-11-06 22:52:21 +00008422 DAG.getIntPtrConstant(0));
Mon P Wang1448aad2008-10-30 08:01:45 +00008423 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8424 isVolatile, Alignment);
8425 StChain.push_back(StOp);
8426
8427 // Check if we are done
8428 if (StWidth == EVTWidth) {
8429 return;
8430 }
8431
8432 unsigned Idx = 1;
8433 StWidth -= EVTWidth;
8434 unsigned Offset = 0;
8435
8436 while (StWidth > 0) {
8437 unsigned Increment = EVTWidth / 8;
8438 Offset += Increment;
8439 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8440 DAG.getIntPtrConstant(Increment));
8441
8442 if (StWidth < EVTWidth) {
8443 // Our current type we are using is too large, use a smaller size by
8444 // using a smaller power of 2
8445 unsigned oEVTWidth = EVTWidth;
8446 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8447 EVTWidth = EVT.getSizeInBits();
8448 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008449 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang1448aad2008-10-30 08:01:45 +00008450 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8451 }
8452
8453 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang257e1c72008-11-15 06:05:52 +00008454 DAG.getIntPtrConstant(Idx++));
Mon P Wang1448aad2008-10-30 08:01:45 +00008455 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8456 SVOffset + Offset, isVolatile,
8457 MinAlign(Alignment, Offset)));
8458 StWidth -= EVTWidth;
8459 }
8460}
8461
8462
8463SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8464 SDValue Chain,
8465 SDValue BasePtr) {
8466 // TODO: It might be cleaner if we can use SplitVector and have more legal
8467 // vector types that can be stored into memory (e.g., v4xi8 can
8468 // be stored as a word). This will not work when a vector register
8469 // to memory mapping is strange (e.g., vector elements are not
8470 // stored in some sequential order).
8471
8472 MVT StVT = ST->getMemoryVT();
8473 SDValue ValOp = ST->getValue();
8474
8475 // Check if we have widen this node with another value
8476 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8477 if (I != WidenNodes.end())
8478 ValOp = I->second;
8479
8480 MVT VVT = ValOp.getValueType();
8481
8482 // It must be true that we the widen vector type is bigger than where
8483 // we need to store.
8484 assert(StVT.isVector() && VVT.isVector());
8485 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8486 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8487
8488 // Store value
8489 SDValueVector StChain;
8490 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8491 ST->getSrcValueOffset(), ST->getAlignment(),
8492 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8493 if (StChain.size() == 1)
8494 return StChain[0];
8495 else
8496 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8497}
8498
8499
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008500// SelectionDAG::Legalize - This is the entry point for the file.
8501//
8502void SelectionDAG::Legalize() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008503 /// run - This is the main entry point to this class.
8504 ///
8505 SelectionDAGLegalize(*this).LegalizeDAG();
8506}
8507