blob: 0a10e4f6d81326e2ab9794475a4158e307388e0d [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
Duncan Sands12ddc802008-12-12 08:13:38 +00001955 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
1956 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, Chain, Addr,
1957 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Cheng6fb06762007-11-09 01:32:10 +00001958 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001959 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1960 // For PIC, the sequence is:
1961 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00001962 // RelocBase can be JumpTable, GOT or some sort of global base.
Evan Cheng6fb06762007-11-09 01:32:10 +00001963 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1964 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001965 }
Evan Cheng6fb06762007-11-09 01:32:10 +00001966 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001967 }
1968 }
1969 break;
1970 case ISD::BRCOND:
1971 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1972 // Ensure that libcalls are emitted before a return.
1973 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1974 Tmp1 = LegalizeOp(Tmp1);
1975 LastCALLSEQ_END = DAG.getEntryNode();
1976
1977 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1978 case Expand: assert(0 && "It's impossible to expand bools");
1979 case Legal:
1980 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1981 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00001982 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001983 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1984
1985 // The top bits of the promoted condition are not necessarily zero, ensure
1986 // that the value is properly zero extended.
Dan Gohman07961cd2008-02-25 21:11:39 +00001987 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001988 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman07961cd2008-02-25 21:11:39 +00001989 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001990 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1991 break;
1992 }
Dan Gohman07961cd2008-02-25 21:11:39 +00001993 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001994
1995 // Basic block destination (Op#2) is always legal.
1996 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1997
1998 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1999 default: assert(0 && "This action is not supported yet!");
2000 case TargetLowering::Legal: break;
2001 case TargetLowering::Custom:
2002 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002003 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002004 break;
2005 case TargetLowering::Expand:
2006 // Expand brcond's setcc into its constituent parts and create a BR_CC
2007 // Node.
2008 if (Tmp2.getOpcode() == ISD::SETCC) {
2009 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
2010 Tmp2.getOperand(0), Tmp2.getOperand(1),
2011 Node->getOperand(2));
2012 } else {
2013 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
2014 DAG.getCondCode(ISD::SETNE), Tmp2,
2015 DAG.getConstant(0, Tmp2.getValueType()),
2016 Node->getOperand(2));
2017 }
2018 break;
2019 }
2020 break;
2021 case ISD::BR_CC:
2022 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2023 // Ensure that libcalls are emitted before a branch.
2024 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2025 Tmp1 = LegalizeOp(Tmp1);
2026 Tmp2 = Node->getOperand(2); // LHS
2027 Tmp3 = Node->getOperand(3); // RHS
2028 Tmp4 = Node->getOperand(1); // CC
2029
Dale Johannesen32100b22008-11-07 22:54:33 +00002030 LegalizeSetCC(TLI.getSetCCResultType(Tmp2), Tmp2, Tmp3, Tmp4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002031 LastCALLSEQ_END = DAG.getEntryNode();
2032
Evan Cheng71343822008-10-15 02:05:31 +00002033 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002034 // the LHS is a legal SETCC itself. In this case, we need to compare
2035 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00002036 if (Tmp3.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002037 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2038 Tmp4 = DAG.getCondCode(ISD::SETNE);
2039 }
2040
2041 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2042 Node->getOperand(4));
2043
2044 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2045 default: assert(0 && "Unexpected action for BR_CC!");
2046 case TargetLowering::Legal: break;
2047 case TargetLowering::Custom:
2048 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002049 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002050 break;
2051 }
2052 break;
2053 case ISD::LOAD: {
2054 LoadSDNode *LD = cast<LoadSDNode>(Node);
2055 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2056 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
2057
2058 ISD::LoadExtType ExtType = LD->getExtensionType();
2059 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands92c43912008-06-06 12:08:01 +00002060 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002061 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2062 Tmp3 = Result.getValue(0);
2063 Tmp4 = Result.getValue(1);
2064
2065 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2066 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002067 case TargetLowering::Legal:
2068 // If this is an unaligned load and the target doesn't support it,
2069 // expand it.
2070 if (!TLI.allowsUnalignedMemoryAccesses()) {
2071 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002072 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002073 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002074 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002075 TLI);
2076 Tmp3 = Result.getOperand(0);
2077 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00002078 Tmp3 = LegalizeOp(Tmp3);
2079 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002080 }
2081 }
2082 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002083 case TargetLowering::Custom:
2084 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002085 if (Tmp1.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002086 Tmp3 = LegalizeOp(Tmp1);
2087 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2088 }
2089 break;
2090 case TargetLowering::Promote: {
2091 // Only promote a load of vector type to another.
Duncan Sands92c43912008-06-06 12:08:01 +00002092 assert(VT.isVector() && "Cannot promote this load!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002093 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002094 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002095
2096 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
2097 LD->getSrcValueOffset(),
2098 LD->isVolatile(), LD->getAlignment());
2099 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2100 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2101 break;
2102 }
2103 }
2104 // Since loads produce two values, make sure to remember that we
2105 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002106 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2107 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif46bf5472008-08-26 22:36:50 +00002108 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002109 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00002110 MVT SrcVT = LD->getMemoryVT();
2111 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sands082524c2008-01-23 20:39:46 +00002112 int SVOffset = LD->getSrcValueOffset();
2113 unsigned Alignment = LD->getAlignment();
2114 bool isVolatile = LD->isVolatile();
2115
Duncan Sands92c43912008-06-06 12:08:01 +00002116 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002117 // Some targets pretend to have an i1 loading operation, and actually
2118 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2119 // bits are guaranteed to be zero; it helps the optimizers understand
2120 // that these bits are zero. It is also useful for EXTLOAD, since it
2121 // tells the optimizers that those bits are undefined. It would be
2122 // nice to have an effective generic way of getting these benefits...
2123 // Until such a way is found, don't insist on promoting i1 here.
2124 (SrcVT != MVT::i1 ||
Evan Cheng08c171a2008-10-14 21:26:46 +00002125 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002126 // Promote to a byte-sized load if not loading an integral number of
2127 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands92c43912008-06-06 12:08:01 +00002128 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2129 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002130 SDValue Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002131
2132 // The extra bits are guaranteed to be zero, since we stored them that
2133 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2134
2135 ISD::LoadExtType NewExtType =
2136 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2137
2138 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2139 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2140 NVT, isVolatile, Alignment);
2141
2142 Ch = Result.getValue(1); // The chain.
2143
2144 if (ExtType == ISD::SEXTLOAD)
2145 // Having the top bits zero doesn't help when sign extending.
2146 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2147 Result, DAG.getValueType(SrcVT));
2148 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2149 // All the top bits are guaranteed to be zero - inform the optimizers.
2150 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2151 DAG.getValueType(SrcVT));
2152
2153 Tmp1 = LegalizeOp(Result);
2154 Tmp2 = LegalizeOp(Ch);
2155 } else if (SrcWidth & (SrcWidth - 1)) {
2156 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands92c43912008-06-06 12:08:01 +00002157 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002158 "Unsupported extload!");
2159 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2160 assert(RoundWidth < SrcWidth);
2161 unsigned ExtraWidth = SrcWidth - RoundWidth;
2162 assert(ExtraWidth < RoundWidth);
2163 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2164 "Load size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002165 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2166 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002167 SDValue Lo, Hi, Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002168 unsigned IncrementSize;
2169
2170 if (TLI.isLittleEndian()) {
2171 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2172 // Load the bottom RoundWidth bits.
2173 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2174 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2175 Alignment);
2176
2177 // Load the remaining ExtraWidth bits.
2178 IncrementSize = RoundWidth / 8;
2179 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2180 DAG.getIntPtrConstant(IncrementSize));
2181 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2182 LD->getSrcValue(), SVOffset + IncrementSize,
2183 ExtraVT, isVolatile,
2184 MinAlign(Alignment, IncrementSize));
2185
2186 // Build a factor node to remember that this load is independent of the
2187 // other one.
2188 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2189 Hi.getValue(1));
2190
2191 // Move the top bits to the right place.
2192 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2193 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2194
2195 // Join the hi and lo parts.
2196 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002197 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00002198 // Big endian - avoid unaligned loads.
2199 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2200 // Load the top RoundWidth bits.
2201 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2202 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2203 Alignment);
2204
2205 // Load the remaining ExtraWidth bits.
2206 IncrementSize = RoundWidth / 8;
2207 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2208 DAG.getIntPtrConstant(IncrementSize));
2209 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2210 LD->getSrcValue(), SVOffset + IncrementSize,
2211 ExtraVT, isVolatile,
2212 MinAlign(Alignment, IncrementSize));
2213
2214 // Build a factor node to remember that this load is independent of the
2215 // other one.
2216 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2217 Hi.getValue(1));
2218
2219 // Move the top bits to the right place.
2220 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2221 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2222
2223 // Join the hi and lo parts.
2224 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2225 }
2226
2227 Tmp1 = LegalizeOp(Result);
2228 Tmp2 = LegalizeOp(Ch);
2229 } else {
Evan Cheng08c171a2008-10-14 21:26:46 +00002230 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002231 default: assert(0 && "This action is not supported yet!");
2232 case TargetLowering::Custom:
2233 isCustom = true;
2234 // FALLTHROUGH
2235 case TargetLowering::Legal:
2236 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2237 Tmp1 = Result.getValue(0);
2238 Tmp2 = Result.getValue(1);
2239
2240 if (isCustom) {
2241 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002242 if (Tmp3.getNode()) {
Duncan Sands082524c2008-01-23 20:39:46 +00002243 Tmp1 = LegalizeOp(Tmp3);
2244 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2245 }
2246 } else {
2247 // If this is an unaligned load and the target doesn't support it,
2248 // expand it.
2249 if (!TLI.allowsUnalignedMemoryAccesses()) {
2250 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002251 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sands082524c2008-01-23 20:39:46 +00002252 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002253 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sands082524c2008-01-23 20:39:46 +00002254 TLI);
2255 Tmp1 = Result.getOperand(0);
2256 Tmp2 = Result.getOperand(1);
2257 Tmp1 = LegalizeOp(Tmp1);
2258 Tmp2 = LegalizeOp(Tmp2);
2259 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002260 }
2261 }
Duncan Sands082524c2008-01-23 20:39:46 +00002262 break;
2263 case TargetLowering::Expand:
2264 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2265 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002266 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sands082524c2008-01-23 20:39:46 +00002267 LD->getSrcValueOffset(),
2268 LD->isVolatile(), LD->getAlignment());
2269 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2270 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2271 Tmp2 = LegalizeOp(Load.getValue(1));
2272 break;
2273 }
2274 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2275 // Turn the unsupported load into an EXTLOAD followed by an explicit
2276 // zero/sign extend inreg.
2277 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2278 Tmp1, Tmp2, LD->getSrcValue(),
2279 LD->getSrcValueOffset(), SrcVT,
2280 LD->isVolatile(), LD->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00002281 SDValue ValRes;
Duncan Sands082524c2008-01-23 20:39:46 +00002282 if (ExtType == ISD::SEXTLOAD)
2283 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2284 Result, DAG.getValueType(SrcVT));
2285 else
2286 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2287 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2288 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002289 break;
2290 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002291 }
Duncan Sands082524c2008-01-23 20:39:46 +00002292
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002293 // Since loads produce two values, make sure to remember that we legalized
2294 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002295 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2296 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002297 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002298 }
2299 }
2300 case ISD::EXTRACT_ELEMENT: {
Duncan Sands92c43912008-06-06 12:08:01 +00002301 MVT OpTy = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002302 switch (getTypeAction(OpTy)) {
2303 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2304 case Legal:
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002305 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002306 // 1 -> Hi
2307 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands92c43912008-06-06 12:08:01 +00002308 DAG.getConstant(OpTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002309 TLI.getShiftAmountTy()));
2310 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2311 } else {
2312 // 0 -> Lo
2313 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2314 Node->getOperand(0));
2315 }
2316 break;
2317 case Expand:
2318 // Get both the low and high parts.
2319 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002320 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002321 Result = Tmp2; // 1 -> Hi
2322 else
2323 Result = Tmp1; // 0 -> Lo
2324 break;
2325 }
2326 break;
2327 }
2328
2329 case ISD::CopyToReg:
2330 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2331
2332 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2333 "Register type must be legal!");
2334 // Legalize the incoming value (must be a legal type).
2335 Tmp2 = LegalizeOp(Node->getOperand(2));
2336 if (Node->getNumValues() == 1) {
2337 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2338 } else {
2339 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2340 if (Node->getNumOperands() == 4) {
2341 Tmp3 = LegalizeOp(Node->getOperand(3));
2342 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2343 Tmp3);
2344 } else {
2345 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2346 }
2347
2348 // Since this produces two values, make sure to remember that we legalized
2349 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002350 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2351 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002352 return Result;
2353 }
2354 break;
2355
2356 case ISD::RET:
2357 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2358
2359 // Ensure that libcalls are emitted before a return.
2360 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2361 Tmp1 = LegalizeOp(Tmp1);
2362 LastCALLSEQ_END = DAG.getEntryNode();
2363
2364 switch (Node->getNumOperands()) {
2365 case 3: // ret val
2366 Tmp2 = Node->getOperand(1);
2367 Tmp3 = Node->getOperand(2); // Signness
2368 switch (getTypeAction(Tmp2.getValueType())) {
2369 case Legal:
2370 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2371 break;
2372 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00002373 if (!Tmp2.getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002374 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002375 ExpandOp(Tmp2, Lo, Hi);
2376
2377 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002378 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002379 std::swap(Lo, Hi);
2380
Gabor Greif1c80d112008-08-28 21:40:38 +00002381 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002382 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2383 else
2384 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2385 Result = LegalizeOp(Result);
2386 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +00002387 SDNode *InVal = Tmp2.getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002388 int InIx = Tmp2.getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002389 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2390 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002391
2392 // Figure out if there is a simple type corresponding to this Vector
2393 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002394 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002395 if (TLI.isTypeLegal(TVT)) {
2396 // Turn this into a return of the vector type.
2397 Tmp2 = LegalizeOp(Tmp2);
2398 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2399 } else if (NumElems == 1) {
2400 // Turn this into a return of the scalar type.
2401 Tmp2 = ScalarizeVectorOp(Tmp2);
2402 Tmp2 = LegalizeOp(Tmp2);
2403 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2404
2405 // FIXME: Returns of gcc generic vectors smaller than a legal type
2406 // should be returned in integer registers!
2407
2408 // The scalarized value type may not be legal, e.g. it might require
2409 // promotion or expansion. Relegalize the return.
2410 Result = LegalizeOp(Result);
2411 } else {
2412 // FIXME: Returns of gcc generic vectors larger than a legal vector
2413 // type should be returned by reference!
Dan Gohman8181bd12008-07-27 21:46:04 +00002414 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002415 SplitVectorOp(Tmp2, Lo, Hi);
2416 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2417 Result = LegalizeOp(Result);
2418 }
2419 }
2420 break;
2421 case Promote:
2422 Tmp2 = PromoteOp(Node->getOperand(1));
2423 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2424 Result = LegalizeOp(Result);
2425 break;
2426 }
2427 break;
2428 case 1: // ret void
2429 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2430 break;
2431 default: { // ret <values>
Dan Gohman8181bd12008-07-27 21:46:04 +00002432 SmallVector<SDValue, 8> NewValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002433 NewValues.push_back(Tmp1);
2434 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2435 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2436 case Legal:
2437 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2438 NewValues.push_back(Node->getOperand(i+1));
2439 break;
2440 case Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002441 SDValue Lo, Hi;
Duncan Sands92c43912008-06-06 12:08:01 +00002442 assert(!Node->getOperand(i).getValueType().isExtended() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002443 "FIXME: TODO: implement returning non-legal vector types!");
2444 ExpandOp(Node->getOperand(i), Lo, Hi);
2445 NewValues.push_back(Lo);
2446 NewValues.push_back(Node->getOperand(i+1));
Gabor Greif1c80d112008-08-28 21:40:38 +00002447 if (Hi.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002448 NewValues.push_back(Hi);
2449 NewValues.push_back(Node->getOperand(i+1));
2450 }
2451 break;
2452 }
2453 case Promote:
2454 assert(0 && "Can't promote multiple return value yet!");
2455 }
2456
2457 if (NewValues.size() == Node->getNumOperands())
2458 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2459 else
2460 Result = DAG.getNode(ISD::RET, MVT::Other,
2461 &NewValues[0], NewValues.size());
2462 break;
2463 }
2464 }
2465
2466 if (Result.getOpcode() == ISD::RET) {
2467 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2468 default: assert(0 && "This action is not supported yet!");
2469 case TargetLowering::Legal: break;
2470 case TargetLowering::Custom:
2471 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002472 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002473 break;
2474 }
2475 }
2476 break;
2477 case ISD::STORE: {
2478 StoreSDNode *ST = cast<StoreSDNode>(Node);
2479 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2480 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2481 int SVOffset = ST->getSrcValueOffset();
2482 unsigned Alignment = ST->getAlignment();
2483 bool isVolatile = ST->isVolatile();
2484
2485 if (!ST->isTruncatingStore()) {
2486 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2487 // FIXME: We shouldn't do this for TargetConstantFP's.
2488 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2489 // to phase ordering between legalized code and the dag combiner. This
2490 // probably means that we need to integrate dag combiner and legalizer
2491 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002492 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002493 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002494 if (CFP->getValueType(0) == MVT::f32 &&
2495 getTypeAction(MVT::i32) == Legal) {
Dan Gohman39509762008-03-11 00:11:06 +00002496 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002497 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen1616e902007-09-11 18:32:33 +00002498 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002499 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2500 SVOffset, isVolatile, Alignment);
2501 break;
2502 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002503 // If this target supports 64-bit registers, do a single 64-bit store.
2504 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002505 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman39509762008-03-11 00:11:06 +00002506 zextOrTrunc(64), MVT::i64);
Chris Lattner19f229a2007-10-15 05:46:06 +00002507 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2508 SVOffset, isVolatile, Alignment);
2509 break;
Duncan Sands2418bec2008-06-13 19:07:40 +00002510 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002511 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2512 // stores. If the target supports neither 32- nor 64-bits, this
2513 // xform is certainly not worth it.
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002514 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman8181bd12008-07-27 21:46:04 +00002515 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2516 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002517 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002518
2519 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2520 SVOffset, isVolatile, Alignment);
2521 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002522 DAG.getIntPtrConstant(4));
Chris Lattner19f229a2007-10-15 05:46:06 +00002523 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002524 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002525
2526 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2527 break;
2528 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002529 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002530 }
2531
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002532 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002533 case Legal: {
2534 Tmp3 = LegalizeOp(ST->getValue());
2535 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2536 ST->getOffset());
2537
Duncan Sands92c43912008-06-06 12:08:01 +00002538 MVT VT = Tmp3.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002539 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2540 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002541 case TargetLowering::Legal:
2542 // If this is an unaligned store and the target doesn't support it,
2543 // expand it.
2544 if (!TLI.allowsUnalignedMemoryAccesses()) {
2545 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002546 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002547 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002548 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002549 TLI);
2550 }
2551 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002552 case TargetLowering::Custom:
2553 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002554 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002555 break;
2556 case TargetLowering::Promote:
Duncan Sands92c43912008-06-06 12:08:01 +00002557 assert(VT.isVector() && "Unknown legal promote case!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002558 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2559 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2560 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2561 ST->getSrcValue(), SVOffset, isVolatile,
2562 Alignment);
2563 break;
2564 }
2565 break;
2566 }
2567 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002568 if (!ST->getMemoryVT().isVector()) {
2569 // Truncate the value and store the result.
2570 Tmp3 = PromoteOp(ST->getValue());
2571 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2572 SVOffset, ST->getMemoryVT(),
2573 isVolatile, Alignment);
2574 break;
2575 }
2576 // Fall thru to expand for vector
2577 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002578 unsigned IncrementSize = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002579 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002580
2581 // If this is a vector type, then we have to calculate the increment as
2582 // the product of the element size in bytes, and the number of elements
2583 // in the high half of the vector.
Duncan Sands92c43912008-06-06 12:08:01 +00002584 if (ST->getValue().getValueType().isVector()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002585 SDNode *InVal = ST->getValue().getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002586 int InIx = ST->getValue().getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002587 MVT InVT = InVal->getValueType(InIx);
2588 unsigned NumElems = InVT.getVectorNumElements();
2589 MVT EVT = InVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002590
2591 // Figure out if there is a simple type corresponding to this Vector
2592 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002593 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002594 if (TLI.isTypeLegal(TVT)) {
2595 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002596 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002597 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2598 SVOffset, isVolatile, Alignment);
2599 Result = LegalizeOp(Result);
2600 break;
2601 } else if (NumElems == 1) {
2602 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002603 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002604 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2605 SVOffset, isVolatile, Alignment);
2606 // The scalarized value type may not be legal, e.g. it might require
2607 // promotion or expansion. Relegalize the scalar store.
2608 Result = LegalizeOp(Result);
2609 break;
2610 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +00002611 // Check if we have widen this node with another value
2612 std::map<SDValue, SDValue>::iterator I =
2613 WidenNodes.find(ST->getValue());
2614 if (I != WidenNodes.end()) {
2615 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2616 break;
2617 }
2618 else {
2619 SplitVectorOp(ST->getValue(), Lo, Hi);
2620 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2621 EVT.getSizeInBits()/8;
2622 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002623 }
2624 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002625 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greif1c80d112008-08-28 21:40:38 +00002626 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002627
Richard Pennington73ae9e42008-09-25 16:15:10 +00002628 if (Hi.getNode() && TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002629 std::swap(Lo, Hi);
2630 }
2631
2632 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2633 SVOffset, isVolatile, Alignment);
2634
Gabor Greif1c80d112008-08-28 21:40:38 +00002635 if (Hi.getNode() == NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002636 // Must be int <-> float one-to-one expansion.
2637 Result = Lo;
2638 break;
2639 }
2640
2641 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002642 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002643 assert(isTypeLegal(Tmp2.getValueType()) &&
2644 "Pointers must be legal!");
2645 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002646 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002647 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2648 SVOffset, isVolatile, Alignment);
2649 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2650 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00002651 } // case Expand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002652 }
2653 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002654 switch (getTypeAction(ST->getValue().getValueType())) {
2655 case Legal:
2656 Tmp3 = LegalizeOp(ST->getValue());
2657 break;
2658 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002659 if (!ST->getValue().getValueType().isVector()) {
2660 // We can promote the value, the truncstore will still take care of it.
2661 Tmp3 = PromoteOp(ST->getValue());
2662 break;
2663 }
2664 // Vector case falls through to expand
Chris Lattner3bc08502008-01-17 19:59:44 +00002665 case Expand:
2666 // Just store the low part. This may become a non-trunc store, so make
2667 // sure to use getTruncStore, not UpdateNodeOperands below.
2668 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2669 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2670 SVOffset, MVT::i8, isVolatile, Alignment);
2671 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002672
Duncan Sands92c43912008-06-06 12:08:01 +00002673 MVT StVT = ST->getMemoryVT();
2674 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands40676662008-01-22 07:17:34 +00002675
Duncan Sands92c43912008-06-06 12:08:01 +00002676 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands40676662008-01-22 07:17:34 +00002677 // Promote to a byte-sized store with upper bits zero if not
2678 // storing an integral number of bytes. For example, promote
2679 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands92c43912008-06-06 12:08:01 +00002680 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands40676662008-01-22 07:17:34 +00002681 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2682 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2683 SVOffset, NVT, isVolatile, Alignment);
2684 } else if (StWidth & (StWidth - 1)) {
2685 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands92c43912008-06-06 12:08:01 +00002686 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands40676662008-01-22 07:17:34 +00002687 "Unsupported truncstore!");
2688 unsigned RoundWidth = 1 << Log2_32(StWidth);
2689 assert(RoundWidth < StWidth);
2690 unsigned ExtraWidth = StWidth - RoundWidth;
2691 assert(ExtraWidth < RoundWidth);
2692 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2693 "Store size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002694 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2695 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002696 SDValue Lo, Hi;
Duncan Sands40676662008-01-22 07:17:34 +00002697 unsigned IncrementSize;
2698
2699 if (TLI.isLittleEndian()) {
2700 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2701 // Store the bottom RoundWidth bits.
2702 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2703 SVOffset, RoundVT,
2704 isVolatile, Alignment);
2705
2706 // Store the remaining ExtraWidth bits.
2707 IncrementSize = RoundWidth / 8;
2708 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2709 DAG.getIntPtrConstant(IncrementSize));
2710 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2711 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2712 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2713 SVOffset + IncrementSize, ExtraVT, isVolatile,
2714 MinAlign(Alignment, IncrementSize));
2715 } else {
2716 // Big endian - avoid unaligned stores.
2717 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2718 // Store the top RoundWidth bits.
2719 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2720 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2721 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2722 RoundVT, isVolatile, Alignment);
2723
2724 // Store the remaining ExtraWidth bits.
2725 IncrementSize = RoundWidth / 8;
2726 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2727 DAG.getIntPtrConstant(IncrementSize));
2728 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2729 SVOffset + IncrementSize, ExtraVT, isVolatile,
2730 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002731 }
Duncan Sands40676662008-01-22 07:17:34 +00002732
2733 // The order of the stores doesn't matter.
2734 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2735 } else {
2736 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2737 Tmp2 != ST->getBasePtr())
2738 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2739 ST->getOffset());
2740
2741 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2742 default: assert(0 && "This action is not supported yet!");
2743 case TargetLowering::Legal:
2744 // If this is an unaligned store and the target doesn't support it,
2745 // expand it.
2746 if (!TLI.allowsUnalignedMemoryAccesses()) {
2747 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002748 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands40676662008-01-22 07:17:34 +00002749 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002750 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands40676662008-01-22 07:17:34 +00002751 TLI);
2752 }
2753 break;
2754 case TargetLowering::Custom:
2755 Result = TLI.LowerOperation(Result, DAG);
2756 break;
2757 case Expand:
2758 // TRUNCSTORE:i16 i32 -> STORE i16
2759 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2760 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2761 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2762 isVolatile, Alignment);
2763 break;
2764 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002765 }
2766 }
2767 break;
2768 }
2769 case ISD::PCMARKER:
2770 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2771 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2772 break;
2773 case ISD::STACKSAVE:
2774 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2775 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2776 Tmp1 = Result.getValue(0);
2777 Tmp2 = Result.getValue(1);
2778
2779 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2780 default: assert(0 && "This action is not supported yet!");
2781 case TargetLowering::Legal: break;
2782 case TargetLowering::Custom:
2783 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002784 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002785 Tmp1 = LegalizeOp(Tmp3);
2786 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2787 }
2788 break;
2789 case TargetLowering::Expand:
2790 // Expand to CopyFromReg if the target set
2791 // StackPointerRegisterToSaveRestore.
2792 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2793 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2794 Node->getValueType(0));
2795 Tmp2 = Tmp1.getValue(1);
2796 } else {
2797 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2798 Tmp2 = Node->getOperand(0);
2799 }
2800 break;
2801 }
2802
2803 // Since stacksave produce two values, make sure to remember that we
2804 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002805 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2806 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002807 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002808
2809 case ISD::STACKRESTORE:
2810 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2811 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2812 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2813
2814 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2815 default: assert(0 && "This action is not supported yet!");
2816 case TargetLowering::Legal: break;
2817 case TargetLowering::Custom:
2818 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002819 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002820 break;
2821 case TargetLowering::Expand:
2822 // Expand to CopyToReg if the target set
2823 // StackPointerRegisterToSaveRestore.
2824 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2825 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2826 } else {
2827 Result = Tmp1;
2828 }
2829 break;
2830 }
2831 break;
2832
2833 case ISD::READCYCLECOUNTER:
2834 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2835 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2836 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2837 Node->getValueType(0))) {
2838 default: assert(0 && "This action is not supported yet!");
2839 case TargetLowering::Legal:
2840 Tmp1 = Result.getValue(0);
2841 Tmp2 = Result.getValue(1);
2842 break;
2843 case TargetLowering::Custom:
2844 Result = TLI.LowerOperation(Result, DAG);
2845 Tmp1 = LegalizeOp(Result.getValue(0));
2846 Tmp2 = LegalizeOp(Result.getValue(1));
2847 break;
2848 }
2849
2850 // Since rdcc produce two values, make sure to remember that we legalized
2851 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002852 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2853 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002854 return Result;
2855
2856 case ISD::SELECT:
2857 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2858 case Expand: assert(0 && "It's impossible to expand bools");
2859 case Legal:
2860 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2861 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002862 case Promote: {
Mon P Wang1448aad2008-10-30 08:01:45 +00002863 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002864 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2865 // Make sure the condition is either zero or one.
Dan Gohman07961cd2008-02-25 21:11:39 +00002866 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002867 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman07961cd2008-02-25 21:11:39 +00002868 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002869 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2870 break;
2871 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002872 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002873 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2874 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2875
2876 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2877
2878 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2879 default: assert(0 && "This action is not supported yet!");
2880 case TargetLowering::Legal: break;
2881 case TargetLowering::Custom: {
2882 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002883 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002884 break;
2885 }
2886 case TargetLowering::Expand:
2887 if (Tmp1.getOpcode() == ISD::SETCC) {
2888 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2889 Tmp2, Tmp3,
2890 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2891 } else {
2892 Result = DAG.getSelectCC(Tmp1,
2893 DAG.getConstant(0, Tmp1.getValueType()),
2894 Tmp2, Tmp3, ISD::SETNE);
2895 }
2896 break;
2897 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00002898 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002899 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2900 unsigned ExtOp, TruncOp;
Duncan Sands92c43912008-06-06 12:08:01 +00002901 if (Tmp2.getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002902 ExtOp = ISD::BIT_CONVERT;
2903 TruncOp = ISD::BIT_CONVERT;
Duncan Sands92c43912008-06-06 12:08:01 +00002904 } else if (Tmp2.getValueType().isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002905 ExtOp = ISD::ANY_EXTEND;
2906 TruncOp = ISD::TRUNCATE;
2907 } else {
2908 ExtOp = ISD::FP_EXTEND;
2909 TruncOp = ISD::FP_ROUND;
2910 }
2911 // Promote each of the values to the new type.
2912 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2913 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2914 // Perform the larger operation, then round down.
2915 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00002916 if (TruncOp != ISD::FP_ROUND)
2917 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2918 else
2919 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2920 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002921 break;
2922 }
2923 }
2924 break;
2925 case ISD::SELECT_CC: {
2926 Tmp1 = Node->getOperand(0); // LHS
2927 Tmp2 = Node->getOperand(1); // RHS
2928 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2929 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman8181bd12008-07-27 21:46:04 +00002930 SDValue CC = Node->getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002931
Dale Johannesen32100b22008-11-07 22:54:33 +00002932 LegalizeSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2, CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002933
Evan Cheng71343822008-10-15 02:05:31 +00002934 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002935 // the LHS is a legal SETCC itself. In this case, we need to compare
2936 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00002937 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002938 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2939 CC = DAG.getCondCode(ISD::SETNE);
2940 }
2941 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2942
2943 // Everything is legal, see if we should expand this op or something.
2944 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2945 default: assert(0 && "This action is not supported yet!");
2946 case TargetLowering::Legal: break;
2947 case TargetLowering::Custom:
2948 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002949 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002950 break;
2951 }
2952 break;
2953 }
2954 case ISD::SETCC:
2955 Tmp1 = Node->getOperand(0);
2956 Tmp2 = Node->getOperand(1);
2957 Tmp3 = Node->getOperand(2);
Evan Cheng71343822008-10-15 02:05:31 +00002958 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002959
2960 // If we had to Expand the SetCC operands into a SELECT node, then it may
2961 // not always be possible to return a true LHS & RHS. In this case, just
2962 // return the value we legalized, returned in the LHS
Gabor Greif1c80d112008-08-28 21:40:38 +00002963 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002964 Result = Tmp1;
2965 break;
2966 }
2967
2968 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
2969 default: assert(0 && "Cannot handle this action for SETCC yet!");
2970 case TargetLowering::Custom:
2971 isCustom = true;
2972 // FALLTHROUGH.
2973 case TargetLowering::Legal:
2974 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2975 if (isCustom) {
2976 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002977 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002978 }
2979 break;
2980 case TargetLowering::Promote: {
2981 // First step, figure out the appropriate operation to use.
2982 // Allow SETCC to not be supported for all legal data types
2983 // Mostly this targets FP
Duncan Sands92c43912008-06-06 12:08:01 +00002984 MVT NewInTy = Node->getOperand(0).getValueType();
2985 MVT OldVT = NewInTy; OldVT = OldVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002986
2987 // Scan for the appropriate larger type to use.
2988 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00002989 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002990
Duncan Sands92c43912008-06-06 12:08:01 +00002991 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002992 "Fell off of the edge of the integer world");
Duncan Sands92c43912008-06-06 12:08:01 +00002993 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002994 "Fell off of the edge of the floating point world");
2995
2996 // If the target supports SETCC of this type, use it.
2997 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
2998 break;
2999 }
Duncan Sands92c43912008-06-06 12:08:01 +00003000 if (NewInTy.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003001 assert(0 && "Cannot promote Legal Integer SETCC yet");
3002 else {
3003 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
3004 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
3005 }
3006 Tmp1 = LegalizeOp(Tmp1);
3007 Tmp2 = LegalizeOp(Tmp2);
3008 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3009 Result = LegalizeOp(Result);
3010 break;
3011 }
3012 case TargetLowering::Expand:
3013 // Expand a setcc node into a select_cc of the same condition, lhs, and
3014 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands92c43912008-06-06 12:08:01 +00003015 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003016 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3017 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3018 Tmp3);
3019 break;
3020 }
3021 break;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003022 case ISD::VSETCC: {
3023 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3024 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman8181bd12008-07-27 21:46:04 +00003025 SDValue CC = Node->getOperand(2);
Nate Begeman9a1ce152008-05-12 19:40:03 +00003026
3027 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3028
3029 // Everything is legal, see if we should expand this op or something.
3030 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3031 default: assert(0 && "This action is not supported yet!");
3032 case TargetLowering::Legal: break;
3033 case TargetLowering::Custom:
3034 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003035 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003036 break;
3037 }
3038 break;
3039 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003040
3041 case ISD::SHL_PARTS:
3042 case ISD::SRA_PARTS:
3043 case ISD::SRL_PARTS: {
Dan Gohman8181bd12008-07-27 21:46:04 +00003044 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003045 bool Changed = false;
3046 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3047 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3048 Changed |= Ops.back() != Node->getOperand(i);
3049 }
3050 if (Changed)
3051 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
3052
3053 switch (TLI.getOperationAction(Node->getOpcode(),
3054 Node->getValueType(0))) {
3055 default: assert(0 && "This action is not supported yet!");
3056 case TargetLowering::Legal: break;
3057 case TargetLowering::Custom:
3058 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003059 if (Tmp1.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003060 SDValue Tmp2, RetVal(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003061 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
3062 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman8181bd12008-07-27 21:46:04 +00003063 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00003064 if (i == Op.getResNo())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065 RetVal = Tmp2;
3066 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003067 assert(RetVal.getNode() && "Illegal result number");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003068 return RetVal;
3069 }
3070 break;
3071 }
3072
3073 // Since these produce multiple values, make sure to remember that we
3074 // legalized all of them.
3075 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman8181bd12008-07-27 21:46:04 +00003076 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00003077 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003078 }
3079
3080 // Binary operators
3081 case ISD::ADD:
3082 case ISD::SUB:
3083 case ISD::MUL:
3084 case ISD::MULHS:
3085 case ISD::MULHU:
3086 case ISD::UDIV:
3087 case ISD::SDIV:
3088 case ISD::AND:
3089 case ISD::OR:
3090 case ISD::XOR:
3091 case ISD::SHL:
3092 case ISD::SRL:
3093 case ISD::SRA:
3094 case ISD::FADD:
3095 case ISD::FSUB:
3096 case ISD::FMUL:
3097 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00003098 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003099 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3100 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3101 case Expand: assert(0 && "Not possible");
3102 case Legal:
3103 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3104 break;
3105 case Promote:
3106 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3107 break;
3108 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003109
3110 if ((Node->getOpcode() == ISD::SHL ||
3111 Node->getOpcode() == ISD::SRL ||
3112 Node->getOpcode() == ISD::SRA) &&
3113 !Node->getValueType(0).isVector()) {
Mon P Wang9901e732008-12-09 05:46:39 +00003114 Tmp2 = LegalizeShiftAmount(Tmp2);
3115 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003116
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003117 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangbff5d9c2008-11-10 04:46:22 +00003118
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003119 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3120 default: assert(0 && "BinOp legalize operation not supported");
3121 case TargetLowering::Legal: break;
3122 case TargetLowering::Custom:
3123 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003124 if (Tmp1.getNode()) {
Nate Begemanbb1ce942008-07-29 15:49:41 +00003125 Result = Tmp1;
3126 break;
Nate Begeman7569e762008-07-29 19:07:27 +00003127 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003128 // Fall through if the custom lower can't deal with the operation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003129 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00003130 MVT VT = Op.getValueType();
Mon P Wang1448aad2008-10-30 08:01:45 +00003131
Dan Gohman5a199552007-10-08 18:33:35 +00003132 // See if multiply or divide can be lowered using two-result operations.
3133 SDVTList VTs = DAG.getVTList(VT, VT);
3134 if (Node->getOpcode() == ISD::MUL) {
3135 // We just need the low half of the multiply; try both the signed
3136 // and unsigned forms. If the target supports both SMUL_LOHI and
3137 // UMUL_LOHI, form a preference by checking which forms of plain
3138 // MULH it supports.
3139 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3140 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3141 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3142 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3143 unsigned OpToUse = 0;
3144 if (HasSMUL_LOHI && !HasMULHS) {
3145 OpToUse = ISD::SMUL_LOHI;
3146 } else if (HasUMUL_LOHI && !HasMULHU) {
3147 OpToUse = ISD::UMUL_LOHI;
3148 } else if (HasSMUL_LOHI) {
3149 OpToUse = ISD::SMUL_LOHI;
3150 } else if (HasUMUL_LOHI) {
3151 OpToUse = ISD::UMUL_LOHI;
3152 }
3153 if (OpToUse) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003154 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003155 break;
3156 }
3157 }
3158 if (Node->getOpcode() == ISD::MULHS &&
3159 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003160 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3161 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003162 break;
3163 }
3164 if (Node->getOpcode() == ISD::MULHU &&
3165 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003166 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3167 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003168 break;
3169 }
3170 if (Node->getOpcode() == ISD::SDIV &&
3171 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003172 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3173 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003174 break;
3175 }
3176 if (Node->getOpcode() == ISD::UDIV &&
3177 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003178 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3179 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003180 break;
3181 }
Mon P Wang1448aad2008-10-30 08:01:45 +00003182
Dan Gohman6d05cac2007-10-11 23:57:53 +00003183 // Check to see if we have a libcall for this operator.
3184 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3185 bool isSigned = false;
3186 switch (Node->getOpcode()) {
3187 case ISD::UDIV:
3188 case ISD::SDIV:
3189 if (VT == MVT::i32) {
3190 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang1448aad2008-10-30 08:01:45 +00003191 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003192 isSigned = Node->getOpcode() == ISD::SDIV;
3193 }
3194 break;
Chris Lattner48188652008-10-04 21:27:46 +00003195 case ISD::MUL:
3196 if (VT == MVT::i32)
3197 LC = RTLIB::MUL_I32;
3198 break;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003199 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00003200 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3201 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003202 break;
3203 default: break;
3204 }
3205 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003206 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003207 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003208 break;
3209 }
Mon P Wang1448aad2008-10-30 08:01:45 +00003210
Duncan Sands92c43912008-06-06 12:08:01 +00003211 assert(Node->getValueType(0).isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003212 "Cannot expand this binary operator!");
3213 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003214 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003215 break;
3216 }
3217 case TargetLowering::Promote: {
3218 switch (Node->getOpcode()) {
3219 default: assert(0 && "Do not know how to promote this BinOp!");
3220 case ISD::AND:
3221 case ISD::OR:
3222 case ISD::XOR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003223 MVT OVT = Node->getValueType(0);
3224 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3225 assert(OVT.isVector() && "Cannot promote this BinOp!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003226 // Bit convert each of the values to the new type.
3227 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3228 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3229 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3230 // Bit convert the result back the original type.
3231 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3232 break;
3233 }
3234 }
3235 }
3236 }
3237 break;
3238
Dan Gohman475cd732007-10-05 14:17:22 +00003239 case ISD::SMUL_LOHI:
3240 case ISD::UMUL_LOHI:
3241 case ISD::SDIVREM:
3242 case ISD::UDIVREM:
3243 // These nodes will only be produced by target-specific lowering, so
3244 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003245 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003246 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003247
3248 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3249 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3250 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003251 break;
3252
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003253 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3254 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3255 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3256 case Expand: assert(0 && "Not possible");
3257 case Legal:
3258 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3259 break;
3260 case Promote:
3261 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3262 break;
3263 }
3264
3265 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3266
3267 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3268 default: assert(0 && "Operation not supported");
3269 case TargetLowering::Custom:
3270 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003271 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003272 break;
3273 case TargetLowering::Legal: break;
3274 case TargetLowering::Expand: {
3275 // If this target supports fabs/fneg natively and select is cheap,
3276 // do this efficiently.
3277 if (!TLI.isSelectExpensive() &&
3278 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3279 TargetLowering::Legal &&
3280 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3281 TargetLowering::Legal) {
3282 // Get the sign bit of the RHS.
Duncan Sands92c43912008-06-06 12:08:01 +00003283 MVT IVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003284 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman8181bd12008-07-27 21:46:04 +00003285 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel502151f2008-03-10 15:42:14 +00003286 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003287 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3288 // Get the absolute value of the result.
Dan Gohman8181bd12008-07-27 21:46:04 +00003289 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003290 // Select between the nabs and abs value based on the sign bit of
3291 // the input.
3292 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3293 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3294 AbsVal),
3295 AbsVal);
3296 Result = LegalizeOp(Result);
3297 break;
3298 }
3299
3300 // Otherwise, do bitwise ops!
Duncan Sands92c43912008-06-06 12:08:01 +00003301 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003302 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3303 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3304 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3305 Result = LegalizeOp(Result);
3306 break;
3307 }
3308 }
3309 break;
3310
3311 case ISD::ADDC:
3312 case ISD::SUBC:
3313 Tmp1 = LegalizeOp(Node->getOperand(0));
3314 Tmp2 = LegalizeOp(Node->getOperand(1));
3315 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003316 Tmp3 = Result.getValue(0);
3317 Tmp4 = Result.getValue(1);
3318
3319 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3320 default: assert(0 && "This action is not supported yet!");
3321 case TargetLowering::Legal:
3322 break;
3323 case TargetLowering::Custom:
3324 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3325 if (Tmp1.getNode() != NULL) {
Sanjiv Guptad57f2e12008-11-27 05:58:04 +00003326 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003327 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3328 }
3329 break;
3330 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003331 // Since this produces two values, make sure to remember that we legalized
3332 // both of them.
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003333 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3334 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3335 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003336
3337 case ISD::ADDE:
3338 case ISD::SUBE:
3339 Tmp1 = LegalizeOp(Node->getOperand(0));
3340 Tmp2 = LegalizeOp(Node->getOperand(1));
3341 Tmp3 = LegalizeOp(Node->getOperand(2));
3342 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003343 Tmp3 = Result.getValue(0);
3344 Tmp4 = Result.getValue(1);
3345
3346 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3347 default: assert(0 && "This action is not supported yet!");
3348 case TargetLowering::Legal:
3349 break;
3350 case TargetLowering::Custom:
3351 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3352 if (Tmp1.getNode() != NULL) {
Sanjiv Guptad57f2e12008-11-27 05:58:04 +00003353 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003354 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3355 }
3356 break;
3357 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003358 // Since this produces two values, make sure to remember that we legalized
3359 // both of them.
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003360 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3361 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3362 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003363
3364 case ISD::BUILD_PAIR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003365 MVT PairTy = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003366 // TODO: handle the case where the Lo and Hi operands are not of legal type
3367 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3368 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3369 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3370 case TargetLowering::Promote:
3371 case TargetLowering::Custom:
3372 assert(0 && "Cannot promote/custom this yet!");
3373 case TargetLowering::Legal:
3374 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3375 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3376 break;
3377 case TargetLowering::Expand:
3378 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3379 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3380 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003381 DAG.getConstant(PairTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003382 TLI.getShiftAmountTy()));
3383 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3384 break;
3385 }
3386 break;
3387 }
3388
3389 case ISD::UREM:
3390 case ISD::SREM:
3391 case ISD::FREM:
3392 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3393 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3394
3395 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3396 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3397 case TargetLowering::Custom:
3398 isCustom = true;
3399 // FALLTHROUGH
3400 case TargetLowering::Legal:
3401 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3402 if (isCustom) {
3403 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003404 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003405 }
3406 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003407 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003408 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3409 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands92c43912008-06-06 12:08:01 +00003410 MVT VT = Node->getValueType(0);
Dan Gohman5a199552007-10-08 18:33:35 +00003411
3412 // See if remainder can be lowered using two-result operations.
3413 SDVTList VTs = DAG.getVTList(VT, VT);
3414 if (Node->getOpcode() == ISD::SREM &&
3415 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003416 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003417 break;
3418 }
3419 if (Node->getOpcode() == ISD::UREM &&
3420 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003421 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003422 break;
3423 }
3424
Duncan Sands92c43912008-06-06 12:08:01 +00003425 if (VT.isInteger()) {
Dan Gohman5a199552007-10-08 18:33:35 +00003426 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003427 TargetLowering::Legal) {
3428 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003429 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3430 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3431 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands92c43912008-06-06 12:08:01 +00003432 } else if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003433 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003434 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003435 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003436 "Cannot expand this binary operator!");
3437 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3438 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman8181bd12008-07-27 21:46:04 +00003439 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003440 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003441 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003442 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00003443 assert(VT.isFloatingPoint() &&
Dan Gohman59b4b102007-11-06 22:11:54 +00003444 "remainder op must have integer or floating-point type");
Duncan Sands92c43912008-06-06 12:08:01 +00003445 if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003446 Result = LegalizeOp(UnrollVectorOp(Op));
3447 } else {
3448 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003449 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3450 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003451 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003452 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003453 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003454 }
3455 break;
3456 }
Dan Gohman5a199552007-10-08 18:33:35 +00003457 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003458 break;
3459 case ISD::VAARG: {
3460 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3461 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3462
Duncan Sands92c43912008-06-06 12:08:01 +00003463 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003464 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3465 default: assert(0 && "This action is not supported yet!");
3466 case TargetLowering::Custom:
3467 isCustom = true;
3468 // FALLTHROUGH
3469 case TargetLowering::Legal:
3470 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3471 Result = Result.getValue(0);
3472 Tmp1 = Result.getValue(1);
3473
3474 if (isCustom) {
3475 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003476 if (Tmp2.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003477 Result = LegalizeOp(Tmp2);
3478 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3479 }
3480 }
3481 break;
3482 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003483 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003484 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003485 // Increment the pointer, VAList, to the next vaarg
Duncan Sands55a4c232008-11-03 11:51:11 +00003486 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3487 DAG.getConstant(TLI.getTargetData()->getABITypeSize(VT.getTypeForMVT()),
3488 TLI.getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003489 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00003490 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003491 // Load the actual argument out of the pointer VAList
3492 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3493 Tmp1 = LegalizeOp(Result.getValue(1));
3494 Result = LegalizeOp(Result);
3495 break;
3496 }
3497 }
3498 // Since VAARG produces two values, make sure to remember that we
3499 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003500 AddLegalizedOperand(SDValue(Node, 0), Result);
3501 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00003502 return Op.getResNo() ? Tmp1 : Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003503 }
3504
3505 case ISD::VACOPY:
3506 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3507 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3508 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3509
3510 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3511 default: assert(0 && "This action is not supported yet!");
3512 case TargetLowering::Custom:
3513 isCustom = true;
3514 // FALLTHROUGH
3515 case TargetLowering::Legal:
3516 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3517 Node->getOperand(3), Node->getOperand(4));
3518 if (isCustom) {
3519 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003520 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003521 }
3522 break;
3523 case TargetLowering::Expand:
3524 // This defaults to loading a pointer from the input and storing it to the
3525 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003526 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3527 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman6b9a08e2008-04-17 02:09:26 +00003528 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3529 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003530 break;
3531 }
3532 break;
3533
3534 case ISD::VAEND:
3535 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3536 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3537
3538 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3539 default: assert(0 && "This action is not supported yet!");
3540 case TargetLowering::Custom:
3541 isCustom = true;
3542 // FALLTHROUGH
3543 case TargetLowering::Legal:
3544 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3545 if (isCustom) {
3546 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003547 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003548 }
3549 break;
3550 case TargetLowering::Expand:
3551 Result = Tmp1; // Default to a no-op, return the chain
3552 break;
3553 }
3554 break;
3555
3556 case ISD::VASTART:
3557 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3558 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3559
3560 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3561
3562 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3563 default: assert(0 && "This action is not supported yet!");
3564 case TargetLowering::Legal: break;
3565 case TargetLowering::Custom:
3566 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003567 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003568 break;
3569 }
3570 break;
3571
3572 case ISD::ROTL:
3573 case ISD::ROTR:
3574 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3575 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3576 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3577 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3578 default:
3579 assert(0 && "ROTL/ROTR legalize operation not supported");
3580 break;
3581 case TargetLowering::Legal:
3582 break;
3583 case TargetLowering::Custom:
3584 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003585 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003586 break;
3587 case TargetLowering::Promote:
3588 assert(0 && "Do not know how to promote ROTL/ROTR");
3589 break;
3590 case TargetLowering::Expand:
3591 assert(0 && "Do not know how to expand ROTL/ROTR");
3592 break;
3593 }
3594 break;
3595
3596 case ISD::BSWAP:
3597 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3598 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3599 case TargetLowering::Custom:
3600 assert(0 && "Cannot custom legalize this yet!");
3601 case TargetLowering::Legal:
3602 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3603 break;
3604 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003605 MVT OVT = Tmp1.getValueType();
3606 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3607 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003608
3609 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3610 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3611 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3612 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3613 break;
3614 }
3615 case TargetLowering::Expand:
3616 Result = ExpandBSWAP(Tmp1);
3617 break;
3618 }
3619 break;
3620
3621 case ISD::CTPOP:
3622 case ISD::CTTZ:
3623 case ISD::CTLZ:
3624 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3625 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003626 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003627 case TargetLowering::Legal:
3628 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003629 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003630 TargetLowering::Custom) {
3631 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003632 if (Tmp1.getNode()) {
Scott Michelbc62b412007-08-02 02:22:46 +00003633 Result = Tmp1;
3634 }
Scott Michel48b63e62007-07-30 21:00:31 +00003635 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003636 break;
3637 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003638 MVT OVT = Tmp1.getValueType();
3639 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003640
3641 // Zero extend the argument.
3642 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3643 // Perform the larger operation, then subtract if needed.
3644 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3645 switch (Node->getOpcode()) {
3646 case ISD::CTPOP:
3647 Result = Tmp1;
3648 break;
3649 case ISD::CTTZ:
3650 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00003651 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003652 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003653 ISD::SETEQ);
3654 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003655 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003656 break;
3657 case ISD::CTLZ:
3658 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3659 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003660 DAG.getConstant(NVT.getSizeInBits() -
3661 OVT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003662 break;
3663 }
3664 break;
3665 }
3666 case TargetLowering::Expand:
3667 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3668 break;
3669 }
3670 break;
3671
3672 // Unary operators
3673 case ISD::FABS:
3674 case ISD::FNEG:
3675 case ISD::FSQRT:
3676 case ISD::FSIN:
3677 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003678 case ISD::FLOG:
3679 case ISD::FLOG2:
3680 case ISD::FLOG10:
3681 case ISD::FEXP:
3682 case ISD::FEXP2:
Dan Gohmanc8b20e22008-08-21 17:55:02 +00003683 case ISD::FTRUNC:
3684 case ISD::FFLOOR:
3685 case ISD::FCEIL:
3686 case ISD::FRINT:
3687 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003688 Tmp1 = LegalizeOp(Node->getOperand(0));
3689 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3690 case TargetLowering::Promote:
3691 case TargetLowering::Custom:
3692 isCustom = true;
3693 // FALLTHROUGH
3694 case TargetLowering::Legal:
3695 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3696 if (isCustom) {
3697 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003698 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003699 }
3700 break;
3701 case TargetLowering::Expand:
3702 switch (Node->getOpcode()) {
3703 default: assert(0 && "Unreachable!");
3704 case ISD::FNEG:
3705 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3706 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3707 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3708 break;
3709 case ISD::FABS: {
3710 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands92c43912008-06-06 12:08:01 +00003711 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003712 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel502151f2008-03-10 15:42:14 +00003713 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00003714 ISD::SETUGT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003715 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3716 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3717 break;
3718 }
Evan Cheng1fac6952008-09-09 23:35:53 +00003719 case ISD::FSQRT:
3720 case ISD::FSIN:
3721 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003722 case ISD::FLOG:
3723 case ISD::FLOG2:
3724 case ISD::FLOG10:
3725 case ISD::FEXP:
3726 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00003727 case ISD::FTRUNC:
3728 case ISD::FFLOOR:
3729 case ISD::FCEIL:
3730 case ISD::FRINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00003731 case ISD::FNEARBYINT: {
Duncan Sands92c43912008-06-06 12:08:01 +00003732 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003733
3734 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003735 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003736 Result = LegalizeOp(UnrollVectorOp(Op));
3737 break;
3738 }
3739
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003740 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3741 switch(Node->getOpcode()) {
3742 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003743 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3744 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003745 break;
3746 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003747 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3748 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003749 break;
3750 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003751 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3752 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003753 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00003754 case ISD::FLOG:
3755 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3756 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3757 break;
3758 case ISD::FLOG2:
3759 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3760 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3761 break;
3762 case ISD::FLOG10:
3763 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3764 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3765 break;
3766 case ISD::FEXP:
3767 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3768 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3769 break;
3770 case ISD::FEXP2:
3771 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3772 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3773 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00003774 case ISD::FTRUNC:
3775 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3776 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3777 break;
3778 case ISD::FFLOOR:
3779 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3780 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3781 break;
3782 case ISD::FCEIL:
3783 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3784 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3785 break;
3786 case ISD::FRINT:
3787 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3788 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3789 break;
3790 case ISD::FNEARBYINT:
3791 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3792 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3793 break;
Evan Cheng1fac6952008-09-09 23:35:53 +00003794 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003795 default: assert(0 && "Unreachable!");
3796 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003797 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003798 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003799 break;
3800 }
3801 }
3802 break;
3803 }
3804 break;
3805 case ISD::FPOWI: {
Duncan Sands92c43912008-06-06 12:08:01 +00003806 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003807
3808 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003809 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003810 Result = LegalizeOp(UnrollVectorOp(Op));
3811 break;
3812 }
3813
3814 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003815 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3816 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003817 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003818 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003819 break;
3820 }
3821 case ISD::BIT_CONVERT:
3822 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003823 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3824 Node->getValueType(0));
Duncan Sands92c43912008-06-06 12:08:01 +00003825 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003826 // The input has to be a vector type, we have to either scalarize it, pack
3827 // it, or convert it based on whether the input vector type is legal.
Gabor Greif1c80d112008-08-28 21:40:38 +00003828 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00003829 int InIx = Node->getOperand(0).getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00003830 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3831 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003832
3833 // Figure out if there is a simple type corresponding to this Vector
3834 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00003835 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003836 if (TLI.isTypeLegal(TVT)) {
3837 // Turn this into a bit convert of the vector input.
3838 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3839 LegalizeOp(Node->getOperand(0)));
3840 break;
3841 } else if (NumElems == 1) {
3842 // Turn this into a bit convert of the scalar input.
3843 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3844 ScalarizeVectorOp(Node->getOperand(0)));
3845 break;
3846 } else {
3847 // FIXME: UNIMP! Store then reload
3848 assert(0 && "Cast from unsupported vector type not implemented yet!");
3849 }
3850 } else {
3851 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3852 Node->getOperand(0).getValueType())) {
3853 default: assert(0 && "Unknown operation action!");
3854 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003855 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3856 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003857 break;
3858 case TargetLowering::Legal:
3859 Tmp1 = LegalizeOp(Node->getOperand(0));
3860 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3861 break;
3862 }
3863 }
3864 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003865 case ISD::CONVERT_RNDSAT: {
3866 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3867 switch (CvtCode) {
3868 default: assert(0 && "Unknown cvt code!");
3869 case ISD::CVT_SF:
3870 case ISD::CVT_UF:
Mon P Wang73d31542008-11-10 20:54:11 +00003871 case ISD::CVT_FF:
Mon P Wang2fc3f9e2008-12-09 07:27:39 +00003872 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003873 case ISD::CVT_FS:
3874 case ISD::CVT_FU:
3875 case ISD::CVT_SS:
3876 case ISD::CVT_SU:
3877 case ISD::CVT_US:
3878 case ISD::CVT_UU: {
3879 SDValue DTyOp = Node->getOperand(1);
3880 SDValue STyOp = Node->getOperand(2);
3881 SDValue RndOp = Node->getOperand(3);
3882 SDValue SatOp = Node->getOperand(4);
3883 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3884 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3885 case Legal:
3886 Tmp1 = LegalizeOp(Node->getOperand(0));
3887 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3888 RndOp, SatOp);
3889 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3890 TargetLowering::Custom) {
3891 Tmp1 = TLI.LowerOperation(Result, DAG);
3892 if (Tmp1.getNode()) Result = Tmp1;
3893 }
3894 break;
3895 case Promote:
3896 Result = PromoteOp(Node->getOperand(0));
3897 // For FP, make Op1 a i32
3898
Mon P Wang2fc3f9e2008-12-09 07:27:39 +00003899 Result = DAG.getConvertRndSat(Op.getValueType(), Result,
Mon P Wang73d31542008-11-10 20:54:11 +00003900 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3901 break;
3902 }
3903 break;
3904 }
3905 } // end switch CvtCode
3906 break;
3907 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003908 // Conversion operators. The source and destination have different types.
3909 case ISD::SINT_TO_FP:
3910 case ISD::UINT_TO_FP: {
3911 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman29c3cef2008-08-14 20:04:46 +00003912 Result = LegalizeINT_TO_FP(Result, isSigned,
3913 Node->getValueType(0), Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003914 break;
3915 }
3916 case ISD::TRUNCATE:
3917 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3918 case Legal:
3919 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michele9b8a402008-12-02 19:55:08 +00003920 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3921 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
3922 case TargetLowering::Custom:
Mon P Wang72fe5462008-12-11 00:44:22 +00003923 isCustom = true;
3924 // FALLTHROUGH
Scott Michele9b8a402008-12-02 19:55:08 +00003925 case TargetLowering::Legal:
Mon P Wang72fe5462008-12-11 00:44:22 +00003926 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3927 if (isCustom) {
3928 Tmp1 = TLI.LowerOperation(Result, DAG);
3929 if (Tmp1.getNode()) Result = Tmp1;
3930 }
3931 break;
Mon P Wang83edba52008-12-12 01:25:51 +00003932 case TargetLowering::Expand:
3933 assert(Result.getValueType().isVector() && "must be vector type");
3934 // Unroll the truncate. We should do better.
3935 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Schellerbfc55ee2008-12-02 12:12:25 +00003936 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003937 break;
3938 case Expand:
3939 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3940
3941 // Since the result is legal, we should just be able to truncate the low
3942 // part of the source.
3943 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3944 break;
3945 case Promote:
3946 Result = PromoteOp(Node->getOperand(0));
3947 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3948 break;
3949 }
3950 break;
3951
3952 case ISD::FP_TO_SINT:
3953 case ISD::FP_TO_UINT:
3954 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3955 case Legal:
3956 Tmp1 = LegalizeOp(Node->getOperand(0));
3957
3958 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3959 default: assert(0 && "Unknown operation action!");
3960 case TargetLowering::Custom:
3961 isCustom = true;
3962 // FALLTHROUGH
3963 case TargetLowering::Legal:
3964 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3965 if (isCustom) {
3966 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003967 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003968 }
3969 break;
3970 case TargetLowering::Promote:
3971 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3972 Node->getOpcode() == ISD::FP_TO_SINT);
3973 break;
3974 case TargetLowering::Expand:
3975 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003976 SDValue True, False;
Duncan Sands92c43912008-06-06 12:08:01 +00003977 MVT VT = Node->getOperand(0).getValueType();
3978 MVT NVT = Node->getValueType(0);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003979 const uint64_t zero[] = {0, 0};
Duncan Sands92c43912008-06-06 12:08:01 +00003980 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3981 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohman88ae8c52008-02-29 01:44:25 +00003982 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003983 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel502151f2008-03-10 15:42:14 +00003984 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003985 Node->getOperand(0), Tmp2, ISD::SETLT);
3986 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3987 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
3988 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
3989 Tmp2));
3990 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman88ae8c52008-02-29 01:44:25 +00003991 DAG.getConstant(x, NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003992 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3993 break;
3994 } else {
3995 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3996 }
3997 break;
3998 }
3999 break;
4000 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00004001 MVT VT = Op.getValueType();
4002 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00004003 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004004 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00004005 if (Node->getOpcode() == ISD::FP_TO_SINT) {
4006 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
4007 Node->getOperand(0), DAG.getValueType(MVT::f64));
4008 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
4009 DAG.getIntPtrConstant(1));
4010 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
4011 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00004012 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4013 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4014 Tmp2 = DAG.getConstantFP(apf, OVT);
4015 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4016 // FIXME: generated code sucks.
4017 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
4018 DAG.getNode(ISD::ADD, MVT::i32,
4019 DAG.getNode(ISD::FP_TO_SINT, VT,
4020 DAG.getNode(ISD::FSUB, OVT,
4021 Node->getOperand(0), Tmp2)),
4022 DAG.getConstant(0x80000000, MVT::i32)),
4023 DAG.getNode(ISD::FP_TO_SINT, VT,
4024 Node->getOperand(0)),
4025 DAG.getCondCode(ISD::SETGE));
4026 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004027 break;
4028 }
Dan Gohmanec51f642008-03-10 23:03:31 +00004029 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsf68dffb2008-07-17 02:36:29 +00004030 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4031 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4032 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman8181bd12008-07-27 21:46:04 +00004033 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00004034 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004035 break;
4036 }
4037 case Promote:
4038 Tmp1 = PromoteOp(Node->getOperand(0));
4039 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4040 Result = LegalizeOp(Result);
4041 break;
4042 }
4043 break;
4044
Chris Lattner56ecde32008-01-16 06:57:07 +00004045 case ISD::FP_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004046 MVT DstVT = Op.getValueType();
4047 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004048 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4049 // The only other way we can lower this is to turn it into a STORE,
4050 // LOAD pair, targetting a temporary location (a stack slot).
4051 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
4052 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00004053 }
4054 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4055 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4056 case Legal:
4057 Tmp1 = LegalizeOp(Node->getOperand(0));
4058 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4059 break;
4060 case Promote:
4061 Tmp1 = PromoteOp(Node->getOperand(0));
4062 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4063 break;
4064 }
4065 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004066 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004067 case ISD::FP_ROUND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004068 MVT DstVT = Op.getValueType();
4069 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004070 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4071 if (SrcVT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004072 SDValue Lo;
Dale Johannesena0d36082008-01-20 01:18:38 +00004073 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00004074 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00004075 if (DstVT!=MVT::f64)
4076 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00004077 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004078 }
Chris Lattner5872a362008-01-17 07:00:52 +00004079 // The only other way we can lower this is to turn it into a STORE,
4080 // LOAD pair, targetting a temporary location (a stack slot).
4081 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4082 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004083 }
Chris Lattner56ecde32008-01-16 06:57:07 +00004084 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4085 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4086 case Legal:
4087 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004088 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004089 break;
4090 case Promote:
4091 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004092 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4093 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004094 break;
4095 }
4096 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004097 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004098 case ISD::ANY_EXTEND:
4099 case ISD::ZERO_EXTEND:
4100 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004101 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4102 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4103 case Legal:
4104 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac54d002008-04-30 00:26:38 +00004105 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michelac7091c2008-02-15 23:05:48 +00004106 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4107 TargetLowering::Custom) {
Scott Michelac54d002008-04-30 00:26:38 +00004108 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004109 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelac7091c2008-02-15 23:05:48 +00004110 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004111 break;
4112 case Promote:
4113 switch (Node->getOpcode()) {
4114 case ISD::ANY_EXTEND:
4115 Tmp1 = PromoteOp(Node->getOperand(0));
4116 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
4117 break;
4118 case ISD::ZERO_EXTEND:
4119 Result = PromoteOp(Node->getOperand(0));
4120 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4121 Result = DAG.getZeroExtendInReg(Result,
4122 Node->getOperand(0).getValueType());
4123 break;
4124 case ISD::SIGN_EXTEND:
4125 Result = PromoteOp(Node->getOperand(0));
4126 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4127 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4128 Result,
4129 DAG.getValueType(Node->getOperand(0).getValueType()));
4130 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004131 }
4132 }
4133 break;
4134 case ISD::FP_ROUND_INREG:
4135 case ISD::SIGN_EXTEND_INREG: {
4136 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004137 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004138
4139 // If this operation is not supported, convert it to a shl/shr or load/store
4140 // pair.
4141 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4142 default: assert(0 && "This action not supported for this op yet!");
4143 case TargetLowering::Legal:
4144 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
4145 break;
4146 case TargetLowering::Expand:
4147 // If this is an integer extend and shifts are supported, do that.
4148 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
4149 // NOTE: we could fall back on load/store here too for targets without
4150 // SAR. However, it is doubtful that any exist.
Duncan Sands92c43912008-06-06 12:08:01 +00004151 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4152 ExtraVT.getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00004153 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004154 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4155 Node->getOperand(0), ShiftCst);
4156 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4157 Result, ShiftCst);
4158 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
4159 // The only way we can lower this is to turn it into a TRUNCSTORE,
4160 // EXTLOAD pair, targetting a temporary location (a stack slot).
4161
4162 // NOTE: there is a choice here between constantly creating new stack
4163 // slots and always reusing the same one. We currently always create
4164 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00004165 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4166 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004167 } else {
4168 assert(0 && "Unknown op");
4169 }
4170 break;
4171 }
4172 break;
4173 }
Duncan Sands38947cd2007-07-27 12:58:54 +00004174 case ISD::TRAMPOLINE: {
Dan Gohman8181bd12008-07-27 21:46:04 +00004175 SDValue Ops[6];
Duncan Sands38947cd2007-07-27 12:58:54 +00004176 for (unsigned i = 0; i != 6; ++i)
4177 Ops[i] = LegalizeOp(Node->getOperand(i));
4178 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4179 // The only option for this node is to custom lower it.
4180 Result = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004181 assert(Result.getNode() && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00004182
4183 // Since trampoline produces two values, make sure to remember that we
4184 // legalized both of them.
4185 Tmp1 = LegalizeOp(Result.getValue(1));
4186 Result = LegalizeOp(Result);
Dan Gohman8181bd12008-07-27 21:46:04 +00004187 AddLegalizedOperand(SDValue(Node, 0), Result);
4188 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00004189 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00004190 }
Dan Gohmane8e4a412008-05-14 00:43:10 +00004191 case ISD::FLT_ROUNDS_: {
Duncan Sands92c43912008-06-06 12:08:01 +00004192 MVT VT = Node->getValueType(0);
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004193 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4194 default: assert(0 && "This action not supported for this op yet!");
4195 case TargetLowering::Custom:
4196 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004197 if (Result.getNode()) break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004198 // Fall Thru
4199 case TargetLowering::Legal:
4200 // If this operation is not supported, lower it to constant 1
4201 Result = DAG.getConstant(1, VT);
4202 break;
4203 }
Dan Gohmane09dc8c2008-05-12 16:07:15 +00004204 break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004205 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004206 case ISD::TRAP: {
Duncan Sands92c43912008-06-06 12:08:01 +00004207 MVT VT = Node->getValueType(0);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004208 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4209 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00004210 case TargetLowering::Legal:
4211 Tmp1 = LegalizeOp(Node->getOperand(0));
4212 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4213 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004214 case TargetLowering::Custom:
4215 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004216 if (Result.getNode()) break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004217 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00004218 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004219 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00004220 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004221 TargetLowering::ArgListTy Args;
Dan Gohman8181bd12008-07-27 21:46:04 +00004222 std::pair<SDValue,SDValue> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004223 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00004224 false, false, false, false, CallingConv::C, false,
Bill Wendlingfef06052008-09-16 21:48:12 +00004225 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner88e03932008-01-15 22:09:33 +00004226 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004227 Result = CallResult.second;
4228 break;
4229 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004230 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004231 }
Bill Wendling913dcf32008-11-22 00:22:52 +00004232
Bill Wendling7e04be62008-12-09 22:08:41 +00004233 case ISD::SADDO:
4234 case ISD::SSUBO: {
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004235 MVT VT = Node->getValueType(0);
4236 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4237 default: assert(0 && "This action not supported for this op yet!");
4238 case TargetLowering::Custom:
4239 Result = TLI.LowerOperation(Op, DAG);
4240 if (Result.getNode()) break;
4241 // FALLTHROUGH
4242 case TargetLowering::Legal: {
4243 SDValue LHS = LegalizeOp(Node->getOperand(0));
4244 SDValue RHS = LegalizeOp(Node->getOperand(1));
4245
Bill Wendling7e04be62008-12-09 22:08:41 +00004246 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
4247 ISD::ADD : ISD::SUB, LHS.getValueType(),
4248 LHS, RHS);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004249 MVT OType = Node->getValueType(1);
4250
Bill Wendlingc65e6e42008-11-25 08:19:22 +00004251 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004252
Bill Wendlingcf4de122008-11-25 19:40:17 +00004253 // LHSSign -> LHS >= 0
4254 // RHSSign -> RHS >= 0
4255 // SumSign -> Sum >= 0
4256 //
Bill Wendling7e04be62008-12-09 22:08:41 +00004257 // Add:
Bill Wendlingcf4de122008-11-25 19:40:17 +00004258 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling7e04be62008-12-09 22:08:41 +00004259 // Sub:
4260 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendlingcf4de122008-11-25 19:40:17 +00004261 //
4262 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4263 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
Bill Wendling7e04be62008-12-09 22:08:41 +00004264 SDValue SignsMatch = DAG.getSetCC(OType, LHSSign, RHSSign,
4265 Node->getOpcode() == ISD::SADDO ?
4266 ISD::SETEQ : ISD::SETNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004267
Bill Wendlingcf4de122008-11-25 19:40:17 +00004268 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4269 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004270
Bill Wendling7e04be62008-12-09 22:08:41 +00004271 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsMatch, SumSignNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004272
4273 MVT ValueVTs[] = { LHS.getValueType(), OType };
4274 SDValue Ops[] = { Sum, Cmp };
4275
Duncan Sands42d7bb82008-12-01 11:41:29 +00004276 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4277 &Ops[0], 2);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004278 SDNode *RNode = Result.getNode();
4279 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4280 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4281 break;
4282 }
4283 }
4284
4285 break;
4286 }
Bill Wendling7e04be62008-12-09 22:08:41 +00004287 case ISD::UADDO:
4288 case ISD::USUBO: {
Bill Wendling4c134df2008-11-24 19:21:46 +00004289 MVT VT = Node->getValueType(0);
4290 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4291 default: assert(0 && "This action not supported for this op yet!");
4292 case TargetLowering::Custom:
4293 Result = TLI.LowerOperation(Op, DAG);
4294 if (Result.getNode()) break;
4295 // FALLTHROUGH
4296 case TargetLowering::Legal: {
4297 SDValue LHS = LegalizeOp(Node->getOperand(0));
4298 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling913dcf32008-11-22 00:22:52 +00004299
Bill Wendling7e04be62008-12-09 22:08:41 +00004300 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
4301 ISD::ADD : ISD::SUB, LHS.getValueType(),
4302 LHS, RHS);
Bill Wendling4c134df2008-11-24 19:21:46 +00004303 MVT OType = Node->getValueType(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00004304 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS,
4305 Node->getOpcode () == ISD::UADDO ?
4306 ISD::SETULT : ISD::SETUGT);
Bill Wendling913dcf32008-11-22 00:22:52 +00004307
Bill Wendling4c134df2008-11-24 19:21:46 +00004308 MVT ValueVTs[] = { LHS.getValueType(), OType };
4309 SDValue Ops[] = { Sum, Cmp };
Bill Wendling913dcf32008-11-22 00:22:52 +00004310
Duncan Sands42d7bb82008-12-01 11:41:29 +00004311 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4312 &Ops[0], 2);
Bill Wendling4c134df2008-11-24 19:21:46 +00004313 SDNode *RNode = Result.getNode();
4314 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4315 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4316 break;
4317 }
4318 }
4319
Bill Wendling913dcf32008-11-22 00:22:52 +00004320 break;
4321 }
Bill Wendling7e04be62008-12-09 22:08:41 +00004322 case ISD::SMULO:
4323 case ISD::UMULO: {
4324 MVT VT = Node->getValueType(0);
4325 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4326 default: assert(0 && "This action is not supported at all!");
4327 case TargetLowering::Custom:
4328 Result = TLI.LowerOperation(Op, DAG);
4329 if (Result.getNode()) break;
4330 // Fall Thru
4331 case TargetLowering::Legal:
4332 // FIXME: According to Hacker's Delight, this can be implemented in
4333 // target independent lowering, but it would be inefficient, since it
Bill Wendling35f1a9d2008-12-10 02:01:32 +00004334 // requires a division + a branch.
Bill Wendling7e04be62008-12-09 22:08:41 +00004335 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4336 break;
4337 }
4338 break;
4339 }
4340
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004341 }
4342
4343 assert(Result.getValueType() == Op.getValueType() &&
4344 "Bad legalization!");
4345
4346 // Make sure that the generated code is itself legal.
4347 if (Result != Op)
4348 Result = LegalizeOp(Result);
4349
4350 // Note that LegalizeOp may be reentered even from single-use nodes, which
4351 // means that we always must cache transformed nodes.
4352 AddLegalizedOperand(Op, Result);
4353 return Result;
4354}
4355
4356/// PromoteOp - Given an operation that produces a value in an invalid type,
4357/// promote it to compute the value into a larger type. The produced value will
4358/// have the correct bits for the low portion of the register, but no guarantee
4359/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +00004360SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00004361 MVT VT = Op.getValueType();
4362 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004363 assert(getTypeAction(VT) == Promote &&
4364 "Caller should expand or legalize operands that are not promotable!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004365 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004366 "Cannot promote to smaller type!");
4367
Dan Gohman8181bd12008-07-27 21:46:04 +00004368 SDValue Tmp1, Tmp2, Tmp3;
4369 SDValue Result;
Gabor Greif1c80d112008-08-28 21:40:38 +00004370 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004371
Dan Gohman8181bd12008-07-27 21:46:04 +00004372 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004373 if (I != PromotedNodes.end()) return I->second;
4374
4375 switch (Node->getOpcode()) {
4376 case ISD::CopyFromReg:
4377 assert(0 && "CopyFromReg must be legal!");
4378 default:
4379#ifndef NDEBUG
4380 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4381#endif
4382 assert(0 && "Do not know how to promote this operator!");
4383 abort();
4384 case ISD::UNDEF:
4385 Result = DAG.getNode(ISD::UNDEF, NVT);
4386 break;
4387 case ISD::Constant:
4388 if (VT != MVT::i1)
4389 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4390 else
4391 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
4392 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4393 break;
4394 case ISD::ConstantFP:
4395 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4396 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4397 break;
4398
4399 case ISD::SETCC:
Scott Michel502151f2008-03-10 15:42:14 +00004400 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004401 && "SetCC type is not legal??");
Scott Michel502151f2008-03-10 15:42:14 +00004402 Result = DAG.getNode(ISD::SETCC,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004403 TLI.getSetCCResultType(Node->getOperand(0)),
4404 Node->getOperand(0), Node->getOperand(1),
4405 Node->getOperand(2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004406 break;
4407
4408 case ISD::TRUNCATE:
4409 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4410 case Legal:
4411 Result = LegalizeOp(Node->getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00004412 assert(Result.getValueType().bitsGE(NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004413 "This truncation doesn't make sense!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004414 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004415 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4416 break;
4417 case Promote:
4418 // The truncation is not required, because we don't guarantee anything
4419 // about high bits anyway.
4420 Result = PromoteOp(Node->getOperand(0));
4421 break;
4422 case Expand:
4423 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4424 // Truncate the low part of the expanded value to the result type
4425 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4426 }
4427 break;
4428 case ISD::SIGN_EXTEND:
4429 case ISD::ZERO_EXTEND:
4430 case ISD::ANY_EXTEND:
4431 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4432 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4433 case Legal:
4434 // Input is legal? Just do extend all the way to the larger type.
4435 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4436 break;
4437 case Promote:
4438 // Promote the reg if it's smaller.
4439 Result = PromoteOp(Node->getOperand(0));
4440 // The high bits are not guaranteed to be anything. Insert an extend.
4441 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4442 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4443 DAG.getValueType(Node->getOperand(0).getValueType()));
4444 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4445 Result = DAG.getZeroExtendInReg(Result,
4446 Node->getOperand(0).getValueType());
4447 break;
4448 }
4449 break;
Mon P Wang73d31542008-11-10 20:54:11 +00004450 case ISD::CONVERT_RNDSAT: {
4451 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4452 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4453 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4454 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4455 "can only promote integers");
4456 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4457 Node->getOperand(1), Node->getOperand(2),
4458 Node->getOperand(3), Node->getOperand(4),
4459 CvtCode);
4460 break;
4461
4462 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004463 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004464 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4465 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004466 Result = PromoteOp(Result);
4467 break;
4468
4469 case ISD::FP_EXTEND:
4470 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4471 case ISD::FP_ROUND:
4472 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4473 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4474 case Promote: assert(0 && "Unreachable with 2 FP types!");
4475 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004476 if (Node->getConstantOperandVal(1) == 0) {
4477 // Input is legal? Do an FP_ROUND_INREG.
4478 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4479 DAG.getValueType(VT));
4480 } else {
4481 // Just remove the truncate, it isn't affecting the value.
4482 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4483 Node->getOperand(1));
4484 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485 break;
4486 }
4487 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004488 case ISD::SINT_TO_FP:
4489 case ISD::UINT_TO_FP:
4490 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4491 case Legal:
4492 // No extra round required here.
4493 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4494 break;
4495
4496 case Promote:
4497 Result = PromoteOp(Node->getOperand(0));
4498 if (Node->getOpcode() == ISD::SINT_TO_FP)
4499 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4500 Result,
4501 DAG.getValueType(Node->getOperand(0).getValueType()));
4502 else
4503 Result = DAG.getZeroExtendInReg(Result,
4504 Node->getOperand(0).getValueType());
4505 // No extra round required here.
4506 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4507 break;
4508 case Expand:
4509 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4510 Node->getOperand(0));
4511 // Round if we cannot tolerate excess precision.
4512 if (NoExcessFPPrecision)
4513 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4514 DAG.getValueType(VT));
4515 break;
4516 }
4517 break;
4518
4519 case ISD::SIGN_EXTEND_INREG:
4520 Result = PromoteOp(Node->getOperand(0));
4521 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4522 Node->getOperand(1));
4523 break;
4524 case ISD::FP_TO_SINT:
4525 case ISD::FP_TO_UINT:
4526 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4527 case Legal:
4528 case Expand:
4529 Tmp1 = Node->getOperand(0);
4530 break;
4531 case Promote:
4532 // The input result is prerounded, so we don't have to do anything
4533 // special.
4534 Tmp1 = PromoteOp(Node->getOperand(0));
4535 break;
4536 }
4537 // If we're promoting a UINT to a larger size, check to see if the new node
4538 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4539 // we can use that instead. This allows us to generate better code for
4540 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4541 // legal, such as PowerPC.
4542 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4543 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4544 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4545 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4546 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4547 } else {
4548 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4549 }
4550 break;
4551
4552 case ISD::FABS:
4553 case ISD::FNEG:
4554 Tmp1 = PromoteOp(Node->getOperand(0));
4555 assert(Tmp1.getValueType() == NVT);
4556 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4557 // NOTE: we do not have to do any extra rounding here for
4558 // NoExcessFPPrecision, because we know the input will have the appropriate
4559 // precision, and these operations don't modify precision at all.
4560 break;
4561
Dale Johannesen92b33082008-09-04 00:47:13 +00004562 case ISD::FLOG:
4563 case ISD::FLOG2:
4564 case ISD::FLOG10:
4565 case ISD::FEXP:
4566 case ISD::FEXP2:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004567 case ISD::FSQRT:
4568 case ISD::FSIN:
4569 case ISD::FCOS:
Dan Gohmanb2158232008-08-21 18:38:14 +00004570 case ISD::FTRUNC:
4571 case ISD::FFLOOR:
4572 case ISD::FCEIL:
4573 case ISD::FRINT:
4574 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004575 Tmp1 = PromoteOp(Node->getOperand(0));
4576 assert(Tmp1.getValueType() == NVT);
4577 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4578 if (NoExcessFPPrecision)
4579 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4580 DAG.getValueType(VT));
4581 break;
4582
Evan Cheng1fac6952008-09-09 23:35:53 +00004583 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004584 case ISD::FPOWI: {
Evan Cheng1fac6952008-09-09 23:35:53 +00004585 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004586 // directly as well, which may be better.
4587 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng1fac6952008-09-09 23:35:53 +00004588 Tmp2 = Node->getOperand(1);
4589 if (Node->getOpcode() == ISD::FPOW)
4590 Tmp2 = PromoteOp(Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004591 assert(Tmp1.getValueType() == NVT);
Evan Cheng1fac6952008-09-09 23:35:53 +00004592 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004593 if (NoExcessFPPrecision)
4594 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4595 DAG.getValueType(VT));
4596 break;
4597 }
4598
Dale Johannesenbc187662008-08-28 02:44:49 +00004599 case ISD::ATOMIC_CMP_SWAP_8:
4600 case ISD::ATOMIC_CMP_SWAP_16:
4601 case ISD::ATOMIC_CMP_SWAP_32:
4602 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004603 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004604 Tmp2 = PromoteOp(Node->getOperand(2));
4605 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004606 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4607 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004608 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004609 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004610 // Remember that we legalized the chain.
4611 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4612 break;
4613 }
Dale Johannesenbc187662008-08-28 02:44:49 +00004614 case ISD::ATOMIC_LOAD_ADD_8:
4615 case ISD::ATOMIC_LOAD_SUB_8:
4616 case ISD::ATOMIC_LOAD_AND_8:
4617 case ISD::ATOMIC_LOAD_OR_8:
4618 case ISD::ATOMIC_LOAD_XOR_8:
4619 case ISD::ATOMIC_LOAD_NAND_8:
4620 case ISD::ATOMIC_LOAD_MIN_8:
4621 case ISD::ATOMIC_LOAD_MAX_8:
4622 case ISD::ATOMIC_LOAD_UMIN_8:
4623 case ISD::ATOMIC_LOAD_UMAX_8:
4624 case ISD::ATOMIC_SWAP_8:
4625 case ISD::ATOMIC_LOAD_ADD_16:
4626 case ISD::ATOMIC_LOAD_SUB_16:
4627 case ISD::ATOMIC_LOAD_AND_16:
4628 case ISD::ATOMIC_LOAD_OR_16:
4629 case ISD::ATOMIC_LOAD_XOR_16:
4630 case ISD::ATOMIC_LOAD_NAND_16:
4631 case ISD::ATOMIC_LOAD_MIN_16:
4632 case ISD::ATOMIC_LOAD_MAX_16:
4633 case ISD::ATOMIC_LOAD_UMIN_16:
4634 case ISD::ATOMIC_LOAD_UMAX_16:
4635 case ISD::ATOMIC_SWAP_16:
4636 case ISD::ATOMIC_LOAD_ADD_32:
4637 case ISD::ATOMIC_LOAD_SUB_32:
4638 case ISD::ATOMIC_LOAD_AND_32:
4639 case ISD::ATOMIC_LOAD_OR_32:
4640 case ISD::ATOMIC_LOAD_XOR_32:
4641 case ISD::ATOMIC_LOAD_NAND_32:
4642 case ISD::ATOMIC_LOAD_MIN_32:
4643 case ISD::ATOMIC_LOAD_MAX_32:
4644 case ISD::ATOMIC_LOAD_UMIN_32:
4645 case ISD::ATOMIC_LOAD_UMAX_32:
4646 case ISD::ATOMIC_SWAP_32:
4647 case ISD::ATOMIC_LOAD_ADD_64:
4648 case ISD::ATOMIC_LOAD_SUB_64:
4649 case ISD::ATOMIC_LOAD_AND_64:
4650 case ISD::ATOMIC_LOAD_OR_64:
4651 case ISD::ATOMIC_LOAD_XOR_64:
4652 case ISD::ATOMIC_LOAD_NAND_64:
4653 case ISD::ATOMIC_LOAD_MIN_64:
4654 case ISD::ATOMIC_LOAD_MAX_64:
4655 case ISD::ATOMIC_LOAD_UMIN_64:
4656 case ISD::ATOMIC_LOAD_UMAX_64:
4657 case ISD::ATOMIC_SWAP_64: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004658 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004659 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004660 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4661 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004662 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004663 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004664 // Remember that we legalized the chain.
4665 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4666 break;
4667 }
4668
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004669 case ISD::AND:
4670 case ISD::OR:
4671 case ISD::XOR:
4672 case ISD::ADD:
4673 case ISD::SUB:
4674 case ISD::MUL:
4675 // The input may have strange things in the top bits of the registers, but
4676 // these operations don't care. They may have weird bits going out, but
4677 // that too is okay if they are integer operations.
4678 Tmp1 = PromoteOp(Node->getOperand(0));
4679 Tmp2 = PromoteOp(Node->getOperand(1));
4680 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4681 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4682 break;
4683 case ISD::FADD:
4684 case ISD::FSUB:
4685 case ISD::FMUL:
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
4691 // Floating point operations will give excess precision that we may not be
4692 // able to tolerate. If we DO allow excess precision, just leave it,
4693 // otherwise excise it.
4694 // FIXME: Why would we need to round FP ops more than integer ones?
4695 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4696 if (NoExcessFPPrecision)
4697 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4698 DAG.getValueType(VT));
4699 break;
4700
4701 case ISD::SDIV:
4702 case ISD::SREM:
4703 // These operators require that their input be sign extended.
4704 Tmp1 = PromoteOp(Node->getOperand(0));
4705 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004706 if (NVT.isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004707 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4708 DAG.getValueType(VT));
4709 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4710 DAG.getValueType(VT));
4711 }
4712 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4713
4714 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands92c43912008-06-06 12:08:01 +00004715 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004716 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4717 DAG.getValueType(VT));
4718 break;
4719 case ISD::FDIV:
4720 case ISD::FREM:
4721 case ISD::FCOPYSIGN:
4722 // These operators require that their input be fp extended.
4723 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004724 case Expand: assert(0 && "not implemented");
4725 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4726 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004727 }
4728 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004729 case Expand: assert(0 && "not implemented");
4730 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4731 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004732 }
4733 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4734
4735 // Perform FP_ROUND: this is probably overly pessimistic.
4736 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4737 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4738 DAG.getValueType(VT));
4739 break;
4740
4741 case ISD::UDIV:
4742 case ISD::UREM:
4743 // These operators require that their input be zero extended.
4744 Tmp1 = PromoteOp(Node->getOperand(0));
4745 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004746 assert(NVT.isInteger() && "Operators don't apply to FP!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004747 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4748 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4749 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4750 break;
4751
4752 case ISD::SHL:
4753 Tmp1 = PromoteOp(Node->getOperand(0));
4754 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4755 break;
4756 case ISD::SRA:
4757 // The input value must be properly sign extended.
4758 Tmp1 = PromoteOp(Node->getOperand(0));
4759 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4760 DAG.getValueType(VT));
4761 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4762 break;
4763 case ISD::SRL:
4764 // The input value must be properly zero extended.
4765 Tmp1 = PromoteOp(Node->getOperand(0));
4766 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4767 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4768 break;
4769
4770 case ISD::VAARG:
4771 Tmp1 = Node->getOperand(0); // Get the chain.
4772 Tmp2 = Node->getOperand(1); // Get the pointer.
4773 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4774 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sandsac496a12008-07-04 11:47:58 +00004775 Result = TLI.LowerOperation(Tmp3, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004776 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004777 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00004778 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004779 // Increment the pointer, VAList, to the next vaarg
4780 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands92c43912008-06-06 12:08:01 +00004781 DAG.getConstant(VT.getSizeInBits()/8,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004782 TLI.getPointerTy()));
4783 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00004784 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004785 // Load the actual argument out of the pointer VAList
4786 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4787 }
4788 // Remember that we legalized the chain.
4789 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4790 break;
4791
4792 case ISD::LOAD: {
4793 LoadSDNode *LD = cast<LoadSDNode>(Node);
4794 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4795 ? ISD::EXTLOAD : LD->getExtensionType();
4796 Result = DAG.getExtLoad(ExtType, NVT,
4797 LD->getChain(), LD->getBasePtr(),
4798 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004799 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004800 LD->isVolatile(),
4801 LD->getAlignment());
4802 // Remember that we legalized the chain.
4803 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4804 break;
4805 }
Scott Michel67224b22008-06-02 22:18:03 +00004806 case ISD::SELECT: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004807 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4808 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel67224b22008-06-02 22:18:03 +00004809
Duncan Sands92c43912008-06-06 12:08:01 +00004810 MVT VT2 = Tmp2.getValueType();
Scott Michel67224b22008-06-02 22:18:03 +00004811 assert(VT2 == Tmp3.getValueType()
Scott Michel7b54de02008-06-03 19:13:20 +00004812 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4813 // Ensure that the resulting node is at least the same size as the operands'
4814 // value types, because we cannot assume that TLI.getSetCCValueType() is
4815 // constant.
4816 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004817 break;
Scott Michel67224b22008-06-02 22:18:03 +00004818 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004819 case ISD::SELECT_CC:
4820 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4821 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4822 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4823 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4824 break;
4825 case ISD::BSWAP:
4826 Tmp1 = Node->getOperand(0);
4827 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4828 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4829 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004830 DAG.getConstant(NVT.getSizeInBits() -
4831 VT.getSizeInBits(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004832 TLI.getShiftAmountTy()));
4833 break;
4834 case ISD::CTPOP:
4835 case ISD::CTTZ:
4836 case ISD::CTLZ:
4837 // Zero extend the argument
4838 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4839 // Perform the larger operation, then subtract if needed.
4840 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4841 switch(Node->getOpcode()) {
4842 case ISD::CTPOP:
4843 Result = Tmp1;
4844 break;
4845 case ISD::CTTZ:
4846 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00004847 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004848 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004849 ISD::SETEQ);
4850 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00004851 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004852 break;
4853 case ISD::CTLZ:
4854 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4855 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004856 DAG.getConstant(NVT.getSizeInBits() -
4857 VT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004858 break;
4859 }
4860 break;
4861 case ISD::EXTRACT_SUBVECTOR:
4862 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4863 break;
4864 case ISD::EXTRACT_VECTOR_ELT:
4865 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4866 break;
4867 }
4868
Gabor Greif1c80d112008-08-28 21:40:38 +00004869 assert(Result.getNode() && "Didn't set a result!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004870
4871 // Make sure the result is itself legal.
4872 Result = LegalizeOp(Result);
4873
4874 // Remember that we promoted this!
4875 AddPromotedOperand(Op, Result);
4876 return Result;
4877}
4878
4879/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4880/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4881/// based on the vector type. The return type of this matches the element type
4882/// of the vector, which may not be legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00004883SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004884 // We know that operand #0 is the Vec vector. If the index is a constant
4885 // or if the invec is a supported hardware type, we can use it. Otherwise,
4886 // lower to a store then an indexed load.
Dan Gohman8181bd12008-07-27 21:46:04 +00004887 SDValue Vec = Op.getOperand(0);
4888 SDValue Idx = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004889
Duncan Sands92c43912008-06-06 12:08:01 +00004890 MVT TVT = Vec.getValueType();
4891 unsigned NumElems = TVT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004892
4893 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4894 default: assert(0 && "This action is not supported yet!");
4895 case TargetLowering::Custom: {
4896 Vec = LegalizeOp(Vec);
4897 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004898 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004899 if (Tmp3.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004900 return Tmp3;
4901 break;
4902 }
4903 case TargetLowering::Legal:
4904 if (isTypeLegal(TVT)) {
4905 Vec = LegalizeOp(Vec);
4906 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004907 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004908 }
4909 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00004910 case TargetLowering::Promote:
4911 assert(TVT.isVector() && "not vector type");
4912 // fall thru to expand since vectors are by default are promote
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004913 case TargetLowering::Expand:
4914 break;
4915 }
4916
4917 if (NumElems == 1) {
4918 // This must be an access of the only element. Return it.
4919 Op = ScalarizeVectorOp(Vec);
4920 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00004921 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004922 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004923 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004924 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004925 if (CIdx->getZExtValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004926 Vec = Lo;
4927 } else {
4928 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004929 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004930 Idx.getValueType());
4931 }
4932
4933 // It's now an extract from the appropriate high or low part. Recurse.
4934 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4935 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4936 } else {
4937 // Store the value to a temporary stack slot, then LOAD the scalar
4938 // element back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00004939 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4940 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004941
4942 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +00004943 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004944 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4945 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004946
Duncan Sandsec142ee2008-06-08 20:54:56 +00004947 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00004948 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004949 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00004950 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004951
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004952 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4953
4954 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
4955 }
4956 return Op;
4957}
4958
4959/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
4960/// we assume the operation can be split if it is not already legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00004961SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004962 // We know that operand #0 is the Vec vector. For now we assume the index
4963 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman8181bd12008-07-27 21:46:04 +00004964 SDValue Vec = Op.getOperand(0);
4965 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004966
Duncan Sands92c43912008-06-06 12:08:01 +00004967 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004968
Duncan Sands92c43912008-06-06 12:08:01 +00004969 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004970 // This must be an access of the desired vector length. Return it.
4971 return Vec;
4972 }
4973
4974 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004975 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004976 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004977 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004978 Vec = Lo;
4979 } else {
4980 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004981 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
4982 Idx.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004983 }
4984
4985 // It's now an extract from the appropriate high or low part. Recurse.
4986 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4987 return ExpandEXTRACT_SUBVECTOR(Op);
4988}
4989
4990/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4991/// with condition CC on the current target. This usually involves legalizing
4992/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4993/// there may be no choice but to create a new SetCC node to represent the
4994/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman8181bd12008-07-27 21:46:04 +00004995/// LHS, and the SDValue returned in RHS has a nil SDNode value.
4996void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
4997 SDValue &RHS,
4998 SDValue &CC) {
4999 SDValue Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005000
5001 switch (getTypeAction(LHS.getValueType())) {
5002 case Legal:
5003 Tmp1 = LegalizeOp(LHS); // LHS
5004 Tmp2 = LegalizeOp(RHS); // RHS
5005 break;
5006 case Promote:
5007 Tmp1 = PromoteOp(LHS); // LHS
5008 Tmp2 = PromoteOp(RHS); // RHS
5009
5010 // If this is an FP compare, the operands have already been extended.
Duncan Sands92c43912008-06-06 12:08:01 +00005011 if (LHS.getValueType().isInteger()) {
5012 MVT VT = LHS.getValueType();
5013 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005014
5015 // Otherwise, we have to insert explicit sign or zero extends. Note
5016 // that we could insert sign extends for ALL conditions, but zero extend
5017 // is cheaper on many machines (an AND instead of two shifts), so prefer
5018 // it.
5019 switch (cast<CondCodeSDNode>(CC)->get()) {
5020 default: assert(0 && "Unknown integer comparison!");
5021 case ISD::SETEQ:
5022 case ISD::SETNE:
5023 case ISD::SETUGE:
5024 case ISD::SETUGT:
5025 case ISD::SETULE:
5026 case ISD::SETULT:
5027 // ALL of these operations will work if we either sign or zero extend
5028 // the operands (including the unsigned comparisons!). Zero extend is
5029 // usually a simpler/cheaper operation, so prefer it.
5030 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
5031 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
5032 break;
5033 case ISD::SETGE:
5034 case ISD::SETGT:
5035 case ISD::SETLT:
5036 case ISD::SETLE:
5037 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
5038 DAG.getValueType(VT));
5039 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
5040 DAG.getValueType(VT));
Evan Chengd901b662008-10-13 18:46:18 +00005041 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5042 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005043 break;
5044 }
5045 }
5046 break;
5047 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00005048 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005049 if (VT == MVT::f32 || VT == MVT::f64) {
5050 // Expand into one or more soft-fp libcall(s).
Evan Cheng24108632008-07-01 21:35:46 +00005051 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005052 switch (cast<CondCodeSDNode>(CC)->get()) {
5053 case ISD::SETEQ:
5054 case ISD::SETOEQ:
5055 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5056 break;
5057 case ISD::SETNE:
5058 case ISD::SETUNE:
5059 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
5060 break;
5061 case ISD::SETGE:
5062 case ISD::SETOGE:
5063 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5064 break;
5065 case ISD::SETLT:
5066 case ISD::SETOLT:
5067 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5068 break;
5069 case ISD::SETLE:
5070 case ISD::SETOLE:
5071 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5072 break;
5073 case ISD::SETGT:
5074 case ISD::SETOGT:
5075 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
5076 break;
5077 case ISD::SETUO:
5078 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5079 break;
5080 case ISD::SETO:
5081 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
5082 break;
5083 default:
5084 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5085 switch (cast<CondCodeSDNode>(CC)->get()) {
5086 case ISD::SETONE:
5087 // SETONE = SETOLT | SETOGT
5088 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5089 // Fallthrough
5090 case ISD::SETUGT:
5091 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
5092 break;
5093 case ISD::SETUGE:
5094 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5095 break;
5096 case ISD::SETULT:
5097 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5098 break;
5099 case ISD::SETULE:
5100 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5101 break;
5102 case ISD::SETUEQ:
5103 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5104 break;
5105 default: assert(0 && "Unsupported FP setcc!");
5106 }
5107 }
Duncan Sandsf19591c2008-06-30 10:19:09 +00005108
Dan Gohman8181bd12008-07-27 21:46:04 +00005109 SDValue Dummy;
5110 SDValue Ops[2] = { LHS, RHS };
Gabor Greif1c80d112008-08-28 21:40:38 +00005111 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005112 false /*sign irrelevant*/, Dummy);
5113 Tmp2 = DAG.getConstant(0, MVT::i32);
5114 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
5115 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel502151f2008-03-10 15:42:14 +00005116 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005117 CC);
Gabor Greif1c80d112008-08-28 21:40:38 +00005118 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005119 false /*sign irrelevant*/, Dummy);
Scott Michel502151f2008-03-10 15:42:14 +00005120 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005121 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
5122 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005123 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005124 }
Evan Cheng18a1ab12008-07-07 07:18:09 +00005125 LHS = LegalizeOp(Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005126 RHS = Tmp2;
5127 return;
5128 }
5129
Dan Gohman8181bd12008-07-27 21:46:04 +00005130 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005131 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005132 ExpandOp(RHS, RHSLo, RHSHi);
5133 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5134
5135 if (VT==MVT::ppcf128) {
5136 // FIXME: This generated code sucks. We want to generate
Dale Johannesen26317b62008-09-12 00:30:56 +00005137 // FCMPU crN, hi1, hi2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005138 // BNE crN, L:
Dale Johannesen26317b62008-09-12 00:30:56 +00005139 // FCMPU crN, lo1, lo2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005140 // The following can be improved, but not that much.
Dale Johannesen26317b62008-09-12 00:30:56 +00005141 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5142 ISD::SETOEQ);
Scott Michel502151f2008-03-10 15:42:14 +00005143 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005144 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Dale Johannesen26317b62008-09-12 00:30:56 +00005145 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5146 ISD::SETUNE);
Scott Michel502151f2008-03-10 15:42:14 +00005147 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005148 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5149 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00005150 Tmp2 = SDValue();
Dale Johannesen472d15d2007-10-06 01:24:11 +00005151 break;
5152 }
5153
5154 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005155 case ISD::SETEQ:
5156 case ISD::SETNE:
5157 if (RHSLo == RHSHi)
5158 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5159 if (RHSCST->isAllOnesValue()) {
5160 // Comparison to -1.
5161 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5162 Tmp2 = RHSLo;
5163 break;
5164 }
5165
5166 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5167 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5168 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5169 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5170 break;
5171 default:
5172 // If this is a comparison of the sign bit, just look at the top part.
5173 // X > -1, x < 0
5174 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5175 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00005176 CST->isNullValue()) || // X < 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005177 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5178 CST->isAllOnesValue())) { // X > -1
5179 Tmp1 = LHSHi;
5180 Tmp2 = RHSHi;
5181 break;
5182 }
5183
5184 // FIXME: This generated code sucks.
5185 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005186 switch (CCCode) {
5187 default: assert(0 && "Unknown integer setcc!");
5188 case ISD::SETLT:
5189 case ISD::SETULT: LowCC = ISD::SETULT; break;
5190 case ISD::SETGT:
5191 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5192 case ISD::SETLE:
5193 case ISD::SETULE: LowCC = ISD::SETULE; break;
5194 case ISD::SETGE:
5195 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5196 }
5197
5198 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5199 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5200 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5201
5202 // NOTE: on targets without efficient SELECT of bools, we can always use
5203 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
5204 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel502151f2008-03-10 15:42:14 +00005205 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005206 LowCC, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005207 if (!Tmp1.getNode())
Scott Michel502151f2008-03-10 15:42:14 +00005208 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
5209 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005210 CCCode, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005211 if (!Tmp2.getNode())
Scott Michel502151f2008-03-10 15:42:14 +00005212 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005213 RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005214
Gabor Greif1c80d112008-08-28 21:40:38 +00005215 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5216 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman9d24dc72008-03-13 22:13:53 +00005217 if ((Tmp1C && Tmp1C->isNullValue()) ||
5218 (Tmp2C && Tmp2C->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005219 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5220 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman9d24dc72008-03-13 22:13:53 +00005221 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005222 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5223 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5224 // low part is known false, returns high part.
5225 // For LE / GE, if high part is known false, ignore the low part.
5226 // For LT / GT, if high part is known true, ignore the low part.
5227 Tmp1 = Tmp2;
Dan Gohman8181bd12008-07-27 21:46:04 +00005228 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005229 } else {
Scott Michel502151f2008-03-10 15:42:14 +00005230 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005231 ISD::SETEQ, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005232 if (!Result.getNode())
Scott Michel502151f2008-03-10 15:42:14 +00005233 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00005234 ISD::SETEQ);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005235 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5236 Result, Tmp1, Tmp2));
5237 Tmp1 = Result;
Dan Gohman8181bd12008-07-27 21:46:04 +00005238 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005239 }
5240 }
5241 }
5242 }
5243 LHS = Tmp1;
5244 RHS = Tmp2;
5245}
5246
Evan Cheng71343822008-10-15 02:05:31 +00005247/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5248/// condition code CC on the current target. This routine assumes LHS and rHS
5249/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5250/// illegal condition code into AND / OR of multiple SETCC values.
5251void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5252 SDValue &LHS, SDValue &RHS,
5253 SDValue &CC) {
5254 MVT OpVT = LHS.getValueType();
5255 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5256 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5257 default: assert(0 && "Unknown condition code action!");
5258 case TargetLowering::Legal:
5259 // Nothing to do.
5260 break;
5261 case TargetLowering::Expand: {
5262 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5263 unsigned Opc = 0;
5264 switch (CCCode) {
5265 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohman2b5b9ca2008-10-21 03:12:54 +00005266 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5267 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5268 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5269 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5270 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5271 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5272 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5273 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5274 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5275 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5276 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5277 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng71343822008-10-15 02:05:31 +00005278 // FIXME: Implement more expansions.
5279 }
5280
5281 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5282 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5283 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5284 RHS = SDValue();
5285 CC = SDValue();
5286 break;
5287 }
5288 }
5289}
5290
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005291/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5292/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5293/// a load from the stack slot to DestVT, extending it if needed.
5294/// The resultant code need not be legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00005295SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5296 MVT SlotVT,
5297 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005298 // Create the stack frame object.
Mon P Wang55854cc2008-07-05 20:40:31 +00005299 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5300 SrcOp.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00005301 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang55854cc2008-07-05 20:40:31 +00005302
Dan Gohman20e37962008-02-11 18:58:42 +00005303 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005304 int SPFI = StackPtrFI->getIndex();
Mon P Wang55854cc2008-07-05 20:40:31 +00005305
Duncan Sands92c43912008-06-06 12:08:01 +00005306 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5307 unsigned SlotSize = SlotVT.getSizeInBits();
5308 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang55854cc2008-07-05 20:40:31 +00005309 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5310 DestVT.getTypeForMVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005311
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005312 // Emit a store to the stack slot. Use a truncstore if the input value is
5313 // later than DestVT.
Dan Gohman8181bd12008-07-27 21:46:04 +00005314 SDValue Store;
Mon P Wang55854cc2008-07-05 20:40:31 +00005315
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005316 if (SrcSize > SlotSize)
Dan Gohman12a9c082008-02-06 22:27:42 +00005317 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005318 PseudoSourceValue::getFixedStack(SPFI), 0,
5319 SlotVT, false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005320 else {
5321 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman12a9c082008-02-06 22:27:42 +00005322 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005323 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang55854cc2008-07-05 20:40:31 +00005324 false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005325 }
5326
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005327 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005328 if (SlotSize == DestSize)
Mon P Wang55854cc2008-07-05 20:40:31 +00005329 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005330
5331 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang55854cc2008-07-05 20:40:31 +00005332 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5333 false, DestAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005334}
5335
Dan Gohman8181bd12008-07-27 21:46:04 +00005336SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005337 // Create a vector sized/aligned stack slot, store the value to element #0,
5338 // then load the whole vector back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00005339 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman12a9c082008-02-06 22:27:42 +00005340
Dan Gohman20e37962008-02-11 18:58:42 +00005341 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005342 int SPFI = StackPtrFI->getIndex();
5343
Dan Gohman8181bd12008-07-27 21:46:04 +00005344 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005345 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00005346 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005347 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005348}
5349
5350
5351/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
5352/// support the operation, but do support the resultant vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00005353SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005354
5355 // If the only non-undef value is the low element, turn this into a
5356 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
5357 unsigned NumElems = Node->getNumOperands();
5358 bool isOnlyLowElement = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005359 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005360
Dan Gohman8181bd12008-07-27 21:46:04 +00005361 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerd8cee732008-03-09 00:29:42 +00005362 // and use a bitmask instead of a list of elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00005363 std::map<SDValue, std::vector<unsigned> > Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005364 Values[SplatValue].push_back(0);
5365 bool isConstant = true;
5366 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5367 SplatValue.getOpcode() != ISD::UNDEF)
5368 isConstant = false;
5369
5370 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005371 SDValue V = Node->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005372 Values[V].push_back(i);
5373 if (V.getOpcode() != ISD::UNDEF)
5374 isOnlyLowElement = false;
5375 if (SplatValue != V)
Dan Gohman8181bd12008-07-27 21:46:04 +00005376 SplatValue = SDValue(0,0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005377
5378 // If this isn't a constant element or an undef, we can't use a constant
5379 // pool load.
5380 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5381 V.getOpcode() != ISD::UNDEF)
5382 isConstant = false;
5383 }
5384
5385 if (isOnlyLowElement) {
5386 // If the low element is an undef too, then this whole things is an undef.
5387 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5388 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5389 // Otherwise, turn this into a scalar_to_vector node.
5390 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5391 Node->getOperand(0));
5392 }
5393
5394 // If all elements are constants, create a load from the constant pool.
5395 if (isConstant) {
Duncan Sands92c43912008-06-06 12:08:01 +00005396 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005397 std::vector<Constant*> CV;
5398 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5399 if (ConstantFPSDNode *V =
5400 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005401 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005402 } else if (ConstantSDNode *V =
Chris Lattner5e0610f2008-04-20 00:41:09 +00005403 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005404 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005405 } else {
5406 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner5e0610f2008-04-20 00:41:09 +00005407 const Type *OpNTy =
Duncan Sands92c43912008-06-06 12:08:01 +00005408 Node->getOperand(0).getValueType().getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005409 CV.push_back(UndefValue::get(OpNTy));
5410 }
5411 }
5412 Constant *CP = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005413 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005414 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman12a9c082008-02-06 22:27:42 +00005415 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005416 PseudoSourceValue::getConstantPool(), 0,
5417 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005418 }
5419
Gabor Greif1c80d112008-08-28 21:40:38 +00005420 if (SplatValue.getNode()) { // Splat of one value?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005421 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands92c43912008-06-06 12:08:01 +00005422 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman8181bd12008-07-27 21:46:04 +00005423 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5424 std::vector<SDValue> ZeroVec(NumElems, Zero);
5425 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005426 &ZeroVec[0], ZeroVec.size());
5427
5428 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
5429 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
5430 // Get the splatted value into the low element of a vector register.
Dan Gohman8181bd12008-07-27 21:46:04 +00005431 SDValue LowValVec =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005432 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5433
5434 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5435 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5436 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5437 SplatMask);
5438 }
5439 }
5440
5441 // If there are only two unique elements, we may be able to turn this into a
5442 // vector shuffle.
5443 if (Values.size() == 2) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005444 // Get the two values in deterministic order.
Dan Gohman8181bd12008-07-27 21:46:04 +00005445 SDValue Val1 = Node->getOperand(1);
5446 SDValue Val2;
5447 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerd8cee732008-03-09 00:29:42 +00005448 if (MI->first != Val1)
5449 Val2 = MI->first;
5450 else
5451 Val2 = (++MI)->first;
5452
5453 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5454 // vector shuffle has the undef vector on the RHS.
5455 if (Val1.getOpcode() == ISD::UNDEF)
5456 std::swap(Val1, Val2);
5457
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005458 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands92c43912008-06-06 12:08:01 +00005459 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5460 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005461 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005462
5463 // Set elements of the shuffle mask for Val1.
5464 std::vector<unsigned> &Val1Elts = Values[Val1];
5465 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5466 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5467
5468 // Set elements of the shuffle mask for Val2.
5469 std::vector<unsigned> &Val2Elts = Values[Val2];
5470 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5471 if (Val2.getOpcode() != ISD::UNDEF)
5472 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5473 else
5474 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5475
Dan Gohman8181bd12008-07-27 21:46:04 +00005476 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005477 &MaskVec[0], MaskVec.size());
5478
Chris Lattnerd8cee732008-03-09 00:29:42 +00005479 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005480 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5481 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005482 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5483 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005484 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005485
5486 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerd8cee732008-03-09 00:29:42 +00005487 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005488 }
5489 }
5490
5491 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5492 // aligned object on the stack, store each element into it, then load
5493 // the result as a vector.
Duncan Sands92c43912008-06-06 12:08:01 +00005494 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005495 // Create the stack frame object.
Dan Gohman8181bd12008-07-27 21:46:04 +00005496 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005497
5498 // Emit a store of each element to the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00005499 SmallVector<SDValue, 8> Stores;
Duncan Sands92c43912008-06-06 12:08:01 +00005500 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005501 // Store (in the right endianness) the elements to memory.
5502 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5503 // Ignore undef elements.
5504 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5505
5506 unsigned Offset = TypeByteSize*i;
5507
Dan Gohman8181bd12008-07-27 21:46:04 +00005508 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005509 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5510
5511 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
5512 NULL, 0));
5513 }
5514
Dan Gohman8181bd12008-07-27 21:46:04 +00005515 SDValue StoreChain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005516 if (!Stores.empty()) // Not all undef elements?
5517 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5518 &Stores[0], Stores.size());
5519 else
5520 StoreChain = DAG.getEntryNode();
5521
5522 // Result is a load from the stack slot.
5523 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
5524}
5525
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005526void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman8181bd12008-07-27 21:46:04 +00005527 SDValue Op, SDValue Amt,
5528 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005529 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00005530 SDValue LHSL, LHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005531 ExpandOp(Op, LHSL, LHSH);
5532
Dan Gohman8181bd12008-07-27 21:46:04 +00005533 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands92c43912008-06-06 12:08:01 +00005534 MVT VT = LHSL.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005535 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5536 Hi = Lo.getValue(1);
5537}
5538
5539
5540/// ExpandShift - Try to find a clever way to expand this shift operation out to
5541/// smaller elements. If we can't find a way that is more efficient than a
5542/// libcall on this target, return false. Otherwise, return true with the
5543/// low-parts expanded into Lo and Hi.
Dan Gohman8181bd12008-07-27 21:46:04 +00005544bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5545 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005546 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5547 "This is not a shift!");
5548
Duncan Sands92c43912008-06-06 12:08:01 +00005549 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman8181bd12008-07-27 21:46:04 +00005550 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands92c43912008-06-06 12:08:01 +00005551 MVT ShTy = ShAmt.getValueType();
5552 unsigned ShBits = ShTy.getSizeInBits();
5553 unsigned VTBits = Op.getValueType().getSizeInBits();
5554 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005555
Chris Lattner8c931452007-10-14 20:35:12 +00005556 // Handle the case when Amt is an immediate.
Gabor Greif1c80d112008-08-28 21:40:38 +00005557 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005558 unsigned Cst = CN->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005559 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005560 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005561 ExpandOp(Op, InL, InH);
5562 switch(Opc) {
5563 case ISD::SHL:
5564 if (Cst > VTBits) {
5565 Lo = DAG.getConstant(0, NVT);
5566 Hi = DAG.getConstant(0, NVT);
5567 } else if (Cst > NVTBits) {
5568 Lo = DAG.getConstant(0, NVT);
5569 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5570 } else if (Cst == NVTBits) {
5571 Lo = DAG.getConstant(0, NVT);
5572 Hi = InL;
5573 } else {
5574 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5575 Hi = DAG.getNode(ISD::OR, NVT,
5576 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5577 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5578 }
5579 return true;
5580 case ISD::SRL:
5581 if (Cst > VTBits) {
5582 Lo = DAG.getConstant(0, NVT);
5583 Hi = DAG.getConstant(0, NVT);
5584 } else if (Cst > NVTBits) {
5585 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5586 Hi = DAG.getConstant(0, NVT);
5587 } else if (Cst == NVTBits) {
5588 Lo = InH;
5589 Hi = DAG.getConstant(0, NVT);
5590 } else {
5591 Lo = DAG.getNode(ISD::OR, NVT,
5592 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5593 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5594 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5595 }
5596 return true;
5597 case ISD::SRA:
5598 if (Cst > VTBits) {
5599 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5600 DAG.getConstant(NVTBits-1, ShTy));
5601 } else if (Cst > NVTBits) {
5602 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5603 DAG.getConstant(Cst-NVTBits, ShTy));
5604 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5605 DAG.getConstant(NVTBits-1, ShTy));
5606 } else if (Cst == NVTBits) {
5607 Lo = InH;
5608 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5609 DAG.getConstant(NVTBits-1, ShTy));
5610 } else {
5611 Lo = DAG.getNode(ISD::OR, NVT,
5612 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5613 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5614 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5615 }
5616 return true;
5617 }
5618 }
5619
5620 // Okay, the shift amount isn't constant. However, if we can tell that it is
5621 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005622 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5623 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005624 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5625
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005626 // If we know that if any of the high bits of the shift amount are one, then
5627 // we can do this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005628 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005629 // Mask out the high bit, which we know is set.
5630 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005631 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005632
5633 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005634 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005635 ExpandOp(Op, InL, InH);
5636 switch(Opc) {
5637 case ISD::SHL:
5638 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5639 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5640 return true;
5641 case ISD::SRL:
5642 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5643 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5644 return true;
5645 case ISD::SRA:
5646 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5647 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5648 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5649 return true;
5650 }
5651 }
5652
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005653 // If we know that the high bits of the shift amount are all zero, then we can
5654 // do this as a couple of simple shifts.
5655 if ((KnownZero & Mask) == Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005656 // Compute 32-amt.
Dan Gohman8181bd12008-07-27 21:46:04 +00005657 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005658 DAG.getConstant(NVTBits, Amt.getValueType()),
5659 Amt);
5660
5661 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005662 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005663 ExpandOp(Op, InL, InH);
5664 switch(Opc) {
5665 case ISD::SHL:
5666 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5667 Hi = DAG.getNode(ISD::OR, NVT,
5668 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5669 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5670 return true;
5671 case ISD::SRL:
5672 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5673 Lo = DAG.getNode(ISD::OR, NVT,
5674 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5675 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5676 return true;
5677 case ISD::SRA:
5678 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5679 Lo = DAG.getNode(ISD::OR, NVT,
5680 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5681 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5682 return true;
5683 }
5684 }
5685
5686 return false;
5687}
5688
5689
5690// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5691// does not fit into a register, return the lo part and set the hi part to the
5692// by-reg argument. If it does fit into a single register, return the result
5693// and leave the Hi part unset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005694SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5695 bool isSigned, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005696 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5697 // The input chain to this libcall is the entry node of the function.
5698 // Legalizing the call will automatically add the previous call to the
5699 // dependence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005700 SDValue InChain = DAG.getEntryNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005701
5702 TargetLowering::ArgListTy Args;
5703 TargetLowering::ArgListEntry Entry;
5704 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00005705 MVT ArgVT = Node->getOperand(i).getValueType();
5706 const Type *ArgTy = ArgVT.getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005707 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5708 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005709 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005710 Args.push_back(Entry);
5711 }
Bill Wendlingfef06052008-09-16 21:48:12 +00005712 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang1448aad2008-10-30 08:01:45 +00005713 TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005714
5715 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands92c43912008-06-06 12:08:01 +00005716 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman8181bd12008-07-27 21:46:04 +00005717 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen67cc9b62008-09-26 19:31:26 +00005718 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5719 CallingConv::C, false, Callee, Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005720
5721 // Legalize the call sequence, starting with the chain. This will advance
5722 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5723 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5724 LegalizeOp(CallInfo.second);
Dan Gohman8181bd12008-07-27 21:46:04 +00005725 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005726 switch (getTypeAction(CallInfo.first.getValueType())) {
5727 default: assert(0 && "Unknown thing");
5728 case Legal:
5729 Result = CallInfo.first;
5730 break;
5731 case Expand:
5732 ExpandOp(CallInfo.first, Result, Hi);
5733 break;
5734 }
5735 return Result;
5736}
5737
Dan Gohman29c3cef2008-08-14 20:04:46 +00005738/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5739///
5740SDValue SelectionDAGLegalize::
5741LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5742 bool isCustom = false;
5743 SDValue Tmp1;
5744 switch (getTypeAction(Op.getValueType())) {
5745 case Legal:
5746 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5747 Op.getValueType())) {
5748 default: assert(0 && "Unknown operation action!");
5749 case TargetLowering::Custom:
5750 isCustom = true;
5751 // FALLTHROUGH
5752 case TargetLowering::Legal:
5753 Tmp1 = LegalizeOp(Op);
Gabor Greif1c80d112008-08-28 21:40:38 +00005754 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005755 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5756 else
5757 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5758 DestTy, Tmp1);
5759 if (isCustom) {
5760 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005761 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman29c3cef2008-08-14 20:04:46 +00005762 }
5763 break;
5764 case TargetLowering::Expand:
5765 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5766 break;
5767 case TargetLowering::Promote:
5768 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5769 break;
5770 }
5771 break;
5772 case Expand:
5773 Result = ExpandIntToFP(isSigned, DestTy, Op);
5774 break;
5775 case Promote:
5776 Tmp1 = PromoteOp(Op);
5777 if (isSigned) {
5778 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5779 Tmp1, DAG.getValueType(Op.getValueType()));
5780 } else {
5781 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5782 Op.getValueType());
5783 }
Gabor Greif1c80d112008-08-28 21:40:38 +00005784 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005785 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5786 else
5787 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5788 DestTy, Tmp1);
5789 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5790 break;
5791 }
5792 return Result;
5793}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005794
5795/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5796///
Dan Gohman8181bd12008-07-27 21:46:04 +00005797SDValue SelectionDAGLegalize::
5798ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands92c43912008-06-06 12:08:01 +00005799 MVT SourceVT = Source.getValueType();
Dan Gohman8b232ff2008-03-11 01:59:03 +00005800 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005801
Dan Gohman29c3cef2008-08-14 20:04:46 +00005802 // Expand unsupported int-to-fp vector casts by unrolling them.
5803 if (DestTy.isVector()) {
5804 if (!ExpandSource)
5805 return LegalizeOp(UnrollVectorOp(Source));
5806 MVT DestEltTy = DestTy.getVectorElementType();
5807 if (DestTy.getVectorNumElements() == 1) {
5808 SDValue Scalar = ScalarizeVectorOp(Source);
5809 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5810 DestEltTy, Scalar);
5811 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5812 }
5813 SDValue Lo, Hi;
5814 SplitVectorOp(Source, Lo, Hi);
5815 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5816 DestTy.getVectorNumElements() / 2);
5817 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5818 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengd901b662008-10-13 18:46:18 +00005819 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5820 HiResult));
Dan Gohman29c3cef2008-08-14 20:04:46 +00005821 }
5822
Evan Chengf99a7752008-04-01 02:18:22 +00005823 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5824 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmana193dba2008-03-05 02:07:31 +00005825 // The integer value loaded will be incorrectly if the 'sign bit' of the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005826 // incoming integer is set. To handle this, we dynamically test to see if
5827 // it is set, and, if so, add a fudge factor.
Dan Gohman8181bd12008-07-27 21:46:04 +00005828 SDValue Hi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005829 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005830 SDValue Lo;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005831 ExpandOp(Source, Lo, Hi);
5832 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5833 } else {
5834 // The comparison for the sign bit will use the entire operand.
5835 Hi = Source;
5836 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005837
Dale Johannesen96db7962008-11-04 20:52:49 +00005838 // Check to see if the target has a custom way to lower this. If so, use
5839 // it. (Note we've already expanded the operand in this case.)
Dale Johannesena359b8b2008-10-21 20:50:01 +00005840 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5841 default: assert(0 && "This action not implemented for this operation!");
5842 case TargetLowering::Legal:
5843 case TargetLowering::Expand:
5844 break; // This case is handled below.
5845 case TargetLowering::Custom: {
5846 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5847 Source), DAG);
5848 if (NV.getNode())
5849 return LegalizeOp(NV);
5850 break; // The target decided this was legal after all
5851 }
5852 }
5853
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005854 // If this is unsigned, and not supported, first perform the conversion to
5855 // signed, then adjust the result if the sign bit is set.
Dan Gohman8181bd12008-07-27 21:46:04 +00005856 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005857
Dan Gohman8181bd12008-07-27 21:46:04 +00005858 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005859 DAG.getConstant(0, Hi.getValueType()),
5860 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005861 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5862 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005863 SignSet, Four, Zero);
5864 uint64_t FF = 0x5f800000ULL;
5865 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmana193dba2008-03-05 02:07:31 +00005866 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005867
Dan Gohman8181bd12008-07-27 21:46:04 +00005868 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005869 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005870 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00005871 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00005872 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005873 if (DestTy == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005874 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005875 PseudoSourceValue::getConstantPool(), 0,
5876 false, Alignment);
Duncan Sandsec142ee2008-06-08 20:54:56 +00005877 else if (DestTy.bitsGT(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005878 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005879 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00005880 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005881 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00005882 MVT::f32, false, Alignment);
Dale Johannesen2fc20782007-09-14 22:26:36 +00005883 else
5884 assert(0 && "Unexpected conversion");
5885
Duncan Sands92c43912008-06-06 12:08:01 +00005886 MVT SCVT = SignedConv.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005887 if (SCVT != DestTy) {
5888 // Destination type needs to be expanded as well. The FADD now we are
5889 // constructing will be expanded into a libcall.
Duncan Sands92c43912008-06-06 12:08:01 +00005890 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5891 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmanc98645c2008-03-05 01:08:17 +00005892 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005893 SignedConv, SignedConv.getValue(1));
5894 }
5895 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5896 }
5897 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5898 }
5899
5900 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmanc98645c2008-03-05 01:08:17 +00005901 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005902 default: assert(0 && "This action not implemented for this operation!");
5903 case TargetLowering::Legal:
5904 case TargetLowering::Expand:
5905 break; // This case is handled below.
5906 case TargetLowering::Custom: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005907 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005908 Source), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005909 if (NV.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005910 return LegalizeOp(NV);
5911 break; // The target decided this was legal after all
5912 }
5913 }
5914
5915 // Expand the source, then glue it back together for the call. We must expand
5916 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman8b232ff2008-03-11 01:59:03 +00005917 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005918 SDValue SrcLo, SrcHi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005919 ExpandOp(Source, SrcLo, SrcHi);
5920 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5921 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005922
Duncan Sandsf68dffb2008-07-17 02:36:29 +00005923 RTLIB::Libcall LC = isSigned ?
5924 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5925 RTLIB::getUINTTOFP(SourceVT, DestTy);
5926 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5927
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005928 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman8181bd12008-07-27 21:46:04 +00005929 SDValue HiPart;
Gabor Greif1c80d112008-08-28 21:40:38 +00005930 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5931 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmanec51f642008-03-10 23:03:31 +00005932 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5933 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005934}
5935
5936/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5937/// INT_TO_FP operation of the specified operand when the target requests that
5938/// we expand it. At this point, we know that the result and operand types are
5939/// legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00005940SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5941 SDValue Op0,
5942 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005943 if (Op0.getValueType() == MVT::i32) {
5944 // simple 32-bit [signed|unsigned] integer to float/double expansion
5945
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005946 // Get the stack frame index of a 8 byte buffer.
Dan Gohman8181bd12008-07-27 21:46:04 +00005947 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005948
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005949 // word offset constant for Hi/Lo address computation
Dan Gohman8181bd12008-07-27 21:46:04 +00005950 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005951 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman8181bd12008-07-27 21:46:04 +00005952 SDValue Hi = StackSlot;
5953 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005954 if (TLI.isLittleEndian())
5955 std::swap(Hi, Lo);
5956
5957 // if signed map to unsigned space
Dan Gohman8181bd12008-07-27 21:46:04 +00005958 SDValue Op0Mapped;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005959 if (isSigned) {
5960 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman8181bd12008-07-27 21:46:04 +00005961 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005962 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5963 } else {
5964 Op0Mapped = Op0;
5965 }
5966 // store the lo of the constructed double - based on integer input
Dan Gohman8181bd12008-07-27 21:46:04 +00005967 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005968 Op0Mapped, Lo, NULL, 0);
5969 // initial hi portion of constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00005970 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005971 // store the hi of the constructed double - biased exponent
Dan Gohman8181bd12008-07-27 21:46:04 +00005972 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005973 // load the constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00005974 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005975 // FP constant to bias correct the final result
Dan Gohman8181bd12008-07-27 21:46:04 +00005976 SDValue Bias = DAG.getConstantFP(isSigned ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005977 BitsToDouble(0x4330000080000000ULL)
5978 : BitsToDouble(0x4330000000000000ULL),
5979 MVT::f64);
5980 // subtract the bias
Dan Gohman8181bd12008-07-27 21:46:04 +00005981 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005982 // final result
Dan Gohman8181bd12008-07-27 21:46:04 +00005983 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005984 // handle final rounding
5985 if (DestVT == MVT::f64) {
5986 // do nothing
5987 Result = Sub;
Duncan Sandsec142ee2008-06-08 20:54:56 +00005988 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner5872a362008-01-17 07:00:52 +00005989 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5990 DAG.getIntPtrConstant(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00005991 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005992 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005993 }
5994 return Result;
5995 }
5996 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman8181bd12008-07-27 21:46:04 +00005997 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005998
Dan Gohman8181bd12008-07-27 21:46:04 +00005999 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006000 DAG.getConstant(0, Op0.getValueType()),
6001 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00006002 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
6003 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006004 SignSet, Four, Zero);
6005
6006 // If the sign bit of the integer is set, the large number will be treated
6007 // as a negative number. To counteract this, the dynamic code adds an
6008 // offset depending on the data type.
6009 uint64_t FF;
Duncan Sands92c43912008-06-06 12:08:01 +00006010 switch (Op0.getValueType().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006011 default: assert(0 && "Unsupported integer type!");
6012 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6013 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6014 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6015 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6016 }
6017 if (TLI.isLittleEndian()) FF <<= 32;
6018 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
6019
Dan Gohman8181bd12008-07-27 21:46:04 +00006020 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00006021 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006022 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00006023 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00006024 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006025 if (DestVT == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00006026 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00006027 PseudoSourceValue::getConstantPool(), 0,
6028 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006029 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00006030 FudgeInReg =
6031 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
6032 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006033 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00006034 MVT::f32, false, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006035 }
6036
6037 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
6038}
6039
6040/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6041/// *INT_TO_FP operation of the specified operand when the target requests that
6042/// we promote it. At this point, we know that the result and operand types are
6043/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6044/// operation that takes a larger input.
Dan Gohman8181bd12008-07-27 21:46:04 +00006045SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6046 MVT DestVT,
6047 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006048 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00006049 MVT NewInTy = LegalOp.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006050
6051 unsigned OpToUse = 0;
6052
6053 // Scan for the appropriate larger type to use.
6054 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00006055 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6056 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006057
6058 // If the target supports SINT_TO_FP of this type, use it.
6059 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6060 default: break;
6061 case TargetLowering::Legal:
6062 if (!TLI.isTypeLegal(NewInTy))
6063 break; // Can't use this datatype.
6064 // FALL THROUGH.
6065 case TargetLowering::Custom:
6066 OpToUse = ISD::SINT_TO_FP;
6067 break;
6068 }
6069 if (OpToUse) break;
6070 if (isSigned) continue;
6071
6072 // If the target supports UINT_TO_FP of this type, use it.
6073 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6074 default: break;
6075 case TargetLowering::Legal:
6076 if (!TLI.isTypeLegal(NewInTy))
6077 break; // Can't use this datatype.
6078 // FALL THROUGH.
6079 case TargetLowering::Custom:
6080 OpToUse = ISD::UINT_TO_FP;
6081 break;
6082 }
6083 if (OpToUse) break;
6084
6085 // Otherwise, try a larger type.
6086 }
6087
6088 // Okay, we found the operation and type to use. Zero extend our input to the
6089 // desired type then run the operation on it.
6090 return DAG.getNode(OpToUse, DestVT,
6091 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
6092 NewInTy, LegalOp));
6093}
6094
6095/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6096/// FP_TO_*INT operation of the specified operand when the target requests that
6097/// we promote it. At this point, we know that the result and operand types are
6098/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6099/// operation that returns a larger result.
Dan Gohman8181bd12008-07-27 21:46:04 +00006100SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6101 MVT DestVT,
6102 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006103 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00006104 MVT NewOutTy = DestVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006105
6106 unsigned OpToUse = 0;
6107
6108 // Scan for the appropriate larger type to use.
6109 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00006110 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6111 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006112
6113 // If the target supports FP_TO_SINT returning this type, use it.
6114 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6115 default: break;
6116 case TargetLowering::Legal:
6117 if (!TLI.isTypeLegal(NewOutTy))
6118 break; // Can't use this datatype.
6119 // FALL THROUGH.
6120 case TargetLowering::Custom:
6121 OpToUse = ISD::FP_TO_SINT;
6122 break;
6123 }
6124 if (OpToUse) break;
6125
6126 // If the target supports FP_TO_UINT of this type, use it.
6127 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6128 default: break;
6129 case TargetLowering::Legal:
6130 if (!TLI.isTypeLegal(NewOutTy))
6131 break; // Can't use this datatype.
6132 // FALL THROUGH.
6133 case TargetLowering::Custom:
6134 OpToUse = ISD::FP_TO_UINT;
6135 break;
6136 }
6137 if (OpToUse) break;
6138
6139 // Otherwise, try a larger type.
6140 }
6141
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006142
6143 // Okay, we found the operation and type to use.
Dan Gohman8181bd12008-07-27 21:46:04 +00006144 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sandsac496a12008-07-04 11:47:58 +00006145
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006146 // If the operation produces an invalid type, it must be custom lowered. Use
6147 // the target lowering hooks to expand it. Just keep the low part of the
6148 // expanded operation, we know that we're truncating anyway.
6149 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006150 SmallVector<SDValue, 2> Results;
6151 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6152 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6153 Operation = Results[0];
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006154 }
Duncan Sandsac496a12008-07-04 11:47:58 +00006155
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006156 // Truncate the result of the extended FP_TO_*INT operation to the desired
6157 // size.
6158 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006159}
6160
6161/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6162///
Dan Gohman8181bd12008-07-27 21:46:04 +00006163SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00006164 MVT VT = Op.getValueType();
6165 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00006166 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands92c43912008-06-06 12:08:01 +00006167 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006168 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6169 case MVT::i16:
6170 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6171 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6172 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6173 case MVT::i32:
6174 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6175 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6176 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6177 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6178 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6179 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6180 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6181 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6182 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6183 case MVT::i64:
6184 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6185 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6186 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6187 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6188 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6189 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6190 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6191 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6192 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6193 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6194 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6195 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6196 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6197 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6198 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6199 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6200 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6201 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6202 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6203 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6204 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6205 }
6206}
6207
6208/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6209///
Dan Gohman8181bd12008-07-27 21:46:04 +00006210SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006211 switch (Opc) {
6212 default: assert(0 && "Cannot expand this yet!");
6213 case ISD::CTPOP: {
6214 static const uint64_t mask[6] = {
6215 0x5555555555555555ULL, 0x3333333333333333ULL,
6216 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6217 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6218 };
Duncan Sands92c43912008-06-06 12:08:01 +00006219 MVT VT = Op.getValueType();
6220 MVT ShVT = TLI.getShiftAmountTy();
6221 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006222 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6223 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman8181bd12008-07-27 21:46:04 +00006224 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6225 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006226 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6227 DAG.getNode(ISD::AND, VT,
6228 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6229 }
6230 return Op;
6231 }
6232 case ISD::CTLZ: {
6233 // for now, we do this:
6234 // x = x | (x >> 1);
6235 // x = x | (x >> 2);
6236 // ...
6237 // x = x | (x >>16);
6238 // x = x | (x >>32); // for 64-bit input
6239 // return popcount(~x);
6240 //
6241 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006242 MVT VT = Op.getValueType();
6243 MVT ShVT = TLI.getShiftAmountTy();
6244 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006245 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006246 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006247 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6248 }
6249 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6250 return DAG.getNode(ISD::CTPOP, VT, Op);
6251 }
6252 case ISD::CTTZ: {
6253 // for now, we use: { return popcount(~x & (x - 1)); }
6254 // unless the target has ctlz but not ctpop, in which case we use:
6255 // { return 32 - nlz(~x & (x-1)); }
6256 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006257 MVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006258 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6259 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006260 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6261 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6262 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6263 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6264 TLI.isOperationLegal(ISD::CTLZ, VT))
6265 return DAG.getNode(ISD::SUB, VT,
Duncan Sands92c43912008-06-06 12:08:01 +00006266 DAG.getConstant(VT.getSizeInBits(), VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006267 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6268 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6269 }
6270 }
6271}
6272
Dan Gohman8181bd12008-07-27 21:46:04 +00006273/// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006274/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman4fc03742008-10-01 15:07:49 +00006275/// LegalizedNodes map is filled in for any results that are not expanded, the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006276/// ExpandedNodes map is filled in for any results that are expanded, and the
6277/// Lo/Hi values are returned.
Dan Gohman8181bd12008-07-27 21:46:04 +00006278void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands92c43912008-06-06 12:08:01 +00006279 MVT VT = Op.getValueType();
6280 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greif1c80d112008-08-28 21:40:38 +00006281 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006282 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00006283 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands92c43912008-06-06 12:08:01 +00006284 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006285
6286 // See if we already expanded it.
Dan Gohman8181bd12008-07-27 21:46:04 +00006287 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006288 = ExpandedNodes.find(Op);
6289 if (I != ExpandedNodes.end()) {
6290 Lo = I->second.first;
6291 Hi = I->second.second;
6292 return;
6293 }
6294
6295 switch (Node->getOpcode()) {
6296 case ISD::CopyFromReg:
6297 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006298 case ISD::FP_ROUND_INREG:
6299 if (VT == MVT::ppcf128 &&
6300 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6301 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006302 SDValue SrcLo, SrcHi, Src;
Dale Johannesend3b6af32007-10-11 23:32:15 +00006303 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6304 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006305 SDValue Result = TLI.LowerOperation(
Dale Johannesend3b6af32007-10-11 23:32:15 +00006306 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006307 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6308 Lo = Result.getNode()->getOperand(0);
6309 Hi = Result.getNode()->getOperand(1);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006310 break;
6311 }
6312 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006313 default:
6314#ifndef NDEBUG
6315 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
6316#endif
6317 assert(0 && "Do not know how to expand this operator!");
6318 abort();
Dan Gohman550c8462008-02-27 01:52:30 +00006319 case ISD::EXTRACT_ELEMENT:
6320 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006321 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman550c8462008-02-27 01:52:30 +00006322 return ExpandOp(Hi, Lo, Hi);
Dan Gohman7e7aa2c2008-02-27 19:44:57 +00006323 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006324 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006325 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6326 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6327 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006328 case ISD::UNDEF:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006329 Lo = DAG.getNode(ISD::UNDEF, NVT);
6330 Hi = DAG.getNode(ISD::UNDEF, NVT);
6331 break;
6332 case ISD::Constant: {
Duncan Sands92c43912008-06-06 12:08:01 +00006333 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman97f1f8e2008-03-03 22:20:46 +00006334 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6335 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6336 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006337 break;
6338 }
6339 case ISD::ConstantFP: {
6340 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00006341 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00006342 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen2aef5692007-10-11 18:07:22 +00006343 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6344 MVT::f64);
6345 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6346 MVT::f64);
6347 break;
6348 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006349 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
6350 if (getTypeAction(Lo.getValueType()) == Expand)
6351 ExpandOp(Lo, Lo, Hi);
6352 break;
6353 }
6354 case ISD::BUILD_PAIR:
6355 // Return the operands.
6356 Lo = Node->getOperand(0);
6357 Hi = Node->getOperand(1);
6358 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006359
6360 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00006361 if (Node->getNumValues() == 1) {
6362 ExpandOp(Op.getOperand(0), Lo, Hi);
6363 break;
6364 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006365 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif46bf5472008-08-26 22:36:50 +00006366 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006367 Op.getValue(1).getValueType() == MVT::Other &&
6368 "unhandled MERGE_VALUES");
6369 ExpandOp(Op.getOperand(0), Lo, Hi);
6370 // Remember that we legalized the chain.
6371 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6372 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006373
6374 case ISD::SIGN_EXTEND_INREG:
6375 ExpandOp(Node->getOperand(0), Lo, Hi);
6376 // sext_inreg the low part if needed.
6377 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6378
6379 // The high part gets the sign extension from the lo-part. This handles
6380 // things like sextinreg V:i64 from i8.
6381 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands92c43912008-06-06 12:08:01 +00006382 DAG.getConstant(NVT.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006383 TLI.getShiftAmountTy()));
6384 break;
6385
6386 case ISD::BSWAP: {
6387 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006388 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006389 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6390 Lo = TempLo;
6391 break;
6392 }
6393
6394 case ISD::CTPOP:
6395 ExpandOp(Node->getOperand(0), Lo, Hi);
6396 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6397 DAG.getNode(ISD::CTPOP, NVT, Lo),
6398 DAG.getNode(ISD::CTPOP, NVT, Hi));
6399 Hi = DAG.getConstant(0, NVT);
6400 break;
6401
6402 case ISD::CTLZ: {
6403 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
6404 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006405 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6406 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
6407 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006408 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006409 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006410 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6411
6412 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6413 Hi = DAG.getConstant(0, NVT);
6414 break;
6415 }
6416
6417 case ISD::CTTZ: {
6418 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
6419 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006420 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6421 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
6422 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006423 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006424 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006425 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6426
6427 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6428 Hi = DAG.getConstant(0, NVT);
6429 break;
6430 }
6431
6432 case ISD::VAARG: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006433 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6434 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006435 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6436 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6437
6438 // Remember that we legalized the chain.
6439 Hi = LegalizeOp(Hi);
6440 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006441 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006442 std::swap(Lo, Hi);
6443 break;
6444 }
6445
6446 case ISD::LOAD: {
6447 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00006448 SDValue Ch = LD->getChain(); // Legalize the chain.
6449 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006450 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman29c3cef2008-08-14 20:04:46 +00006451 const Value *SV = LD->getSrcValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006452 int SVOffset = LD->getSrcValueOffset();
6453 unsigned Alignment = LD->getAlignment();
6454 bool isVolatile = LD->isVolatile();
6455
6456 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman29c3cef2008-08-14 20:04:46 +00006457 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006458 isVolatile, Alignment);
6459 if (VT == MVT::f32 || VT == MVT::f64) {
6460 // f32->i32 or f64->i64 one to one expansion.
6461 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006462 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006463 // Recursively expand the new load.
6464 if (getTypeAction(NVT) == Expand)
6465 ExpandOp(Lo, Lo, Hi);
6466 break;
6467 }
6468
6469 // Increment the pointer to the other half.
Duncan Sands92c43912008-06-06 12:08:01 +00006470 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006471 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006472 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006473 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006474 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00006475 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006476 isVolatile, Alignment);
6477
6478 // Build a factor node to remember that this load is independent of the
6479 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00006480 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006481 Hi.getValue(1));
6482
6483 // Remember that we legalized the chain.
6484 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006485 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006486 std::swap(Lo, Hi);
6487 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00006488 MVT EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006489
Dale Johannesen2550e3a2007-10-19 20:29:00 +00006490 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6491 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006492 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman29c3cef2008-08-14 20:04:46 +00006493 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006494 SVOffset, isVolatile, Alignment);
6495 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006496 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006497 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6498 break;
6499 }
6500
6501 if (EVT == NVT)
Dan Gohman29c3cef2008-08-14 20:04:46 +00006502 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006503 SVOffset, isVolatile, Alignment);
6504 else
Dan Gohman29c3cef2008-08-14 20:04:46 +00006505 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006506 SVOffset, EVT, isVolatile,
6507 Alignment);
6508
6509 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006510 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006511
6512 if (ExtType == ISD::SEXTLOAD) {
6513 // The high part is obtained by SRA'ing all but one of the bits of the
6514 // lo part.
Duncan Sands92c43912008-06-06 12:08:01 +00006515 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006516 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6517 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6518 } else if (ExtType == ISD::ZEXTLOAD) {
6519 // The high part is just a zero.
6520 Hi = DAG.getConstant(0, NVT);
6521 } else /* if (ExtType == ISD::EXTLOAD) */ {
6522 // The high part is undefined.
6523 Hi = DAG.getNode(ISD::UNDEF, NVT);
6524 }
6525 }
6526 break;
6527 }
6528 case ISD::AND:
6529 case ISD::OR:
6530 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +00006531 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006532 ExpandOp(Node->getOperand(0), LL, LH);
6533 ExpandOp(Node->getOperand(1), RL, RH);
6534 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6535 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6536 break;
6537 }
6538 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006539 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006540 ExpandOp(Node->getOperand(1), LL, LH);
6541 ExpandOp(Node->getOperand(2), RL, RH);
6542 if (getTypeAction(NVT) == Expand)
6543 NVT = TLI.getTypeToExpandTo(NVT);
6544 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
6545 if (VT != MVT::f32)
6546 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
6547 break;
6548 }
6549 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006550 SDValue TL, TH, FL, FH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006551 ExpandOp(Node->getOperand(2), TL, TH);
6552 ExpandOp(Node->getOperand(3), FL, FH);
6553 if (getTypeAction(NVT) == Expand)
6554 NVT = TLI.getTypeToExpandTo(NVT);
6555 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6556 Node->getOperand(1), TL, FL, Node->getOperand(4));
6557 if (VT != MVT::f32)
6558 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6559 Node->getOperand(1), TH, FH, Node->getOperand(4));
6560 break;
6561 }
6562 case ISD::ANY_EXTEND:
6563 // The low part is any extension of the input (which degenerates to a copy).
6564 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6565 // The high part is undefined.
6566 Hi = DAG.getNode(ISD::UNDEF, NVT);
6567 break;
6568 case ISD::SIGN_EXTEND: {
6569 // The low part is just a sign extension of the input (which degenerates to
6570 // a copy).
6571 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
6572
6573 // The high part is obtained by SRA'ing all but one of the bits of the lo
6574 // part.
Duncan Sands92c43912008-06-06 12:08:01 +00006575 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006576 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6577 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6578 break;
6579 }
6580 case ISD::ZERO_EXTEND:
6581 // The low part is just a zero extension of the input (which degenerates to
6582 // a copy).
6583 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
6584
6585 // The high part is just a zero.
6586 Hi = DAG.getConstant(0, NVT);
6587 break;
6588
6589 case ISD::TRUNCATE: {
6590 // The input value must be larger than this value. Expand *it*.
Dan Gohman8181bd12008-07-27 21:46:04 +00006591 SDValue NewLo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006592 ExpandOp(Node->getOperand(0), NewLo, Hi);
6593
6594 // The low part is now either the right size, or it is closer. If not the
6595 // right size, make an illegal truncate so we recursively expand it.
6596 if (NewLo.getValueType() != Node->getValueType(0))
6597 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6598 ExpandOp(NewLo, Lo, Hi);
6599 break;
6600 }
6601
6602 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006603 SDValue Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006604 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6605 // If the target wants to, allow it to lower this itself.
6606 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6607 case Expand: assert(0 && "cannot expand FP!");
6608 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6609 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6610 }
6611 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6612 }
6613
6614 // f32 / f64 must be expanded to i32 / i64.
6615 if (VT == MVT::f32 || VT == MVT::f64) {
6616 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6617 if (getTypeAction(NVT) == Expand)
6618 ExpandOp(Lo, Lo, Hi);
6619 break;
6620 }
6621
6622 // If source operand will be expanded to the same type as VT, i.e.
6623 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands92c43912008-06-06 12:08:01 +00006624 MVT VT0 = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006625 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6626 ExpandOp(Node->getOperand(0), Lo, Hi);
6627 break;
6628 }
6629
6630 // Turn this into a load/store pair by default.
Gabor Greif1c80d112008-08-28 21:40:38 +00006631 if (Tmp.getNode() == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00006632 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006633
6634 ExpandOp(Tmp, Lo, Hi);
6635 break;
6636 }
6637
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006638 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006639 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6640 TargetLowering::Custom &&
6641 "Must custom expand ReadCycleCounter");
Dan Gohman8181bd12008-07-27 21:46:04 +00006642 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006643 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006644 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006645 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006646 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006647 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006648 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006649
Dale Johannesen44eb5372008-10-03 19:41:08 +00006650 case ISD::ATOMIC_CMP_SWAP_64: {
6651 // This operation does not need a loop.
6652 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6653 assert(Tmp.getNode() && "Node must be custom expanded!");
6654 ExpandOp(Tmp.getValue(0), Lo, Hi);
6655 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6656 LegalizeOp(Tmp.getValue(1)));
6657 break;
6658 }
6659
Dale Johannesenf160d802008-10-02 18:53:47 +00006660 case ISD::ATOMIC_LOAD_ADD_64:
6661 case ISD::ATOMIC_LOAD_SUB_64:
6662 case ISD::ATOMIC_LOAD_AND_64:
6663 case ISD::ATOMIC_LOAD_OR_64:
6664 case ISD::ATOMIC_LOAD_XOR_64:
6665 case ISD::ATOMIC_LOAD_NAND_64:
Dale Johannesen44eb5372008-10-03 19:41:08 +00006666 case ISD::ATOMIC_SWAP_64: {
6667 // These operations require a loop to be generated. We can't do that yet,
6668 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesenf160d802008-10-02 18:53:47 +00006669 SDValue In2Lo, In2Hi, In2;
6670 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6671 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006672 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6673 SDValue Replace =
6674 DAG.getAtomic(Op.getOpcode(), Op.getOperand(0), Op.getOperand(1), In2,
6675 Anode->getSrcValue(), Anode->getAlignment());
6676 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006677 ExpandOp(Result.getValue(0), Lo, Hi);
6678 // Remember that we legalized the chain.
6679 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharth81580822008-03-05 01:15:49 +00006680 break;
6681 }
6682
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006683 // These operators cannot be expanded directly, emit them as calls to
6684 // library functions.
6685 case ISD::FP_TO_SINT: {
6686 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006687 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006688 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6689 case Expand: assert(0 && "cannot expand FP!");
6690 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6691 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6692 }
6693
6694 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6695
6696 // Now that the custom expander is done, expand the result, which is still
6697 // VT.
Gabor Greif1c80d112008-08-28 21:40:38 +00006698 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006699 ExpandOp(Op, Lo, Hi);
6700 break;
6701 }
6702 }
6703
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006704 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6705 VT);
6706 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6707 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006708 break;
6709 }
6710
6711 case ISD::FP_TO_UINT: {
6712 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006713 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006714 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6715 case Expand: assert(0 && "cannot expand FP!");
6716 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6717 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6718 }
6719
6720 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6721
6722 // Now that the custom expander is done, expand the result.
Gabor Greif1c80d112008-08-28 21:40:38 +00006723 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006724 ExpandOp(Op, Lo, Hi);
6725 break;
6726 }
6727 }
6728
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006729 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6730 VT);
6731 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6732 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006733 break;
6734 }
6735
6736 case ISD::SHL: {
6737 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006738 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006739 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006740 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006741 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006742 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006743 // Now that the custom expander is done, expand the result, which is
6744 // still VT.
6745 ExpandOp(Op, Lo, Hi);
6746 break;
6747 }
6748 }
6749
6750 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6751 // this X << 1 as X+X.
6752 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00006753 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006754 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006755 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006756 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6757 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6758 LoOps[1] = LoOps[0];
6759 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6760
6761 HiOps[1] = HiOps[0];
6762 HiOps[2] = Lo.getValue(1);
6763 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6764 break;
6765 }
6766 }
6767
6768 // If we can emit an efficient shift operation, do so now.
6769 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6770 break;
6771
6772 // If this target supports SHL_PARTS, use it.
6773 TargetLowering::LegalizeAction Action =
6774 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6775 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6776 Action == TargetLowering::Custom) {
6777 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6778 break;
6779 }
6780
6781 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006782 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006783 break;
6784 }
6785
6786 case ISD::SRA: {
6787 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006788 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006789 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006790 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006791 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006792 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006793 // Now that the custom expander is done, expand the result, which is
6794 // still VT.
6795 ExpandOp(Op, Lo, Hi);
6796 break;
6797 }
6798 }
6799
6800 // If we can emit an efficient shift operation, do so now.
6801 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6802 break;
6803
6804 // If this target supports SRA_PARTS, use it.
6805 TargetLowering::LegalizeAction Action =
6806 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6807 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6808 Action == TargetLowering::Custom) {
6809 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6810 break;
6811 }
6812
6813 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006814 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006815 break;
6816 }
6817
6818 case ISD::SRL: {
6819 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006820 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006821 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006822 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006823 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006824 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006825 // Now that the custom expander is done, expand the result, which is
6826 // still VT.
6827 ExpandOp(Op, Lo, Hi);
6828 break;
6829 }
6830 }
6831
6832 // If we can emit an efficient shift operation, do so now.
6833 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6834 break;
6835
6836 // If this target supports SRL_PARTS, use it.
6837 TargetLowering::LegalizeAction Action =
6838 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6839 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6840 Action == TargetLowering::Custom) {
6841 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6842 break;
6843 }
6844
6845 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006846 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006847 break;
6848 }
6849
6850 case ISD::ADD:
6851 case ISD::SUB: {
6852 // If the target wants to custom expand this, let them.
6853 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6854 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006855 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006856 if (Result.getNode()) {
Duncan Sands4c3885b2008-06-22 09:42:16 +00006857 ExpandOp(Result, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006858 break;
6859 }
6860 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006861 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006862 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006863 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6864 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman8181bd12008-07-27 21:46:04 +00006865 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006866 LoOps[0] = LHSL;
6867 LoOps[1] = RHSL;
6868 HiOps[0] = LHSH;
6869 HiOps[1] = RHSH;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006870
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006871 //cascaded check to see if any smaller size has a a carry flag.
6872 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6873 bool hasCarry = false;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006874 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6875 MVT AVT = MVT::getIntegerVT(BitSize);
6876 if (TLI.isOperationLegal(OpV, AVT)) {
6877 hasCarry = true;
6878 break;
6879 }
6880 }
6881
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006882 if(hasCarry) {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00006883 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006884 if (Node->getOpcode() == ISD::ADD) {
6885 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6886 HiOps[2] = Lo.getValue(1);
6887 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6888 } else {
6889 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6890 HiOps[2] = Lo.getValue(1);
6891 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6892 }
6893 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006894 } else {
Andrew Lenharth5e814462008-10-07 14:15:42 +00006895 if (Node->getOpcode() == ISD::ADD) {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00006896 Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
6897 Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006898 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6899 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006900 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6901 DAG.getConstant(1, NVT),
6902 DAG.getConstant(0, NVT));
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006903 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6904 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006905 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6906 DAG.getConstant(1, NVT),
6907 Carry1);
6908 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6909 } else {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00006910 Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
6911 Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006912 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6913 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6914 DAG.getConstant(1, NVT),
6915 DAG.getConstant(0, NVT));
6916 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6917 }
6918 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006919 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006920 }
6921
6922 case ISD::ADDC:
6923 case ISD::SUBC: {
6924 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006925 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006926 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6927 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6928 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006929 SDValue LoOps[2] = { LHSL, RHSL };
6930 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006931
6932 if (Node->getOpcode() == ISD::ADDC) {
6933 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6934 HiOps[2] = Lo.getValue(1);
6935 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6936 } else {
6937 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6938 HiOps[2] = Lo.getValue(1);
6939 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6940 }
6941 // Remember that we legalized the flag.
6942 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6943 break;
6944 }
6945 case ISD::ADDE:
6946 case ISD::SUBE: {
6947 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006948 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006949 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6950 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6951 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006952 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6953 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006954
6955 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6956 HiOps[2] = Lo.getValue(1);
6957 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6958
6959 // Remember that we legalized the flag.
6960 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6961 break;
6962 }
6963 case ISD::MUL: {
6964 // If the target wants to custom expand this, let them.
6965 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006966 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006967 if (New.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006968 ExpandOp(New, Lo, Hi);
6969 break;
6970 }
6971 }
6972
6973 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6974 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00006975 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6976 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6977 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006978 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006979 ExpandOp(Node->getOperand(0), LL, LH);
6980 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman07961cd2008-02-25 21:11:39 +00006981 unsigned OuterBitSize = Op.getValueSizeInBits();
6982 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman5a199552007-10-08 18:33:35 +00006983 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6984 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman2594d942008-03-10 20:42:19 +00006985 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6986 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6987 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman5a199552007-10-08 18:33:35 +00006988 // The inputs are both zero-extended.
6989 if (HasUMUL_LOHI) {
6990 // We can emit a umul_lohi.
6991 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00006992 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00006993 break;
6994 }
6995 if (HasMULHU) {
6996 // We can emit a mulhu+mul.
6997 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6998 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6999 break;
7000 }
Dan Gohman5a199552007-10-08 18:33:35 +00007001 }
Dan Gohman07961cd2008-02-25 21:11:39 +00007002 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman5a199552007-10-08 18:33:35 +00007003 // The input values are both sign-extended.
7004 if (HasSMUL_LOHI) {
7005 // We can emit a smul_lohi.
7006 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00007007 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00007008 break;
7009 }
7010 if (HasMULHS) {
7011 // We can emit a mulhs+mul.
7012 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7013 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
7014 break;
7015 }
7016 }
7017 if (HasUMUL_LOHI) {
7018 // Lo,Hi = umul LHS, RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00007019 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman5a199552007-10-08 18:33:35 +00007020 DAG.getVTList(NVT, NVT), LL, RL);
7021 Lo = UMulLOHI;
7022 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007023 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7024 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7025 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7026 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7027 break;
7028 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00007029 if (HasMULHU) {
7030 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7031 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7032 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7033 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7034 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7035 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7036 break;
7037 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007038 }
7039
Dan Gohman5a199552007-10-08 18:33:35 +00007040 // If nothing else, we can make a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007041 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007042 break;
7043 }
7044 case ISD::SDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007045 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007046 break;
7047 case ISD::UDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007048 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007049 break;
7050 case ISD::SREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007051 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007052 break;
7053 case ISD::UREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007054 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007055 break;
7056
7057 case ISD::FADD:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007058 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7059 RTLIB::ADD_F64,
7060 RTLIB::ADD_F80,
7061 RTLIB::ADD_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007062 Node, false, Hi);
7063 break;
7064 case ISD::FSUB:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007065 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7066 RTLIB::SUB_F64,
7067 RTLIB::SUB_F80,
7068 RTLIB::SUB_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007069 Node, false, Hi);
7070 break;
7071 case ISD::FMUL:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007072 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7073 RTLIB::MUL_F64,
7074 RTLIB::MUL_F80,
7075 RTLIB::MUL_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007076 Node, false, Hi);
7077 break;
7078 case ISD::FDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007079 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7080 RTLIB::DIV_F64,
7081 RTLIB::DIV_F80,
7082 RTLIB::DIV_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007083 Node, false, Hi);
7084 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007085 case ISD::FP_EXTEND: {
Dale Johannesen4c14d512007-10-12 01:37:08 +00007086 if (VT == MVT::ppcf128) {
7087 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7088 Node->getOperand(0).getValueType()==MVT::f64);
7089 const uint64_t zero = 0;
7090 if (Node->getOperand(0).getValueType()==MVT::f32)
7091 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
7092 else
7093 Hi = Node->getOperand(0);
7094 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7095 break;
7096 }
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007097 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7098 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7099 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007100 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007101 }
7102 case ISD::FP_ROUND: {
7103 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7104 VT);
7105 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7106 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007107 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007108 }
Evan Cheng5316b392008-09-09 23:02:14 +00007109 case ISD::FSQRT:
7110 case ISD::FSIN:
7111 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007112 case ISD::FLOG:
7113 case ISD::FLOG2:
7114 case ISD::FLOG10:
7115 case ISD::FEXP:
7116 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00007117 case ISD::FTRUNC:
7118 case ISD::FFLOOR:
7119 case ISD::FCEIL:
7120 case ISD::FRINT:
7121 case ISD::FNEARBYINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00007122 case ISD::FPOW:
7123 case ISD::FPOWI: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007124 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
7125 switch(Node->getOpcode()) {
7126 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00007127 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7128 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007129 break;
7130 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00007131 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7132 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007133 break;
7134 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00007135 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7136 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007137 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00007138 case ISD::FLOG:
7139 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7140 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7141 break;
7142 case ISD::FLOG2:
7143 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7144 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7145 break;
7146 case ISD::FLOG10:
7147 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7148 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7149 break;
7150 case ISD::FEXP:
7151 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7152 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7153 break;
7154 case ISD::FEXP2:
7155 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7156 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7157 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00007158 case ISD::FTRUNC:
7159 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7160 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7161 break;
7162 case ISD::FFLOOR:
7163 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7164 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7165 break;
7166 case ISD::FCEIL:
7167 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7168 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7169 break;
7170 case ISD::FRINT:
7171 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7172 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7173 break;
7174 case ISD::FNEARBYINT:
7175 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7176 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7177 break;
Evan Cheng5316b392008-09-09 23:02:14 +00007178 case ISD::FPOW:
7179 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7180 RTLIB::POW_PPCF128);
7181 break;
7182 case ISD::FPOWI:
7183 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7184 RTLIB::POWI_PPCF128);
7185 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007186 default: assert(0 && "Unreachable!");
7187 }
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007188 Lo = ExpandLibCall(LC, Node, false, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007189 break;
7190 }
7191 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007192 if (VT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007193 SDValue Tmp;
Dale Johannesen5707ef82007-10-12 19:02:17 +00007194 ExpandOp(Node->getOperand(0), Lo, Tmp);
7195 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7196 // lo = hi==fabs(hi) ? lo : -lo;
7197 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7198 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7199 DAG.getCondCode(ISD::SETEQ));
7200 break;
7201 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007202 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007203 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7204 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7205 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7206 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7207 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7208 if (getTypeAction(NVT) == Expand)
7209 ExpandOp(Lo, Lo, Hi);
7210 break;
7211 }
7212 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007213 if (VT == MVT::ppcf128) {
7214 ExpandOp(Node->getOperand(0), Lo, Hi);
7215 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7216 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7217 break;
7218 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007219 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007220 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7221 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7222 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7223 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7224 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7225 if (getTypeAction(NVT) == Expand)
7226 ExpandOp(Lo, Lo, Hi);
7227 break;
7228 }
7229 case ISD::FCOPYSIGN: {
7230 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7231 if (getTypeAction(NVT) == Expand)
7232 ExpandOp(Lo, Lo, Hi);
7233 break;
7234 }
7235 case ISD::SINT_TO_FP:
7236 case ISD::UINT_TO_FP: {
7237 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands92c43912008-06-06 12:08:01 +00007238 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007239
7240 // Promote the operand if needed. Do this before checking for
7241 // ppcf128 so conversions of i16 and i8 work.
7242 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007243 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesen6a779c82008-03-18 17:28:38 +00007244 Tmp = isSigned
7245 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7246 DAG.getValueType(SrcVT))
7247 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greif1c80d112008-08-28 21:40:38 +00007248 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007249 SrcVT = Node->getOperand(0).getValueType();
7250 }
7251
Dan Gohmanec51f642008-03-10 23:03:31 +00007252 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman84d00962008-02-25 21:39:34 +00007253 static const uint64_t zero = 0;
Dale Johannesen4c14d512007-10-12 01:37:08 +00007254 if (isSigned) {
7255 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7256 Node->getOperand(0)));
7257 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7258 } else {
Dan Gohman84d00962008-02-25 21:39:34 +00007259 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen4c14d512007-10-12 01:37:08 +00007260 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7261 Node->getOperand(0)));
7262 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7263 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007264 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00007265 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7266 DAG.getConstant(0, MVT::i32),
7267 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7268 DAG.getConstantFP(
7269 APFloat(APInt(128, 2, TwoE32)),
7270 MVT::ppcf128)),
7271 Hi,
7272 DAG.getCondCode(ISD::SETLT)),
7273 Lo, Hi);
7274 }
7275 break;
7276 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007277 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7278 // si64->ppcf128 done by libcall, below
Dan Gohman84d00962008-02-25 21:39:34 +00007279 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007280 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7281 Lo, Hi);
7282 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7283 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7284 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7285 DAG.getConstant(0, MVT::i64),
7286 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7287 DAG.getConstantFP(
7288 APFloat(APInt(128, 2, TwoE64)),
7289 MVT::ppcf128)),
7290 Hi,
7291 DAG.getCondCode(ISD::SETLT)),
7292 Lo, Hi);
7293 break;
7294 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007295
Dan Gohmanec51f642008-03-10 23:03:31 +00007296 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7297 Node->getOperand(0));
Evan Chenga8740032008-04-01 01:50:16 +00007298 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng4a2f6df2008-04-01 01:51:26 +00007299 // float to i32 etc. can be 'expanded' to a single node.
Evan Chenga8740032008-04-01 01:50:16 +00007300 ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007301 break;
7302 }
7303 }
7304
7305 // Make sure the resultant values have been legalized themselves, unless this
7306 // is a type that requires multi-step expansion.
7307 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7308 Lo = LegalizeOp(Lo);
Gabor Greif1c80d112008-08-28 21:40:38 +00007309 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007310 // Don't legalize the high part if it is expanded to a single node.
7311 Hi = LegalizeOp(Hi);
7312 }
7313
7314 // Remember in a map if the values will be reused later.
Dan Gohman55d19662008-07-07 17:46:23 +00007315 bool isNew =
7316 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007317 assert(isNew && "Value already expanded?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007318 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007319}
7320
7321/// SplitVectorOp - Given an operand of vector type, break it down into
7322/// two smaller values, still of vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00007323void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7324 SDValue &Hi) {
Duncan Sands92c43912008-06-06 12:08:01 +00007325 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007326 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007327 unsigned NumElements = Op.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007328 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00007329
Duncan Sands92c43912008-06-06 12:08:01 +00007330 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman4a365ad2007-11-15 21:15:26 +00007331
7332 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7333 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7334
Duncan Sands92c43912008-06-06 12:08:01 +00007335 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7336 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007337
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007338 // See if we already split it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007339 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007340 = SplitNodes.find(Op);
7341 if (I != SplitNodes.end()) {
7342 Lo = I->second.first;
7343 Hi = I->second.second;
7344 return;
7345 }
7346
7347 switch (Node->getOpcode()) {
7348 default:
7349#ifndef NDEBUG
7350 Node->dump(&DAG);
7351#endif
7352 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00007353 case ISD::UNDEF:
7354 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7355 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7356 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007357 case ISD::BUILD_PAIR:
7358 Lo = Node->getOperand(0);
7359 Hi = Node->getOperand(1);
7360 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007361 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007362 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7363 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007364 unsigned Index = Idx->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007365 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007366 if (Index < NewNumElts_Lo)
7367 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7368 DAG.getIntPtrConstant(Index));
7369 else
7370 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7371 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7372 break;
7373 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007374 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007375 Node->getOperand(1),
7376 Node->getOperand(2));
7377 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007378 break;
7379 }
Chris Lattner587c46d2007-11-19 21:16:54 +00007380 case ISD::VECTOR_SHUFFLE: {
7381 // Build the low part.
Dan Gohman8181bd12008-07-27 21:46:04 +00007382 SDValue Mask = Node->getOperand(2);
7383 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +00007384 MVT PtrVT = TLI.getPointerTy();
Chris Lattner587c46d2007-11-19 21:16:54 +00007385
7386 // Insert all of the elements from the input that are needed. We use
7387 // buildvector of extractelement here because the input vectors will have
7388 // to be legalized, so this makes the code simpler.
7389 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007390 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007391 if (IdxNode.getOpcode() == ISD::UNDEF) {
7392 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7393 continue;
7394 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007395 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007396 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007397 if (Idx >= NumElements) {
7398 InVec = Node->getOperand(1);
7399 Idx -= NumElements;
7400 }
7401 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7402 DAG.getConstant(Idx, PtrVT)));
7403 }
7404 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7405 Ops.clear();
7406
7407 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007408 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007409 if (IdxNode.getOpcode() == ISD::UNDEF) {
7410 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7411 continue;
7412 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007413 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007414 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007415 if (Idx >= NumElements) {
7416 InVec = Node->getOperand(1);
7417 Idx -= NumElements;
7418 }
7419 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7420 DAG.getConstant(Idx, PtrVT)));
7421 }
Mon P Wang2e89b112008-07-25 01:30:26 +00007422 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner587c46d2007-11-19 21:16:54 +00007423 break;
7424 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007425 case ISD::BUILD_VECTOR: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007426 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00007427 Node->op_begin()+NewNumElts_Lo);
7428 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007429
Dan Gohman8181bd12008-07-27 21:46:04 +00007430 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007431 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007432 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007433 break;
7434 }
7435 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00007436 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007437 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7438 if (NewNumSubvectors == 1) {
7439 Lo = Node->getOperand(0);
7440 Hi = Node->getOperand(1);
7441 } else {
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007442 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7443 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007444 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007445
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007446 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007447 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007448 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007449 }
7450 break;
7451 }
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007452 case ISD::EXTRACT_SUBVECTOR: {
7453 SDValue Vec = Op.getOperand(0);
7454 SDValue Idx = Op.getOperand(1);
7455 MVT IdxVT = Idx.getValueType();
7456
7457 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7458 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7459 if (CIdx) {
7460 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7461 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7462 IdxVT));
7463 } else {
7464 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7465 DAG.getConstant(NewNumElts_Lo, IdxVT));
7466 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7467 }
7468 break;
7469 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00007470 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007471 SDValue Cond = Node->getOperand(0);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007472
Dan Gohman8181bd12008-07-27 21:46:04 +00007473 SDValue LL, LH, RL, RH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007474 SplitVectorOp(Node->getOperand(1), LL, LH);
7475 SplitVectorOp(Node->getOperand(2), RL, RH);
7476
Duncan Sands92c43912008-06-06 12:08:01 +00007477 if (Cond.getValueType().isVector()) {
Dan Gohmand5d4c872007-10-17 14:48:28 +00007478 // Handle a vector merge.
Dan Gohman8181bd12008-07-27 21:46:04 +00007479 SDValue CL, CH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007480 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007481 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7482 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007483 } else {
7484 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00007485 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7486 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007487 }
7488 break;
7489 }
Chris Lattnerc7471452008-06-30 02:43:01 +00007490 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007491 SDValue CondLHS = Node->getOperand(0);
7492 SDValue CondRHS = Node->getOperand(1);
7493 SDValue CondCode = Node->getOperand(4);
Chris Lattnerc7471452008-06-30 02:43:01 +00007494
Dan Gohman8181bd12008-07-27 21:46:04 +00007495 SDValue LL, LH, RL, RH;
Chris Lattnerc7471452008-06-30 02:43:01 +00007496 SplitVectorOp(Node->getOperand(2), LL, LH);
7497 SplitVectorOp(Node->getOperand(3), RL, RH);
7498
7499 // Handle a simple select with vector operands.
7500 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7501 LL, RL, CondCode);
7502 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7503 LH, RH, CondCode);
7504 break;
7505 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00007506 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007507 SDValue LL, LH, RL, RH;
Nate Begeman9a1ce152008-05-12 19:40:03 +00007508 SplitVectorOp(Node->getOperand(0), LL, LH);
7509 SplitVectorOp(Node->getOperand(1), RL, RH);
7510 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7511 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7512 break;
7513 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007514 case ISD::ADD:
7515 case ISD::SUB:
7516 case ISD::MUL:
7517 case ISD::FADD:
7518 case ISD::FSUB:
7519 case ISD::FMUL:
7520 case ISD::SDIV:
7521 case ISD::UDIV:
7522 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007523 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007524 case ISD::AND:
7525 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00007526 case ISD::XOR:
7527 case ISD::UREM:
7528 case ISD::SREM:
7529 case ISD::FREM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007530 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007531 SplitVectorOp(Node->getOperand(0), LL, LH);
7532 SplitVectorOp(Node->getOperand(1), RL, RH);
7533
Nate Begeman4a365ad2007-11-15 21:15:26 +00007534 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7535 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007536 break;
7537 }
Dan Gohman29c3cef2008-08-14 20:04:46 +00007538 case ISD::FP_ROUND:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007539 case ISD::FPOWI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007540 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007541 SplitVectorOp(Node->getOperand(0), L, H);
7542
Nate Begeman4a365ad2007-11-15 21:15:26 +00007543 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7544 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00007545 break;
7546 }
7547 case ISD::CTTZ:
7548 case ISD::CTLZ:
7549 case ISD::CTPOP:
7550 case ISD::FNEG:
7551 case ISD::FABS:
7552 case ISD::FSQRT:
7553 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00007554 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007555 case ISD::FLOG:
7556 case ISD::FLOG2:
7557 case ISD::FLOG10:
7558 case ISD::FEXP:
7559 case ISD::FEXP2:
Nate Begeman78246ca2007-11-17 03:58:34 +00007560 case ISD::FP_TO_SINT:
7561 case ISD::FP_TO_UINT:
7562 case ISD::SINT_TO_FP:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007563 case ISD::UINT_TO_FP:
7564 case ISD::TRUNCATE:
7565 case ISD::ANY_EXTEND:
7566 case ISD::SIGN_EXTEND:
7567 case ISD::ZERO_EXTEND:
7568 case ISD::FP_EXTEND: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007569 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007570 SplitVectorOp(Node->getOperand(0), L, H);
7571
Nate Begeman4a365ad2007-11-15 21:15:26 +00007572 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7573 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00007574 break;
7575 }
Mon P Wang73d31542008-11-10 20:54:11 +00007576 case ISD::CONVERT_RNDSAT: {
7577 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7578 SDValue L, H;
7579 SplitVectorOp(Node->getOperand(0), L, H);
7580 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7581 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7582 SDValue STyOpL = DAG.getValueType(L.getValueType());
7583 SDValue STyOpH = DAG.getValueType(H.getValueType());
7584
7585 SDValue RndOp = Node->getOperand(3);
7586 SDValue SatOp = Node->getOperand(4);
7587
7588 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7589 RndOp, SatOp, CvtCode);
7590 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7591 RndOp, SatOp, CvtCode);
7592 break;
7593 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007594 case ISD::LOAD: {
7595 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007596 SDValue Ch = LD->getChain();
7597 SDValue Ptr = LD->getBasePtr();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007598 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007599 const Value *SV = LD->getSrcValue();
7600 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007601 MVT MemoryVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007602 unsigned Alignment = LD->getAlignment();
7603 bool isVolatile = LD->isVolatile();
7604
Dan Gohman29c3cef2008-08-14 20:04:46 +00007605 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7606 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7607
7608 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7609 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7610 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7611
7612 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7613 NewVT_Lo, Ch, Ptr, Offset,
7614 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7615 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007616 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00007617 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007618 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00007619 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00007620 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7621 NewVT_Hi, Ch, Ptr, Offset,
7622 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007623
7624 // Build a factor node to remember that this load is independent of the
7625 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00007626 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007627 Hi.getValue(1));
7628
7629 // Remember that we legalized the chain.
7630 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
7631 break;
7632 }
7633 case ISD::BIT_CONVERT: {
7634 // We know the result is a vector. The input may be either a vector or a
7635 // scalar value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007636 SDValue InOp = Node->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007637 if (!InOp.getValueType().isVector() ||
7638 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007639 // The input is a scalar or single-element vector.
7640 // Lower to a store/load so that it can be split.
7641 // FIXME: this could be improved probably.
Mon P Wang36b59ac2008-07-15 05:28:34 +00007642 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7643 Op.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00007644 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greif1c80d112008-08-28 21:40:38 +00007645 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007646
Dan Gohman8181bd12008-07-27 21:46:04 +00007647 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00007648 InOp, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007649 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00007650 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007651 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007652 }
7653 // Split the vector and convert each of the pieces now.
7654 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007655 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7656 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007657 break;
7658 }
7659 }
7660
7661 // Remember in a map if the values will be reused later.
7662 bool isNew =
7663 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
7664 assert(isNew && "Value already split?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007665 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007666}
7667
7668
7669/// ScalarizeVectorOp - Given an operand of single-element vector type
7670/// (e.g. v1f32), convert it into the equivalent operation that returns a
7671/// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007672SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00007673 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007674 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007675 MVT NewVT = Op.getValueType().getVectorElementType();
7676 assert(Op.getValueType().getVectorNumElements() == 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007677
7678 // See if we already scalarized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007679 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007680 if (I != ScalarizedNodes.end()) return I->second;
7681
Dan Gohman8181bd12008-07-27 21:46:04 +00007682 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007683 switch (Node->getOpcode()) {
7684 default:
7685#ifndef NDEBUG
7686 Node->dump(&DAG); cerr << "\n";
7687#endif
7688 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7689 case ISD::ADD:
7690 case ISD::FADD:
7691 case ISD::SUB:
7692 case ISD::FSUB:
7693 case ISD::MUL:
7694 case ISD::FMUL:
7695 case ISD::SDIV:
7696 case ISD::UDIV:
7697 case ISD::FDIV:
7698 case ISD::SREM:
7699 case ISD::UREM:
7700 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007701 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007702 case ISD::AND:
7703 case ISD::OR:
7704 case ISD::XOR:
7705 Result = DAG.getNode(Node->getOpcode(),
7706 NewVT,
7707 ScalarizeVectorOp(Node->getOperand(0)),
7708 ScalarizeVectorOp(Node->getOperand(1)));
7709 break;
7710 case ISD::FNEG:
7711 case ISD::FABS:
7712 case ISD::FSQRT:
7713 case ISD::FSIN:
7714 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007715 case ISD::FLOG:
7716 case ISD::FLOG2:
7717 case ISD::FLOG10:
7718 case ISD::FEXP:
7719 case ISD::FEXP2:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007720 case ISD::FP_TO_SINT:
7721 case ISD::FP_TO_UINT:
7722 case ISD::SINT_TO_FP:
7723 case ISD::UINT_TO_FP:
7724 case ISD::SIGN_EXTEND:
7725 case ISD::ZERO_EXTEND:
7726 case ISD::ANY_EXTEND:
7727 case ISD::TRUNCATE:
7728 case ISD::FP_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007729 Result = DAG.getNode(Node->getOpcode(),
7730 NewVT,
7731 ScalarizeVectorOp(Node->getOperand(0)));
7732 break;
Mon P Wang73d31542008-11-10 20:54:11 +00007733 case ISD::CONVERT_RNDSAT: {
7734 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7735 Result = DAG.getConvertRndSat(NewVT, Op0,
7736 DAG.getValueType(NewVT),
7737 DAG.getValueType(Op0.getValueType()),
7738 Node->getOperand(3),
7739 Node->getOperand(4),
7740 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7741 break;
7742 }
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007743 case ISD::FPOWI:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007744 case ISD::FP_ROUND:
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007745 Result = DAG.getNode(Node->getOpcode(),
7746 NewVT,
7747 ScalarizeVectorOp(Node->getOperand(0)),
7748 Node->getOperand(1));
7749 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007750 case ISD::LOAD: {
7751 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007752 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7753 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman29c3cef2008-08-14 20:04:46 +00007754 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007755 const Value *SV = LD->getSrcValue();
7756 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007757 MVT MemoryVT = LD->getMemoryVT();
7758 unsigned Alignment = LD->getAlignment();
7759 bool isVolatile = LD->isVolatile();
7760
7761 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7762 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7763
7764 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7765 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7766 MemoryVT.getVectorElementType(),
7767 isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007768
7769 // Remember that we legalized the chain.
7770 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7771 break;
7772 }
7773 case ISD::BUILD_VECTOR:
7774 Result = Node->getOperand(0);
7775 break;
7776 case ISD::INSERT_VECTOR_ELT:
7777 // Returning the inserted scalar element.
7778 Result = Node->getOperand(1);
7779 break;
7780 case ISD::CONCAT_VECTORS:
7781 assert(Node->getOperand(0).getValueType() == NewVT &&
7782 "Concat of non-legal vectors not yet supported!");
7783 Result = Node->getOperand(0);
7784 break;
7785 case ISD::VECTOR_SHUFFLE: {
7786 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007787 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007788 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007789 Result = ScalarizeVectorOp(Node->getOperand(1));
7790 else
7791 Result = ScalarizeVectorOp(Node->getOperand(0));
7792 break;
7793 }
7794 case ISD::EXTRACT_SUBVECTOR:
Mon P Wang927daf52008-11-06 22:52:21 +00007795 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007796 Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007797 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007798 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007799 SDValue Op0 = Op.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007800 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng2cc16e72008-05-16 17:19:05 +00007801 Op0 = ScalarizeVectorOp(Op0);
7802 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007803 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007804 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007805 case ISD::SELECT:
7806 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
7807 ScalarizeVectorOp(Op.getOperand(1)),
7808 ScalarizeVectorOp(Op.getOperand(2)));
7809 break;
Chris Lattnerc7471452008-06-30 02:43:01 +00007810 case ISD::SELECT_CC:
7811 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7812 Node->getOperand(1),
7813 ScalarizeVectorOp(Op.getOperand(2)),
7814 ScalarizeVectorOp(Op.getOperand(3)),
7815 Node->getOperand(4));
7816 break;
Nate Begeman78ca4f92008-05-12 23:09:43 +00007817 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007818 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7819 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Nate Begeman78ca4f92008-05-12 23:09:43 +00007820 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7821 Op.getOperand(2));
7822 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7823 DAG.getConstant(-1ULL, NewVT),
7824 DAG.getConstant(0ULL, NewVT));
7825 break;
7826 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007827 }
7828
7829 if (TLI.isTypeLegal(NewVT))
7830 Result = LegalizeOp(Result);
7831 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7832 assert(isNew && "Value already scalarized?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007833 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007834 return Result;
7835}
7836
7837
Mon P Wang1448aad2008-10-30 08:01:45 +00007838SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7839 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7840 if (I != WidenNodes.end()) return I->second;
7841
7842 MVT VT = Op.getValueType();
7843 assert(VT.isVector() && "Cannot widen non-vector type!");
7844
7845 SDValue Result;
7846 SDNode *Node = Op.getNode();
7847 MVT EVT = VT.getVectorElementType();
7848
7849 unsigned NumElts = VT.getVectorNumElements();
7850 unsigned NewNumElts = WidenVT.getVectorNumElements();
7851 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7852 assert(NewNumElts < 17);
7853
7854 // When widen is called, it is assumed that it is more efficient to use a
7855 // wide type. The default action is to widen to operation to a wider legal
7856 // vector type and then do the operation if it is legal by calling LegalizeOp
7857 // again. If there is no vector equivalent, we will unroll the operation, do
7858 // it, and rebuild the vector. If most of the operations are vectorizible to
7859 // the legal type, the resulting code will be more efficient. If this is not
7860 // the case, the resulting code will preform badly as we end up generating
7861 // code to pack/unpack the results. It is the function that calls widen
Mon P Wanga5a239f2008-11-06 05:31:54 +00007862 // that is responsible for seeing this doesn't happen.
Mon P Wang1448aad2008-10-30 08:01:45 +00007863 switch (Node->getOpcode()) {
7864 default:
7865#ifndef NDEBUG
7866 Node->dump(&DAG);
7867#endif
7868 assert(0 && "Unexpected operation in WidenVectorOp!");
7869 break;
7870 case ISD::CopyFromReg:
Mon P Wang257e1c72008-11-15 06:05:52 +00007871 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang1448aad2008-10-30 08:01:45 +00007872 case ISD::Constant:
7873 case ISD::ConstantFP:
7874 // To build a vector of these elements, clients should call BuildVector
7875 // and with each element instead of creating a node with a vector type
7876 assert(0 && "Unexpected operation in WidenVectorOp!");
7877 case ISD::VAARG:
7878 // Variable Arguments with vector types doesn't make any sense to me
7879 assert(0 && "Unexpected operation in WidenVectorOp!");
7880 break;
Mon P Wang257e1c72008-11-15 06:05:52 +00007881 case ISD::UNDEF:
7882 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7883 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00007884 case ISD::BUILD_VECTOR: {
7885 // Build a vector with undefined for the new nodes
7886 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7887 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7888 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7889 }
7890 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7891 break;
7892 }
7893 case ISD::INSERT_VECTOR_ELT: {
7894 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7895 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7896 Node->getOperand(1), Node->getOperand(2));
7897 break;
7898 }
7899 case ISD::VECTOR_SHUFFLE: {
7900 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7901 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7902 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7903 // used as permutation array. We build the vector here instead of widening
7904 // because we don't want to legalize and have it turned to something else.
7905 SDValue PermOp = Node->getOperand(2);
7906 SDValueVector NewOps;
7907 MVT PVT = PermOp.getValueType().getVectorElementType();
7908 for (unsigned i = 0; i < NumElts; ++i) {
7909 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7910 NewOps.push_back(PermOp.getOperand(i));
7911 } else {
7912 unsigned Idx =
7913 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
7914 if (Idx < NumElts) {
7915 NewOps.push_back(PermOp.getOperand(i));
7916 }
7917 else {
7918 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7919 PermOp.getOperand(i).getValueType()));
7920 }
7921 }
7922 }
7923 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7924 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
7925 }
7926
7927 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
7928 MVT::getVectorVT(PVT, NewOps.size()),
7929 &NewOps[0], NewOps.size());
7930
7931 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
7932 break;
7933 }
7934 case ISD::LOAD: {
7935 // If the load widen returns true, we can use a single load for the
7936 // vector. Otherwise, it is returning a token factor for multiple
7937 // loads.
7938 SDValue TFOp;
7939 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
7940 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
7941 else
7942 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
7943 break;
7944 }
7945
7946 case ISD::BIT_CONVERT: {
7947 SDValue Tmp1 = Node->getOperand(0);
7948 // Converts between two different types so we need to determine
7949 // the correct widen type for the input operand.
7950 MVT TVT = Tmp1.getValueType();
7951 assert(TVT.isVector() && "can not widen non vector type");
7952 MVT TEVT = TVT.getVectorElementType();
7953 assert(WidenVT.getSizeInBits() % EVT.getSizeInBits() == 0 &&
7954 "can not widen bit bit convert that are not multiple of element type");
7955 MVT TWidenVT = MVT::getVectorVT(TEVT,
7956 WidenVT.getSizeInBits()/EVT.getSizeInBits());
7957 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7958 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
7959 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7960
7961 TargetLowering::LegalizeAction action =
7962 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7963 switch (action) {
7964 default: assert(0 && "action not supported");
7965 case TargetLowering::Legal:
7966 break;
7967 case TargetLowering::Promote:
7968 // We defer the promotion to when we legalize the op
7969 break;
7970 case TargetLowering::Expand:
7971 // Expand the operation into a bunch of nasty scalar code.
7972 Result = LegalizeOp(UnrollVectorOp(Result));
7973 break;
7974 }
7975 break;
7976 }
7977
7978 case ISD::SINT_TO_FP:
7979 case ISD::UINT_TO_FP:
7980 case ISD::FP_TO_SINT:
7981 case ISD::FP_TO_UINT: {
7982 SDValue Tmp1 = Node->getOperand(0);
7983 // Converts between two different types so we need to determine
7984 // the correct widen type for the input operand.
7985 MVT TVT = Tmp1.getValueType();
7986 assert(TVT.isVector() && "can not widen non vector type");
7987 MVT TEVT = TVT.getVectorElementType();
7988 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
7989 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7990 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
7991 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang1448aad2008-10-30 08:01:45 +00007992 break;
7993 }
7994
7995 case ISD::FP_EXTEND:
7996 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
7997 case ISD::TRUNCATE:
7998 case ISD::SIGN_EXTEND:
7999 case ISD::ZERO_EXTEND:
8000 case ISD::ANY_EXTEND:
8001 case ISD::FP_ROUND:
8002 case ISD::SIGN_EXTEND_INREG:
8003 case ISD::FABS:
8004 case ISD::FNEG:
8005 case ISD::FSQRT:
8006 case ISD::FSIN:
Mon P Wang257e1c72008-11-15 06:05:52 +00008007 case ISD::FCOS:
8008 case ISD::CTPOP:
8009 case ISD::CTTZ:
8010 case ISD::CTLZ: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008011 // Unary op widening
8012 SDValue Tmp1;
Mon P Wang1448aad2008-10-30 08:01:45 +00008013 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8014 assert(Tmp1.getValueType() == WidenVT);
8015 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang1448aad2008-10-30 08:01:45 +00008016 break;
8017 }
Mon P Wang73d31542008-11-10 20:54:11 +00008018 case ISD::CONVERT_RNDSAT: {
8019 SDValue RndOp = Node->getOperand(3);
8020 SDValue SatOp = Node->getOperand(4);
Mon P Wang73d31542008-11-10 20:54:11 +00008021 SDValue SrcOp = Node->getOperand(0);
8022
8023 // Converts between two different types so we need to determine
8024 // the correct widen type for the input operand.
8025 MVT SVT = SrcOp.getValueType();
8026 assert(SVT.isVector() && "can not widen non vector type");
8027 MVT SEVT = SVT.getVectorElementType();
8028 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8029
8030 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8031 assert(SrcOp.getValueType() == WidenVT);
8032 SDValue DTyOp = DAG.getValueType(WidenVT);
8033 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8034 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8035
8036 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
8037 RndOp, SatOp, CvtCode);
Mon P Wang73d31542008-11-10 20:54:11 +00008038 break;
8039 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008040 case ISD::FPOW:
8041 case ISD::FPOWI:
8042 case ISD::ADD:
8043 case ISD::SUB:
8044 case ISD::MUL:
8045 case ISD::MULHS:
8046 case ISD::MULHU:
8047 case ISD::AND:
8048 case ISD::OR:
8049 case ISD::XOR:
8050 case ISD::FADD:
8051 case ISD::FSUB:
8052 case ISD::FMUL:
8053 case ISD::SDIV:
8054 case ISD::SREM:
8055 case ISD::FDIV:
8056 case ISD::FREM:
8057 case ISD::FCOPYSIGN:
8058 case ISD::UDIV:
8059 case ISD::UREM:
8060 case ISD::BSWAP: {
8061 // Binary op widening
Mon P Wang1448aad2008-10-30 08:01:45 +00008062 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8063 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8064 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8065 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
Mon P Wang1448aad2008-10-30 08:01:45 +00008066 break;
8067 }
8068
8069 case ISD::SHL:
8070 case ISD::SRA:
8071 case ISD::SRL: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008072 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8073 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangd5638262008-12-02 07:35:08 +00008074 SDValue ShOp = Node->getOperand(1);
8075 MVT ShVT = ShOp.getValueType();
8076 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8077 WidenVT.getVectorNumElements());
8078 ShOp = WidenVectorOp(ShOp, NewShVT);
8079 assert(ShOp.getValueType() == NewShVT);
8080 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, ShOp);
Mon P Wang1448aad2008-10-30 08:01:45 +00008081 break;
8082 }
Mon P Wangd5638262008-12-02 07:35:08 +00008083
Mon P Wang1448aad2008-10-30 08:01:45 +00008084 case ISD::EXTRACT_VECTOR_ELT: {
8085 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8086 assert(Tmp1.getValueType() == WidenVT);
8087 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8088 break;
8089 }
8090 case ISD::CONCAT_VECTORS: {
8091 // We concurrently support only widen on a multiple of the incoming vector.
8092 // We could widen on a multiple of the incoming operand if necessary.
8093 unsigned NumConcat = NewNumElts / NumElts;
8094 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Mon P Wangd5638262008-12-02 07:35:08 +00008095 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
Mon P Wang1448aad2008-10-30 08:01:45 +00008096 SmallVector<SDValue, 8> MOps;
8097 MOps.push_back(Op);
8098 for (unsigned i = 1; i != NumConcat; ++i) {
8099 MOps.push_back(UndefVal);
8100 }
8101 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8102 &MOps[0], MOps.size()));
8103 break;
8104 }
8105 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang257e1c72008-11-15 06:05:52 +00008106 SDValue Tmp1 = Node->getOperand(0);
8107 SDValue Idx = Node->getOperand(1);
8108 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8109 if (CIdx && CIdx->getZExtValue() == 0) {
8110 // Since we are access the start of the vector, the incoming
8111 // vector type might be the proper.
8112 MVT Tmp1VT = Tmp1.getValueType();
8113 if (Tmp1VT == WidenVT)
8114 return Tmp1;
8115 else {
8116 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8117 if (Tmp1VTNumElts < NewNumElts)
8118 Result = WidenVectorOp(Tmp1, WidenVT);
8119 else
8120 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8121 }
8122 } else if (NewNumElts % NumElts == 0) {
8123 // Widen the extracted subvector.
8124 unsigned NumConcat = NewNumElts / NumElts;
8125 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8126 SmallVector<SDValue, 8> MOps;
8127 MOps.push_back(Op);
8128 for (unsigned i = 1; i != NumConcat; ++i) {
8129 MOps.push_back(UndefVal);
8130 }
8131 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8132 &MOps[0], MOps.size()));
8133 } else {
8134 assert(0 && "can not widen extract subvector");
8135 // This could be implemented using insert and build vector but I would
8136 // like to see when this happens.
8137 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008138 break;
8139 }
8140
8141 case ISD::SELECT: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008142 // Determine new condition widen type and widen
8143 SDValue Cond1 = Node->getOperand(0);
8144 MVT CondVT = Cond1.getValueType();
8145 assert(CondVT.isVector() && "can not widen non vector type");
8146 MVT CondEVT = CondVT.getVectorElementType();
8147 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8148 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8149 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8150
8151 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8152 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8153 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8154 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang1448aad2008-10-30 08:01:45 +00008155 break;
8156 }
8157
8158 case ISD::SELECT_CC: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008159 // Determine new condition widen type and widen
8160 SDValue Cond1 = Node->getOperand(0);
8161 SDValue Cond2 = Node->getOperand(1);
8162 MVT CondVT = Cond1.getValueType();
8163 assert(CondVT.isVector() && "can not widen non vector type");
8164 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8165 MVT CondEVT = CondVT.getVectorElementType();
8166 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8167 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8168 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8169 assert(Cond1.getValueType() == CondWidenVT &&
8170 Cond2.getValueType() == CondWidenVT && "condition not widen");
8171
8172 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8173 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8174 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8175 "operands not widen");
8176 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8177 Tmp2, Node->getOperand(4));
Mon P Wang1448aad2008-10-30 08:01:45 +00008178 break;
Mon P Wang42ac14e2008-10-30 18:21:52 +00008179 }
8180 case ISD::VSETCC: {
8181 // Determine widen for the operand
8182 SDValue Tmp1 = Node->getOperand(0);
8183 MVT TmpVT = Tmp1.getValueType();
8184 assert(TmpVT.isVector() && "can not widen non vector type");
8185 MVT TmpEVT = TmpVT.getVectorElementType();
8186 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8187 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8188 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
8189 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
8190 Node->getOperand(2));
Mon P Wang1448aad2008-10-30 08:01:45 +00008191 break;
8192 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008193 case ISD::ATOMIC_CMP_SWAP_8:
8194 case ISD::ATOMIC_CMP_SWAP_16:
8195 case ISD::ATOMIC_CMP_SWAP_32:
8196 case ISD::ATOMIC_CMP_SWAP_64:
8197 case ISD::ATOMIC_LOAD_ADD_8:
8198 case ISD::ATOMIC_LOAD_SUB_8:
8199 case ISD::ATOMIC_LOAD_AND_8:
8200 case ISD::ATOMIC_LOAD_OR_8:
8201 case ISD::ATOMIC_LOAD_XOR_8:
8202 case ISD::ATOMIC_LOAD_NAND_8:
8203 case ISD::ATOMIC_LOAD_MIN_8:
8204 case ISD::ATOMIC_LOAD_MAX_8:
8205 case ISD::ATOMIC_LOAD_UMIN_8:
8206 case ISD::ATOMIC_LOAD_UMAX_8:
8207 case ISD::ATOMIC_SWAP_8:
8208 case ISD::ATOMIC_LOAD_ADD_16:
8209 case ISD::ATOMIC_LOAD_SUB_16:
8210 case ISD::ATOMIC_LOAD_AND_16:
8211 case ISD::ATOMIC_LOAD_OR_16:
8212 case ISD::ATOMIC_LOAD_XOR_16:
8213 case ISD::ATOMIC_LOAD_NAND_16:
8214 case ISD::ATOMIC_LOAD_MIN_16:
8215 case ISD::ATOMIC_LOAD_MAX_16:
8216 case ISD::ATOMIC_LOAD_UMIN_16:
8217 case ISD::ATOMIC_LOAD_UMAX_16:
8218 case ISD::ATOMIC_SWAP_16:
8219 case ISD::ATOMIC_LOAD_ADD_32:
8220 case ISD::ATOMIC_LOAD_SUB_32:
8221 case ISD::ATOMIC_LOAD_AND_32:
8222 case ISD::ATOMIC_LOAD_OR_32:
8223 case ISD::ATOMIC_LOAD_XOR_32:
8224 case ISD::ATOMIC_LOAD_NAND_32:
8225 case ISD::ATOMIC_LOAD_MIN_32:
8226 case ISD::ATOMIC_LOAD_MAX_32:
8227 case ISD::ATOMIC_LOAD_UMIN_32:
8228 case ISD::ATOMIC_LOAD_UMAX_32:
8229 case ISD::ATOMIC_SWAP_32:
8230 case ISD::ATOMIC_LOAD_ADD_64:
8231 case ISD::ATOMIC_LOAD_SUB_64:
8232 case ISD::ATOMIC_LOAD_AND_64:
8233 case ISD::ATOMIC_LOAD_OR_64:
8234 case ISD::ATOMIC_LOAD_XOR_64:
8235 case ISD::ATOMIC_LOAD_NAND_64:
8236 case ISD::ATOMIC_LOAD_MIN_64:
8237 case ISD::ATOMIC_LOAD_MAX_64:
8238 case ISD::ATOMIC_LOAD_UMIN_64:
8239 case ISD::ATOMIC_LOAD_UMAX_64:
8240 case ISD::ATOMIC_SWAP_64: {
8241 // For now, we assume that using vectors for these operations don't make
8242 // much sense so we just split it. We return an empty result
8243 SDValue X, Y;
8244 SplitVectorOp(Op, X, Y);
8245 return Result;
8246 break;
8247 }
8248
8249 } // end switch (Node->getOpcode())
8250
8251 assert(Result.getNode() && "Didn't set a result!");
8252 if (Result != Op)
8253 Result = LegalizeOp(Result);
8254
Mon P Wanga5a239f2008-11-06 05:31:54 +00008255 AddWidenedOperand(Op, Result);
Mon P Wang1448aad2008-10-30 08:01:45 +00008256 return Result;
8257}
8258
8259// Utility function to find a legal vector type and its associated element
8260// type from a preferred width and whose vector type must be the same size
8261// as the VVT.
8262// TLI: Target lowering used to determine legal types
8263// Width: Preferred width of element type
8264// VVT: Vector value type whose size we must match.
8265// Returns VecEVT and EVT - the vector type and its associated element type
8266static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8267 MVT& EVT, MVT& VecEVT) {
8268 // We start with the preferred width, make it a power of 2 and see if
8269 // we can find a vector type of that width. If not, we reduce it by
8270 // another power of 2. If we have widen the type, a vector of bytes should
8271 // always be legal.
8272 assert(TLI.isTypeLegal(VVT));
8273 unsigned EWidth = Width + 1;
8274 do {
8275 assert(EWidth > 0);
8276 EWidth = (1 << Log2_32(EWidth-1));
8277 EVT = MVT::getIntegerVT(EWidth);
8278 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8279 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8280 } while (!TLI.isTypeLegal(VecEVT) ||
8281 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8282}
8283
8284SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8285 SDValue Chain,
8286 SDValue BasePtr,
8287 const Value *SV,
8288 int SVOffset,
8289 unsigned Alignment,
8290 bool isVolatile,
8291 unsigned LdWidth,
8292 MVT ResType) {
8293 // We assume that we have good rules to handle loading power of two loads so
8294 // we break down the operations to power of 2 loads. The strategy is to
8295 // load the largest power of 2 that we can easily transform to a legal vector
8296 // and then insert into that vector, and the cast the result into the legal
8297 // vector that we want. This avoids unnecessary stack converts.
8298 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8299 // the load is nonvolatile, we an use a wider load for the value.
8300 // Find a vector length we can load a large chunk
8301 MVT EVT, VecEVT;
8302 unsigned EVTWidth;
8303 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8304 EVTWidth = EVT.getSizeInBits();
8305
8306 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8307 isVolatile, Alignment);
8308 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8309 LdChain.push_back(LdOp.getValue(1));
8310
8311 // Check if we can load the element with one instruction
8312 if (LdWidth == EVTWidth) {
8313 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8314 }
8315
8316 // The vector element order is endianness dependent.
8317 unsigned Idx = 1;
8318 LdWidth -= EVTWidth;
8319 unsigned Offset = 0;
8320
8321 while (LdWidth > 0) {
8322 unsigned Increment = EVTWidth / 8;
8323 Offset += Increment;
8324 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8325 DAG.getIntPtrConstant(Increment));
8326
8327 if (LdWidth < EVTWidth) {
8328 // Our current type we are using is too large, use a smaller size by
8329 // using a smaller power of 2
8330 unsigned oEVTWidth = EVTWidth;
8331 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8332 EVTWidth = EVT.getSizeInBits();
8333 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008334 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang1448aad2008-10-30 08:01:45 +00008335 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8336 }
8337
8338 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8339 SVOffset+Offset, isVolatile,
8340 MinAlign(Alignment, Offset));
8341 LdChain.push_back(LdOp.getValue(1));
8342 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8343 DAG.getIntPtrConstant(Idx++));
8344
8345 LdWidth -= EVTWidth;
8346 }
8347
8348 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8349}
8350
8351bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8352 SDValue& TFOp,
8353 SDValue Op,
8354 MVT NVT) {
8355 // TODO: Add support for ConcatVec and the ability to load many vector
8356 // types (e.g., v4i8). This will not work when a vector register
8357 // to memory mapping is strange (e.g., vector elements are not
8358 // stored in some sequential order).
8359
8360 // It must be true that the widen vector type is bigger than where
8361 // we need to load from.
8362 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8363 MVT LdVT = LD->getMemoryVT();
8364 assert(LdVT.isVector() && NVT.isVector());
8365 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8366
8367 // Load information
8368 SDValue Chain = LD->getChain();
8369 SDValue BasePtr = LD->getBasePtr();
8370 int SVOffset = LD->getSrcValueOffset();
8371 unsigned Alignment = LD->getAlignment();
8372 bool isVolatile = LD->isVolatile();
8373 const Value *SV = LD->getSrcValue();
8374 unsigned int LdWidth = LdVT.getSizeInBits();
8375
8376 // Load value as a large register
8377 SDValueVector LdChain;
8378 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8379 Alignment, isVolatile, LdWidth, NVT);
8380
8381 if (LdChain.size() == 1) {
8382 TFOp = LdChain[0];
8383 return true;
8384 }
8385 else {
8386 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8387 return false;
8388 }
8389}
8390
8391
8392void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8393 SDValue Chain,
8394 SDValue BasePtr,
8395 const Value *SV,
8396 int SVOffset,
8397 unsigned Alignment,
8398 bool isVolatile,
Mon P Wang257e1c72008-11-15 06:05:52 +00008399 SDValue ValOp,
Mon P Wang1448aad2008-10-30 08:01:45 +00008400 unsigned StWidth) {
8401 // Breaks the stores into a series of power of 2 width stores. For any
8402 // width, we convert the vector to the vector of element size that we
8403 // want to store. This avoids requiring a stack convert.
8404
8405 // Find a width of the element type we can store with
8406 MVT VVT = ValOp.getValueType();
8407 MVT EVT, VecEVT;
8408 unsigned EVTWidth;
8409 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8410 EVTWidth = EVT.getSizeInBits();
8411
8412 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8413 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang927daf52008-11-06 22:52:21 +00008414 DAG.getIntPtrConstant(0));
Mon P Wang1448aad2008-10-30 08:01:45 +00008415 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8416 isVolatile, Alignment);
8417 StChain.push_back(StOp);
8418
8419 // Check if we are done
8420 if (StWidth == EVTWidth) {
8421 return;
8422 }
8423
8424 unsigned Idx = 1;
8425 StWidth -= EVTWidth;
8426 unsigned Offset = 0;
8427
8428 while (StWidth > 0) {
8429 unsigned Increment = EVTWidth / 8;
8430 Offset += Increment;
8431 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8432 DAG.getIntPtrConstant(Increment));
8433
8434 if (StWidth < EVTWidth) {
8435 // Our current type we are using is too large, use a smaller size by
8436 // using a smaller power of 2
8437 unsigned oEVTWidth = EVTWidth;
8438 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8439 EVTWidth = EVT.getSizeInBits();
8440 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008441 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang1448aad2008-10-30 08:01:45 +00008442 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8443 }
8444
8445 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang257e1c72008-11-15 06:05:52 +00008446 DAG.getIntPtrConstant(Idx++));
Mon P Wang1448aad2008-10-30 08:01:45 +00008447 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8448 SVOffset + Offset, isVolatile,
8449 MinAlign(Alignment, Offset)));
8450 StWidth -= EVTWidth;
8451 }
8452}
8453
8454
8455SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8456 SDValue Chain,
8457 SDValue BasePtr) {
8458 // TODO: It might be cleaner if we can use SplitVector and have more legal
8459 // vector types that can be stored into memory (e.g., v4xi8 can
8460 // be stored as a word). This will not work when a vector register
8461 // to memory mapping is strange (e.g., vector elements are not
8462 // stored in some sequential order).
8463
8464 MVT StVT = ST->getMemoryVT();
8465 SDValue ValOp = ST->getValue();
8466
8467 // Check if we have widen this node with another value
8468 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8469 if (I != WidenNodes.end())
8470 ValOp = I->second;
8471
8472 MVT VVT = ValOp.getValueType();
8473
8474 // It must be true that we the widen vector type is bigger than where
8475 // we need to store.
8476 assert(StVT.isVector() && VVT.isVector());
8477 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8478 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8479
8480 // Store value
8481 SDValueVector StChain;
8482 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8483 ST->getSrcValueOffset(), ST->getAlignment(),
8484 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8485 if (StChain.size() == 1)
8486 return StChain[0];
8487 else
8488 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8489}
8490
8491
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008492// SelectionDAG::Legalize - This is the entry point for the file.
8493//
8494void SelectionDAG::Legalize() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008495 /// run - This is the main entry point to this class.
8496 ///
8497 SelectionDAGLegalize(*this).LegalizeDAG();
8498}
8499