blob: 18edb40239e3a872b061b77661f09291f123160b [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"
Devang Patelfcf1c752009-01-13 00:35:13 +000019#include "llvm/CodeGen/DwarfWriter.h"
20#include "llvm/Analysis/DebugInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000021#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga448bc42007-08-16 23:50:06 +000022#include "llvm/Target/TargetFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Target/TargetLowering.h"
24#include "llvm/Target/TargetData.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetOptions.h"
Dan Gohmane8b391e2008-04-12 04:36:06 +000027#include "llvm/Target/TargetSubtarget.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/CallingConv.h"
29#include "llvm/Constants.h"
30#include "llvm/DerivedTypes.h"
Devang Patelfcf1c752009-01-13 00:35:13 +000031#include "llvm/GlobalVariable.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Compiler.h"
Duncan Sandsa3691432007-10-28 12:59:45 +000034#include "llvm/Support/MathExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/ADT/DenseMap.h"
36#include "llvm/ADT/SmallVector.h"
37#include "llvm/ADT/SmallPtrSet.h"
38#include <map>
39using namespace llvm;
40
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041//===----------------------------------------------------------------------===//
42/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
43/// hacks on it until the target machine can handle it. This involves
44/// eliminating value sizes the machine cannot handle (promoting small sizes to
45/// large sizes or splitting up large values into small values) as well as
46/// eliminating operations the machine cannot handle.
47///
48/// This code also does a small amount of optimization and recognition of idioms
49/// as part of its processing. For example, if a target does not support a
50/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
51/// will attempt merge setcc and brc instructions into brcc's.
52///
53namespace {
54class VISIBILITY_HIDDEN SelectionDAGLegalize {
55 TargetLowering &TLI;
56 SelectionDAG &DAG;
Duncan Sandse016a2e2008-12-14 09:43:15 +000057 bool TypesNeedLegalizing;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
59 // Libcall insertion helpers.
60
61 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
62 /// legalized. We use this to ensure that calls are properly serialized
63 /// against each other, including inserted libcalls.
Dan Gohman8181bd12008-07-27 21:46:04 +000064 SDValue LastCALLSEQ_END;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065
66 /// IsLegalizingCall - This member is used *only* for purposes of providing
67 /// helpful assertions that a libcall isn't created while another call is
68 /// being legalized (which could lead to non-serialized call sequences).
69 bool IsLegalizingCall;
70
71 enum LegalizeAction {
72 Legal, // The target natively supports this operation.
73 Promote, // This operation should be executed in a larger type.
74 Expand // Try to expand this to other ops, otherwise use a libcall.
75 };
76
77 /// ValueTypeActions - This is a bitvector that contains two bits for each
78 /// value type, where the two bits correspond to the LegalizeAction enum.
79 /// This can be queried with "getTypeAction(VT)".
80 TargetLowering::ValueTypeActionImpl ValueTypeActions;
81
82 /// LegalizedNodes - For nodes that are of legal width, and that have more
83 /// than one use, this map indicates what regularized operand to use. This
84 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000085 DenseMap<SDValue, SDValue> LegalizedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086
87 /// PromotedNodes - For nodes that are below legal width, and that have more
88 /// than one use, this map indicates what promoted value to use. This allows
89 /// us to avoid promoting the same thing more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000090 DenseMap<SDValue, SDValue> PromotedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091
92 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang1448aad2008-10-30 08:01:45 +000093 /// which operands are the expanded version of the input. This allows
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 /// us to avoid expanding the same node more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000095 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096
97 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang1448aad2008-10-30 08:01:45 +000098 /// which operands are the split version of the input. This allows us
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 /// to avoid splitting the same node more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +0000100 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101
102 /// ScalarizedNodes - For nodes that need to be converted from vector types to
103 /// scalar types, this contains the mapping of ones we have already
104 /// processed to the result.
Dan Gohman8181bd12008-07-27 21:46:04 +0000105 std::map<SDValue, SDValue> ScalarizedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
Mon P Wanga5a239f2008-11-06 05:31:54 +0000107 /// WidenNodes - For nodes that need to be widened from one vector type to
108 /// another, this contains the mapping of those that we have already widen.
109 /// This allows us to avoid widening more than once.
Mon P Wang1448aad2008-10-30 08:01:45 +0000110 std::map<SDValue, SDValue> WidenNodes;
111
Dan Gohman8181bd12008-07-27 21:46:04 +0000112 void AddLegalizedOperand(SDValue From, SDValue To) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 LegalizedNodes.insert(std::make_pair(From, To));
114 // If someone requests legalization of the new node, return itself.
115 if (From != To)
116 LegalizedNodes.insert(std::make_pair(To, To));
117 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000118 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman55d19662008-07-07 17:46:23 +0000119 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 assert(isNew && "Got into the map somehow?");
Evan Chengcf576fd2008-11-24 07:09:49 +0000121 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 // If someone requests legalization of the new node, return itself.
123 LegalizedNodes.insert(std::make_pair(To, To));
124 }
Mon P Wanga5a239f2008-11-06 05:31:54 +0000125 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang1448aad2008-10-30 08:01:45 +0000126 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
127 assert(isNew && "Got into the map somehow?");
Evan Chengcf576fd2008-11-24 07:09:49 +0000128 isNew = isNew;
Mon P Wang1448aad2008-10-30 08:01:45 +0000129 // If someone requests legalization of the new node, return itself.
130 LegalizedNodes.insert(std::make_pair(To, To));
131 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132
133public:
Duncan Sandse016a2e2008-12-14 09:43:15 +0000134 explicit SelectionDAGLegalize(SelectionDAG &DAG, bool TypesNeedLegalizing);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135
136 /// getTypeAction - Return how we should legalize values of this type, either
137 /// it is already legal or we need to expand it into multiple registers of
138 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands92c43912008-06-06 12:08:01 +0000139 LegalizeAction getTypeAction(MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
141 }
142
143 /// isTypeLegal - Return true if this type is legal on this target.
144 ///
Duncan Sands92c43912008-06-06 12:08:01 +0000145 bool isTypeLegal(MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 return getTypeAction(VT) == Legal;
147 }
148
149 void LegalizeDAG();
150
151private:
152 /// HandleOp - Legalize, Promote, or Expand the specified operand as
153 /// appropriate for its type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000154 void HandleOp(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155
156 /// LegalizeOp - We know that the specified value has a legal type.
157 /// Recursively ensure that the operands have legal types, then return the
158 /// result.
Dan Gohman8181bd12008-07-27 21:46:04 +0000159 SDValue LegalizeOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160
Dan Gohman6d05cac2007-10-11 23:57:53 +0000161 /// UnrollVectorOp - We know that the given vector has a legal type, however
162 /// the operation it performs is not legal and is an operation that we have
163 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
164 /// operating on each element individually.
Dan Gohman8181bd12008-07-27 21:46:04 +0000165 SDValue UnrollVectorOp(SDValue O);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000166
167 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
168 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
169 /// is necessary to spill the vector being inserted into to memory, perform
170 /// the insert there, and then read the result back.
Dan Gohman8181bd12008-07-27 21:46:04 +0000171 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
172 SDValue Idx);
Dan Gohman6d05cac2007-10-11 23:57:53 +0000173
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 /// PromoteOp - Given an operation that produces a value in an invalid type,
175 /// promote it to compute the value into a larger type. The produced value
176 /// will have the correct bits for the low portion of the register, but no
177 /// guarantee is made about the top bits: it may be zero, sign-extended, or
178 /// garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +0000179 SDValue PromoteOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180
Dan Gohman8181bd12008-07-27 21:46:04 +0000181 /// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman4fc03742008-10-01 15:07:49 +0000183 /// the LegalizedNodes map is filled in for any results that are not expanded,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 /// the ExpandedNodes map is filled in for any results that are expanded, and
185 /// the Lo/Hi values are returned. This applies to integer types and Vector
186 /// types.
Dan Gohman8181bd12008-07-27 21:46:04 +0000187 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188
Mon P Wanga5a239f2008-11-06 05:31:54 +0000189 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
190 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
191 /// for the existing elements but no guarantee is made about the new elements
192 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
193 /// when we have an instruction operating on an illegal vector type and we
194 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang1448aad2008-10-30 08:01:45 +0000195 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
196
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 /// SplitVectorOp - Given an operand of vector type, break it down into
198 /// two smaller values.
Dan Gohman8181bd12008-07-27 21:46:04 +0000199 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200
201 /// ScalarizeVectorOp - Given an operand of single-element vector type
202 /// (e.g. v1f32), convert it into the equivalent operation that returns a
203 /// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +0000204 SDValue ScalarizeVectorOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205
Mon P Wanga5a239f2008-11-06 05:31:54 +0000206 /// Useful 16 element vector type that is used to pass operands for widening.
Mon P Wang1448aad2008-10-30 08:01:45 +0000207 typedef SmallVector<SDValue, 16> SDValueVector;
208
209 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
210 /// the LdChain contains a single load and false if it contains a token
211 /// factor for multiple loads. It takes
212 /// Result: location to return the result
213 /// LdChain: location to return the load chain
214 /// Op: load operation to widen
215 /// NVT: widen vector result type we want for the load
216 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
217 SDValue Op, MVT NVT);
218
219 /// Helper genWidenVectorLoads - Helper function to generate a set of
220 /// loads to load a vector with a resulting wider type. It takes
221 /// LdChain: list of chains for the load we have generated
222 /// Chain: incoming chain for the ld vector
223 /// BasePtr: base pointer to load from
224 /// SV: memory disambiguation source value
225 /// SVOffset: memory disambiugation offset
226 /// Alignment: alignment of the memory
227 /// isVolatile: volatile load
228 /// LdWidth: width of memory that we want to load
229 /// ResType: the wider result result type for the resulting loaded vector
230 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
231 SDValue BasePtr, const Value *SV,
232 int SVOffset, unsigned Alignment,
233 bool isVolatile, unsigned LdWidth,
234 MVT ResType);
235
236 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
237 /// location. It takes
238 /// ST: store node that we want to replace
239 /// Chain: incoming store chain
240 /// BasePtr: base address of where we want to store into
241 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
242 SDValue BasePtr);
243
244 /// Helper genWidenVectorStores - Helper function to generate a set of
245 /// stores to store a widen vector into non widen memory
246 // It takes
247 // StChain: list of chains for the stores we have generated
248 // Chain: incoming chain for the ld vector
249 // BasePtr: base pointer to load from
250 // SV: memory disambiguation source value
251 // SVOffset: memory disambiugation offset
252 // Alignment: alignment of the memory
253 // isVolatile: volatile lod
254 // ValOp: value to store
255 // StWidth: width of memory that we want to store
256 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
257 SDValue BasePtr, const Value *SV,
258 int SVOffset, unsigned Alignment,
259 bool isVolatile, SDValue ValOp,
260 unsigned StWidth);
261
Duncan Sandsd3ace282008-07-21 10:20:31 +0000262 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 /// specified mask and type. Targets can specify exactly which masks they
264 /// support and the code generator is tasked with not creating illegal masks.
265 ///
266 /// Note that this will also return true for shuffles that are promoted to a
267 /// different type.
268 ///
269 /// If this is a legal shuffle, this method returns the (possibly promoted)
270 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman8181bd12008-07-27 21:46:04 +0000271 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272
273 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
274 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
275
Dan Gohman8181bd12008-07-27 21:46:04 +0000276 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Evan Cheng71343822008-10-15 02:05:31 +0000277 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC);
278 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC) {
279 LegalizeSetCCOperands(LHS, RHS, CC);
280 LegalizeSetCCCondCode(VT, LHS, RHS, CC);
281 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282
Dan Gohman8181bd12008-07-27 21:46:04 +0000283 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
284 SDValue &Hi);
285 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286
Dan Gohman8181bd12008-07-27 21:46:04 +0000287 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
288 SDValue ExpandBUILD_VECTOR(SDNode *Node);
289 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dan Gohman29c3cef2008-08-14 20:04:46 +0000290 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op);
Dan Gohman8181bd12008-07-27 21:46:04 +0000291 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
292 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
293 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294
Dan Gohman8181bd12008-07-27 21:46:04 +0000295 SDValue ExpandBSWAP(SDValue Op);
296 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
297 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
298 SDValue &Lo, SDValue &Hi);
299 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
300 SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301
Dan Gohman8181bd12008-07-27 21:46:04 +0000302 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
303 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Mon P Wang9901e732008-12-09 05:46:39 +0000304
305 // Returns the legalized (truncated or extended) shift amount.
306 SDValue LegalizeShiftAmount(SDValue ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307};
308}
309
310/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
311/// specified mask and type. Targets can specify exactly which masks they
312/// support and the code generator is tasked with not creating illegal masks.
313///
314/// Note that this will also return true for shuffles that are promoted to a
315/// different type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000316SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
318 default: return 0;
319 case TargetLowering::Legal:
320 case TargetLowering::Custom:
321 break;
322 case TargetLowering::Promote: {
323 // If this is promoted to a different type, convert the shuffle mask and
324 // ask if it is legal in the promoted type!
Duncan Sands92c43912008-06-06 12:08:01 +0000325 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd3ace282008-07-21 10:20:31 +0000326 MVT EltVT = NVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327
328 // If we changed # elements, change the shuffle mask.
329 unsigned NumEltsGrowth =
Duncan Sands92c43912008-06-06 12:08:01 +0000330 NVT.getVectorNumElements() / VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
332 if (NumEltsGrowth > 1) {
333 // Renumber the elements.
Dan Gohman8181bd12008-07-27 21:46:04 +0000334 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000336 SDValue InOp = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
338 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd3ace282008-07-21 10:20:31 +0000339 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000341 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd3ace282008-07-21 10:20:31 +0000342 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 }
344 }
345 }
346 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
347 }
348 VT = NVT;
349 break;
350 }
351 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000352 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353}
354
Duncan Sandse016a2e2008-12-14 09:43:15 +0000355SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag, bool types)
356 : TLI(dag.getTargetLoweringInfo()), DAG(dag), TypesNeedLegalizing(types),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 ValueTypeActions(TLI.getValueTypeActions()) {
358 assert(MVT::LAST_VALUETYPE <= 32 &&
359 "Too many value types for ValueTypeActions to hold!");
360}
361
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362void SelectionDAGLegalize::LegalizeDAG() {
363 LastCALLSEQ_END = DAG.getEntryNode();
364 IsLegalizingCall = false;
365
366 // The legalize process is inherently a bottom-up recursive process (users
367 // legalize their uses before themselves). Given infinite stack space, we
368 // could just start legalizing on the root and traverse the whole graph. In
369 // practice however, this causes us to run out of stack space on large basic
370 // blocks. To avoid this problem, compute an ordering of the nodes where each
371 // node is only legalized after all of its operands are legalized.
Dan Gohman2d2a7a32008-09-30 18:30:35 +0000372 DAG.AssignTopologicalOrder();
373 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
374 E = prior(DAG.allnodes_end()); I != next(E); ++I)
375 HandleOp(SDValue(I, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376
377 // Finally, it's possible the root changed. Get the new root.
Dan Gohman8181bd12008-07-27 21:46:04 +0000378 SDValue OldRoot = DAG.getRoot();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
380 DAG.setRoot(LegalizedNodes[OldRoot]);
381
382 ExpandedNodes.clear();
383 LegalizedNodes.clear();
384 PromotedNodes.clear();
385 SplitNodes.clear();
386 ScalarizedNodes.clear();
Mon P Wang1448aad2008-10-30 08:01:45 +0000387 WidenNodes.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388
389 // Remove dead nodes now.
390 DAG.RemoveDeadNodes();
391}
392
393
394/// FindCallEndFromCallStart - Given a chained node that is part of a call
395/// sequence, find the CALLSEQ_END node that terminates the call sequence.
396static SDNode *FindCallEndFromCallStart(SDNode *Node) {
397 if (Node->getOpcode() == ISD::CALLSEQ_END)
398 return Node;
399 if (Node->use_empty())
400 return 0; // No CallSeqEnd
401
402 // The chain is usually at the end.
Dan Gohman8181bd12008-07-27 21:46:04 +0000403 SDValue TheChain(Node, Node->getNumValues()-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 if (TheChain.getValueType() != MVT::Other) {
405 // Sometimes it's at the beginning.
Dan Gohman8181bd12008-07-27 21:46:04 +0000406 TheChain = SDValue(Node, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 if (TheChain.getValueType() != MVT::Other) {
408 // Otherwise, hunt for it.
409 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
410 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000411 TheChain = SDValue(Node, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412 break;
413 }
414
415 // Otherwise, we walked into a node without a chain.
416 if (TheChain.getValueType() != MVT::Other)
417 return 0;
418 }
419 }
420
421 for (SDNode::use_iterator UI = Node->use_begin(),
422 E = Node->use_end(); UI != E; ++UI) {
423
424 // Make sure to only follow users of our token chain.
Dan Gohman0c97f1d2008-07-27 20:43:25 +0000425 SDNode *User = *UI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
427 if (User->getOperand(i) == TheChain)
428 if (SDNode *Result = FindCallEndFromCallStart(User))
429 return Result;
430 }
431 return 0;
432}
433
434/// FindCallStartFromCallEnd - Given a chained node that is part of a call
435/// sequence, find the CALLSEQ_START node that initiates the call sequence.
436static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
437 assert(Node && "Didn't find callseq_start for a call??");
438 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
439
440 assert(Node->getOperand(0).getValueType() == MVT::Other &&
441 "Node doesn't have a token chain argument!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000442 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443}
444
445/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
446/// see if any uses can reach Dest. If no dest operands can get to dest,
447/// legalize them, legalize ourself, and return false, otherwise, return true.
448///
449/// Keep track of the nodes we fine that actually do lead to Dest in
450/// NodesLeadingTo. This avoids retraversing them exponential number of times.
451///
452bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
453 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
454 if (N == Dest) return true; // N certainly leads to Dest :)
455
456 // If we've already processed this node and it does lead to Dest, there is no
457 // need to reprocess it.
458 if (NodesLeadingTo.count(N)) return true;
459
460 // If the first result of this node has been already legalized, then it cannot
461 // reach N.
462 switch (getTypeAction(N->getValueType(0))) {
463 case Legal:
Dan Gohman8181bd12008-07-27 21:46:04 +0000464 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 break;
466 case Promote:
Dan Gohman8181bd12008-07-27 21:46:04 +0000467 if (PromotedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 break;
469 case Expand:
Dan Gohman8181bd12008-07-27 21:46:04 +0000470 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 break;
472 }
473
474 // Okay, this node has not already been legalized. Check and legalize all
475 // operands. If none lead to Dest, then we can legalize this node.
476 bool OperandsLeadToDest = false;
477 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
478 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greif1c80d112008-08-28 21:40:38 +0000479 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480
481 if (OperandsLeadToDest) {
482 NodesLeadingTo.insert(N);
483 return true;
484 }
485
486 // Okay, this node looks safe, legalize it and return false.
Dan Gohman8181bd12008-07-27 21:46:04 +0000487 HandleOp(SDValue(N, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 return false;
489}
490
Mon P Wang1448aad2008-10-30 08:01:45 +0000491/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492/// appropriate for its type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000493void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000494 MVT VT = Op.getValueType();
Duncan Sandse016a2e2008-12-14 09:43:15 +0000495 // If the type legalizer was run then we should never see any illegal result
496 // types here except for target constants (the type legalizer does not touch
Mon P Wang26342922008-12-18 20:03:17 +0000497 // those) or for build vector used as a mask for a vector shuffle.
498 // FIXME: We can removed the BUILD_VECTOR case when we fix PR2957.
Duncan Sandse016a2e2008-12-14 09:43:15 +0000499 assert((TypesNeedLegalizing || getTypeAction(VT) == Legal ||
Mon P Wang26342922008-12-18 20:03:17 +0000500 Op.getOpcode() == ISD::TargetConstant ||
501 Op.getOpcode() == ISD::BUILD_VECTOR) &&
Duncan Sandse016a2e2008-12-14 09:43:15 +0000502 "Illegal type introduced after type legalization?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503 switch (getTypeAction(VT)) {
504 default: assert(0 && "Bad type action!");
505 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang1448aad2008-10-30 08:01:45 +0000506 case Promote:
507 if (!VT.isVector()) {
508 (void)PromoteOp(Op);
509 break;
510 }
511 else {
512 // See if we can widen otherwise use Expand to either scalarize or split
513 MVT WidenVT = TLI.getWidenVectorType(VT);
514 if (WidenVT != MVT::Other) {
515 (void) WidenVectorOp(Op, WidenVT);
516 break;
517 }
518 // else fall thru to expand since we can't widen the vector
519 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +0000521 if (!VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 // If this is an illegal scalar, expand it into its two component
523 // pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +0000524 SDValue X, Y;
Chris Lattnerdad577b2007-08-25 01:00:22 +0000525 if (Op.getOpcode() == ISD::TargetConstant)
526 break; // Allow illegal target nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 ExpandOp(Op, X, Y);
Duncan Sands92c43912008-06-06 12:08:01 +0000528 } else if (VT.getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 // If this is an illegal single element vector, convert it to a
530 // scalar operation.
531 (void)ScalarizeVectorOp(Op);
532 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +0000533 // This is an illegal multiple element vector.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 // Split it in half and legalize both parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000535 SDValue X, Y;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 SplitVectorOp(Op, X, Y);
537 }
538 break;
539 }
540}
541
542/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
543/// a load from the constant pool.
Dan Gohman8181bd12008-07-27 21:46:04 +0000544static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 SelectionDAG &DAG, TargetLowering &TLI) {
546 bool Extend = false;
547
548 // If a FP immediate is precise when represented as a float and if the
549 // target can do an extending load from float to double, we put it into
550 // the constant pool as a float, even if it's is statically typed as a
Chris Lattnere718cc52008-03-05 06:46:58 +0000551 // double. This shrinks FP constants and canonicalizes them for targets where
552 // an FP extending load is the same cost as a normal load (such as on the x87
553 // fp stack or PPC FP unit).
Duncan Sands92c43912008-06-06 12:08:01 +0000554 MVT VT = CFP->getValueType(0);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000555 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 if (!UseCP) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000557 if (VT!=MVT::f64 && VT!=MVT::f32)
558 assert(0 && "Invalid type expansion");
Dale Johannesen49cc7ce2008-10-09 18:53:47 +0000559 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Cheng354be062008-03-04 08:05:30 +0000560 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 }
562
Duncan Sands92c43912008-06-06 12:08:01 +0000563 MVT OrigVT = VT;
564 MVT SVT = VT;
Evan Cheng354be062008-03-04 08:05:30 +0000565 while (SVT != MVT::f32) {
Duncan Sands92c43912008-06-06 12:08:01 +0000566 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Cheng354be062008-03-04 08:05:30 +0000567 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
568 // Only do this if the target has a native EXTLOAD instruction from
569 // smaller type.
Evan Cheng08c171a2008-10-14 21:26:46 +0000570 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattnere718cc52008-03-05 06:46:58 +0000571 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000572 const Type *SType = SVT.getTypeForMVT();
Evan Cheng354be062008-03-04 08:05:30 +0000573 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
574 VT = SVT;
575 Extend = true;
576 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577 }
578
Dan Gohman8181bd12008-07-27 21:46:04 +0000579 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +0000580 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Cheng354be062008-03-04 08:05:30 +0000581 if (Extend)
582 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohmanfb020b62008-02-07 18:41:25 +0000583 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman04637d12008-09-16 22:05:41 +0000584 0, VT, false, Alignment);
Evan Cheng354be062008-03-04 08:05:30 +0000585 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +0000586 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587}
588
589
590/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
591/// operations.
592static
Dan Gohman8181bd12008-07-27 21:46:04 +0000593SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
594 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +0000595 MVT VT = Node->getValueType(0);
596 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
598 "fcopysign expansion only supported for f32 and f64");
Duncan Sands92c43912008-06-06 12:08:01 +0000599 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600
601 // First get the sign bit of second operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000602 SDValue Mask1 = (SrcVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
604 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
605 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000606 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
608 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands92c43912008-06-06 12:08:01 +0000609 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 if (SizeDiff > 0) {
611 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
612 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
613 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnere6fa1452008-07-10 23:46:13 +0000614 } else if (SizeDiff < 0) {
615 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
616 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
617 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
618 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619
620 // Clear the sign bit of first operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000621 SDValue Mask2 = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
623 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
624 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman8181bd12008-07-27 21:46:04 +0000625 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
627
628 // Or the value with the sign bit.
629 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
630 return Result;
631}
632
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000633/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
634static
Dan Gohman8181bd12008-07-27 21:46:04 +0000635SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
636 TargetLowering &TLI) {
637 SDValue Chain = ST->getChain();
638 SDValue Ptr = ST->getBasePtr();
639 SDValue Val = ST->getValue();
Duncan Sands92c43912008-06-06 12:08:01 +0000640 MVT VT = Val.getValueType();
Dale Johannesen08275382007-09-08 19:29:23 +0000641 int Alignment = ST->getAlignment();
642 int SVOffset = ST->getSrcValueOffset();
Duncan Sands92c43912008-06-06 12:08:01 +0000643 if (ST->getMemoryVT().isFloatingPoint() ||
644 ST->getMemoryVT().isVector()) {
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000645 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
646 if (TLI.isTypeLegal(intVT)) {
647 // Expand to a bitconvert of the value to the integer type of the
648 // same size, then a (misaligned) int store.
649 // FIXME: Does not handle truncating floating point stores!
650 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
651 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
652 SVOffset, ST->isVolatile(), Alignment);
653 } else {
654 // Do a (aligned) store to a stack slot, then copy from the stack slot
655 // to the final destination using (unaligned) integer loads and stores.
656 MVT StoredVT = ST->getMemoryVT();
657 MVT RegVT =
658 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
659 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
660 unsigned RegBytes = RegVT.getSizeInBits() / 8;
661 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen08275382007-09-08 19:29:23 +0000662
Duncan Sands734f49b2008-12-13 07:18:38 +0000663 // Make sure the stack slot is also aligned for the register type.
664 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
665
666 // Perform the original store, only redirected to the stack slot.
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000667 SDValue Store = DAG.getTruncStore(Chain, Val, StackPtr, NULL, 0,StoredVT);
668 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
669 SmallVector<SDValue, 8> Stores;
670 unsigned Offset = 0;
671
672 // Do all but one copies using the full register width.
673 for (unsigned i = 1; i < NumRegs; i++) {
674 // Load one integer register's worth from the stack slot.
675 SDValue Load = DAG.getLoad(RegVT, Store, StackPtr, NULL, 0);
676 // Store it to the final location. Remember the store.
677 Stores.push_back(DAG.getStore(Load.getValue(1), Load, Ptr,
678 ST->getSrcValue(), SVOffset + Offset,
679 ST->isVolatile(),
680 MinAlign(ST->getAlignment(), Offset)));
681 // Increment the pointers.
682 Offset += RegBytes;
683 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
684 Increment);
685 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
686 }
687
Duncan Sands734f49b2008-12-13 07:18:38 +0000688 // The last store may be partial. Do a truncating store. On big-endian
689 // machines this requires an extending load from the stack slot to ensure
690 // that the bits are in the right place.
691 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000692
Duncan Sands734f49b2008-12-13 07:18:38 +0000693 // Load from the stack slot.
694 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Store, StackPtr,
695 NULL, 0, MemVT);
696
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000697 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, Ptr,
698 ST->getSrcValue(), SVOffset + Offset,
Duncan Sands734f49b2008-12-13 07:18:38 +0000699 MemVT, ST->isVolatile(),
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000700 MinAlign(ST->getAlignment(), Offset)));
701 // The order of the stores doesn't matter - say it with a TokenFactor.
702 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
703 Stores.size());
704 }
Dale Johannesen08275382007-09-08 19:29:23 +0000705 }
Duncan Sands92c43912008-06-06 12:08:01 +0000706 assert(ST->getMemoryVT().isInteger() &&
707 !ST->getMemoryVT().isVector() &&
Dale Johannesen08275382007-09-08 19:29:23 +0000708 "Unaligned store of unknown type.");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000709 // Get the half-size VT
Duncan Sands92c43912008-06-06 12:08:01 +0000710 MVT NewStoredVT =
711 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
712 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000713 int IncrementSize = NumBits / 8;
714
715 // Divide the stored value in two parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000716 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
717 SDValue Lo = Val;
718 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000719
720 // Store the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000721 SDValue Store1, Store2;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000722 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
723 ST->getSrcValue(), SVOffset, NewStoredVT,
724 ST->isVolatile(), Alignment);
725 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
726 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsa3691432007-10-28 12:59:45 +0000727 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000728 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
729 ST->getSrcValue(), SVOffset + IncrementSize,
730 NewStoredVT, ST->isVolatile(), Alignment);
731
732 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
733}
734
735/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
736static
Dan Gohman8181bd12008-07-27 21:46:04 +0000737SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
738 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000739 int SVOffset = LD->getSrcValueOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +0000740 SDValue Chain = LD->getChain();
741 SDValue Ptr = LD->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +0000742 MVT VT = LD->getValueType(0);
743 MVT LoadedVT = LD->getMemoryVT();
744 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000745 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
746 if (TLI.isTypeLegal(intVT)) {
747 // Expand to a (misaligned) integer load of the same size,
748 // then bitconvert to floating point or vector.
749 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
750 SVOffset, LD->isVolatile(),
Dale Johannesen08275382007-09-08 19:29:23 +0000751 LD->getAlignment());
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000752 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
753 if (VT.isFloatingPoint() && LoadedVT != VT)
754 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
Dale Johannesen08275382007-09-08 19:29:23 +0000755
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000756 SDValue Ops[] = { Result, Chain };
757 return DAG.getMergeValues(Ops, 2);
758 } else {
759 // Copy the value to a (aligned) stack slot using (unaligned) integer
760 // loads and stores, then do a (aligned) load from the stack slot.
761 MVT RegVT = TLI.getRegisterType(intVT);
762 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
763 unsigned RegBytes = RegVT.getSizeInBits() / 8;
764 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
765
Duncan Sands734f49b2008-12-13 07:18:38 +0000766 // Make sure the stack slot is also aligned for the register type.
767 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
768
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000769 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
770 SmallVector<SDValue, 8> Stores;
771 SDValue StackPtr = StackBase;
772 unsigned Offset = 0;
773
774 // Do all but one copies using the full register width.
775 for (unsigned i = 1; i < NumRegs; i++) {
776 // Load one integer register's worth from the original location.
777 SDValue Load = DAG.getLoad(RegVT, Chain, Ptr, LD->getSrcValue(),
778 SVOffset + Offset, LD->isVolatile(),
779 MinAlign(LD->getAlignment(), Offset));
780 // Follow the load with a store to the stack slot. Remember the store.
781 Stores.push_back(DAG.getStore(Load.getValue(1), Load, StackPtr,
782 NULL, 0));
783 // Increment the pointers.
784 Offset += RegBytes;
785 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
786 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
787 Increment);
788 }
789
790 // The last copy may be partial. Do an extending load.
Duncan Sands734f49b2008-12-13 07:18:38 +0000791 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000792 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Chain, Ptr,
793 LD->getSrcValue(), SVOffset + Offset,
Duncan Sands734f49b2008-12-13 07:18:38 +0000794 MemVT, LD->isVolatile(),
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000795 MinAlign(LD->getAlignment(), Offset));
796 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sands734f49b2008-12-13 07:18:38 +0000797 // On big-endian machines this requires a truncating store to ensure
798 // that the bits end up in the right place.
799 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, StackPtr,
800 NULL, 0, MemVT));
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000801
802 // The order of the stores doesn't matter - say it with a TokenFactor.
803 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
804 Stores.size());
805
806 // Finally, perform the original load only redirected to the stack slot.
807 Load = DAG.getExtLoad(LD->getExtensionType(), VT, TF, StackBase,
808 NULL, 0, LoadedVT);
809
810 // Callers expect a MERGE_VALUES node.
811 SDValue Ops[] = { Load, TF };
812 return DAG.getMergeValues(Ops, 2);
813 }
Dale Johannesen08275382007-09-08 19:29:23 +0000814 }
Duncan Sands92c43912008-06-06 12:08:01 +0000815 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000816 "Unaligned load of unsupported type.");
817
Dale Johannesendc0ee192008-02-27 22:36:00 +0000818 // Compute the new VT that is half the size of the old one. This is an
819 // integer MVT.
Duncan Sands92c43912008-06-06 12:08:01 +0000820 unsigned NumBits = LoadedVT.getSizeInBits();
821 MVT NewLoadedVT;
822 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000823 NumBits >>= 1;
824
825 unsigned Alignment = LD->getAlignment();
826 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000827 ISD::LoadExtType HiExtType = LD->getExtensionType();
828
829 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
830 if (HiExtType == ISD::NON_EXTLOAD)
831 HiExtType = ISD::ZEXTLOAD;
832
833 // Load the value in two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000834 SDValue Lo, Hi;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000835 if (TLI.isLittleEndian()) {
836 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
837 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
838 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
839 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
840 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
841 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000842 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000843 } else {
844 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
845 NewLoadedVT,LD->isVolatile(), Alignment);
846 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
847 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
848 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
849 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000850 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000851 }
852
853 // aggregate the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000854 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
855 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000856 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
857
Dan Gohman8181bd12008-07-27 21:46:04 +0000858 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000859 Hi.getValue(1));
860
Dan Gohman8181bd12008-07-27 21:46:04 +0000861 SDValue Ops[] = { Result, TF };
Duncan Sands698842f2008-07-02 17:40:58 +0000862 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000863}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864
Dan Gohman6d05cac2007-10-11 23:57:53 +0000865/// UnrollVectorOp - We know that the given vector has a legal type, however
866/// the operation it performs is not legal and is an operation that we have
867/// no way of lowering. "Unroll" the vector, splitting out the scalars and
868/// operating on each element individually.
Dan Gohman8181bd12008-07-27 21:46:04 +0000869SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000870 MVT VT = Op.getValueType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000871 assert(isTypeLegal(VT) &&
872 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000873 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman6d05cac2007-10-11 23:57:53 +0000874 "Can't unroll a vector with multiple results!");
Duncan Sands92c43912008-06-06 12:08:01 +0000875 unsigned NE = VT.getVectorNumElements();
876 MVT EltVT = VT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000877
Dan Gohman8181bd12008-07-27 21:46:04 +0000878 SmallVector<SDValue, 8> Scalars;
879 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman6d05cac2007-10-11 23:57:53 +0000880 for (unsigned i = 0; i != NE; ++i) {
881 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000882 SDValue Operand = Op.getOperand(j);
Duncan Sands92c43912008-06-06 12:08:01 +0000883 MVT OperandVT = Operand.getValueType();
884 if (OperandVT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +0000885 // A vector operand; extract a single element.
Duncan Sands92c43912008-06-06 12:08:01 +0000886 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000887 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
888 OperandEltVT,
889 Operand,
890 DAG.getConstant(i, MVT::i32));
891 } else {
892 // A scalar operand; just use it as is.
893 Operands[j] = Operand;
894 }
895 }
Mon P Wang9901e732008-12-09 05:46:39 +0000896
897 switch (Op.getOpcode()) {
898 default:
899 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
900 &Operands[0], Operands.size()));
901 break;
902 case ISD::SHL:
903 case ISD::SRA:
904 case ISD::SRL:
905 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT, Operands[0],
906 LegalizeShiftAmount(Operands[1])));
907 break;
908 }
Dan Gohman6d05cac2007-10-11 23:57:53 +0000909 }
910
911 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
912}
913
Duncan Sands37a3f472008-01-10 10:28:30 +0000914/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands92c43912008-06-06 12:08:01 +0000915static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands37a3f472008-01-10 10:28:30 +0000916 RTLIB::Libcall Call_F32,
917 RTLIB::Libcall Call_F64,
918 RTLIB::Libcall Call_F80,
919 RTLIB::Libcall Call_PPCF128) {
920 return
921 VT == MVT::f32 ? Call_F32 :
922 VT == MVT::f64 ? Call_F64 :
923 VT == MVT::f80 ? Call_F80 :
924 VT == MVT::ppcf128 ? Call_PPCF128 :
925 RTLIB::UNKNOWN_LIBCALL;
926}
927
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000928/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
929/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
930/// is necessary to spill the vector being inserted into to memory, perform
931/// the insert there, and then read the result back.
Dan Gohman8181bd12008-07-27 21:46:04 +0000932SDValue SelectionDAGLegalize::
933PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
934 SDValue Tmp1 = Vec;
935 SDValue Tmp2 = Val;
936 SDValue Tmp3 = Idx;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000937
938 // If the target doesn't support this, we have to spill the input vector
939 // to a temporary stack slot, update the element, then reload it. This is
940 // badness. We could also load the value into a vector register (either
941 // with a "move to register" or "extload into register" instruction, then
942 // permute it into place, if the idx is a constant and if the idx is
943 // supported by the target.
Duncan Sands92c43912008-06-06 12:08:01 +0000944 MVT VT = Tmp1.getValueType();
945 MVT EltVT = VT.getVectorElementType();
946 MVT IdxVT = Tmp3.getValueType();
947 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +0000948 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000949
Gabor Greif1c80d112008-08-28 21:40:38 +0000950 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000951
952 // Store the vector.
Dan Gohman8181bd12008-07-27 21:46:04 +0000953 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang1448aad2008-10-30 08:01:45 +0000954 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000955
956 // Truncate or zero extend offset to target pointer type.
Duncan Sandsec142ee2008-06-08 20:54:56 +0000957 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000958 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
959 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +0000960 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000961 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman8181bd12008-07-27 21:46:04 +0000962 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000963 // Store the scalar value.
964 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000965 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000966 // Load the updated vector.
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000967 return DAG.getLoad(VT, Ch, StackPtr,
968 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000969}
970
Mon P Wang9901e732008-12-09 05:46:39 +0000971SDValue SelectionDAGLegalize::LegalizeShiftAmount(SDValue ShiftAmt) {
972 if (TLI.getShiftAmountTy().bitsLT(ShiftAmt.getValueType()))
973 return DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), ShiftAmt);
974
975 if (TLI.getShiftAmountTy().bitsGT(ShiftAmt.getValueType()))
976 return DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), ShiftAmt);
977
978 return ShiftAmt;
979}
980
981
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982/// LegalizeOp - We know that the specified value has a legal type, and
983/// that its operands are legal. Now ensure that the operation itself
984/// is legal, recursively ensuring that the operands' operations remain
985/// legal.
Dan Gohman8181bd12008-07-27 21:46:04 +0000986SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattnerdad577b2007-08-25 01:00:22 +0000987 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
988 return Op;
989
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000990 assert(isTypeLegal(Op.getValueType()) &&
991 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000992 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993
994 // If this operation defines any values that cannot be represented in a
995 // register on this target, make sure to expand or promote them.
996 if (Node->getNumValues() > 1) {
997 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
998 if (getTypeAction(Node->getValueType(i)) != Legal) {
999 HandleOp(Op.getValue(i));
1000 assert(LegalizedNodes.count(Op) &&
1001 "Handling didn't add legal operands!");
1002 return LegalizedNodes[Op];
1003 }
1004 }
1005
1006 // Note that LegalizeOp may be reentered even from single-use nodes, which
1007 // means that we always must cache transformed nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00001008 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 if (I != LegalizedNodes.end()) return I->second;
1010
Dan Gohman8181bd12008-07-27 21:46:04 +00001011 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
1012 SDValue Result = Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013 bool isCustom = false;
1014
1015 switch (Node->getOpcode()) {
1016 case ISD::FrameIndex:
1017 case ISD::EntryToken:
1018 case ISD::Register:
1019 case ISD::BasicBlock:
1020 case ISD::TargetFrameIndex:
1021 case ISD::TargetJumpTable:
1022 case ISD::TargetConstant:
1023 case ISD::TargetConstantFP:
1024 case ISD::TargetConstantPool:
1025 case ISD::TargetGlobalAddress:
1026 case ISD::TargetGlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +00001027 case ISD::TargetExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001028 case ISD::VALUETYPE:
1029 case ISD::SRCVALUE:
Dan Gohman12a9c082008-02-06 22:27:42 +00001030 case ISD::MEMOPERAND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031 case ISD::CONDCODE:
Duncan Sandsc93fae32008-03-21 09:14:45 +00001032 case ISD::ARG_FLAGS:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001033 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00001034 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 "This must be legal!");
1036 break;
1037 default:
1038 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1039 // If this is a target node, legalize it by legalizing the operands then
1040 // passing it through.
Dan Gohman8181bd12008-07-27 21:46:04 +00001041 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001042 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1043 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1044
1045 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
1046
1047 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1048 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00001049 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001050 }
1051 // Otherwise this is an unhandled builtin node. splat.
1052#ifndef NDEBUG
1053 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
1054#endif
1055 assert(0 && "Do not know how to legalize this operator!");
1056 abort();
1057 case ISD::GLOBAL_OFFSET_TABLE:
1058 case ISD::GlobalAddress:
1059 case ISD::GlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +00001060 case ISD::ExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061 case ISD::ConstantPool:
1062 case ISD::JumpTable: // Nothing to do.
1063 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1064 default: assert(0 && "This action is not supported yet!");
1065 case TargetLowering::Custom:
1066 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001067 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068 // FALLTHROUGH if the target doesn't want to lower this op after all.
1069 case TargetLowering::Legal:
1070 break;
1071 }
1072 break;
1073 case ISD::FRAMEADDR:
1074 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075 // The only option for these nodes is to custom lower them. If the target
1076 // does not custom lower them, then return zero.
1077 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001078 if (Tmp1.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001079 Result = Tmp1;
1080 else
1081 Result = DAG.getConstant(0, TLI.getPointerTy());
1082 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +00001083 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands92c43912008-06-06 12:08:01 +00001084 MVT VT = Node->getValueType(0);
Anton Korobeynikov09386bd2007-08-29 19:28:29 +00001085 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1086 default: assert(0 && "This action is not supported yet!");
1087 case TargetLowering::Custom:
1088 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001089 if (Result.getNode()) break;
Anton Korobeynikov09386bd2007-08-29 19:28:29 +00001090 // Fall Thru
1091 case TargetLowering::Legal:
1092 Result = DAG.getConstant(0, VT);
1093 break;
1094 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +00001095 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +00001096 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001097 case ISD::EXCEPTIONADDR: {
1098 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00001099 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001100 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1101 default: assert(0 && "This action is not supported yet!");
1102 case TargetLowering::Expand: {
1103 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001104 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001105 }
1106 break;
1107 case TargetLowering::Custom:
1108 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001109 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001110 // Fall Thru
1111 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001112 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands698842f2008-07-02 17:40:58 +00001113 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001114 break;
1115 }
1116 }
1117 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001118 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001119
Gabor Greif1c80d112008-08-28 21:40:38 +00001120 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001121 "Cannot return more than two values!");
1122
1123 // Since we produced two values, make sure to remember that we
1124 // legalized both of them.
1125 Tmp1 = LegalizeOp(Result);
1126 Tmp2 = LegalizeOp(Result.getValue(1));
1127 AddLegalizedOperand(Op.getValue(0), Tmp1);
1128 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001129 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130 case ISD::EHSELECTION: {
1131 Tmp1 = LegalizeOp(Node->getOperand(0));
1132 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00001133 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001134 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1135 default: assert(0 && "This action is not supported yet!");
1136 case TargetLowering::Expand: {
1137 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001138 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001139 }
1140 break;
1141 case TargetLowering::Custom:
1142 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001143 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001144 // Fall Thru
1145 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001146 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands698842f2008-07-02 17:40:58 +00001147 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001148 break;
1149 }
1150 }
1151 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001152 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001153
Gabor Greif1c80d112008-08-28 21:40:38 +00001154 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001155 "Cannot return more than two values!");
1156
1157 // Since we produced two values, make sure to remember that we
1158 // legalized both of them.
1159 Tmp1 = LegalizeOp(Result);
1160 Tmp2 = LegalizeOp(Result.getValue(1));
1161 AddLegalizedOperand(Op.getValue(0), Tmp1);
1162 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001163 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001164 case ISD::EH_RETURN: {
Duncan Sands92c43912008-06-06 12:08:01 +00001165 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001166 // The only "good" option for this node is to custom lower it.
1167 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1168 default: assert(0 && "This action is not supported at all!");
1169 case TargetLowering::Custom:
1170 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001171 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001172 // Fall Thru
1173 case TargetLowering::Legal:
1174 // Target does not know, how to lower this, lower to noop
1175 Result = LegalizeOp(Node->getOperand(0));
1176 break;
1177 }
1178 }
1179 break;
1180 case ISD::AssertSext:
1181 case ISD::AssertZext:
1182 Tmp1 = LegalizeOp(Node->getOperand(0));
1183 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1184 break;
1185 case ISD::MERGE_VALUES:
1186 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif46bf5472008-08-26 22:36:50 +00001187 Result = Node->getOperand(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001188 break;
1189 case ISD::CopyFromReg:
1190 Tmp1 = LegalizeOp(Node->getOperand(0));
1191 Result = Op.getValue(0);
1192 if (Node->getNumValues() == 2) {
1193 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1194 } else {
1195 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
1196 if (Node->getNumOperands() == 3) {
1197 Tmp2 = LegalizeOp(Node->getOperand(2));
1198 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1199 } else {
1200 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1201 }
1202 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1203 }
1204 // Since CopyFromReg produces two values, make sure to remember that we
1205 // legalized both of them.
1206 AddLegalizedOperand(Op.getValue(0), Result);
1207 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001208 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001209 case ISD::UNDEF: {
Duncan Sands92c43912008-06-06 12:08:01 +00001210 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001211 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
1212 default: assert(0 && "This action is not supported yet!");
1213 case TargetLowering::Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00001214 if (VT.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001215 Result = DAG.getConstant(0, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00001216 else if (VT.isFloatingPoint())
1217 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesen20b76352007-09-26 17:26:49 +00001218 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001219 else
1220 assert(0 && "Unknown value type!");
1221 break;
1222 case TargetLowering::Legal:
1223 break;
1224 }
1225 break;
1226 }
1227
1228 case ISD::INTRINSIC_W_CHAIN:
1229 case ISD::INTRINSIC_WO_CHAIN:
1230 case ISD::INTRINSIC_VOID: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001231 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001232 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1233 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1234 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1235
1236 // Allow the target to custom lower its intrinsics if it wants to.
1237 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1238 TargetLowering::Custom) {
1239 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001240 if (Tmp3.getNode()) Result = Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001241 }
1242
Gabor Greif1c80d112008-08-28 21:40:38 +00001243 if (Result.getNode()->getNumValues() == 1) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244
1245 // Must have return value and chain result.
Gabor Greif1c80d112008-08-28 21:40:38 +00001246 assert(Result.getNode()->getNumValues() == 2 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 "Cannot return more than two values!");
1248
1249 // Since loads produce two values, make sure to remember that we
1250 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001251 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1252 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001253 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001254 }
1255
Dan Gohman472d12c2008-06-30 20:59:49 +00001256 case ISD::DBG_STOPPOINT:
1257 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001258 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1259
Dan Gohman472d12c2008-06-30 20:59:49 +00001260 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001261 case TargetLowering::Promote:
1262 default: assert(0 && "This action is not supported yet!");
1263 case TargetLowering::Expand: {
Devang Patelfcf1c752009-01-13 00:35:13 +00001264 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001266 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001267
Dan Gohman472d12c2008-06-30 20:59:49 +00001268 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Devang Patelfcf1c752009-01-13 00:35:13 +00001269 GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
1270 if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
1271 DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
1272 unsigned SrcFile = DW->RecordSource(CU.getDirectory(),
1273 CU.getFilename());
1274
Dan Gohman472d12c2008-06-30 20:59:49 +00001275 unsigned Line = DSP->getLine();
1276 unsigned Col = DSP->getColumn();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001277
1278 if (useDEBUG_LOC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001279 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Chengd6f57682008-07-08 20:06:39 +00001280 DAG.getConstant(Col, MVT::i32),
1281 DAG.getConstant(SrcFile, MVT::i32) };
1282 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283 } else {
Devang Patelfcf1c752009-01-13 00:35:13 +00001284 unsigned ID = DW->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001285 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001286 }
1287 } else {
1288 Result = Tmp1; // chain
1289 }
1290 break;
1291 }
Evan Chengd6f57682008-07-08 20:06:39 +00001292 case TargetLowering::Legal: {
1293 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1294 if (Action == Legal && Tmp1 == Node->getOperand(0))
1295 break;
1296
Dan Gohman8181bd12008-07-27 21:46:04 +00001297 SmallVector<SDValue, 8> Ops;
Evan Chengd6f57682008-07-08 20:06:39 +00001298 Ops.push_back(Tmp1);
1299 if (Action == Legal) {
1300 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1301 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1302 } else {
1303 // Otherwise promote them.
1304 Ops.push_back(PromoteOp(Node->getOperand(1)));
1305 Ops.push_back(PromoteOp(Node->getOperand(2)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306 }
Evan Chengd6f57682008-07-08 20:06:39 +00001307 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1308 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1309 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001310 break;
1311 }
Evan Chengd6f57682008-07-08 20:06:39 +00001312 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001313 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001314
1315 case ISD::DECLARE:
1316 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1317 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1318 default: assert(0 && "This action is not supported yet!");
1319 case TargetLowering::Legal:
1320 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1321 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1322 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1323 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1324 break;
Chris Lattner203cd052008-02-28 05:53:40 +00001325 case TargetLowering::Expand:
1326 Result = LegalizeOp(Node->getOperand(0));
1327 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001328 }
1329 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001330
1331 case ISD::DEBUG_LOC:
1332 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1333 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1334 default: assert(0 && "This action is not supported yet!");
Evan Chengd6f57682008-07-08 20:06:39 +00001335 case TargetLowering::Legal: {
1336 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001337 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Chengd6f57682008-07-08 20:06:39 +00001338 if (Action == Legal && Tmp1 == Node->getOperand(0))
1339 break;
1340 if (Action == Legal) {
1341 Tmp2 = Node->getOperand(1);
1342 Tmp3 = Node->getOperand(2);
1343 Tmp4 = Node->getOperand(3);
1344 } else {
1345 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1346 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1347 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1348 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001349 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1350 break;
1351 }
Evan Chengd6f57682008-07-08 20:06:39 +00001352 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353 break;
1354
Dan Gohmanfa607c92008-07-01 00:05:16 +00001355 case ISD::DBG_LABEL:
1356 case ISD::EH_LABEL:
1357 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1358 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359 default: assert(0 && "This action is not supported yet!");
1360 case TargetLowering::Legal:
1361 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohmanfa607c92008-07-01 00:05:16 +00001362 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001363 break;
1364 case TargetLowering::Expand:
1365 Result = LegalizeOp(Node->getOperand(0));
1366 break;
1367 }
1368 break;
1369
Evan Chengd1d68072008-03-08 00:58:38 +00001370 case ISD::PREFETCH:
1371 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1372 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1373 default: assert(0 && "This action is not supported yet!");
1374 case TargetLowering::Legal:
1375 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1376 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1377 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1378 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1379 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1380 break;
1381 case TargetLowering::Expand:
1382 // It's a noop.
1383 Result = LegalizeOp(Node->getOperand(0));
1384 break;
1385 }
1386 break;
1387
Andrew Lenharth785610d2008-02-16 01:24:58 +00001388 case ISD::MEMBARRIER: {
1389 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001390 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1391 default: assert(0 && "This action is not supported yet!");
1392 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001393 SDValue Ops[6];
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001394 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands3ee041a2008-02-27 08:53:44 +00001395 for (int x = 1; x < 6; ++x) {
1396 Ops[x] = Node->getOperand(x);
1397 if (!isTypeLegal(Ops[x].getValueType()))
1398 Ops[x] = PromoteOp(Ops[x]);
1399 }
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001400 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1401 break;
1402 }
1403 case TargetLowering::Expand:
1404 //There is no libgcc call for this op
1405 Result = Node->getOperand(0); // Noop
1406 break;
1407 }
Andrew Lenharth785610d2008-02-16 01:24:58 +00001408 break;
1409 }
1410
Dan Gohmanbebba8d2008-12-23 21:37:04 +00001411 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001412 unsigned int num_operands = 4;
1413 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001414 SDValue Ops[4];
Mon P Wang078a62d2008-05-05 19:05:59 +00001415 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001416 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang078a62d2008-05-05 19:05:59 +00001417 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1418
1419 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1420 default: assert(0 && "This action is not supported yet!");
1421 case TargetLowering::Custom:
1422 Result = TLI.LowerOperation(Result, DAG);
1423 break;
1424 case TargetLowering::Legal:
1425 break;
1426 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001427 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1428 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001429 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001430 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00001431 case ISD::ATOMIC_LOAD_ADD:
1432 case ISD::ATOMIC_LOAD_SUB:
1433 case ISD::ATOMIC_LOAD_AND:
1434 case ISD::ATOMIC_LOAD_OR:
1435 case ISD::ATOMIC_LOAD_XOR:
1436 case ISD::ATOMIC_LOAD_NAND:
1437 case ISD::ATOMIC_LOAD_MIN:
1438 case ISD::ATOMIC_LOAD_MAX:
1439 case ISD::ATOMIC_LOAD_UMIN:
1440 case ISD::ATOMIC_LOAD_UMAX:
1441 case ISD::ATOMIC_SWAP: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001442 unsigned int num_operands = 3;
1443 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001444 SDValue Ops[3];
Mon P Wang078a62d2008-05-05 19:05:59 +00001445 for (unsigned int x = 0; x < num_operands; ++x)
1446 Ops[x] = LegalizeOp(Node->getOperand(x));
1447 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sandsac496a12008-07-04 11:47:58 +00001448
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001449 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001450 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001451 case TargetLowering::Custom:
1452 Result = TLI.LowerOperation(Result, DAG);
1453 break;
1454 case TargetLowering::Legal:
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001455 break;
1456 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001457 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1458 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001459 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001460 }
Scott Michelf2e2b702007-08-08 23:23:31 +00001461 case ISD::Constant: {
1462 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1463 unsigned opAction =
1464 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1465
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001466 // We know we don't need to expand constants here, constants only have one
1467 // value and we check that it is fine above.
1468
Scott Michelf2e2b702007-08-08 23:23:31 +00001469 if (opAction == TargetLowering::Custom) {
1470 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001471 if (Tmp1.getNode())
Scott Michelf2e2b702007-08-08 23:23:31 +00001472 Result = Tmp1;
1473 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001474 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001475 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001476 case ISD::ConstantFP: {
1477 // Spill FP immediates to the constant pool if the target cannot directly
1478 // codegen them. Targets often have some immediate values that can be
1479 // efficiently generated into an FP register without a load. We explicitly
1480 // leave these constants as ConstantFP nodes for the target to deal with.
1481 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1482
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001483 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1484 default: assert(0 && "This action is not supported yet!");
Nate Begemane2ba64f2008-02-14 08:57:00 +00001485 case TargetLowering::Legal:
1486 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001487 case TargetLowering::Custom:
1488 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001489 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001490 Result = Tmp3;
1491 break;
1492 }
1493 // FALLTHROUGH
Nate Begemane2ba64f2008-02-14 08:57:00 +00001494 case TargetLowering::Expand: {
1495 // Check to see if this FP immediate is already legal.
1496 bool isLegal = false;
1497 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1498 E = TLI.legal_fpimm_end(); I != E; ++I) {
1499 if (CFP->isExactlyValue(*I)) {
1500 isLegal = true;
1501 break;
1502 }
1503 }
1504 // If this is a legal constant, turn it into a TargetConstantFP node.
1505 if (isLegal)
1506 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001507 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1508 }
Nate Begemane2ba64f2008-02-14 08:57:00 +00001509 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001510 break;
1511 }
1512 case ISD::TokenFactor:
1513 if (Node->getNumOperands() == 2) {
1514 Tmp1 = LegalizeOp(Node->getOperand(0));
1515 Tmp2 = LegalizeOp(Node->getOperand(1));
1516 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1517 } else if (Node->getNumOperands() == 3) {
1518 Tmp1 = LegalizeOp(Node->getOperand(0));
1519 Tmp2 = LegalizeOp(Node->getOperand(1));
1520 Tmp3 = LegalizeOp(Node->getOperand(2));
1521 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1522 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00001523 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001524 // Legalize the operands.
1525 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1526 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1527 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1528 }
1529 break;
1530
1531 case ISD::FORMAL_ARGUMENTS:
1532 case ISD::CALL:
1533 // The only option for this is to custom lower it.
1534 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001535 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesenac246272008-03-05 19:14:03 +00001536 // A call within a calling sequence must be legalized to something
1537 // other than the normal CALLSEQ_END. Violating this gets Legalize
1538 // into an infinite loop.
1539 assert ((!IsLegalizingCall ||
1540 Node->getOpcode() != ISD::CALL ||
Gabor Greif1c80d112008-08-28 21:40:38 +00001541 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesenac246272008-03-05 19:14:03 +00001542 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001543
1544 // The number of incoming and outgoing values should match; unless the final
1545 // outgoing value is a flag.
Gabor Greif1c80d112008-08-28 21:40:38 +00001546 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1547 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1548 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling22f8deb2007-11-13 00:44:25 +00001549 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001550 "Lowering call/formal_arguments produced unexpected # results!");
1551
1552 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1553 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greif1c80d112008-08-28 21:40:38 +00001554 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1555 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling22f8deb2007-11-13 00:44:25 +00001556 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001557 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00001558 if (Op.getResNo() == i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001559 Tmp2 = Tmp1;
Dan Gohman8181bd12008-07-27 21:46:04 +00001560 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001561 }
1562 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001563 case ISD::EXTRACT_SUBREG: {
1564 Tmp1 = LegalizeOp(Node->getOperand(0));
1565 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1566 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001567 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001568 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1569 }
1570 break;
1571 case ISD::INSERT_SUBREG: {
1572 Tmp1 = LegalizeOp(Node->getOperand(0));
1573 Tmp2 = LegalizeOp(Node->getOperand(1));
1574 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1575 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001576 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001577 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1578 }
1579 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001580 case ISD::BUILD_VECTOR:
1581 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1582 default: assert(0 && "This action is not supported yet!");
1583 case TargetLowering::Custom:
1584 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001585 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001586 Result = Tmp3;
1587 break;
1588 }
1589 // FALLTHROUGH
1590 case TargetLowering::Expand:
Gabor Greif1c80d112008-08-28 21:40:38 +00001591 Result = ExpandBUILD_VECTOR(Result.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001592 break;
1593 }
1594 break;
1595 case ISD::INSERT_VECTOR_ELT:
1596 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001597 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001598
1599 // The type of the value to insert may not be legal, even though the vector
1600 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1601 // here.
1602 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1603 default: assert(0 && "Cannot expand insert element operand");
1604 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1605 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang1448aad2008-10-30 08:01:45 +00001606 case Expand:
1607 // FIXME: An alternative would be to check to see if the target is not
1608 // going to custom lower this operation, we could bitcast to half elt
1609 // width and perform two inserts at that width, if that is legal.
1610 Tmp2 = Node->getOperand(1);
1611 break;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001612 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001613 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1614
1615 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1616 Node->getValueType(0))) {
1617 default: assert(0 && "This action is not supported yet!");
1618 case TargetLowering::Legal:
1619 break;
1620 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001621 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001622 if (Tmp4.getNode()) {
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001623 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001624 break;
1625 }
1626 // FALLTHROUGH
Mon P Wang1448aad2008-10-30 08:01:45 +00001627 case TargetLowering::Promote:
1628 // Fall thru for vector case
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001629 case TargetLowering::Expand: {
1630 // If the insert index is a constant, codegen this as a scalar_to_vector,
1631 // then a shuffle that inserts it into the right position in the vector.
1632 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001633 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1634 // match the element type of the vector being created.
1635 if (Tmp2.getValueType() ==
Duncan Sands92c43912008-06-06 12:08:01 +00001636 Op.getValueType().getVectorElementType()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001637 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001638 Tmp1.getValueType(), Tmp2);
1639
Duncan Sands92c43912008-06-06 12:08:01 +00001640 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1641 MVT ShufMaskVT =
1642 MVT::getIntVectorWithNumElements(NumElts);
1643 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001644
1645 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1646 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1647 // elt 0 of the RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00001648 SmallVector<SDValue, 8> ShufOps;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001649 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001650 if (i != InsertPos->getZExtValue())
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001651 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1652 else
1653 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1654 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001655 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001656 &ShufOps[0], ShufOps.size());
1657
1658 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1659 Tmp1, ScVec, ShufMask);
1660 Result = LegalizeOp(Result);
1661 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001662 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001663 }
Nate Begeman7c9e4b72008-04-25 18:07:40 +00001664 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001665 break;
1666 }
1667 }
1668 break;
1669 case ISD::SCALAR_TO_VECTOR:
1670 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1671 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1672 break;
1673 }
1674
1675 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1676 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1677 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1678 Node->getValueType(0))) {
1679 default: assert(0 && "This action is not supported yet!");
1680 case TargetLowering::Legal:
1681 break;
1682 case TargetLowering::Custom:
1683 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001684 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001685 Result = Tmp3;
1686 break;
1687 }
1688 // FALLTHROUGH
1689 case TargetLowering::Expand:
1690 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1691 break;
1692 }
1693 break;
1694 case ISD::VECTOR_SHUFFLE:
1695 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1696 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1697 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1698
1699 // Allow targets to custom lower the SHUFFLEs they support.
1700 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1701 default: assert(0 && "Unknown operation action!");
1702 case TargetLowering::Legal:
1703 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1704 "vector shuffle should not be created if not legal!");
1705 break;
1706 case TargetLowering::Custom:
1707 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001708 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001709 Result = Tmp3;
1710 break;
1711 }
1712 // FALLTHROUGH
1713 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00001714 MVT VT = Node->getValueType(0);
1715 MVT EltVT = VT.getVectorElementType();
1716 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00001717 SDValue Mask = Node->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001718 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00001719 SmallVector<SDValue,8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001720 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001721 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001722 if (Arg.getOpcode() == ISD::UNDEF) {
1723 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1724 } else {
1725 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001726 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001727 if (Idx < NumElems)
1728 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1729 DAG.getConstant(Idx, PtrVT)));
1730 else
1731 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1732 DAG.getConstant(Idx - NumElems, PtrVT)));
1733 }
1734 }
1735 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1736 break;
1737 }
1738 case TargetLowering::Promote: {
1739 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00001740 MVT OVT = Node->getValueType(0);
1741 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001742
1743 // Cast the two input vectors.
1744 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1745 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1746
1747 // Convert the shuffle mask to the right # elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00001748 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001749 assert(Tmp3.getNode() && "Shuffle not legal?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001750 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1751 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1752 break;
1753 }
1754 }
1755 break;
1756
1757 case ISD::EXTRACT_VECTOR_ELT:
1758 Tmp1 = Node->getOperand(0);
1759 Tmp2 = LegalizeOp(Node->getOperand(1));
1760 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1761 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1762 break;
1763
1764 case ISD::EXTRACT_SUBVECTOR:
1765 Tmp1 = Node->getOperand(0);
1766 Tmp2 = LegalizeOp(Node->getOperand(1));
1767 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1768 Result = ExpandEXTRACT_SUBVECTOR(Result);
1769 break;
1770
Mon P Wang1448aad2008-10-30 08:01:45 +00001771 case ISD::CONCAT_VECTORS: {
1772 // Use extract/insert/build vector for now. We might try to be
1773 // more clever later.
1774 MVT PtrVT = TLI.getPointerTy();
1775 SmallVector<SDValue, 8> Ops;
1776 unsigned NumOperands = Node->getNumOperands();
1777 for (unsigned i=0; i < NumOperands; ++i) {
1778 SDValue SubOp = Node->getOperand(i);
1779 MVT VVT = SubOp.getNode()->getValueType(0);
1780 MVT EltVT = VVT.getVectorElementType();
1781 unsigned NumSubElem = VVT.getVectorNumElements();
1782 for (unsigned j=0; j < NumSubElem; ++j) {
1783 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1784 DAG.getConstant(j, PtrVT)));
1785 }
1786 }
1787 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1788 &Ops[0], Ops.size()));
1789 }
1790
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001791 case ISD::CALLSEQ_START: {
1792 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1793
1794 // Recursively Legalize all of the inputs of the call end that do not lead
1795 // to this call start. This ensures that any libcalls that need be inserted
1796 // are inserted *before* the CALLSEQ_START.
1797 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1798 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greif1c80d112008-08-28 21:40:38 +00001799 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001800 NodesLeadingTo);
1801 }
1802
1803 // Now that we legalized all of the inputs (which may have inserted
1804 // libcalls) create the new CALLSEQ_START node.
1805 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1806
1807 // Merge in the last call, to ensure that this call start after the last
1808 // call ended.
1809 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1810 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1811 Tmp1 = LegalizeOp(Tmp1);
1812 }
1813
1814 // Do not try to legalize the target-specific arguments (#1+).
1815 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001816 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001817 Ops[0] = Tmp1;
1818 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1819 }
1820
1821 // Remember that the CALLSEQ_START is legalized.
1822 AddLegalizedOperand(Op.getValue(0), Result);
1823 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1824 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1825
1826 // Now that the callseq_start and all of the non-call nodes above this call
1827 // sequence have been legalized, legalize the call itself. During this
1828 // process, no libcalls can/will be inserted, guaranteeing that no calls
1829 // can overlap.
1830 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001831 // Note that we are selecting this call!
Dan Gohman8181bd12008-07-27 21:46:04 +00001832 LastCALLSEQ_END = SDValue(CallEnd, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001833 IsLegalizingCall = true;
1834
1835 // Legalize the call, starting from the CALLSEQ_END.
1836 LegalizeOp(LastCALLSEQ_END);
1837 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1838 return Result;
1839 }
1840 case ISD::CALLSEQ_END:
1841 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1842 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greif1c80d112008-08-28 21:40:38 +00001843 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001844 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1845 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001846 assert(I != LegalizedNodes.end() &&
1847 "Legalizing the call start should have legalized this node!");
1848 return I->second;
1849 }
1850
1851 // Otherwise, the call start has been legalized and everything is going
1852 // according to plan. Just legalize ourselves normally here.
1853 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1854 // Do not try to legalize the target-specific arguments (#1+), except for
1855 // an optional flag input.
1856 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1857 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001858 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001859 Ops[0] = Tmp1;
1860 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1861 }
1862 } else {
1863 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1864 if (Tmp1 != Node->getOperand(0) ||
1865 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001866 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001867 Ops[0] = Tmp1;
1868 Ops.back() = Tmp2;
1869 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1870 }
1871 }
1872 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1873 // This finishes up call legalization.
1874 IsLegalizingCall = false;
1875
1876 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001877 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001878 if (Node->getNumValues() == 2)
Dan Gohman8181bd12008-07-27 21:46:04 +00001879 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001880 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001881 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands92c43912008-06-06 12:08:01 +00001882 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001883 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1884 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1885 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1886 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1887
1888 Tmp1 = Result.getValue(0);
1889 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001890 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001891 default: assert(0 && "This action is not supported yet!");
1892 case TargetLowering::Expand: {
1893 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1894 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1895 " not tell us which reg is the stack pointer!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001896 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001897
1898 // Chain the dynamic stack allocation so that it doesn't modify the stack
1899 // pointer when other instructions are using the stack.
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001900 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling22f8deb2007-11-13 00:44:25 +00001901
Dan Gohman8181bd12008-07-27 21:46:04 +00001902 SDValue Size = Tmp2.getOperand(1);
1903 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Chenga448bc42007-08-16 23:50:06 +00001904 Chain = SP.getValue(1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001905 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Chenga448bc42007-08-16 23:50:06 +00001906 unsigned StackAlign =
1907 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1908 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001909 SP = DAG.getNode(ISD::AND, VT, SP,
1910 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001911 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling22f8deb2007-11-13 00:44:25 +00001912 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1913
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001914 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1915 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling22f8deb2007-11-13 00:44:25 +00001916
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001917 Tmp1 = LegalizeOp(Tmp1);
1918 Tmp2 = LegalizeOp(Tmp2);
1919 break;
1920 }
1921 case TargetLowering::Custom:
1922 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001923 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001924 Tmp1 = LegalizeOp(Tmp3);
1925 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1926 }
1927 break;
1928 case TargetLowering::Legal:
1929 break;
1930 }
1931 // Since this op produce two values, make sure to remember that we
1932 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001933 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1934 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001935 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001936 }
1937 case ISD::INLINEASM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001938 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001939 bool Changed = false;
1940 // Legalize all of the operands of the inline asm, in case they are nodes
1941 // that need to be expanded or something. Note we skip the asm string and
1942 // all of the TargetConstant flags.
Dan Gohman8181bd12008-07-27 21:46:04 +00001943 SDValue Op = LegalizeOp(Ops[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001944 Changed = Op != Ops[0];
1945 Ops[0] = Op;
1946
1947 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1948 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001949 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001950 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001951 SDValue Op = LegalizeOp(Ops[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001952 if (Op != Ops[i]) {
1953 Changed = true;
1954 Ops[i] = Op;
1955 }
1956 }
1957 }
1958
1959 if (HasInFlag) {
1960 Op = LegalizeOp(Ops.back());
1961 Changed |= Op != Ops.back();
1962 Ops.back() = Op;
1963 }
1964
1965 if (Changed)
1966 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1967
1968 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman8181bd12008-07-27 21:46:04 +00001969 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1970 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001971 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001972 }
1973 case ISD::BR:
1974 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1975 // Ensure that libcalls are emitted before a branch.
1976 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1977 Tmp1 = LegalizeOp(Tmp1);
1978 LastCALLSEQ_END = DAG.getEntryNode();
1979
1980 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1981 break;
1982 case ISD::BRIND:
1983 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1984 // Ensure that libcalls are emitted before a branch.
1985 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1986 Tmp1 = LegalizeOp(Tmp1);
1987 LastCALLSEQ_END = DAG.getEntryNode();
1988
1989 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1990 default: assert(0 && "Indirect target must be legal type (pointer)!");
1991 case Legal:
1992 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1993 break;
1994 }
1995 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1996 break;
1997 case ISD::BR_JT:
1998 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1999 // Ensure that libcalls are emitted before a branch.
2000 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2001 Tmp1 = LegalizeOp(Tmp1);
2002 LastCALLSEQ_END = DAG.getEntryNode();
2003
2004 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
2005 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2006
2007 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
2008 default: assert(0 && "This action is not supported yet!");
2009 case TargetLowering::Legal: break;
2010 case TargetLowering::Custom:
2011 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002012 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002013 break;
2014 case TargetLowering::Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002015 SDValue Chain = Result.getOperand(0);
2016 SDValue Table = Result.getOperand(1);
2017 SDValue Index = Result.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002018
Duncan Sands92c43912008-06-06 12:08:01 +00002019 MVT PTy = TLI.getPointerTy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002020 MachineFunction &MF = DAG.getMachineFunction();
2021 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
2022 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman8181bd12008-07-27 21:46:04 +00002023 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002024
Duncan Sands12ddc802008-12-12 08:13:38 +00002025 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
2026 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, Chain, Addr,
2027 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Cheng6fb06762007-11-09 01:32:10 +00002028 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002029 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2030 // For PIC, the sequence is:
2031 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00002032 // RelocBase can be JumpTable, GOT or some sort of global base.
Evan Cheng6fb06762007-11-09 01:32:10 +00002033 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
2034 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002035 }
Evan Cheng6fb06762007-11-09 01:32:10 +00002036 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002037 }
2038 }
2039 break;
2040 case ISD::BRCOND:
2041 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2042 // Ensure that libcalls are emitted before a return.
2043 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2044 Tmp1 = LegalizeOp(Tmp1);
2045 LastCALLSEQ_END = DAG.getEntryNode();
2046
2047 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2048 case Expand: assert(0 && "It's impossible to expand bools");
2049 case Legal:
2050 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2051 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002052 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002053 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
2054
2055 // The top bits of the promoted condition are not necessarily zero, ensure
2056 // that the value is properly zero extended.
Dan Gohman07961cd2008-02-25 21:11:39 +00002057 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002058 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman07961cd2008-02-25 21:11:39 +00002059 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002060 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
2061 break;
2062 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002063 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002064
2065 // Basic block destination (Op#2) is always legal.
2066 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2067
2068 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
2069 default: assert(0 && "This action is not supported yet!");
2070 case TargetLowering::Legal: break;
2071 case TargetLowering::Custom:
2072 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002073 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002074 break;
2075 case TargetLowering::Expand:
2076 // Expand brcond's setcc into its constituent parts and create a BR_CC
2077 // Node.
2078 if (Tmp2.getOpcode() == ISD::SETCC) {
2079 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
2080 Tmp2.getOperand(0), Tmp2.getOperand(1),
2081 Node->getOperand(2));
2082 } else {
2083 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
2084 DAG.getCondCode(ISD::SETNE), Tmp2,
2085 DAG.getConstant(0, Tmp2.getValueType()),
2086 Node->getOperand(2));
2087 }
2088 break;
2089 }
2090 break;
2091 case ISD::BR_CC:
2092 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2093 // Ensure that libcalls are emitted before a branch.
2094 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2095 Tmp1 = LegalizeOp(Tmp1);
2096 Tmp2 = Node->getOperand(2); // LHS
2097 Tmp3 = Node->getOperand(3); // RHS
2098 Tmp4 = Node->getOperand(1); // CC
2099
Duncan Sands4a361272009-01-01 15:52:00 +00002100 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()), Tmp2, Tmp3,Tmp4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002101 LastCALLSEQ_END = DAG.getEntryNode();
2102
Evan Cheng71343822008-10-15 02:05:31 +00002103 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002104 // the LHS is a legal SETCC itself. In this case, we need to compare
2105 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00002106 if (Tmp3.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002107 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2108 Tmp4 = DAG.getCondCode(ISD::SETNE);
2109 }
2110
2111 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2112 Node->getOperand(4));
2113
2114 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2115 default: assert(0 && "Unexpected action for BR_CC!");
2116 case TargetLowering::Legal: break;
2117 case TargetLowering::Custom:
2118 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002119 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002120 break;
2121 }
2122 break;
2123 case ISD::LOAD: {
2124 LoadSDNode *LD = cast<LoadSDNode>(Node);
2125 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2126 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
2127
2128 ISD::LoadExtType ExtType = LD->getExtensionType();
2129 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands92c43912008-06-06 12:08:01 +00002130 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002131 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2132 Tmp3 = Result.getValue(0);
2133 Tmp4 = Result.getValue(1);
2134
2135 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2136 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002137 case TargetLowering::Legal:
2138 // If this is an unaligned load and the target doesn't support it,
2139 // expand it.
2140 if (!TLI.allowsUnalignedMemoryAccesses()) {
2141 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002142 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002143 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002144 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002145 TLI);
2146 Tmp3 = Result.getOperand(0);
2147 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00002148 Tmp3 = LegalizeOp(Tmp3);
2149 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002150 }
2151 }
2152 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002153 case TargetLowering::Custom:
2154 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002155 if (Tmp1.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002156 Tmp3 = LegalizeOp(Tmp1);
2157 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2158 }
2159 break;
2160 case TargetLowering::Promote: {
2161 // Only promote a load of vector type to another.
Duncan Sands92c43912008-06-06 12:08:01 +00002162 assert(VT.isVector() && "Cannot promote this load!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002163 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002164 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002165
2166 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
2167 LD->getSrcValueOffset(),
2168 LD->isVolatile(), LD->getAlignment());
2169 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2170 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2171 break;
2172 }
2173 }
2174 // Since loads produce two values, make sure to remember that we
2175 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002176 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2177 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif46bf5472008-08-26 22:36:50 +00002178 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002179 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00002180 MVT SrcVT = LD->getMemoryVT();
2181 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sands082524c2008-01-23 20:39:46 +00002182 int SVOffset = LD->getSrcValueOffset();
2183 unsigned Alignment = LD->getAlignment();
2184 bool isVolatile = LD->isVolatile();
2185
Duncan Sands92c43912008-06-06 12:08:01 +00002186 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002187 // Some targets pretend to have an i1 loading operation, and actually
2188 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2189 // bits are guaranteed to be zero; it helps the optimizers understand
2190 // that these bits are zero. It is also useful for EXTLOAD, since it
2191 // tells the optimizers that those bits are undefined. It would be
2192 // nice to have an effective generic way of getting these benefits...
2193 // Until such a way is found, don't insist on promoting i1 here.
2194 (SrcVT != MVT::i1 ||
Evan Cheng08c171a2008-10-14 21:26:46 +00002195 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002196 // Promote to a byte-sized load if not loading an integral number of
2197 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands92c43912008-06-06 12:08:01 +00002198 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2199 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002200 SDValue Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002201
2202 // The extra bits are guaranteed to be zero, since we stored them that
2203 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2204
2205 ISD::LoadExtType NewExtType =
2206 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2207
2208 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2209 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2210 NVT, isVolatile, Alignment);
2211
2212 Ch = Result.getValue(1); // The chain.
2213
2214 if (ExtType == ISD::SEXTLOAD)
2215 // Having the top bits zero doesn't help when sign extending.
2216 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2217 Result, DAG.getValueType(SrcVT));
2218 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2219 // All the top bits are guaranteed to be zero - inform the optimizers.
2220 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2221 DAG.getValueType(SrcVT));
2222
2223 Tmp1 = LegalizeOp(Result);
2224 Tmp2 = LegalizeOp(Ch);
2225 } else if (SrcWidth & (SrcWidth - 1)) {
2226 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands92c43912008-06-06 12:08:01 +00002227 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002228 "Unsupported extload!");
2229 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2230 assert(RoundWidth < SrcWidth);
2231 unsigned ExtraWidth = SrcWidth - RoundWidth;
2232 assert(ExtraWidth < RoundWidth);
2233 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2234 "Load size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002235 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2236 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002237 SDValue Lo, Hi, Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002238 unsigned IncrementSize;
2239
2240 if (TLI.isLittleEndian()) {
2241 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2242 // Load the bottom RoundWidth bits.
2243 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2244 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2245 Alignment);
2246
2247 // Load the remaining ExtraWidth bits.
2248 IncrementSize = RoundWidth / 8;
2249 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2250 DAG.getIntPtrConstant(IncrementSize));
2251 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2252 LD->getSrcValue(), SVOffset + IncrementSize,
2253 ExtraVT, isVolatile,
2254 MinAlign(Alignment, IncrementSize));
2255
2256 // Build a factor node to remember that this load is independent of the
2257 // other one.
2258 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2259 Hi.getValue(1));
2260
2261 // Move the top bits to the right place.
2262 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2263 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2264
2265 // Join the hi and lo parts.
2266 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002267 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00002268 // Big endian - avoid unaligned loads.
2269 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2270 // Load the top RoundWidth bits.
2271 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2272 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2273 Alignment);
2274
2275 // Load the remaining ExtraWidth bits.
2276 IncrementSize = RoundWidth / 8;
2277 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2278 DAG.getIntPtrConstant(IncrementSize));
2279 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2280 LD->getSrcValue(), SVOffset + IncrementSize,
2281 ExtraVT, isVolatile,
2282 MinAlign(Alignment, IncrementSize));
2283
2284 // Build a factor node to remember that this load is independent of the
2285 // other one.
2286 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2287 Hi.getValue(1));
2288
2289 // Move the top bits to the right place.
2290 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2291 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2292
2293 // Join the hi and lo parts.
2294 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2295 }
2296
2297 Tmp1 = LegalizeOp(Result);
2298 Tmp2 = LegalizeOp(Ch);
2299 } else {
Evan Cheng08c171a2008-10-14 21:26:46 +00002300 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002301 default: assert(0 && "This action is not supported yet!");
2302 case TargetLowering::Custom:
2303 isCustom = true;
2304 // FALLTHROUGH
2305 case TargetLowering::Legal:
2306 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2307 Tmp1 = Result.getValue(0);
2308 Tmp2 = Result.getValue(1);
2309
2310 if (isCustom) {
2311 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002312 if (Tmp3.getNode()) {
Duncan Sands082524c2008-01-23 20:39:46 +00002313 Tmp1 = LegalizeOp(Tmp3);
2314 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2315 }
2316 } else {
2317 // If this is an unaligned load and the target doesn't support it,
2318 // expand it.
2319 if (!TLI.allowsUnalignedMemoryAccesses()) {
2320 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002321 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sands082524c2008-01-23 20:39:46 +00002322 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002323 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sands082524c2008-01-23 20:39:46 +00002324 TLI);
2325 Tmp1 = Result.getOperand(0);
2326 Tmp2 = Result.getOperand(1);
2327 Tmp1 = LegalizeOp(Tmp1);
2328 Tmp2 = LegalizeOp(Tmp2);
2329 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002330 }
2331 }
Duncan Sands082524c2008-01-23 20:39:46 +00002332 break;
2333 case TargetLowering::Expand:
2334 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2335 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002336 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sands082524c2008-01-23 20:39:46 +00002337 LD->getSrcValueOffset(),
2338 LD->isVolatile(), LD->getAlignment());
2339 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2340 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2341 Tmp2 = LegalizeOp(Load.getValue(1));
2342 break;
2343 }
2344 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2345 // Turn the unsupported load into an EXTLOAD followed by an explicit
2346 // zero/sign extend inreg.
2347 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2348 Tmp1, Tmp2, LD->getSrcValue(),
2349 LD->getSrcValueOffset(), SrcVT,
2350 LD->isVolatile(), LD->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00002351 SDValue ValRes;
Duncan Sands082524c2008-01-23 20:39:46 +00002352 if (ExtType == ISD::SEXTLOAD)
2353 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2354 Result, DAG.getValueType(SrcVT));
2355 else
2356 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2357 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2358 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002359 break;
2360 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002361 }
Duncan Sands082524c2008-01-23 20:39:46 +00002362
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002363 // Since loads produce two values, make sure to remember that we legalized
2364 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002365 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2366 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002367 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002368 }
2369 }
2370 case ISD::EXTRACT_ELEMENT: {
Duncan Sands92c43912008-06-06 12:08:01 +00002371 MVT OpTy = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002372 switch (getTypeAction(OpTy)) {
2373 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2374 case Legal:
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002375 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002376 // 1 -> Hi
2377 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands92c43912008-06-06 12:08:01 +00002378 DAG.getConstant(OpTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002379 TLI.getShiftAmountTy()));
2380 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2381 } else {
2382 // 0 -> Lo
2383 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2384 Node->getOperand(0));
2385 }
2386 break;
2387 case Expand:
2388 // Get both the low and high parts.
2389 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002390 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002391 Result = Tmp2; // 1 -> Hi
2392 else
2393 Result = Tmp1; // 0 -> Lo
2394 break;
2395 }
2396 break;
2397 }
2398
2399 case ISD::CopyToReg:
2400 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2401
2402 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2403 "Register type must be legal!");
2404 // Legalize the incoming value (must be a legal type).
2405 Tmp2 = LegalizeOp(Node->getOperand(2));
2406 if (Node->getNumValues() == 1) {
2407 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2408 } else {
2409 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2410 if (Node->getNumOperands() == 4) {
2411 Tmp3 = LegalizeOp(Node->getOperand(3));
2412 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2413 Tmp3);
2414 } else {
2415 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2416 }
2417
2418 // Since this produces two values, make sure to remember that we legalized
2419 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002420 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2421 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002422 return Result;
2423 }
2424 break;
2425
2426 case ISD::RET:
2427 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2428
2429 // Ensure that libcalls are emitted before a return.
2430 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2431 Tmp1 = LegalizeOp(Tmp1);
2432 LastCALLSEQ_END = DAG.getEntryNode();
2433
2434 switch (Node->getNumOperands()) {
2435 case 3: // ret val
2436 Tmp2 = Node->getOperand(1);
2437 Tmp3 = Node->getOperand(2); // Signness
2438 switch (getTypeAction(Tmp2.getValueType())) {
2439 case Legal:
2440 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2441 break;
2442 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00002443 if (!Tmp2.getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002444 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002445 ExpandOp(Tmp2, Lo, Hi);
2446
2447 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002448 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002449 std::swap(Lo, Hi);
2450
Gabor Greif1c80d112008-08-28 21:40:38 +00002451 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002452 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2453 else
2454 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2455 Result = LegalizeOp(Result);
2456 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +00002457 SDNode *InVal = Tmp2.getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002458 int InIx = Tmp2.getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002459 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2460 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002461
2462 // Figure out if there is a simple type corresponding to this Vector
2463 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002464 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002465 if (TLI.isTypeLegal(TVT)) {
2466 // Turn this into a return of the vector type.
2467 Tmp2 = LegalizeOp(Tmp2);
2468 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2469 } else if (NumElems == 1) {
2470 // Turn this into a return of the scalar type.
2471 Tmp2 = ScalarizeVectorOp(Tmp2);
2472 Tmp2 = LegalizeOp(Tmp2);
2473 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2474
2475 // FIXME: Returns of gcc generic vectors smaller than a legal type
2476 // should be returned in integer registers!
2477
2478 // The scalarized value type may not be legal, e.g. it might require
2479 // promotion or expansion. Relegalize the return.
2480 Result = LegalizeOp(Result);
2481 } else {
2482 // FIXME: Returns of gcc generic vectors larger than a legal vector
2483 // type should be returned by reference!
Dan Gohman8181bd12008-07-27 21:46:04 +00002484 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002485 SplitVectorOp(Tmp2, Lo, Hi);
2486 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2487 Result = LegalizeOp(Result);
2488 }
2489 }
2490 break;
2491 case Promote:
2492 Tmp2 = PromoteOp(Node->getOperand(1));
2493 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2494 Result = LegalizeOp(Result);
2495 break;
2496 }
2497 break;
2498 case 1: // ret void
2499 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2500 break;
2501 default: { // ret <values>
Dan Gohman8181bd12008-07-27 21:46:04 +00002502 SmallVector<SDValue, 8> NewValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002503 NewValues.push_back(Tmp1);
2504 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2505 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2506 case Legal:
2507 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2508 NewValues.push_back(Node->getOperand(i+1));
2509 break;
2510 case Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002511 SDValue Lo, Hi;
Duncan Sands92c43912008-06-06 12:08:01 +00002512 assert(!Node->getOperand(i).getValueType().isExtended() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002513 "FIXME: TODO: implement returning non-legal vector types!");
2514 ExpandOp(Node->getOperand(i), Lo, Hi);
2515 NewValues.push_back(Lo);
2516 NewValues.push_back(Node->getOperand(i+1));
Gabor Greif1c80d112008-08-28 21:40:38 +00002517 if (Hi.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002518 NewValues.push_back(Hi);
2519 NewValues.push_back(Node->getOperand(i+1));
2520 }
2521 break;
2522 }
2523 case Promote:
2524 assert(0 && "Can't promote multiple return value yet!");
2525 }
2526
2527 if (NewValues.size() == Node->getNumOperands())
2528 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2529 else
2530 Result = DAG.getNode(ISD::RET, MVT::Other,
2531 &NewValues[0], NewValues.size());
2532 break;
2533 }
2534 }
2535
2536 if (Result.getOpcode() == ISD::RET) {
2537 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2538 default: assert(0 && "This action is not supported yet!");
2539 case TargetLowering::Legal: break;
2540 case TargetLowering::Custom:
2541 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002542 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002543 break;
2544 }
2545 }
2546 break;
2547 case ISD::STORE: {
2548 StoreSDNode *ST = cast<StoreSDNode>(Node);
2549 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2550 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2551 int SVOffset = ST->getSrcValueOffset();
2552 unsigned Alignment = ST->getAlignment();
2553 bool isVolatile = ST->isVolatile();
2554
2555 if (!ST->isTruncatingStore()) {
2556 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2557 // FIXME: We shouldn't do this for TargetConstantFP's.
2558 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2559 // to phase ordering between legalized code and the dag combiner. This
2560 // probably means that we need to integrate dag combiner and legalizer
2561 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002562 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002563 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002564 if (CFP->getValueType(0) == MVT::f32 &&
2565 getTypeAction(MVT::i32) == Legal) {
Dan Gohman39509762008-03-11 00:11:06 +00002566 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002567 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen1616e902007-09-11 18:32:33 +00002568 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002569 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2570 SVOffset, isVolatile, Alignment);
2571 break;
2572 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002573 // If this target supports 64-bit registers, do a single 64-bit store.
2574 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002575 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman39509762008-03-11 00:11:06 +00002576 zextOrTrunc(64), MVT::i64);
Chris Lattner19f229a2007-10-15 05:46:06 +00002577 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2578 SVOffset, isVolatile, Alignment);
2579 break;
Duncan Sands2418bec2008-06-13 19:07:40 +00002580 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002581 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2582 // stores. If the target supports neither 32- nor 64-bits, this
2583 // xform is certainly not worth it.
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002584 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman8181bd12008-07-27 21:46:04 +00002585 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2586 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002587 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002588
2589 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2590 SVOffset, isVolatile, Alignment);
2591 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002592 DAG.getIntPtrConstant(4));
Chris Lattner19f229a2007-10-15 05:46:06 +00002593 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002594 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002595
2596 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2597 break;
2598 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002599 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002600 }
2601
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002602 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002603 case Legal: {
2604 Tmp3 = LegalizeOp(ST->getValue());
2605 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2606 ST->getOffset());
2607
Duncan Sands92c43912008-06-06 12:08:01 +00002608 MVT VT = Tmp3.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002609 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2610 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002611 case TargetLowering::Legal:
2612 // If this is an unaligned store and the target doesn't support it,
2613 // expand it.
2614 if (!TLI.allowsUnalignedMemoryAccesses()) {
2615 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002616 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002617 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002618 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002619 TLI);
2620 }
2621 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002622 case TargetLowering::Custom:
2623 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002624 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002625 break;
2626 case TargetLowering::Promote:
Duncan Sands92c43912008-06-06 12:08:01 +00002627 assert(VT.isVector() && "Unknown legal promote case!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002628 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2629 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2630 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2631 ST->getSrcValue(), SVOffset, isVolatile,
2632 Alignment);
2633 break;
2634 }
2635 break;
2636 }
2637 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002638 if (!ST->getMemoryVT().isVector()) {
2639 // Truncate the value and store the result.
2640 Tmp3 = PromoteOp(ST->getValue());
2641 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2642 SVOffset, ST->getMemoryVT(),
2643 isVolatile, Alignment);
2644 break;
2645 }
2646 // Fall thru to expand for vector
2647 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002648 unsigned IncrementSize = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002649 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002650
2651 // If this is a vector type, then we have to calculate the increment as
2652 // the product of the element size in bytes, and the number of elements
2653 // in the high half of the vector.
Duncan Sands92c43912008-06-06 12:08:01 +00002654 if (ST->getValue().getValueType().isVector()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002655 SDNode *InVal = ST->getValue().getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002656 int InIx = ST->getValue().getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002657 MVT InVT = InVal->getValueType(InIx);
2658 unsigned NumElems = InVT.getVectorNumElements();
2659 MVT EVT = InVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002660
2661 // Figure out if there is a simple type corresponding to this Vector
2662 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002663 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002664 if (TLI.isTypeLegal(TVT)) {
2665 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002666 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002667 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2668 SVOffset, isVolatile, Alignment);
2669 Result = LegalizeOp(Result);
2670 break;
2671 } else if (NumElems == 1) {
2672 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002673 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002674 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2675 SVOffset, isVolatile, Alignment);
2676 // The scalarized value type may not be legal, e.g. it might require
2677 // promotion or expansion. Relegalize the scalar store.
2678 Result = LegalizeOp(Result);
2679 break;
2680 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +00002681 // Check if we have widen this node with another value
2682 std::map<SDValue, SDValue>::iterator I =
2683 WidenNodes.find(ST->getValue());
2684 if (I != WidenNodes.end()) {
2685 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2686 break;
2687 }
2688 else {
2689 SplitVectorOp(ST->getValue(), Lo, Hi);
2690 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2691 EVT.getSizeInBits()/8;
2692 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002693 }
2694 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002695 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greif1c80d112008-08-28 21:40:38 +00002696 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002697
Richard Pennington73ae9e42008-09-25 16:15:10 +00002698 if (Hi.getNode() && TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002699 std::swap(Lo, Hi);
2700 }
2701
2702 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2703 SVOffset, isVolatile, Alignment);
2704
Gabor Greif1c80d112008-08-28 21:40:38 +00002705 if (Hi.getNode() == NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002706 // Must be int <-> float one-to-one expansion.
2707 Result = Lo;
2708 break;
2709 }
2710
2711 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002712 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002713 assert(isTypeLegal(Tmp2.getValueType()) &&
2714 "Pointers must be legal!");
2715 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002716 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002717 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2718 SVOffset, isVolatile, Alignment);
2719 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2720 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00002721 } // case Expand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002722 }
2723 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002724 switch (getTypeAction(ST->getValue().getValueType())) {
2725 case Legal:
2726 Tmp3 = LegalizeOp(ST->getValue());
2727 break;
2728 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002729 if (!ST->getValue().getValueType().isVector()) {
2730 // We can promote the value, the truncstore will still take care of it.
2731 Tmp3 = PromoteOp(ST->getValue());
2732 break;
2733 }
2734 // Vector case falls through to expand
Chris Lattner3bc08502008-01-17 19:59:44 +00002735 case Expand:
2736 // Just store the low part. This may become a non-trunc store, so make
2737 // sure to use getTruncStore, not UpdateNodeOperands below.
2738 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2739 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2740 SVOffset, MVT::i8, isVolatile, Alignment);
2741 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002742
Duncan Sands92c43912008-06-06 12:08:01 +00002743 MVT StVT = ST->getMemoryVT();
2744 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands40676662008-01-22 07:17:34 +00002745
Duncan Sands92c43912008-06-06 12:08:01 +00002746 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands40676662008-01-22 07:17:34 +00002747 // Promote to a byte-sized store with upper bits zero if not
2748 // storing an integral number of bytes. For example, promote
2749 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands92c43912008-06-06 12:08:01 +00002750 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands40676662008-01-22 07:17:34 +00002751 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2752 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2753 SVOffset, NVT, isVolatile, Alignment);
2754 } else if (StWidth & (StWidth - 1)) {
2755 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands92c43912008-06-06 12:08:01 +00002756 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands40676662008-01-22 07:17:34 +00002757 "Unsupported truncstore!");
2758 unsigned RoundWidth = 1 << Log2_32(StWidth);
2759 assert(RoundWidth < StWidth);
2760 unsigned ExtraWidth = StWidth - RoundWidth;
2761 assert(ExtraWidth < RoundWidth);
2762 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2763 "Store size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002764 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2765 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002766 SDValue Lo, Hi;
Duncan Sands40676662008-01-22 07:17:34 +00002767 unsigned IncrementSize;
2768
2769 if (TLI.isLittleEndian()) {
2770 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2771 // Store the bottom RoundWidth bits.
2772 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2773 SVOffset, RoundVT,
2774 isVolatile, Alignment);
2775
2776 // Store the remaining ExtraWidth bits.
2777 IncrementSize = RoundWidth / 8;
2778 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2779 DAG.getIntPtrConstant(IncrementSize));
2780 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2781 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2782 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2783 SVOffset + IncrementSize, ExtraVT, isVolatile,
2784 MinAlign(Alignment, IncrementSize));
2785 } else {
2786 // Big endian - avoid unaligned stores.
2787 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2788 // Store the top RoundWidth bits.
2789 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2790 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2791 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2792 RoundVT, isVolatile, Alignment);
2793
2794 // Store the remaining ExtraWidth bits.
2795 IncrementSize = RoundWidth / 8;
2796 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2797 DAG.getIntPtrConstant(IncrementSize));
2798 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2799 SVOffset + IncrementSize, ExtraVT, isVolatile,
2800 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002801 }
Duncan Sands40676662008-01-22 07:17:34 +00002802
2803 // The order of the stores doesn't matter.
2804 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2805 } else {
2806 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2807 Tmp2 != ST->getBasePtr())
2808 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2809 ST->getOffset());
2810
2811 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2812 default: assert(0 && "This action is not supported yet!");
2813 case TargetLowering::Legal:
2814 // If this is an unaligned store and the target doesn't support it,
2815 // expand it.
2816 if (!TLI.allowsUnalignedMemoryAccesses()) {
2817 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002818 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands40676662008-01-22 07:17:34 +00002819 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002820 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands40676662008-01-22 07:17:34 +00002821 TLI);
2822 }
2823 break;
2824 case TargetLowering::Custom:
2825 Result = TLI.LowerOperation(Result, DAG);
2826 break;
2827 case Expand:
2828 // TRUNCSTORE:i16 i32 -> STORE i16
2829 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2830 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2831 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2832 isVolatile, Alignment);
2833 break;
2834 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835 }
2836 }
2837 break;
2838 }
2839 case ISD::PCMARKER:
2840 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2841 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2842 break;
2843 case ISD::STACKSAVE:
2844 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2845 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2846 Tmp1 = Result.getValue(0);
2847 Tmp2 = Result.getValue(1);
2848
2849 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2850 default: assert(0 && "This action is not supported yet!");
2851 case TargetLowering::Legal: break;
2852 case TargetLowering::Custom:
2853 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002854 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002855 Tmp1 = LegalizeOp(Tmp3);
2856 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2857 }
2858 break;
2859 case TargetLowering::Expand:
2860 // Expand to CopyFromReg if the target set
2861 // StackPointerRegisterToSaveRestore.
2862 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2863 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2864 Node->getValueType(0));
2865 Tmp2 = Tmp1.getValue(1);
2866 } else {
2867 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2868 Tmp2 = Node->getOperand(0);
2869 }
2870 break;
2871 }
2872
2873 // Since stacksave produce two values, make sure to remember that we
2874 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002875 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2876 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002877 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002878
2879 case ISD::STACKRESTORE:
2880 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2881 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2882 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2883
2884 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2885 default: assert(0 && "This action is not supported yet!");
2886 case TargetLowering::Legal: break;
2887 case TargetLowering::Custom:
2888 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002889 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002890 break;
2891 case TargetLowering::Expand:
2892 // Expand to CopyToReg if the target set
2893 // StackPointerRegisterToSaveRestore.
2894 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2895 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2896 } else {
2897 Result = Tmp1;
2898 }
2899 break;
2900 }
2901 break;
2902
2903 case ISD::READCYCLECOUNTER:
2904 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2905 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2906 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2907 Node->getValueType(0))) {
2908 default: assert(0 && "This action is not supported yet!");
2909 case TargetLowering::Legal:
2910 Tmp1 = Result.getValue(0);
2911 Tmp2 = Result.getValue(1);
2912 break;
2913 case TargetLowering::Custom:
2914 Result = TLI.LowerOperation(Result, DAG);
2915 Tmp1 = LegalizeOp(Result.getValue(0));
2916 Tmp2 = LegalizeOp(Result.getValue(1));
2917 break;
2918 }
2919
2920 // Since rdcc produce two values, make sure to remember that we legalized
2921 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002922 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2923 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002924 return Result;
2925
2926 case ISD::SELECT:
2927 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2928 case Expand: assert(0 && "It's impossible to expand bools");
2929 case Legal:
2930 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2931 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002932 case Promote: {
Mon P Wang1448aad2008-10-30 08:01:45 +00002933 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002934 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2935 // Make sure the condition is either zero or one.
Dan Gohman07961cd2008-02-25 21:11:39 +00002936 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002937 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman07961cd2008-02-25 21:11:39 +00002938 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002939 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2940 break;
2941 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002942 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2944 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2945
2946 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2947
2948 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2949 default: assert(0 && "This action is not supported yet!");
2950 case TargetLowering::Legal: break;
2951 case TargetLowering::Custom: {
2952 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002953 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002954 break;
2955 }
2956 case TargetLowering::Expand:
2957 if (Tmp1.getOpcode() == ISD::SETCC) {
2958 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2959 Tmp2, Tmp3,
2960 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2961 } else {
2962 Result = DAG.getSelectCC(Tmp1,
2963 DAG.getConstant(0, Tmp1.getValueType()),
2964 Tmp2, Tmp3, ISD::SETNE);
2965 }
2966 break;
2967 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00002968 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002969 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2970 unsigned ExtOp, TruncOp;
Duncan Sands92c43912008-06-06 12:08:01 +00002971 if (Tmp2.getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002972 ExtOp = ISD::BIT_CONVERT;
2973 TruncOp = ISD::BIT_CONVERT;
Duncan Sands92c43912008-06-06 12:08:01 +00002974 } else if (Tmp2.getValueType().isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002975 ExtOp = ISD::ANY_EXTEND;
2976 TruncOp = ISD::TRUNCATE;
2977 } else {
2978 ExtOp = ISD::FP_EXTEND;
2979 TruncOp = ISD::FP_ROUND;
2980 }
2981 // Promote each of the values to the new type.
2982 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2983 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2984 // Perform the larger operation, then round down.
2985 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00002986 if (TruncOp != ISD::FP_ROUND)
2987 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2988 else
2989 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2990 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002991 break;
2992 }
2993 }
2994 break;
2995 case ISD::SELECT_CC: {
2996 Tmp1 = Node->getOperand(0); // LHS
2997 Tmp2 = Node->getOperand(1); // RHS
2998 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2999 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman8181bd12008-07-27 21:46:04 +00003000 SDValue CC = Node->getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003001
Duncan Sands4a361272009-01-01 15:52:00 +00003002 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003003
Evan Cheng71343822008-10-15 02:05:31 +00003004 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003005 // the LHS is a legal SETCC itself. In this case, we need to compare
3006 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00003007 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003008 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3009 CC = DAG.getCondCode(ISD::SETNE);
3010 }
3011 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
3012
3013 // Everything is legal, see if we should expand this op or something.
3014 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
3015 default: assert(0 && "This action is not supported yet!");
3016 case TargetLowering::Legal: break;
3017 case TargetLowering::Custom:
3018 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003019 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003020 break;
3021 }
3022 break;
3023 }
3024 case ISD::SETCC:
3025 Tmp1 = Node->getOperand(0);
3026 Tmp2 = Node->getOperand(1);
3027 Tmp3 = Node->getOperand(2);
Evan Cheng71343822008-10-15 02:05:31 +00003028 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003029
3030 // If we had to Expand the SetCC operands into a SELECT node, then it may
3031 // not always be possible to return a true LHS & RHS. In this case, just
3032 // return the value we legalized, returned in the LHS
Gabor Greif1c80d112008-08-28 21:40:38 +00003033 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003034 Result = Tmp1;
3035 break;
3036 }
3037
3038 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
3039 default: assert(0 && "Cannot handle this action for SETCC yet!");
3040 case TargetLowering::Custom:
3041 isCustom = true;
3042 // FALLTHROUGH.
3043 case TargetLowering::Legal:
3044 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3045 if (isCustom) {
3046 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003047 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003048 }
3049 break;
3050 case TargetLowering::Promote: {
3051 // First step, figure out the appropriate operation to use.
3052 // Allow SETCC to not be supported for all legal data types
3053 // Mostly this targets FP
Duncan Sands92c43912008-06-06 12:08:01 +00003054 MVT NewInTy = Node->getOperand(0).getValueType();
3055 MVT OldVT = NewInTy; OldVT = OldVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003056
3057 // Scan for the appropriate larger type to use.
3058 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00003059 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003060
Duncan Sands92c43912008-06-06 12:08:01 +00003061 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003062 "Fell off of the edge of the integer world");
Duncan Sands92c43912008-06-06 12:08:01 +00003063 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003064 "Fell off of the edge of the floating point world");
3065
3066 // If the target supports SETCC of this type, use it.
3067 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
3068 break;
3069 }
Duncan Sands92c43912008-06-06 12:08:01 +00003070 if (NewInTy.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003071 assert(0 && "Cannot promote Legal Integer SETCC yet");
3072 else {
3073 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
3074 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
3075 }
3076 Tmp1 = LegalizeOp(Tmp1);
3077 Tmp2 = LegalizeOp(Tmp2);
3078 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3079 Result = LegalizeOp(Result);
3080 break;
3081 }
3082 case TargetLowering::Expand:
3083 // Expand a setcc node into a select_cc of the same condition, lhs, and
3084 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands92c43912008-06-06 12:08:01 +00003085 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003086 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3087 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3088 Tmp3);
3089 break;
3090 }
3091 break;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003092 case ISD::VSETCC: {
3093 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3094 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman8181bd12008-07-27 21:46:04 +00003095 SDValue CC = Node->getOperand(2);
Nate Begeman9a1ce152008-05-12 19:40:03 +00003096
3097 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3098
3099 // Everything is legal, see if we should expand this op or something.
3100 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3101 default: assert(0 && "This action is not supported yet!");
3102 case TargetLowering::Legal: break;
3103 case TargetLowering::Custom:
3104 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003105 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003106 break;
Mon P Wangec428ad2008-12-13 08:15:14 +00003107 case TargetLowering::Expand: {
3108 // Unroll into a nasty set of scalar code for now.
3109 MVT VT = Node->getValueType(0);
3110 unsigned NumElems = VT.getVectorNumElements();
3111 MVT EltVT = VT.getVectorElementType();
3112 MVT TmpEltVT = Tmp1.getValueType().getVectorElementType();
3113 SmallVector<SDValue, 8> Ops(NumElems);
3114 for (unsigned i = 0; i < NumElems; ++i) {
3115 SDValue In1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3116 Tmp1, DAG.getIntPtrConstant(i));
Duncan Sands4a361272009-01-01 15:52:00 +00003117 Ops[i] = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(TmpEltVT), In1,
Mon P Wang77bc9cd2008-12-17 08:49:47 +00003118 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3119 Tmp2, DAG.getIntPtrConstant(i)),
3120 CC);
3121 Ops[i] = DAG.getNode(ISD::SELECT, EltVT, Ops[i],
3122 DAG.getConstant(EltVT.getIntegerVTBitMask(),EltVT),
3123 DAG.getConstant(0, EltVT));
Mon P Wangec428ad2008-12-13 08:15:14 +00003124 }
3125 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], NumElems);
3126 break;
3127 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00003128 }
3129 break;
3130 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003131
3132 case ISD::SHL_PARTS:
3133 case ISD::SRA_PARTS:
3134 case ISD::SRL_PARTS: {
Dan Gohman8181bd12008-07-27 21:46:04 +00003135 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003136 bool Changed = false;
3137 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3138 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3139 Changed |= Ops.back() != Node->getOperand(i);
3140 }
3141 if (Changed)
3142 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
3143
3144 switch (TLI.getOperationAction(Node->getOpcode(),
3145 Node->getValueType(0))) {
3146 default: assert(0 && "This action is not supported yet!");
3147 case TargetLowering::Legal: break;
3148 case TargetLowering::Custom:
3149 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003150 if (Tmp1.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003151 SDValue Tmp2, RetVal(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003152 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
3153 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman8181bd12008-07-27 21:46:04 +00003154 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00003155 if (i == Op.getResNo())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003156 RetVal = Tmp2;
3157 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003158 assert(RetVal.getNode() && "Illegal result number");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003159 return RetVal;
3160 }
3161 break;
3162 }
3163
3164 // Since these produce multiple values, make sure to remember that we
3165 // legalized all of them.
3166 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman8181bd12008-07-27 21:46:04 +00003167 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00003168 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003169 }
3170
3171 // Binary operators
3172 case ISD::ADD:
3173 case ISD::SUB:
3174 case ISD::MUL:
3175 case ISD::MULHS:
3176 case ISD::MULHU:
3177 case ISD::UDIV:
3178 case ISD::SDIV:
3179 case ISD::AND:
3180 case ISD::OR:
3181 case ISD::XOR:
3182 case ISD::SHL:
3183 case ISD::SRL:
3184 case ISD::SRA:
3185 case ISD::FADD:
3186 case ISD::FSUB:
3187 case ISD::FMUL:
3188 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00003189 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003190 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3191 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3192 case Expand: assert(0 && "Not possible");
3193 case Legal:
3194 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3195 break;
3196 case Promote:
3197 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3198 break;
3199 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003200
3201 if ((Node->getOpcode() == ISD::SHL ||
3202 Node->getOpcode() == ISD::SRL ||
3203 Node->getOpcode() == ISD::SRA) &&
3204 !Node->getValueType(0).isVector()) {
Mon P Wang9901e732008-12-09 05:46:39 +00003205 Tmp2 = LegalizeShiftAmount(Tmp2);
Mon P Wangec428ad2008-12-13 08:15:14 +00003206 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003207
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003208 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangbff5d9c2008-11-10 04:46:22 +00003209
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003210 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3211 default: assert(0 && "BinOp legalize operation not supported");
3212 case TargetLowering::Legal: break;
3213 case TargetLowering::Custom:
3214 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003215 if (Tmp1.getNode()) {
Nate Begemanbb1ce942008-07-29 15:49:41 +00003216 Result = Tmp1;
3217 break;
Nate Begeman7569e762008-07-29 19:07:27 +00003218 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003219 // Fall through if the custom lower can't deal with the operation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003220 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00003221 MVT VT = Op.getValueType();
Mon P Wang1448aad2008-10-30 08:01:45 +00003222
Dan Gohman5a199552007-10-08 18:33:35 +00003223 // See if multiply or divide can be lowered using two-result operations.
3224 SDVTList VTs = DAG.getVTList(VT, VT);
3225 if (Node->getOpcode() == ISD::MUL) {
3226 // We just need the low half of the multiply; try both the signed
3227 // and unsigned forms. If the target supports both SMUL_LOHI and
3228 // UMUL_LOHI, form a preference by checking which forms of plain
3229 // MULH it supports.
3230 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3231 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3232 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3233 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3234 unsigned OpToUse = 0;
3235 if (HasSMUL_LOHI && !HasMULHS) {
3236 OpToUse = ISD::SMUL_LOHI;
3237 } else if (HasUMUL_LOHI && !HasMULHU) {
3238 OpToUse = ISD::UMUL_LOHI;
3239 } else if (HasSMUL_LOHI) {
3240 OpToUse = ISD::SMUL_LOHI;
3241 } else if (HasUMUL_LOHI) {
3242 OpToUse = ISD::UMUL_LOHI;
3243 }
3244 if (OpToUse) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003245 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003246 break;
3247 }
3248 }
3249 if (Node->getOpcode() == ISD::MULHS &&
3250 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003251 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3252 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003253 break;
3254 }
3255 if (Node->getOpcode() == ISD::MULHU &&
3256 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003257 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3258 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003259 break;
3260 }
3261 if (Node->getOpcode() == ISD::SDIV &&
3262 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003263 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3264 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003265 break;
3266 }
3267 if (Node->getOpcode() == ISD::UDIV &&
3268 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003269 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3270 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003271 break;
3272 }
Mon P Wang26342922008-12-18 20:03:17 +00003273
Dan Gohman6d05cac2007-10-11 23:57:53 +00003274 // Check to see if we have a libcall for this operator.
3275 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3276 bool isSigned = false;
3277 switch (Node->getOpcode()) {
3278 case ISD::UDIV:
3279 case ISD::SDIV:
3280 if (VT == MVT::i32) {
3281 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang1448aad2008-10-30 08:01:45 +00003282 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003283 isSigned = Node->getOpcode() == ISD::SDIV;
3284 }
3285 break;
Chris Lattner48188652008-10-04 21:27:46 +00003286 case ISD::MUL:
3287 if (VT == MVT::i32)
3288 LC = RTLIB::MUL_I32;
Scott Michel81215042008-12-29 03:21:37 +00003289 else if (VT == MVT::i64)
3290 LC = RTLIB::MUL_I64;
Chris Lattner48188652008-10-04 21:27:46 +00003291 break;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003292 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00003293 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3294 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003295 break;
3296 default: break;
3297 }
3298 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003299 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003300 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003301 break;
3302 }
Mon P Wang1448aad2008-10-30 08:01:45 +00003303
Duncan Sands92c43912008-06-06 12:08:01 +00003304 assert(Node->getValueType(0).isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003305 "Cannot expand this binary operator!");
3306 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003307 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003308 break;
3309 }
3310 case TargetLowering::Promote: {
3311 switch (Node->getOpcode()) {
3312 default: assert(0 && "Do not know how to promote this BinOp!");
3313 case ISD::AND:
3314 case ISD::OR:
3315 case ISD::XOR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003316 MVT OVT = Node->getValueType(0);
3317 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3318 assert(OVT.isVector() && "Cannot promote this BinOp!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003319 // Bit convert each of the values to the new type.
3320 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3321 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3322 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3323 // Bit convert the result back the original type.
3324 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3325 break;
3326 }
3327 }
3328 }
3329 }
3330 break;
3331
Dan Gohman475cd732007-10-05 14:17:22 +00003332 case ISD::SMUL_LOHI:
3333 case ISD::UMUL_LOHI:
3334 case ISD::SDIVREM:
3335 case ISD::UDIVREM:
3336 // These nodes will only be produced by target-specific lowering, so
3337 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003338 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003339 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003340
3341 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3342 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3343 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003344 break;
3345
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003346 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3347 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3348 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3349 case Expand: assert(0 && "Not possible");
3350 case Legal:
3351 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3352 break;
3353 case Promote:
3354 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3355 break;
3356 }
3357
3358 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3359
3360 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3361 default: assert(0 && "Operation not supported");
3362 case TargetLowering::Custom:
3363 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003364 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003365 break;
3366 case TargetLowering::Legal: break;
3367 case TargetLowering::Expand: {
3368 // If this target supports fabs/fneg natively and select is cheap,
3369 // do this efficiently.
3370 if (!TLI.isSelectExpensive() &&
3371 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3372 TargetLowering::Legal &&
3373 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3374 TargetLowering::Legal) {
3375 // Get the sign bit of the RHS.
Duncan Sands92c43912008-06-06 12:08:01 +00003376 MVT IVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003377 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman8181bd12008-07-27 21:46:04 +00003378 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Duncan Sands4a361272009-01-01 15:52:00 +00003379 SignBit = DAG.getSetCC(TLI.getSetCCResultType(IVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003380 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3381 // Get the absolute value of the result.
Dan Gohman8181bd12008-07-27 21:46:04 +00003382 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003383 // Select between the nabs and abs value based on the sign bit of
3384 // the input.
3385 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3386 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3387 AbsVal),
3388 AbsVal);
3389 Result = LegalizeOp(Result);
3390 break;
3391 }
3392
3393 // Otherwise, do bitwise ops!
Duncan Sands92c43912008-06-06 12:08:01 +00003394 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003395 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3396 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3397 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3398 Result = LegalizeOp(Result);
3399 break;
3400 }
3401 }
3402 break;
3403
3404 case ISD::ADDC:
3405 case ISD::SUBC:
3406 Tmp1 = LegalizeOp(Node->getOperand(0));
3407 Tmp2 = LegalizeOp(Node->getOperand(1));
3408 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003409 Tmp3 = Result.getValue(0);
3410 Tmp4 = Result.getValue(1);
3411
3412 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3413 default: assert(0 && "This action is not supported yet!");
3414 case TargetLowering::Legal:
3415 break;
3416 case TargetLowering::Custom:
3417 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3418 if (Tmp1.getNode() != NULL) {
Sanjiv Guptad57f2e12008-11-27 05:58:04 +00003419 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003420 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3421 }
3422 break;
3423 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003424 // Since this produces two values, make sure to remember that we legalized
3425 // both of them.
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003426 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3427 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3428 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003429
3430 case ISD::ADDE:
3431 case ISD::SUBE:
3432 Tmp1 = LegalizeOp(Node->getOperand(0));
3433 Tmp2 = LegalizeOp(Node->getOperand(1));
3434 Tmp3 = LegalizeOp(Node->getOperand(2));
3435 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003436 Tmp3 = Result.getValue(0);
3437 Tmp4 = Result.getValue(1);
3438
3439 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3440 default: assert(0 && "This action is not supported yet!");
3441 case TargetLowering::Legal:
3442 break;
3443 case TargetLowering::Custom:
3444 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3445 if (Tmp1.getNode() != NULL) {
Sanjiv Guptad57f2e12008-11-27 05:58:04 +00003446 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003447 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3448 }
3449 break;
3450 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003451 // Since this produces two values, make sure to remember that we legalized
3452 // both of them.
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003453 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3454 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3455 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003456
3457 case ISD::BUILD_PAIR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003458 MVT PairTy = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003459 // TODO: handle the case where the Lo and Hi operands are not of legal type
3460 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3461 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3462 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3463 case TargetLowering::Promote:
3464 case TargetLowering::Custom:
3465 assert(0 && "Cannot promote/custom this yet!");
3466 case TargetLowering::Legal:
3467 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3468 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3469 break;
3470 case TargetLowering::Expand:
3471 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3472 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3473 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003474 DAG.getConstant(PairTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003475 TLI.getShiftAmountTy()));
3476 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3477 break;
3478 }
3479 break;
3480 }
3481
3482 case ISD::UREM:
3483 case ISD::SREM:
3484 case ISD::FREM:
3485 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3486 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3487
3488 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3489 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3490 case TargetLowering::Custom:
3491 isCustom = true;
3492 // FALLTHROUGH
3493 case TargetLowering::Legal:
3494 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3495 if (isCustom) {
3496 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003497 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003498 }
3499 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003500 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003501 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3502 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands92c43912008-06-06 12:08:01 +00003503 MVT VT = Node->getValueType(0);
Dan Gohman5a199552007-10-08 18:33:35 +00003504
3505 // See if remainder can be lowered using two-result operations.
3506 SDVTList VTs = DAG.getVTList(VT, VT);
3507 if (Node->getOpcode() == ISD::SREM &&
3508 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003509 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003510 break;
3511 }
3512 if (Node->getOpcode() == ISD::UREM &&
3513 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003514 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003515 break;
3516 }
3517
Duncan Sands92c43912008-06-06 12:08:01 +00003518 if (VT.isInteger()) {
Dan Gohman5a199552007-10-08 18:33:35 +00003519 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003520 TargetLowering::Legal) {
3521 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003522 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3523 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3524 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands92c43912008-06-06 12:08:01 +00003525 } else if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003526 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003527 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003528 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003529 "Cannot expand this binary operator!");
3530 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3531 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman8181bd12008-07-27 21:46:04 +00003532 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003533 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003534 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003535 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00003536 assert(VT.isFloatingPoint() &&
Dan Gohman59b4b102007-11-06 22:11:54 +00003537 "remainder op must have integer or floating-point type");
Duncan Sands92c43912008-06-06 12:08:01 +00003538 if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003539 Result = LegalizeOp(UnrollVectorOp(Op));
3540 } else {
3541 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003542 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3543 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003544 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003545 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003546 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003547 }
3548 break;
3549 }
Dan Gohman5a199552007-10-08 18:33:35 +00003550 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003551 break;
3552 case ISD::VAARG: {
3553 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3554 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3555
Duncan Sands92c43912008-06-06 12:08:01 +00003556 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003557 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3558 default: assert(0 && "This action is not supported yet!");
3559 case TargetLowering::Custom:
3560 isCustom = true;
3561 // FALLTHROUGH
3562 case TargetLowering::Legal:
3563 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3564 Result = Result.getValue(0);
3565 Tmp1 = Result.getValue(1);
3566
3567 if (isCustom) {
3568 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003569 if (Tmp2.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003570 Result = LegalizeOp(Tmp2);
3571 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3572 }
3573 }
3574 break;
3575 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003576 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003577 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003578 // Increment the pointer, VAList, to the next vaarg
Duncan Sands55a4c232008-11-03 11:51:11 +00003579 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sandsd68f13b2009-01-12 20:38:59 +00003580 DAG.getConstant(TLI.getTargetData()->
3581 getTypePaddedSize(VT.getTypeForMVT()),
3582 TLI.getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003583 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00003584 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003585 // Load the actual argument out of the pointer VAList
3586 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3587 Tmp1 = LegalizeOp(Result.getValue(1));
3588 Result = LegalizeOp(Result);
3589 break;
3590 }
3591 }
3592 // Since VAARG produces two values, make sure to remember that we
3593 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003594 AddLegalizedOperand(SDValue(Node, 0), Result);
3595 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00003596 return Op.getResNo() ? Tmp1 : Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003597 }
3598
3599 case ISD::VACOPY:
3600 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3601 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3602 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3603
3604 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3605 default: assert(0 && "This action is not supported yet!");
3606 case TargetLowering::Custom:
3607 isCustom = true;
3608 // FALLTHROUGH
3609 case TargetLowering::Legal:
3610 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3611 Node->getOperand(3), Node->getOperand(4));
3612 if (isCustom) {
3613 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003614 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003615 }
3616 break;
3617 case TargetLowering::Expand:
3618 // This defaults to loading a pointer from the input and storing it to the
3619 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003620 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3621 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman6b9a08e2008-04-17 02:09:26 +00003622 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3623 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003624 break;
3625 }
3626 break;
3627
3628 case ISD::VAEND:
3629 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3630 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3631
3632 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3633 default: assert(0 && "This action is not supported yet!");
3634 case TargetLowering::Custom:
3635 isCustom = true;
3636 // FALLTHROUGH
3637 case TargetLowering::Legal:
3638 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3639 if (isCustom) {
3640 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003641 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003642 }
3643 break;
3644 case TargetLowering::Expand:
3645 Result = Tmp1; // Default to a no-op, return the chain
3646 break;
3647 }
3648 break;
3649
3650 case ISD::VASTART:
3651 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3652 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3653
3654 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3655
3656 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3657 default: assert(0 && "This action is not supported yet!");
3658 case TargetLowering::Legal: break;
3659 case TargetLowering::Custom:
3660 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003661 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003662 break;
3663 }
3664 break;
3665
3666 case ISD::ROTL:
3667 case ISD::ROTR:
3668 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3669 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3670 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3671 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3672 default:
3673 assert(0 && "ROTL/ROTR legalize operation not supported");
3674 break;
3675 case TargetLowering::Legal:
3676 break;
3677 case TargetLowering::Custom:
3678 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003679 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003680 break;
3681 case TargetLowering::Promote:
3682 assert(0 && "Do not know how to promote ROTL/ROTR");
3683 break;
3684 case TargetLowering::Expand:
3685 assert(0 && "Do not know how to expand ROTL/ROTR");
3686 break;
3687 }
3688 break;
3689
3690 case ISD::BSWAP:
3691 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3692 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3693 case TargetLowering::Custom:
3694 assert(0 && "Cannot custom legalize this yet!");
3695 case TargetLowering::Legal:
3696 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3697 break;
3698 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003699 MVT OVT = Tmp1.getValueType();
3700 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3701 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003702
3703 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3704 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3705 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3706 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3707 break;
3708 }
3709 case TargetLowering::Expand:
3710 Result = ExpandBSWAP(Tmp1);
3711 break;
3712 }
3713 break;
3714
3715 case ISD::CTPOP:
3716 case ISD::CTTZ:
3717 case ISD::CTLZ:
3718 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3719 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003720 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003721 case TargetLowering::Legal:
3722 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003723 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003724 TargetLowering::Custom) {
3725 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003726 if (Tmp1.getNode()) {
Scott Michelbc62b412007-08-02 02:22:46 +00003727 Result = Tmp1;
3728 }
Scott Michel48b63e62007-07-30 21:00:31 +00003729 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003730 break;
3731 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003732 MVT OVT = Tmp1.getValueType();
3733 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003734
3735 // Zero extend the argument.
3736 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3737 // Perform the larger operation, then subtract if needed.
3738 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3739 switch (Node->getOpcode()) {
3740 case ISD::CTPOP:
3741 Result = Tmp1;
3742 break;
3743 case ISD::CTTZ:
3744 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Duncan Sands4a361272009-01-01 15:52:00 +00003745 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003746 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003747 ISD::SETEQ);
3748 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003749 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003750 break;
3751 case ISD::CTLZ:
3752 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3753 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003754 DAG.getConstant(NVT.getSizeInBits() -
3755 OVT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003756 break;
3757 }
3758 break;
3759 }
3760 case TargetLowering::Expand:
3761 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3762 break;
3763 }
3764 break;
3765
3766 // Unary operators
3767 case ISD::FABS:
3768 case ISD::FNEG:
3769 case ISD::FSQRT:
3770 case ISD::FSIN:
3771 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003772 case ISD::FLOG:
3773 case ISD::FLOG2:
3774 case ISD::FLOG10:
3775 case ISD::FEXP:
3776 case ISD::FEXP2:
Dan Gohmanc8b20e22008-08-21 17:55:02 +00003777 case ISD::FTRUNC:
3778 case ISD::FFLOOR:
3779 case ISD::FCEIL:
3780 case ISD::FRINT:
3781 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003782 Tmp1 = LegalizeOp(Node->getOperand(0));
3783 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3784 case TargetLowering::Promote:
3785 case TargetLowering::Custom:
3786 isCustom = true;
3787 // FALLTHROUGH
3788 case TargetLowering::Legal:
3789 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3790 if (isCustom) {
3791 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003792 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003793 }
3794 break;
3795 case TargetLowering::Expand:
3796 switch (Node->getOpcode()) {
3797 default: assert(0 && "Unreachable!");
3798 case ISD::FNEG:
3799 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3800 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3801 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3802 break;
3803 case ISD::FABS: {
3804 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands92c43912008-06-06 12:08:01 +00003805 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003806 Tmp2 = DAG.getConstantFP(0.0, VT);
Duncan Sands4a361272009-01-01 15:52:00 +00003807 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
3808 Tmp1, Tmp2, ISD::SETUGT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003809 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3810 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3811 break;
3812 }
Evan Cheng1fac6952008-09-09 23:35:53 +00003813 case ISD::FSQRT:
3814 case ISD::FSIN:
3815 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003816 case ISD::FLOG:
3817 case ISD::FLOG2:
3818 case ISD::FLOG10:
3819 case ISD::FEXP:
3820 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00003821 case ISD::FTRUNC:
3822 case ISD::FFLOOR:
3823 case ISD::FCEIL:
3824 case ISD::FRINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00003825 case ISD::FNEARBYINT: {
Duncan Sands92c43912008-06-06 12:08:01 +00003826 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003827
3828 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003829 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003830 Result = LegalizeOp(UnrollVectorOp(Op));
3831 break;
3832 }
3833
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003834 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3835 switch(Node->getOpcode()) {
3836 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003837 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3838 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003839 break;
3840 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003841 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3842 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003843 break;
3844 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003845 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3846 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003847 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00003848 case ISD::FLOG:
3849 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3850 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3851 break;
3852 case ISD::FLOG2:
3853 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3854 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3855 break;
3856 case ISD::FLOG10:
3857 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3858 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3859 break;
3860 case ISD::FEXP:
3861 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3862 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3863 break;
3864 case ISD::FEXP2:
3865 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3866 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3867 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00003868 case ISD::FTRUNC:
3869 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3870 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3871 break;
3872 case ISD::FFLOOR:
3873 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3874 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3875 break;
3876 case ISD::FCEIL:
3877 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3878 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3879 break;
3880 case ISD::FRINT:
3881 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3882 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3883 break;
3884 case ISD::FNEARBYINT:
3885 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3886 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3887 break;
Evan Cheng1fac6952008-09-09 23:35:53 +00003888 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003889 default: assert(0 && "Unreachable!");
3890 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003891 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003892 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003893 break;
3894 }
3895 }
3896 break;
3897 }
3898 break;
3899 case ISD::FPOWI: {
Duncan Sands92c43912008-06-06 12:08:01 +00003900 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003901
3902 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003903 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003904 Result = LegalizeOp(UnrollVectorOp(Op));
3905 break;
3906 }
3907
3908 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003909 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3910 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003911 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003912 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003913 break;
3914 }
3915 case ISD::BIT_CONVERT:
3916 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003917 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3918 Node->getValueType(0));
Duncan Sands92c43912008-06-06 12:08:01 +00003919 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003920 // The input has to be a vector type, we have to either scalarize it, pack
3921 // it, or convert it based on whether the input vector type is legal.
Gabor Greif1c80d112008-08-28 21:40:38 +00003922 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00003923 int InIx = Node->getOperand(0).getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00003924 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3925 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003926
3927 // Figure out if there is a simple type corresponding to this Vector
3928 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00003929 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003930 if (TLI.isTypeLegal(TVT)) {
3931 // Turn this into a bit convert of the vector input.
3932 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3933 LegalizeOp(Node->getOperand(0)));
3934 break;
3935 } else if (NumElems == 1) {
3936 // Turn this into a bit convert of the scalar input.
3937 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3938 ScalarizeVectorOp(Node->getOperand(0)));
3939 break;
3940 } else {
3941 // FIXME: UNIMP! Store then reload
3942 assert(0 && "Cast from unsupported vector type not implemented yet!");
3943 }
3944 } else {
3945 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3946 Node->getOperand(0).getValueType())) {
3947 default: assert(0 && "Unknown operation action!");
3948 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003949 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3950 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003951 break;
3952 case TargetLowering::Legal:
3953 Tmp1 = LegalizeOp(Node->getOperand(0));
3954 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3955 break;
3956 }
3957 }
3958 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003959 case ISD::CONVERT_RNDSAT: {
3960 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3961 switch (CvtCode) {
3962 default: assert(0 && "Unknown cvt code!");
3963 case ISD::CVT_SF:
3964 case ISD::CVT_UF:
Mon P Wang73d31542008-11-10 20:54:11 +00003965 case ISD::CVT_FF:
Mon P Wang2fc3f9e2008-12-09 07:27:39 +00003966 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003967 case ISD::CVT_FS:
3968 case ISD::CVT_FU:
3969 case ISD::CVT_SS:
3970 case ISD::CVT_SU:
3971 case ISD::CVT_US:
3972 case ISD::CVT_UU: {
3973 SDValue DTyOp = Node->getOperand(1);
3974 SDValue STyOp = Node->getOperand(2);
3975 SDValue RndOp = Node->getOperand(3);
3976 SDValue SatOp = Node->getOperand(4);
3977 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3978 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3979 case Legal:
3980 Tmp1 = LegalizeOp(Node->getOperand(0));
3981 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3982 RndOp, SatOp);
3983 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3984 TargetLowering::Custom) {
3985 Tmp1 = TLI.LowerOperation(Result, DAG);
3986 if (Tmp1.getNode()) Result = Tmp1;
3987 }
3988 break;
3989 case Promote:
3990 Result = PromoteOp(Node->getOperand(0));
3991 // For FP, make Op1 a i32
3992
Mon P Wang2fc3f9e2008-12-09 07:27:39 +00003993 Result = DAG.getConvertRndSat(Op.getValueType(), Result,
Mon P Wang73d31542008-11-10 20:54:11 +00003994 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3995 break;
3996 }
3997 break;
3998 }
3999 } // end switch CvtCode
4000 break;
4001 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004002 // Conversion operators. The source and destination have different types.
4003 case ISD::SINT_TO_FP:
4004 case ISD::UINT_TO_FP: {
4005 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman29c3cef2008-08-14 20:04:46 +00004006 Result = LegalizeINT_TO_FP(Result, isSigned,
4007 Node->getValueType(0), Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004008 break;
4009 }
4010 case ISD::TRUNCATE:
4011 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4012 case Legal:
4013 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michele9b8a402008-12-02 19:55:08 +00004014 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
4015 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
4016 case TargetLowering::Custom:
Mon P Wang72fe5462008-12-11 00:44:22 +00004017 isCustom = true;
4018 // FALLTHROUGH
Scott Michele9b8a402008-12-02 19:55:08 +00004019 case TargetLowering::Legal:
Mon P Wang72fe5462008-12-11 00:44:22 +00004020 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4021 if (isCustom) {
4022 Tmp1 = TLI.LowerOperation(Result, DAG);
4023 if (Tmp1.getNode()) Result = Tmp1;
4024 }
4025 break;
Mon P Wang83edba52008-12-12 01:25:51 +00004026 case TargetLowering::Expand:
4027 assert(Result.getValueType().isVector() && "must be vector type");
4028 // Unroll the truncate. We should do better.
4029 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Schellerbfc55ee2008-12-02 12:12:25 +00004030 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004031 break;
4032 case Expand:
4033 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4034
4035 // Since the result is legal, we should just be able to truncate the low
4036 // part of the source.
4037 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
4038 break;
4039 case Promote:
4040 Result = PromoteOp(Node->getOperand(0));
4041 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
4042 break;
4043 }
4044 break;
4045
4046 case ISD::FP_TO_SINT:
4047 case ISD::FP_TO_UINT:
4048 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4049 case Legal:
4050 Tmp1 = LegalizeOp(Node->getOperand(0));
4051
4052 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
4053 default: assert(0 && "Unknown operation action!");
4054 case TargetLowering::Custom:
4055 isCustom = true;
4056 // FALLTHROUGH
4057 case TargetLowering::Legal:
4058 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4059 if (isCustom) {
4060 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004061 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004062 }
4063 break;
4064 case TargetLowering::Promote:
4065 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
4066 Node->getOpcode() == ISD::FP_TO_SINT);
4067 break;
4068 case TargetLowering::Expand:
4069 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004070 SDValue True, False;
Duncan Sands92c43912008-06-06 12:08:01 +00004071 MVT VT = Node->getOperand(0).getValueType();
4072 MVT NVT = Node->getValueType(0);
Dale Johannesen958b08b2007-09-19 23:55:34 +00004073 const uint64_t zero[] = {0, 0};
Duncan Sands92c43912008-06-06 12:08:01 +00004074 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
4075 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohman88ae8c52008-02-29 01:44:25 +00004076 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00004077 Tmp2 = DAG.getConstantFP(apf, VT);
Duncan Sands4a361272009-01-01 15:52:00 +00004078 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(VT), Node->getOperand(0),
4079 Tmp2, ISD::SETLT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004080 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
4081 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
4082 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
4083 Tmp2));
4084 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman88ae8c52008-02-29 01:44:25 +00004085 DAG.getConstant(x, NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004086 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
4087 break;
4088 } else {
4089 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
4090 }
4091 break;
4092 }
4093 break;
4094 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00004095 MVT VT = Op.getValueType();
4096 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00004097 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004098 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00004099 if (Node->getOpcode() == ISD::FP_TO_SINT) {
4100 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
4101 Node->getOperand(0), DAG.getValueType(MVT::f64));
4102 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
4103 DAG.getIntPtrConstant(1));
4104 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
4105 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00004106 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4107 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4108 Tmp2 = DAG.getConstantFP(apf, OVT);
4109 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4110 // FIXME: generated code sucks.
4111 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
4112 DAG.getNode(ISD::ADD, MVT::i32,
4113 DAG.getNode(ISD::FP_TO_SINT, VT,
4114 DAG.getNode(ISD::FSUB, OVT,
4115 Node->getOperand(0), Tmp2)),
4116 DAG.getConstant(0x80000000, MVT::i32)),
4117 DAG.getNode(ISD::FP_TO_SINT, VT,
4118 Node->getOperand(0)),
4119 DAG.getCondCode(ISD::SETGE));
4120 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004121 break;
4122 }
Dan Gohmanec51f642008-03-10 23:03:31 +00004123 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsf68dffb2008-07-17 02:36:29 +00004124 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4125 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4126 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman8181bd12008-07-27 21:46:04 +00004127 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00004128 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004129 break;
4130 }
4131 case Promote:
4132 Tmp1 = PromoteOp(Node->getOperand(0));
4133 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4134 Result = LegalizeOp(Result);
4135 break;
4136 }
4137 break;
4138
Chris Lattner56ecde32008-01-16 06:57:07 +00004139 case ISD::FP_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004140 MVT DstVT = Op.getValueType();
4141 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004142 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4143 // The only other way we can lower this is to turn it into a STORE,
4144 // LOAD pair, targetting a temporary location (a stack slot).
4145 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
4146 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00004147 }
4148 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4149 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4150 case Legal:
4151 Tmp1 = LegalizeOp(Node->getOperand(0));
4152 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4153 break;
4154 case Promote:
4155 Tmp1 = PromoteOp(Node->getOperand(0));
4156 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4157 break;
4158 }
4159 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004160 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004161 case ISD::FP_ROUND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004162 MVT DstVT = Op.getValueType();
4163 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004164 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4165 if (SrcVT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004166 SDValue Lo;
Dale Johannesena0d36082008-01-20 01:18:38 +00004167 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00004168 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00004169 if (DstVT!=MVT::f64)
4170 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00004171 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004172 }
Chris Lattner5872a362008-01-17 07:00:52 +00004173 // The only other way we can lower this is to turn it into a STORE,
4174 // LOAD pair, targetting a temporary location (a stack slot).
4175 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4176 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004177 }
Chris Lattner56ecde32008-01-16 06:57:07 +00004178 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4179 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4180 case Legal:
4181 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004182 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004183 break;
4184 case Promote:
4185 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004186 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4187 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004188 break;
4189 }
4190 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004191 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004192 case ISD::ANY_EXTEND:
4193 case ISD::ZERO_EXTEND:
4194 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004195 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4196 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4197 case Legal:
4198 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac54d002008-04-30 00:26:38 +00004199 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michelac7091c2008-02-15 23:05:48 +00004200 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4201 TargetLowering::Custom) {
Scott Michelac54d002008-04-30 00:26:38 +00004202 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004203 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelac7091c2008-02-15 23:05:48 +00004204 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004205 break;
4206 case Promote:
4207 switch (Node->getOpcode()) {
4208 case ISD::ANY_EXTEND:
4209 Tmp1 = PromoteOp(Node->getOperand(0));
4210 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
4211 break;
4212 case ISD::ZERO_EXTEND:
4213 Result = PromoteOp(Node->getOperand(0));
4214 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4215 Result = DAG.getZeroExtendInReg(Result,
4216 Node->getOperand(0).getValueType());
4217 break;
4218 case ISD::SIGN_EXTEND:
4219 Result = PromoteOp(Node->getOperand(0));
4220 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4221 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4222 Result,
4223 DAG.getValueType(Node->getOperand(0).getValueType()));
4224 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004225 }
4226 }
4227 break;
4228 case ISD::FP_ROUND_INREG:
4229 case ISD::SIGN_EXTEND_INREG: {
4230 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004231 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004232
4233 // If this operation is not supported, convert it to a shl/shr or load/store
4234 // pair.
4235 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4236 default: assert(0 && "This action not supported for this op yet!");
4237 case TargetLowering::Legal:
4238 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
4239 break;
4240 case TargetLowering::Expand:
4241 // If this is an integer extend and shifts are supported, do that.
4242 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
4243 // NOTE: we could fall back on load/store here too for targets without
4244 // SAR. However, it is doubtful that any exist.
Duncan Sands92c43912008-06-06 12:08:01 +00004245 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4246 ExtraVT.getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00004247 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004248 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4249 Node->getOperand(0), ShiftCst);
4250 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4251 Result, ShiftCst);
4252 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
4253 // The only way we can lower this is to turn it into a TRUNCSTORE,
4254 // EXTLOAD pair, targetting a temporary location (a stack slot).
4255
4256 // NOTE: there is a choice here between constantly creating new stack
4257 // slots and always reusing the same one. We currently always create
4258 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00004259 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4260 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004261 } else {
4262 assert(0 && "Unknown op");
4263 }
4264 break;
4265 }
4266 break;
4267 }
Duncan Sands38947cd2007-07-27 12:58:54 +00004268 case ISD::TRAMPOLINE: {
Dan Gohman8181bd12008-07-27 21:46:04 +00004269 SDValue Ops[6];
Duncan Sands38947cd2007-07-27 12:58:54 +00004270 for (unsigned i = 0; i != 6; ++i)
4271 Ops[i] = LegalizeOp(Node->getOperand(i));
4272 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4273 // The only option for this node is to custom lower it.
4274 Result = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004275 assert(Result.getNode() && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00004276
4277 // Since trampoline produces two values, make sure to remember that we
4278 // legalized both of them.
4279 Tmp1 = LegalizeOp(Result.getValue(1));
4280 Result = LegalizeOp(Result);
Dan Gohman8181bd12008-07-27 21:46:04 +00004281 AddLegalizedOperand(SDValue(Node, 0), Result);
4282 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00004283 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00004284 }
Dan Gohmane8e4a412008-05-14 00:43:10 +00004285 case ISD::FLT_ROUNDS_: {
Duncan Sands92c43912008-06-06 12:08:01 +00004286 MVT VT = Node->getValueType(0);
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004287 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4288 default: assert(0 && "This action not supported for this op yet!");
4289 case TargetLowering::Custom:
4290 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004291 if (Result.getNode()) break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004292 // Fall Thru
4293 case TargetLowering::Legal:
4294 // If this operation is not supported, lower it to constant 1
4295 Result = DAG.getConstant(1, VT);
4296 break;
4297 }
Dan Gohmane09dc8c2008-05-12 16:07:15 +00004298 break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004299 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004300 case ISD::TRAP: {
Duncan Sands92c43912008-06-06 12:08:01 +00004301 MVT VT = Node->getValueType(0);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004302 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4303 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00004304 case TargetLowering::Legal:
4305 Tmp1 = LegalizeOp(Node->getOperand(0));
4306 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4307 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004308 case TargetLowering::Custom:
4309 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004310 if (Result.getNode()) break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004311 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00004312 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004313 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00004314 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004315 TargetLowering::ArgListTy Args;
Dan Gohman8181bd12008-07-27 21:46:04 +00004316 std::pair<SDValue,SDValue> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004317 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00004318 false, false, false, false, CallingConv::C, false,
Bill Wendlingfef06052008-09-16 21:48:12 +00004319 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner88e03932008-01-15 22:09:33 +00004320 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004321 Result = CallResult.second;
4322 break;
4323 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004324 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004325 }
Bill Wendling913dcf32008-11-22 00:22:52 +00004326
Bill Wendling7e04be62008-12-09 22:08:41 +00004327 case ISD::SADDO:
4328 case ISD::SSUBO: {
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004329 MVT VT = Node->getValueType(0);
4330 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4331 default: assert(0 && "This action not supported for this op yet!");
4332 case TargetLowering::Custom:
4333 Result = TLI.LowerOperation(Op, DAG);
4334 if (Result.getNode()) break;
4335 // FALLTHROUGH
4336 case TargetLowering::Legal: {
4337 SDValue LHS = LegalizeOp(Node->getOperand(0));
4338 SDValue RHS = LegalizeOp(Node->getOperand(1));
4339
Bill Wendling7e04be62008-12-09 22:08:41 +00004340 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
4341 ISD::ADD : ISD::SUB, LHS.getValueType(),
4342 LHS, RHS);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004343 MVT OType = Node->getValueType(1);
4344
Bill Wendlingc65e6e42008-11-25 08:19:22 +00004345 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004346
Bill Wendlingcf4de122008-11-25 19:40:17 +00004347 // LHSSign -> LHS >= 0
4348 // RHSSign -> RHS >= 0
4349 // SumSign -> Sum >= 0
4350 //
Bill Wendling7e04be62008-12-09 22:08:41 +00004351 // Add:
Bill Wendlingcf4de122008-11-25 19:40:17 +00004352 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling7e04be62008-12-09 22:08:41 +00004353 // Sub:
4354 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendlingcf4de122008-11-25 19:40:17 +00004355 //
4356 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4357 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
Bill Wendling7e04be62008-12-09 22:08:41 +00004358 SDValue SignsMatch = DAG.getSetCC(OType, LHSSign, RHSSign,
4359 Node->getOpcode() == ISD::SADDO ?
4360 ISD::SETEQ : ISD::SETNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004361
Bill Wendlingcf4de122008-11-25 19:40:17 +00004362 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4363 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004364
Bill Wendling7e04be62008-12-09 22:08:41 +00004365 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsMatch, SumSignNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004366
4367 MVT ValueVTs[] = { LHS.getValueType(), OType };
4368 SDValue Ops[] = { Sum, Cmp };
4369
Duncan Sands42d7bb82008-12-01 11:41:29 +00004370 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4371 &Ops[0], 2);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004372 SDNode *RNode = Result.getNode();
4373 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4374 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4375 break;
4376 }
4377 }
4378
4379 break;
4380 }
Bill Wendling7e04be62008-12-09 22:08:41 +00004381 case ISD::UADDO:
4382 case ISD::USUBO: {
Bill Wendling4c134df2008-11-24 19:21:46 +00004383 MVT VT = Node->getValueType(0);
4384 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4385 default: assert(0 && "This action not supported for this op yet!");
4386 case TargetLowering::Custom:
4387 Result = TLI.LowerOperation(Op, DAG);
4388 if (Result.getNode()) break;
4389 // FALLTHROUGH
4390 case TargetLowering::Legal: {
4391 SDValue LHS = LegalizeOp(Node->getOperand(0));
4392 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling913dcf32008-11-22 00:22:52 +00004393
Bill Wendling7e04be62008-12-09 22:08:41 +00004394 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
4395 ISD::ADD : ISD::SUB, LHS.getValueType(),
4396 LHS, RHS);
Bill Wendling4c134df2008-11-24 19:21:46 +00004397 MVT OType = Node->getValueType(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00004398 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS,
4399 Node->getOpcode () == ISD::UADDO ?
4400 ISD::SETULT : ISD::SETUGT);
Bill Wendling913dcf32008-11-22 00:22:52 +00004401
Bill Wendling4c134df2008-11-24 19:21:46 +00004402 MVT ValueVTs[] = { LHS.getValueType(), OType };
4403 SDValue Ops[] = { Sum, Cmp };
Bill Wendling913dcf32008-11-22 00:22:52 +00004404
Duncan Sands42d7bb82008-12-01 11:41:29 +00004405 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4406 &Ops[0], 2);
Bill Wendling4c134df2008-11-24 19:21:46 +00004407 SDNode *RNode = Result.getNode();
4408 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4409 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4410 break;
4411 }
4412 }
4413
Bill Wendling913dcf32008-11-22 00:22:52 +00004414 break;
4415 }
Bill Wendling7e04be62008-12-09 22:08:41 +00004416 case ISD::SMULO:
4417 case ISD::UMULO: {
4418 MVT VT = Node->getValueType(0);
4419 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4420 default: assert(0 && "This action is not supported at all!");
4421 case TargetLowering::Custom:
4422 Result = TLI.LowerOperation(Op, DAG);
4423 if (Result.getNode()) break;
4424 // Fall Thru
4425 case TargetLowering::Legal:
4426 // FIXME: According to Hacker's Delight, this can be implemented in
4427 // target independent lowering, but it would be inefficient, since it
Bill Wendling35f1a9d2008-12-10 02:01:32 +00004428 // requires a division + a branch.
Bill Wendling7e04be62008-12-09 22:08:41 +00004429 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4430 break;
4431 }
4432 break;
4433 }
4434
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004435 }
4436
4437 assert(Result.getValueType() == Op.getValueType() &&
4438 "Bad legalization!");
4439
4440 // Make sure that the generated code is itself legal.
4441 if (Result != Op)
4442 Result = LegalizeOp(Result);
4443
4444 // Note that LegalizeOp may be reentered even from single-use nodes, which
4445 // means that we always must cache transformed nodes.
4446 AddLegalizedOperand(Op, Result);
4447 return Result;
4448}
4449
4450/// PromoteOp - Given an operation that produces a value in an invalid type,
4451/// promote it to compute the value into a larger type. The produced value will
4452/// have the correct bits for the low portion of the register, but no guarantee
4453/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +00004454SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00004455 MVT VT = Op.getValueType();
4456 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004457 assert(getTypeAction(VT) == Promote &&
4458 "Caller should expand or legalize operands that are not promotable!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004459 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004460 "Cannot promote to smaller type!");
4461
Dan Gohman8181bd12008-07-27 21:46:04 +00004462 SDValue Tmp1, Tmp2, Tmp3;
4463 SDValue Result;
Gabor Greif1c80d112008-08-28 21:40:38 +00004464 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004465
Dan Gohman8181bd12008-07-27 21:46:04 +00004466 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004467 if (I != PromotedNodes.end()) return I->second;
4468
4469 switch (Node->getOpcode()) {
4470 case ISD::CopyFromReg:
4471 assert(0 && "CopyFromReg must be legal!");
4472 default:
4473#ifndef NDEBUG
4474 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4475#endif
4476 assert(0 && "Do not know how to promote this operator!");
4477 abort();
4478 case ISD::UNDEF:
4479 Result = DAG.getNode(ISD::UNDEF, NVT);
4480 break;
4481 case ISD::Constant:
4482 if (VT != MVT::i1)
4483 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4484 else
4485 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
4486 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4487 break;
4488 case ISD::ConstantFP:
4489 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4490 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4491 break;
4492
Duncan Sands4a361272009-01-01 15:52:00 +00004493 case ISD::SETCC: {
4494 MVT VT0 = Node->getOperand(0).getValueType();
4495 assert(isTypeLegal(TLI.getSetCCResultType(VT0))
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004496 && "SetCC type is not legal??");
Duncan Sands4a361272009-01-01 15:52:00 +00004497 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(VT0),
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004498 Node->getOperand(0), Node->getOperand(1),
4499 Node->getOperand(2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004500 break;
Duncan Sands4a361272009-01-01 15:52:00 +00004501 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004502 case ISD::TRUNCATE:
4503 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4504 case Legal:
4505 Result = LegalizeOp(Node->getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00004506 assert(Result.getValueType().bitsGE(NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004507 "This truncation doesn't make sense!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004508 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004509 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4510 break;
4511 case Promote:
4512 // The truncation is not required, because we don't guarantee anything
4513 // about high bits anyway.
4514 Result = PromoteOp(Node->getOperand(0));
4515 break;
4516 case Expand:
4517 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4518 // Truncate the low part of the expanded value to the result type
4519 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4520 }
4521 break;
4522 case ISD::SIGN_EXTEND:
4523 case ISD::ZERO_EXTEND:
4524 case ISD::ANY_EXTEND:
4525 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4526 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4527 case Legal:
4528 // Input is legal? Just do extend all the way to the larger type.
4529 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4530 break;
4531 case Promote:
4532 // Promote the reg if it's smaller.
4533 Result = PromoteOp(Node->getOperand(0));
4534 // The high bits are not guaranteed to be anything. Insert an extend.
4535 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4536 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4537 DAG.getValueType(Node->getOperand(0).getValueType()));
4538 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4539 Result = DAG.getZeroExtendInReg(Result,
4540 Node->getOperand(0).getValueType());
4541 break;
4542 }
4543 break;
Mon P Wang73d31542008-11-10 20:54:11 +00004544 case ISD::CONVERT_RNDSAT: {
4545 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4546 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4547 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4548 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4549 "can only promote integers");
4550 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4551 Node->getOperand(1), Node->getOperand(2),
4552 Node->getOperand(3), Node->getOperand(4),
4553 CvtCode);
4554 break;
4555
4556 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004557 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004558 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4559 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004560 Result = PromoteOp(Result);
4561 break;
4562
4563 case ISD::FP_EXTEND:
4564 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4565 case ISD::FP_ROUND:
4566 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4567 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4568 case Promote: assert(0 && "Unreachable with 2 FP types!");
4569 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004570 if (Node->getConstantOperandVal(1) == 0) {
4571 // Input is legal? Do an FP_ROUND_INREG.
4572 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4573 DAG.getValueType(VT));
4574 } else {
4575 // Just remove the truncate, it isn't affecting the value.
4576 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4577 Node->getOperand(1));
4578 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004579 break;
4580 }
4581 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004582 case ISD::SINT_TO_FP:
4583 case ISD::UINT_TO_FP:
4584 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4585 case Legal:
4586 // No extra round required here.
4587 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4588 break;
4589
4590 case Promote:
4591 Result = PromoteOp(Node->getOperand(0));
4592 if (Node->getOpcode() == ISD::SINT_TO_FP)
4593 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4594 Result,
4595 DAG.getValueType(Node->getOperand(0).getValueType()));
4596 else
4597 Result = DAG.getZeroExtendInReg(Result,
4598 Node->getOperand(0).getValueType());
4599 // No extra round required here.
4600 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4601 break;
4602 case Expand:
4603 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4604 Node->getOperand(0));
4605 // Round if we cannot tolerate excess precision.
4606 if (NoExcessFPPrecision)
4607 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4608 DAG.getValueType(VT));
4609 break;
4610 }
4611 break;
4612
4613 case ISD::SIGN_EXTEND_INREG:
4614 Result = PromoteOp(Node->getOperand(0));
4615 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4616 Node->getOperand(1));
4617 break;
4618 case ISD::FP_TO_SINT:
4619 case ISD::FP_TO_UINT:
4620 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4621 case Legal:
4622 case Expand:
4623 Tmp1 = Node->getOperand(0);
4624 break;
4625 case Promote:
4626 // The input result is prerounded, so we don't have to do anything
4627 // special.
4628 Tmp1 = PromoteOp(Node->getOperand(0));
4629 break;
4630 }
4631 // If we're promoting a UINT to a larger size, check to see if the new node
4632 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4633 // we can use that instead. This allows us to generate better code for
4634 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4635 // legal, such as PowerPC.
4636 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4637 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4638 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4639 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4640 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4641 } else {
4642 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4643 }
4644 break;
4645
4646 case ISD::FABS:
4647 case ISD::FNEG:
4648 Tmp1 = PromoteOp(Node->getOperand(0));
4649 assert(Tmp1.getValueType() == NVT);
4650 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4651 // NOTE: we do not have to do any extra rounding here for
4652 // NoExcessFPPrecision, because we know the input will have the appropriate
4653 // precision, and these operations don't modify precision at all.
4654 break;
4655
Dale Johannesen92b33082008-09-04 00:47:13 +00004656 case ISD::FLOG:
4657 case ISD::FLOG2:
4658 case ISD::FLOG10:
4659 case ISD::FEXP:
4660 case ISD::FEXP2:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004661 case ISD::FSQRT:
4662 case ISD::FSIN:
4663 case ISD::FCOS:
Dan Gohmanb2158232008-08-21 18:38:14 +00004664 case ISD::FTRUNC:
4665 case ISD::FFLOOR:
4666 case ISD::FCEIL:
4667 case ISD::FRINT:
4668 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004669 Tmp1 = PromoteOp(Node->getOperand(0));
4670 assert(Tmp1.getValueType() == NVT);
4671 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4672 if (NoExcessFPPrecision)
4673 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4674 DAG.getValueType(VT));
4675 break;
4676
Evan Cheng1fac6952008-09-09 23:35:53 +00004677 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004678 case ISD::FPOWI: {
Evan Cheng1fac6952008-09-09 23:35:53 +00004679 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004680 // directly as well, which may be better.
4681 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng1fac6952008-09-09 23:35:53 +00004682 Tmp2 = Node->getOperand(1);
4683 if (Node->getOpcode() == ISD::FPOW)
4684 Tmp2 = PromoteOp(Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004685 assert(Tmp1.getValueType() == NVT);
Evan Cheng1fac6952008-09-09 23:35:53 +00004686 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004687 if (NoExcessFPPrecision)
4688 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4689 DAG.getValueType(VT));
4690 break;
4691 }
4692
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004693 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004694 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004695 Tmp2 = PromoteOp(Node->getOperand(2));
4696 Tmp3 = PromoteOp(Node->getOperand(3));
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004697 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getMemoryVT(),
4698 AtomNode->getChain(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004699 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004700 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004701 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004702 // Remember that we legalized the chain.
4703 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4704 break;
4705 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004706 case ISD::ATOMIC_LOAD_ADD:
4707 case ISD::ATOMIC_LOAD_SUB:
4708 case ISD::ATOMIC_LOAD_AND:
4709 case ISD::ATOMIC_LOAD_OR:
4710 case ISD::ATOMIC_LOAD_XOR:
4711 case ISD::ATOMIC_LOAD_NAND:
4712 case ISD::ATOMIC_LOAD_MIN:
4713 case ISD::ATOMIC_LOAD_MAX:
4714 case ISD::ATOMIC_LOAD_UMIN:
4715 case ISD::ATOMIC_LOAD_UMAX:
4716 case ISD::ATOMIC_SWAP: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004717 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004718 Tmp2 = PromoteOp(Node->getOperand(2));
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004719 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getMemoryVT(),
4720 AtomNode->getChain(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004721 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004722 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004723 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004724 // Remember that we legalized the chain.
4725 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4726 break;
4727 }
4728
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004729 case ISD::AND:
4730 case ISD::OR:
4731 case ISD::XOR:
4732 case ISD::ADD:
4733 case ISD::SUB:
4734 case ISD::MUL:
4735 // The input may have strange things in the top bits of the registers, but
4736 // these operations don't care. They may have weird bits going out, but
4737 // that too is okay if they are integer operations.
4738 Tmp1 = PromoteOp(Node->getOperand(0));
4739 Tmp2 = PromoteOp(Node->getOperand(1));
4740 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4741 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4742 break;
4743 case ISD::FADD:
4744 case ISD::FSUB:
4745 case ISD::FMUL:
4746 Tmp1 = PromoteOp(Node->getOperand(0));
4747 Tmp2 = PromoteOp(Node->getOperand(1));
4748 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4749 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4750
4751 // Floating point operations will give excess precision that we may not be
4752 // able to tolerate. If we DO allow excess precision, just leave it,
4753 // otherwise excise it.
4754 // FIXME: Why would we need to round FP ops more than integer ones?
4755 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4756 if (NoExcessFPPrecision)
4757 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4758 DAG.getValueType(VT));
4759 break;
4760
4761 case ISD::SDIV:
4762 case ISD::SREM:
4763 // These operators require that their input be sign extended.
4764 Tmp1 = PromoteOp(Node->getOperand(0));
4765 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004766 if (NVT.isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004767 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4768 DAG.getValueType(VT));
4769 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4770 DAG.getValueType(VT));
4771 }
4772 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4773
4774 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands92c43912008-06-06 12:08:01 +00004775 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004776 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4777 DAG.getValueType(VT));
4778 break;
4779 case ISD::FDIV:
4780 case ISD::FREM:
4781 case ISD::FCOPYSIGN:
4782 // These operators require that their input be fp extended.
4783 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004784 case Expand: assert(0 && "not implemented");
4785 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4786 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004787 }
4788 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004789 case Expand: assert(0 && "not implemented");
4790 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4791 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004792 }
4793 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4794
4795 // Perform FP_ROUND: this is probably overly pessimistic.
4796 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4797 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4798 DAG.getValueType(VT));
4799 break;
4800
4801 case ISD::UDIV:
4802 case ISD::UREM:
4803 // These operators require that their input be zero extended.
4804 Tmp1 = PromoteOp(Node->getOperand(0));
4805 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004806 assert(NVT.isInteger() && "Operators don't apply to FP!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004807 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4808 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4809 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4810 break;
4811
4812 case ISD::SHL:
4813 Tmp1 = PromoteOp(Node->getOperand(0));
4814 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4815 break;
4816 case ISD::SRA:
4817 // The input value must be properly sign extended.
4818 Tmp1 = PromoteOp(Node->getOperand(0));
4819 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4820 DAG.getValueType(VT));
4821 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4822 break;
4823 case ISD::SRL:
4824 // The input value must be properly zero extended.
4825 Tmp1 = PromoteOp(Node->getOperand(0));
4826 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4827 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4828 break;
4829
4830 case ISD::VAARG:
4831 Tmp1 = Node->getOperand(0); // Get the chain.
4832 Tmp2 = Node->getOperand(1); // Get the pointer.
4833 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4834 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sandsac496a12008-07-04 11:47:58 +00004835 Result = TLI.LowerOperation(Tmp3, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004836 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004837 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00004838 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004839 // Increment the pointer, VAList, to the next vaarg
4840 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands92c43912008-06-06 12:08:01 +00004841 DAG.getConstant(VT.getSizeInBits()/8,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004842 TLI.getPointerTy()));
4843 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00004844 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004845 // Load the actual argument out of the pointer VAList
4846 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4847 }
4848 // Remember that we legalized the chain.
4849 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4850 break;
4851
4852 case ISD::LOAD: {
4853 LoadSDNode *LD = cast<LoadSDNode>(Node);
4854 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4855 ? ISD::EXTLOAD : LD->getExtensionType();
4856 Result = DAG.getExtLoad(ExtType, NVT,
4857 LD->getChain(), LD->getBasePtr(),
4858 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004859 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004860 LD->isVolatile(),
4861 LD->getAlignment());
4862 // Remember that we legalized the chain.
4863 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4864 break;
4865 }
Scott Michel67224b22008-06-02 22:18:03 +00004866 case ISD::SELECT: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004867 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4868 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel67224b22008-06-02 22:18:03 +00004869
Duncan Sands92c43912008-06-06 12:08:01 +00004870 MVT VT2 = Tmp2.getValueType();
Scott Michel67224b22008-06-02 22:18:03 +00004871 assert(VT2 == Tmp3.getValueType()
Scott Michel7b54de02008-06-03 19:13:20 +00004872 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4873 // Ensure that the resulting node is at least the same size as the operands'
4874 // value types, because we cannot assume that TLI.getSetCCValueType() is
4875 // constant.
4876 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004877 break;
Scott Michel67224b22008-06-02 22:18:03 +00004878 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004879 case ISD::SELECT_CC:
4880 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4881 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4882 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4883 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4884 break;
4885 case ISD::BSWAP:
4886 Tmp1 = Node->getOperand(0);
4887 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4888 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4889 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004890 DAG.getConstant(NVT.getSizeInBits() -
4891 VT.getSizeInBits(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004892 TLI.getShiftAmountTy()));
4893 break;
4894 case ISD::CTPOP:
4895 case ISD::CTTZ:
4896 case ISD::CTLZ:
4897 // Zero extend the argument
4898 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4899 // Perform the larger operation, then subtract if needed.
4900 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4901 switch(Node->getOpcode()) {
4902 case ISD::CTPOP:
4903 Result = Tmp1;
4904 break;
4905 case ISD::CTTZ:
4906 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Duncan Sands4a361272009-01-01 15:52:00 +00004907 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004908 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004909 ISD::SETEQ);
4910 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00004911 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004912 break;
4913 case ISD::CTLZ:
4914 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4915 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004916 DAG.getConstant(NVT.getSizeInBits() -
4917 VT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004918 break;
4919 }
4920 break;
4921 case ISD::EXTRACT_SUBVECTOR:
4922 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4923 break;
4924 case ISD::EXTRACT_VECTOR_ELT:
4925 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4926 break;
4927 }
4928
Gabor Greif1c80d112008-08-28 21:40:38 +00004929 assert(Result.getNode() && "Didn't set a result!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004930
4931 // Make sure the result is itself legal.
4932 Result = LegalizeOp(Result);
4933
4934 // Remember that we promoted this!
4935 AddPromotedOperand(Op, Result);
4936 return Result;
4937}
4938
4939/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4940/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4941/// based on the vector type. The return type of this matches the element type
4942/// of the vector, which may not be legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00004943SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004944 // We know that operand #0 is the Vec vector. If the index is a constant
4945 // or if the invec is a supported hardware type, we can use it. Otherwise,
4946 // lower to a store then an indexed load.
Dan Gohman8181bd12008-07-27 21:46:04 +00004947 SDValue Vec = Op.getOperand(0);
4948 SDValue Idx = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004949
Duncan Sands92c43912008-06-06 12:08:01 +00004950 MVT TVT = Vec.getValueType();
4951 unsigned NumElems = TVT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004952
4953 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4954 default: assert(0 && "This action is not supported yet!");
4955 case TargetLowering::Custom: {
4956 Vec = LegalizeOp(Vec);
4957 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004958 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004959 if (Tmp3.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004960 return Tmp3;
4961 break;
4962 }
4963 case TargetLowering::Legal:
4964 if (isTypeLegal(TVT)) {
4965 Vec = LegalizeOp(Vec);
4966 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004967 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004968 }
4969 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00004970 case TargetLowering::Promote:
4971 assert(TVT.isVector() && "not vector type");
4972 // fall thru to expand since vectors are by default are promote
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004973 case TargetLowering::Expand:
4974 break;
4975 }
4976
4977 if (NumElems == 1) {
4978 // This must be an access of the only element. Return it.
4979 Op = ScalarizeVectorOp(Vec);
4980 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00004981 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004982 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004983 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004984 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004985 if (CIdx->getZExtValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004986 Vec = Lo;
4987 } else {
4988 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004989 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004990 Idx.getValueType());
4991 }
4992
4993 // It's now an extract from the appropriate high or low part. Recurse.
4994 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4995 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4996 } else {
4997 // Store the value to a temporary stack slot, then LOAD the scalar
4998 // element back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00004999 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
5000 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005001
5002 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +00005003 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005004 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
5005 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00005006
Duncan Sandsec142ee2008-06-08 20:54:56 +00005007 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00005008 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00005009 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00005010 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00005011
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005012 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
5013
5014 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
5015 }
5016 return Op;
5017}
5018
5019/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
5020/// we assume the operation can be split if it is not already legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00005021SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005022 // We know that operand #0 is the Vec vector. For now we assume the index
5023 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman8181bd12008-07-27 21:46:04 +00005024 SDValue Vec = Op.getOperand(0);
5025 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005026
Duncan Sands92c43912008-06-06 12:08:01 +00005027 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005028
Duncan Sands92c43912008-06-06 12:08:01 +00005029 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005030 // This must be an access of the desired vector length. Return it.
5031 return Vec;
5032 }
5033
5034 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00005035 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005036 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005037 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005038 Vec = Lo;
5039 } else {
5040 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005041 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
5042 Idx.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005043 }
5044
5045 // It's now an extract from the appropriate high or low part. Recurse.
5046 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
5047 return ExpandEXTRACT_SUBVECTOR(Op);
5048}
5049
5050/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
5051/// with condition CC on the current target. This usually involves legalizing
5052/// or promoting the arguments. In the case where LHS and RHS must be expanded,
5053/// there may be no choice but to create a new SetCC node to represent the
5054/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman8181bd12008-07-27 21:46:04 +00005055/// LHS, and the SDValue returned in RHS has a nil SDNode value.
5056void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
5057 SDValue &RHS,
5058 SDValue &CC) {
5059 SDValue Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005060
5061 switch (getTypeAction(LHS.getValueType())) {
5062 case Legal:
5063 Tmp1 = LegalizeOp(LHS); // LHS
5064 Tmp2 = LegalizeOp(RHS); // RHS
5065 break;
5066 case Promote:
5067 Tmp1 = PromoteOp(LHS); // LHS
5068 Tmp2 = PromoteOp(RHS); // RHS
5069
5070 // If this is an FP compare, the operands have already been extended.
Duncan Sands92c43912008-06-06 12:08:01 +00005071 if (LHS.getValueType().isInteger()) {
5072 MVT VT = LHS.getValueType();
5073 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005074
5075 // Otherwise, we have to insert explicit sign or zero extends. Note
5076 // that we could insert sign extends for ALL conditions, but zero extend
5077 // is cheaper on many machines (an AND instead of two shifts), so prefer
5078 // it.
5079 switch (cast<CondCodeSDNode>(CC)->get()) {
5080 default: assert(0 && "Unknown integer comparison!");
5081 case ISD::SETEQ:
5082 case ISD::SETNE:
5083 case ISD::SETUGE:
5084 case ISD::SETUGT:
5085 case ISD::SETULE:
5086 case ISD::SETULT:
5087 // ALL of these operations will work if we either sign or zero extend
5088 // the operands (including the unsigned comparisons!). Zero extend is
5089 // usually a simpler/cheaper operation, so prefer it.
5090 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
5091 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
5092 break;
5093 case ISD::SETGE:
5094 case ISD::SETGT:
5095 case ISD::SETLT:
5096 case ISD::SETLE:
5097 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
5098 DAG.getValueType(VT));
5099 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
5100 DAG.getValueType(VT));
Evan Chengd901b662008-10-13 18:46:18 +00005101 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5102 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005103 break;
5104 }
5105 }
5106 break;
5107 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00005108 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005109 if (VT == MVT::f32 || VT == MVT::f64) {
5110 // Expand into one or more soft-fp libcall(s).
Evan Cheng24108632008-07-01 21:35:46 +00005111 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005112 switch (cast<CondCodeSDNode>(CC)->get()) {
5113 case ISD::SETEQ:
5114 case ISD::SETOEQ:
5115 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5116 break;
5117 case ISD::SETNE:
5118 case ISD::SETUNE:
5119 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
5120 break;
5121 case ISD::SETGE:
5122 case ISD::SETOGE:
5123 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5124 break;
5125 case ISD::SETLT:
5126 case ISD::SETOLT:
5127 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5128 break;
5129 case ISD::SETLE:
5130 case ISD::SETOLE:
5131 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5132 break;
5133 case ISD::SETGT:
5134 case ISD::SETOGT:
5135 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
5136 break;
5137 case ISD::SETUO:
5138 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5139 break;
5140 case ISD::SETO:
5141 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
5142 break;
5143 default:
5144 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5145 switch (cast<CondCodeSDNode>(CC)->get()) {
5146 case ISD::SETONE:
5147 // SETONE = SETOLT | SETOGT
5148 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5149 // Fallthrough
5150 case ISD::SETUGT:
5151 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
5152 break;
5153 case ISD::SETUGE:
5154 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5155 break;
5156 case ISD::SETULT:
5157 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5158 break;
5159 case ISD::SETULE:
5160 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5161 break;
5162 case ISD::SETUEQ:
5163 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5164 break;
5165 default: assert(0 && "Unsupported FP setcc!");
5166 }
5167 }
Duncan Sandsf19591c2008-06-30 10:19:09 +00005168
Dan Gohman8181bd12008-07-27 21:46:04 +00005169 SDValue Dummy;
5170 SDValue Ops[2] = { LHS, RHS };
Gabor Greif1c80d112008-08-28 21:40:38 +00005171 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005172 false /*sign irrelevant*/, Dummy);
5173 Tmp2 = DAG.getConstant(0, MVT::i32);
5174 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
5175 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Duncan Sands4a361272009-01-01 15:52:00 +00005176 Tmp1 = DAG.getNode(ISD::SETCC,
5177 TLI.getSetCCResultType(Tmp1.getValueType()),
5178 Tmp1, Tmp2, CC);
Gabor Greif1c80d112008-08-28 21:40:38 +00005179 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005180 false /*sign irrelevant*/, Dummy);
Duncan Sands4a361272009-01-01 15:52:00 +00005181 Tmp2 = DAG.getNode(ISD::SETCC,
5182 TLI.getSetCCResultType(LHS.getValueType()), LHS,
5183 Tmp2, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005184 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005185 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005186 }
Evan Cheng18a1ab12008-07-07 07:18:09 +00005187 LHS = LegalizeOp(Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005188 RHS = Tmp2;
5189 return;
5190 }
5191
Dan Gohman8181bd12008-07-27 21:46:04 +00005192 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005193 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005194 ExpandOp(RHS, RHSLo, RHSHi);
5195 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5196
5197 if (VT==MVT::ppcf128) {
5198 // FIXME: This generated code sucks. We want to generate
Dale Johannesen26317b62008-09-12 00:30:56 +00005199 // FCMPU crN, hi1, hi2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005200 // BNE crN, L:
Dale Johannesen26317b62008-09-12 00:30:56 +00005201 // FCMPU crN, lo1, lo2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005202 // The following can be improved, but not that much.
Duncan Sands4a361272009-01-01 15:52:00 +00005203 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5204 LHSHi, RHSHi, ISD::SETOEQ);
5205 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5206 LHSLo, RHSLo, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005207 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Duncan Sands4a361272009-01-01 15:52:00 +00005208 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5209 LHSHi, RHSHi, ISD::SETUNE);
5210 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5211 LHSHi, RHSHi, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005212 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5213 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00005214 Tmp2 = SDValue();
Dale Johannesen472d15d2007-10-06 01:24:11 +00005215 break;
5216 }
5217
5218 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005219 case ISD::SETEQ:
5220 case ISD::SETNE:
5221 if (RHSLo == RHSHi)
5222 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5223 if (RHSCST->isAllOnesValue()) {
5224 // Comparison to -1.
5225 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5226 Tmp2 = RHSLo;
5227 break;
5228 }
5229
5230 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5231 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5232 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5233 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5234 break;
5235 default:
5236 // If this is a comparison of the sign bit, just look at the top part.
5237 // X > -1, x < 0
5238 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5239 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00005240 CST->isNullValue()) || // X < 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005241 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5242 CST->isAllOnesValue())) { // X > -1
5243 Tmp1 = LHSHi;
5244 Tmp2 = RHSHi;
5245 break;
5246 }
5247
5248 // FIXME: This generated code sucks.
5249 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005250 switch (CCCode) {
5251 default: assert(0 && "Unknown integer setcc!");
5252 case ISD::SETLT:
5253 case ISD::SETULT: LowCC = ISD::SETULT; break;
5254 case ISD::SETGT:
5255 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5256 case ISD::SETLE:
5257 case ISD::SETULE: LowCC = ISD::SETULE; break;
5258 case ISD::SETGE:
5259 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5260 }
5261
5262 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5263 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5264 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5265
5266 // NOTE: on targets without efficient SELECT of bools, we can always use
5267 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
5268 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Duncan Sands4a361272009-01-01 15:52:00 +00005269 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5270 LHSLo, RHSLo, LowCC, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005271 if (!Tmp1.getNode())
Duncan Sands4a361272009-01-01 15:52:00 +00005272 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5273 LHSLo, RHSLo, LowCC);
5274 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5275 LHSHi, RHSHi, CCCode, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005276 if (!Tmp2.getNode())
Duncan Sands4a361272009-01-01 15:52:00 +00005277 Tmp2 = DAG.getNode(ISD::SETCC,
5278 TLI.getSetCCResultType(LHSHi.getValueType()),
5279 LHSHi, RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005280
Gabor Greif1c80d112008-08-28 21:40:38 +00005281 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5282 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman9d24dc72008-03-13 22:13:53 +00005283 if ((Tmp1C && Tmp1C->isNullValue()) ||
5284 (Tmp2C && Tmp2C->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005285 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5286 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman9d24dc72008-03-13 22:13:53 +00005287 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005288 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5289 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5290 // low part is known false, returns high part.
5291 // For LE / GE, if high part is known false, ignore the low part.
5292 // For LT / GT, if high part is known true, ignore the low part.
5293 Tmp1 = Tmp2;
Dan Gohman8181bd12008-07-27 21:46:04 +00005294 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005295 } else {
Duncan Sands4a361272009-01-01 15:52:00 +00005296 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5297 LHSHi, RHSHi, ISD::SETEQ, false,
5298 DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005299 if (!Result.getNode())
Duncan Sands4a361272009-01-01 15:52:00 +00005300 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5301 LHSHi, RHSHi, ISD::SETEQ);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005302 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5303 Result, Tmp1, Tmp2));
5304 Tmp1 = Result;
Dan Gohman8181bd12008-07-27 21:46:04 +00005305 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005306 }
5307 }
5308 }
5309 }
5310 LHS = Tmp1;
5311 RHS = Tmp2;
5312}
5313
Evan Cheng71343822008-10-15 02:05:31 +00005314/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5315/// condition code CC on the current target. This routine assumes LHS and rHS
5316/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5317/// illegal condition code into AND / OR of multiple SETCC values.
5318void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5319 SDValue &LHS, SDValue &RHS,
5320 SDValue &CC) {
5321 MVT OpVT = LHS.getValueType();
5322 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5323 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5324 default: assert(0 && "Unknown condition code action!");
5325 case TargetLowering::Legal:
5326 // Nothing to do.
5327 break;
5328 case TargetLowering::Expand: {
5329 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5330 unsigned Opc = 0;
5331 switch (CCCode) {
5332 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohman2b5b9ca2008-10-21 03:12:54 +00005333 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5334 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5335 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5336 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5337 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5338 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5339 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5340 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5341 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5342 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5343 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5344 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng71343822008-10-15 02:05:31 +00005345 // FIXME: Implement more expansions.
5346 }
5347
5348 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5349 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5350 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5351 RHS = SDValue();
5352 CC = SDValue();
5353 break;
5354 }
5355 }
5356}
5357
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005358/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5359/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5360/// a load from the stack slot to DestVT, extending it if needed.
5361/// The resultant code need not be legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00005362SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5363 MVT SlotVT,
5364 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005365 // Create the stack frame object.
Mon P Wang55854cc2008-07-05 20:40:31 +00005366 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5367 SrcOp.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00005368 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang55854cc2008-07-05 20:40:31 +00005369
Dan Gohman20e37962008-02-11 18:58:42 +00005370 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005371 int SPFI = StackPtrFI->getIndex();
Mon P Wang55854cc2008-07-05 20:40:31 +00005372
Duncan Sands92c43912008-06-06 12:08:01 +00005373 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5374 unsigned SlotSize = SlotVT.getSizeInBits();
5375 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang55854cc2008-07-05 20:40:31 +00005376 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5377 DestVT.getTypeForMVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005378
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005379 // Emit a store to the stack slot. Use a truncstore if the input value is
5380 // later than DestVT.
Dan Gohman8181bd12008-07-27 21:46:04 +00005381 SDValue Store;
Mon P Wang55854cc2008-07-05 20:40:31 +00005382
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005383 if (SrcSize > SlotSize)
Dan Gohman12a9c082008-02-06 22:27:42 +00005384 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005385 PseudoSourceValue::getFixedStack(SPFI), 0,
5386 SlotVT, false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005387 else {
5388 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman12a9c082008-02-06 22:27:42 +00005389 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005390 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang55854cc2008-07-05 20:40:31 +00005391 false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005392 }
5393
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005394 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005395 if (SlotSize == DestSize)
Mon P Wang55854cc2008-07-05 20:40:31 +00005396 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005397
5398 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang55854cc2008-07-05 20:40:31 +00005399 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5400 false, DestAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005401}
5402
Dan Gohman8181bd12008-07-27 21:46:04 +00005403SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005404 // Create a vector sized/aligned stack slot, store the value to element #0,
5405 // then load the whole vector back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00005406 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman12a9c082008-02-06 22:27:42 +00005407
Dan Gohman20e37962008-02-11 18:58:42 +00005408 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005409 int SPFI = StackPtrFI->getIndex();
5410
Dan Gohman8181bd12008-07-27 21:46:04 +00005411 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005412 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00005413 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005414 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005415}
5416
5417
5418/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
5419/// support the operation, but do support the resultant vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00005420SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005421
5422 // If the only non-undef value is the low element, turn this into a
5423 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
5424 unsigned NumElems = Node->getNumOperands();
5425 bool isOnlyLowElement = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005426 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005427
Dan Gohman8181bd12008-07-27 21:46:04 +00005428 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerd8cee732008-03-09 00:29:42 +00005429 // and use a bitmask instead of a list of elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00005430 std::map<SDValue, std::vector<unsigned> > Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005431 Values[SplatValue].push_back(0);
5432 bool isConstant = true;
5433 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5434 SplatValue.getOpcode() != ISD::UNDEF)
5435 isConstant = false;
5436
5437 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005438 SDValue V = Node->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005439 Values[V].push_back(i);
5440 if (V.getOpcode() != ISD::UNDEF)
5441 isOnlyLowElement = false;
5442 if (SplatValue != V)
Dan Gohman8181bd12008-07-27 21:46:04 +00005443 SplatValue = SDValue(0,0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005444
5445 // If this isn't a constant element or an undef, we can't use a constant
5446 // pool load.
5447 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5448 V.getOpcode() != ISD::UNDEF)
5449 isConstant = false;
5450 }
5451
5452 if (isOnlyLowElement) {
5453 // If the low element is an undef too, then this whole things is an undef.
5454 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5455 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5456 // Otherwise, turn this into a scalar_to_vector node.
5457 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5458 Node->getOperand(0));
5459 }
5460
5461 // If all elements are constants, create a load from the constant pool.
5462 if (isConstant) {
Duncan Sands92c43912008-06-06 12:08:01 +00005463 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005464 std::vector<Constant*> CV;
5465 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5466 if (ConstantFPSDNode *V =
5467 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005468 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005469 } else if (ConstantSDNode *V =
Chris Lattner5e0610f2008-04-20 00:41:09 +00005470 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005471 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005472 } else {
5473 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner5e0610f2008-04-20 00:41:09 +00005474 const Type *OpNTy =
Duncan Sands92c43912008-06-06 12:08:01 +00005475 Node->getOperand(0).getValueType().getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005476 CV.push_back(UndefValue::get(OpNTy));
5477 }
5478 }
5479 Constant *CP = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005480 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005481 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman12a9c082008-02-06 22:27:42 +00005482 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005483 PseudoSourceValue::getConstantPool(), 0,
5484 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005485 }
5486
Gabor Greif1c80d112008-08-28 21:40:38 +00005487 if (SplatValue.getNode()) { // Splat of one value?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005488 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands92c43912008-06-06 12:08:01 +00005489 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman8181bd12008-07-27 21:46:04 +00005490 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5491 std::vector<SDValue> ZeroVec(NumElems, Zero);
5492 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005493 &ZeroVec[0], ZeroVec.size());
5494
5495 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
5496 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
5497 // Get the splatted value into the low element of a vector register.
Dan Gohman8181bd12008-07-27 21:46:04 +00005498 SDValue LowValVec =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005499 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5500
5501 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5502 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5503 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5504 SplatMask);
5505 }
5506 }
5507
5508 // If there are only two unique elements, we may be able to turn this into a
5509 // vector shuffle.
5510 if (Values.size() == 2) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005511 // Get the two values in deterministic order.
Dan Gohman8181bd12008-07-27 21:46:04 +00005512 SDValue Val1 = Node->getOperand(1);
5513 SDValue Val2;
5514 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerd8cee732008-03-09 00:29:42 +00005515 if (MI->first != Val1)
5516 Val2 = MI->first;
5517 else
5518 Val2 = (++MI)->first;
5519
5520 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5521 // vector shuffle has the undef vector on the RHS.
5522 if (Val1.getOpcode() == ISD::UNDEF)
5523 std::swap(Val1, Val2);
5524
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005525 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands92c43912008-06-06 12:08:01 +00005526 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5527 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005528 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005529
5530 // Set elements of the shuffle mask for Val1.
5531 std::vector<unsigned> &Val1Elts = Values[Val1];
5532 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5533 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5534
5535 // Set elements of the shuffle mask for Val2.
5536 std::vector<unsigned> &Val2Elts = Values[Val2];
5537 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5538 if (Val2.getOpcode() != ISD::UNDEF)
5539 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5540 else
5541 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5542
Dan Gohman8181bd12008-07-27 21:46:04 +00005543 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005544 &MaskVec[0], MaskVec.size());
5545
Chris Lattnerd8cee732008-03-09 00:29:42 +00005546 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005547 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5548 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005549 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5550 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005551 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005552
5553 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerd8cee732008-03-09 00:29:42 +00005554 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005555 }
5556 }
5557
5558 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5559 // aligned object on the stack, store each element into it, then load
5560 // the result as a vector.
Duncan Sands92c43912008-06-06 12:08:01 +00005561 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005562 // Create the stack frame object.
Dan Gohman8181bd12008-07-27 21:46:04 +00005563 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005564
5565 // Emit a store of each element to the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00005566 SmallVector<SDValue, 8> Stores;
Duncan Sands92c43912008-06-06 12:08:01 +00005567 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005568 // Store (in the right endianness) the elements to memory.
5569 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5570 // Ignore undef elements.
5571 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5572
5573 unsigned Offset = TypeByteSize*i;
5574
Dan Gohman8181bd12008-07-27 21:46:04 +00005575 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005576 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5577
5578 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
5579 NULL, 0));
5580 }
5581
Dan Gohman8181bd12008-07-27 21:46:04 +00005582 SDValue StoreChain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005583 if (!Stores.empty()) // Not all undef elements?
5584 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5585 &Stores[0], Stores.size());
5586 else
5587 StoreChain = DAG.getEntryNode();
5588
5589 // Result is a load from the stack slot.
5590 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
5591}
5592
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005593void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman8181bd12008-07-27 21:46:04 +00005594 SDValue Op, SDValue Amt,
5595 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005596 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00005597 SDValue LHSL, LHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005598 ExpandOp(Op, LHSL, LHSH);
5599
Dan Gohman8181bd12008-07-27 21:46:04 +00005600 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands92c43912008-06-06 12:08:01 +00005601 MVT VT = LHSL.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005602 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5603 Hi = Lo.getValue(1);
5604}
5605
5606
5607/// ExpandShift - Try to find a clever way to expand this shift operation out to
5608/// smaller elements. If we can't find a way that is more efficient than a
5609/// libcall on this target, return false. Otherwise, return true with the
5610/// low-parts expanded into Lo and Hi.
Dan Gohman8181bd12008-07-27 21:46:04 +00005611bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5612 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005613 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5614 "This is not a shift!");
5615
Duncan Sands92c43912008-06-06 12:08:01 +00005616 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman8181bd12008-07-27 21:46:04 +00005617 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands92c43912008-06-06 12:08:01 +00005618 MVT ShTy = ShAmt.getValueType();
5619 unsigned ShBits = ShTy.getSizeInBits();
5620 unsigned VTBits = Op.getValueType().getSizeInBits();
5621 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005622
Chris Lattner8c931452007-10-14 20:35:12 +00005623 // Handle the case when Amt is an immediate.
Gabor Greif1c80d112008-08-28 21:40:38 +00005624 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005625 unsigned Cst = CN->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005626 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005627 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005628 ExpandOp(Op, InL, InH);
5629 switch(Opc) {
5630 case ISD::SHL:
5631 if (Cst > VTBits) {
5632 Lo = DAG.getConstant(0, NVT);
5633 Hi = DAG.getConstant(0, NVT);
5634 } else if (Cst > NVTBits) {
5635 Lo = DAG.getConstant(0, NVT);
5636 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5637 } else if (Cst == NVTBits) {
5638 Lo = DAG.getConstant(0, NVT);
5639 Hi = InL;
5640 } else {
5641 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5642 Hi = DAG.getNode(ISD::OR, NVT,
5643 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5644 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5645 }
5646 return true;
5647 case ISD::SRL:
5648 if (Cst > VTBits) {
5649 Lo = DAG.getConstant(0, NVT);
5650 Hi = DAG.getConstant(0, NVT);
5651 } else if (Cst > NVTBits) {
5652 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5653 Hi = DAG.getConstant(0, NVT);
5654 } else if (Cst == NVTBits) {
5655 Lo = InH;
5656 Hi = DAG.getConstant(0, NVT);
5657 } else {
5658 Lo = DAG.getNode(ISD::OR, NVT,
5659 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5660 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5661 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5662 }
5663 return true;
5664 case ISD::SRA:
5665 if (Cst > VTBits) {
5666 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5667 DAG.getConstant(NVTBits-1, ShTy));
5668 } else if (Cst > NVTBits) {
5669 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5670 DAG.getConstant(Cst-NVTBits, ShTy));
5671 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5672 DAG.getConstant(NVTBits-1, ShTy));
5673 } else if (Cst == NVTBits) {
5674 Lo = InH;
5675 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5676 DAG.getConstant(NVTBits-1, ShTy));
5677 } else {
5678 Lo = DAG.getNode(ISD::OR, NVT,
5679 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5680 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5681 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5682 }
5683 return true;
5684 }
5685 }
5686
5687 // Okay, the shift amount isn't constant. However, if we can tell that it is
5688 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005689 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5690 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005691 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5692
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005693 // If we know that if any of the high bits of the shift amount are one, then
5694 // we can do this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005695 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005696 // Mask out the high bit, which we know is set.
5697 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005698 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005699
5700 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005701 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005702 ExpandOp(Op, InL, InH);
5703 switch(Opc) {
5704 case ISD::SHL:
5705 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5706 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5707 return true;
5708 case ISD::SRL:
5709 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5710 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5711 return true;
5712 case ISD::SRA:
5713 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5714 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5715 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5716 return true;
5717 }
5718 }
5719
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005720 // If we know that the high bits of the shift amount are all zero, then we can
5721 // do this as a couple of simple shifts.
5722 if ((KnownZero & Mask) == Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005723 // Compute 32-amt.
Dan Gohman8181bd12008-07-27 21:46:04 +00005724 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005725 DAG.getConstant(NVTBits, Amt.getValueType()),
5726 Amt);
5727
5728 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005729 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005730 ExpandOp(Op, InL, InH);
5731 switch(Opc) {
5732 case ISD::SHL:
5733 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5734 Hi = DAG.getNode(ISD::OR, NVT,
5735 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5736 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5737 return true;
5738 case ISD::SRL:
5739 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5740 Lo = DAG.getNode(ISD::OR, NVT,
5741 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5742 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5743 return true;
5744 case ISD::SRA:
5745 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5746 Lo = DAG.getNode(ISD::OR, NVT,
5747 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5748 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5749 return true;
5750 }
5751 }
5752
5753 return false;
5754}
5755
5756
5757// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5758// does not fit into a register, return the lo part and set the hi part to the
5759// by-reg argument. If it does fit into a single register, return the result
5760// and leave the Hi part unset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005761SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5762 bool isSigned, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005763 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5764 // The input chain to this libcall is the entry node of the function.
5765 // Legalizing the call will automatically add the previous call to the
5766 // dependence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005767 SDValue InChain = DAG.getEntryNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005768
5769 TargetLowering::ArgListTy Args;
5770 TargetLowering::ArgListEntry Entry;
5771 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00005772 MVT ArgVT = Node->getOperand(i).getValueType();
5773 const Type *ArgTy = ArgVT.getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005774 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5775 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005776 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005777 Args.push_back(Entry);
5778 }
Bill Wendlingfef06052008-09-16 21:48:12 +00005779 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang1448aad2008-10-30 08:01:45 +00005780 TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005781
5782 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands92c43912008-06-06 12:08:01 +00005783 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman8181bd12008-07-27 21:46:04 +00005784 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen67cc9b62008-09-26 19:31:26 +00005785 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5786 CallingConv::C, false, Callee, Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005787
5788 // Legalize the call sequence, starting with the chain. This will advance
5789 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5790 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5791 LegalizeOp(CallInfo.second);
Dan Gohman8181bd12008-07-27 21:46:04 +00005792 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005793 switch (getTypeAction(CallInfo.first.getValueType())) {
5794 default: assert(0 && "Unknown thing");
5795 case Legal:
5796 Result = CallInfo.first;
5797 break;
5798 case Expand:
5799 ExpandOp(CallInfo.first, Result, Hi);
5800 break;
5801 }
5802 return Result;
5803}
5804
Dan Gohman29c3cef2008-08-14 20:04:46 +00005805/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5806///
5807SDValue SelectionDAGLegalize::
5808LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5809 bool isCustom = false;
5810 SDValue Tmp1;
5811 switch (getTypeAction(Op.getValueType())) {
5812 case Legal:
5813 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5814 Op.getValueType())) {
5815 default: assert(0 && "Unknown operation action!");
5816 case TargetLowering::Custom:
5817 isCustom = true;
5818 // FALLTHROUGH
5819 case TargetLowering::Legal:
5820 Tmp1 = LegalizeOp(Op);
Gabor Greif1c80d112008-08-28 21:40:38 +00005821 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005822 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5823 else
5824 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5825 DestTy, Tmp1);
5826 if (isCustom) {
5827 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005828 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman29c3cef2008-08-14 20:04:46 +00005829 }
5830 break;
5831 case TargetLowering::Expand:
5832 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5833 break;
5834 case TargetLowering::Promote:
5835 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5836 break;
5837 }
5838 break;
5839 case Expand:
5840 Result = ExpandIntToFP(isSigned, DestTy, Op);
5841 break;
5842 case Promote:
5843 Tmp1 = PromoteOp(Op);
5844 if (isSigned) {
5845 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5846 Tmp1, DAG.getValueType(Op.getValueType()));
5847 } else {
5848 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5849 Op.getValueType());
5850 }
Gabor Greif1c80d112008-08-28 21:40:38 +00005851 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005852 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5853 else
5854 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5855 DestTy, Tmp1);
5856 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5857 break;
5858 }
5859 return Result;
5860}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005861
5862/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5863///
Dan Gohman8181bd12008-07-27 21:46:04 +00005864SDValue SelectionDAGLegalize::
5865ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands92c43912008-06-06 12:08:01 +00005866 MVT SourceVT = Source.getValueType();
Dan Gohman8b232ff2008-03-11 01:59:03 +00005867 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005868
Dan Gohman29c3cef2008-08-14 20:04:46 +00005869 // Expand unsupported int-to-fp vector casts by unrolling them.
5870 if (DestTy.isVector()) {
5871 if (!ExpandSource)
5872 return LegalizeOp(UnrollVectorOp(Source));
5873 MVT DestEltTy = DestTy.getVectorElementType();
5874 if (DestTy.getVectorNumElements() == 1) {
5875 SDValue Scalar = ScalarizeVectorOp(Source);
5876 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5877 DestEltTy, Scalar);
5878 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5879 }
5880 SDValue Lo, Hi;
5881 SplitVectorOp(Source, Lo, Hi);
5882 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5883 DestTy.getVectorNumElements() / 2);
5884 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5885 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengd901b662008-10-13 18:46:18 +00005886 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5887 HiResult));
Dan Gohman29c3cef2008-08-14 20:04:46 +00005888 }
5889
Evan Chengf99a7752008-04-01 02:18:22 +00005890 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5891 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmana193dba2008-03-05 02:07:31 +00005892 // The integer value loaded will be incorrectly if the 'sign bit' of the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005893 // incoming integer is set. To handle this, we dynamically test to see if
5894 // it is set, and, if so, add a fudge factor.
Dan Gohman8181bd12008-07-27 21:46:04 +00005895 SDValue Hi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005896 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005897 SDValue Lo;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005898 ExpandOp(Source, Lo, Hi);
5899 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5900 } else {
5901 // The comparison for the sign bit will use the entire operand.
5902 Hi = Source;
5903 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005904
Dale Johannesen96db7962008-11-04 20:52:49 +00005905 // Check to see if the target has a custom way to lower this. If so, use
5906 // it. (Note we've already expanded the operand in this case.)
Dale Johannesena359b8b2008-10-21 20:50:01 +00005907 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5908 default: assert(0 && "This action not implemented for this operation!");
5909 case TargetLowering::Legal:
5910 case TargetLowering::Expand:
5911 break; // This case is handled below.
5912 case TargetLowering::Custom: {
5913 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5914 Source), DAG);
5915 if (NV.getNode())
5916 return LegalizeOp(NV);
5917 break; // The target decided this was legal after all
5918 }
5919 }
5920
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005921 // If this is unsigned, and not supported, first perform the conversion to
5922 // signed, then adjust the result if the sign bit is set.
Dan Gohman8181bd12008-07-27 21:46:04 +00005923 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005924
Duncan Sands4a361272009-01-01 15:52:00 +00005925 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi.getValueType()),
5926 Hi, DAG.getConstant(0, Hi.getValueType()),
5927 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005928 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5929 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005930 SignSet, Four, Zero);
5931 uint64_t FF = 0x5f800000ULL;
5932 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmana193dba2008-03-05 02:07:31 +00005933 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005934
Dan Gohman8181bd12008-07-27 21:46:04 +00005935 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005936 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005937 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00005938 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00005939 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005940 if (DestTy == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005941 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005942 PseudoSourceValue::getConstantPool(), 0,
5943 false, Alignment);
Duncan Sandsec142ee2008-06-08 20:54:56 +00005944 else if (DestTy.bitsGT(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005945 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005946 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00005947 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005948 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00005949 MVT::f32, false, Alignment);
Dale Johannesen2fc20782007-09-14 22:26:36 +00005950 else
5951 assert(0 && "Unexpected conversion");
5952
Duncan Sands92c43912008-06-06 12:08:01 +00005953 MVT SCVT = SignedConv.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005954 if (SCVT != DestTy) {
5955 // Destination type needs to be expanded as well. The FADD now we are
5956 // constructing will be expanded into a libcall.
Duncan Sands92c43912008-06-06 12:08:01 +00005957 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5958 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmanc98645c2008-03-05 01:08:17 +00005959 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005960 SignedConv, SignedConv.getValue(1));
5961 }
5962 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5963 }
5964 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5965 }
5966
5967 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmanc98645c2008-03-05 01:08:17 +00005968 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005969 default: assert(0 && "This action not implemented for this operation!");
5970 case TargetLowering::Legal:
5971 case TargetLowering::Expand:
5972 break; // This case is handled below.
5973 case TargetLowering::Custom: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005974 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005975 Source), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005976 if (NV.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005977 return LegalizeOp(NV);
5978 break; // The target decided this was legal after all
5979 }
5980 }
5981
5982 // Expand the source, then glue it back together for the call. We must expand
5983 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman8b232ff2008-03-11 01:59:03 +00005984 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005985 SDValue SrcLo, SrcHi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005986 ExpandOp(Source, SrcLo, SrcHi);
5987 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5988 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005989
Duncan Sandsf68dffb2008-07-17 02:36:29 +00005990 RTLIB::Libcall LC = isSigned ?
5991 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5992 RTLIB::getUINTTOFP(SourceVT, DestTy);
5993 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5994
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005995 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman8181bd12008-07-27 21:46:04 +00005996 SDValue HiPart;
Gabor Greif1c80d112008-08-28 21:40:38 +00005997 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5998 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmanec51f642008-03-10 23:03:31 +00005999 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
6000 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006001}
6002
6003/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
6004/// INT_TO_FP operation of the specified operand when the target requests that
6005/// we expand it. At this point, we know that the result and operand types are
6006/// legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00006007SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
6008 SDValue Op0,
6009 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006010 if (Op0.getValueType() == MVT::i32) {
6011 // simple 32-bit [signed|unsigned] integer to float/double expansion
6012
Chris Lattner0aeb1d02008-01-16 07:03:22 +00006013 // Get the stack frame index of a 8 byte buffer.
Dan Gohman8181bd12008-07-27 21:46:04 +00006014 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner0aeb1d02008-01-16 07:03:22 +00006015
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006016 // word offset constant for Hi/Lo address computation
Dan Gohman8181bd12008-07-27 21:46:04 +00006017 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006018 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman8181bd12008-07-27 21:46:04 +00006019 SDValue Hi = StackSlot;
6020 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006021 if (TLI.isLittleEndian())
6022 std::swap(Hi, Lo);
6023
6024 // if signed map to unsigned space
Dan Gohman8181bd12008-07-27 21:46:04 +00006025 SDValue Op0Mapped;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006026 if (isSigned) {
6027 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman8181bd12008-07-27 21:46:04 +00006028 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006029 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
6030 } else {
6031 Op0Mapped = Op0;
6032 }
6033 // store the lo of the constructed double - based on integer input
Dan Gohman8181bd12008-07-27 21:46:04 +00006034 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006035 Op0Mapped, Lo, NULL, 0);
6036 // initial hi portion of constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00006037 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006038 // store the hi of the constructed double - biased exponent
Dan Gohman8181bd12008-07-27 21:46:04 +00006039 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006040 // load the constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00006041 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006042 // FP constant to bias correct the final result
Dan Gohman8181bd12008-07-27 21:46:04 +00006043 SDValue Bias = DAG.getConstantFP(isSigned ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006044 BitsToDouble(0x4330000080000000ULL)
6045 : BitsToDouble(0x4330000000000000ULL),
6046 MVT::f64);
6047 // subtract the bias
Dan Gohman8181bd12008-07-27 21:46:04 +00006048 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006049 // final result
Dan Gohman8181bd12008-07-27 21:46:04 +00006050 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006051 // handle final rounding
6052 if (DestVT == MVT::f64) {
6053 // do nothing
6054 Result = Sub;
Duncan Sandsec142ee2008-06-08 20:54:56 +00006055 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner5872a362008-01-17 07:00:52 +00006056 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
6057 DAG.getIntPtrConstant(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00006058 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenb17a7a22007-09-16 16:51:49 +00006059 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006060 }
6061 return Result;
6062 }
6063 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman8181bd12008-07-27 21:46:04 +00006064 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006065
Duncan Sands4a361272009-01-01 15:52:00 +00006066 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0.getValueType()),
6067 Op0, DAG.getConstant(0, Op0.getValueType()),
6068 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00006069 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
6070 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006071 SignSet, Four, Zero);
6072
6073 // If the sign bit of the integer is set, the large number will be treated
6074 // as a negative number. To counteract this, the dynamic code adds an
6075 // offset depending on the data type.
6076 uint64_t FF;
Duncan Sands92c43912008-06-06 12:08:01 +00006077 switch (Op0.getValueType().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006078 default: assert(0 && "Unsupported integer type!");
6079 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6080 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6081 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6082 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6083 }
6084 if (TLI.isLittleEndian()) FF <<= 32;
6085 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
6086
Dan Gohman8181bd12008-07-27 21:46:04 +00006087 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00006088 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006089 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00006090 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00006091 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006092 if (DestVT == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00006093 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00006094 PseudoSourceValue::getConstantPool(), 0,
6095 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006096 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00006097 FudgeInReg =
6098 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
6099 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006100 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00006101 MVT::f32, false, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006102 }
6103
6104 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
6105}
6106
6107/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6108/// *INT_TO_FP operation of the specified operand when the target requests that
6109/// we promote it. At this point, we know that the result and operand types are
6110/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6111/// operation that takes a larger input.
Dan Gohman8181bd12008-07-27 21:46:04 +00006112SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6113 MVT DestVT,
6114 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006115 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00006116 MVT NewInTy = LegalOp.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006117
6118 unsigned OpToUse = 0;
6119
6120 // Scan for the appropriate larger type to use.
6121 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00006122 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6123 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006124
6125 // If the target supports SINT_TO_FP of this type, use it.
6126 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6127 default: break;
6128 case TargetLowering::Legal:
6129 if (!TLI.isTypeLegal(NewInTy))
6130 break; // Can't use this datatype.
6131 // FALL THROUGH.
6132 case TargetLowering::Custom:
6133 OpToUse = ISD::SINT_TO_FP;
6134 break;
6135 }
6136 if (OpToUse) break;
6137 if (isSigned) continue;
6138
6139 // If the target supports UINT_TO_FP of this type, use it.
6140 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6141 default: break;
6142 case TargetLowering::Legal:
6143 if (!TLI.isTypeLegal(NewInTy))
6144 break; // Can't use this datatype.
6145 // FALL THROUGH.
6146 case TargetLowering::Custom:
6147 OpToUse = ISD::UINT_TO_FP;
6148 break;
6149 }
6150 if (OpToUse) break;
6151
6152 // Otherwise, try a larger type.
6153 }
6154
6155 // Okay, we found the operation and type to use. Zero extend our input to the
6156 // desired type then run the operation on it.
6157 return DAG.getNode(OpToUse, DestVT,
6158 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
6159 NewInTy, LegalOp));
6160}
6161
6162/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6163/// FP_TO_*INT operation of the specified operand when the target requests that
6164/// we promote it. At this point, we know that the result and operand types are
6165/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6166/// operation that returns a larger result.
Dan Gohman8181bd12008-07-27 21:46:04 +00006167SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6168 MVT DestVT,
6169 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006170 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00006171 MVT NewOutTy = DestVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006172
6173 unsigned OpToUse = 0;
6174
6175 // Scan for the appropriate larger type to use.
6176 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00006177 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6178 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006179
6180 // If the target supports FP_TO_SINT returning this type, use it.
6181 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6182 default: break;
6183 case TargetLowering::Legal:
6184 if (!TLI.isTypeLegal(NewOutTy))
6185 break; // Can't use this datatype.
6186 // FALL THROUGH.
6187 case TargetLowering::Custom:
6188 OpToUse = ISD::FP_TO_SINT;
6189 break;
6190 }
6191 if (OpToUse) break;
6192
6193 // If the target supports FP_TO_UINT of this type, use it.
6194 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6195 default: break;
6196 case TargetLowering::Legal:
6197 if (!TLI.isTypeLegal(NewOutTy))
6198 break; // Can't use this datatype.
6199 // FALL THROUGH.
6200 case TargetLowering::Custom:
6201 OpToUse = ISD::FP_TO_UINT;
6202 break;
6203 }
6204 if (OpToUse) break;
6205
6206 // Otherwise, try a larger type.
6207 }
6208
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006209
6210 // Okay, we found the operation and type to use.
Dan Gohman8181bd12008-07-27 21:46:04 +00006211 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sandsac496a12008-07-04 11:47:58 +00006212
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006213 // If the operation produces an invalid type, it must be custom lowered. Use
6214 // the target lowering hooks to expand it. Just keep the low part of the
6215 // expanded operation, we know that we're truncating anyway.
6216 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006217 SmallVector<SDValue, 2> Results;
6218 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6219 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6220 Operation = Results[0];
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006221 }
Duncan Sandsac496a12008-07-04 11:47:58 +00006222
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006223 // Truncate the result of the extended FP_TO_*INT operation to the desired
6224 // size.
6225 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006226}
6227
6228/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6229///
Dan Gohman8181bd12008-07-27 21:46:04 +00006230SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00006231 MVT VT = Op.getValueType();
6232 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00006233 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands92c43912008-06-06 12:08:01 +00006234 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006235 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6236 case MVT::i16:
6237 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6238 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6239 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6240 case MVT::i32:
6241 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6242 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6243 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6244 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6245 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6246 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6247 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6248 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6249 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6250 case MVT::i64:
6251 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6252 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6253 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6254 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6255 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6256 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6257 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6258 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6259 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6260 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6261 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6262 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6263 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6264 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6265 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6266 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6267 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6268 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6269 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6270 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6271 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6272 }
6273}
6274
6275/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6276///
Dan Gohman8181bd12008-07-27 21:46:04 +00006277SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006278 switch (Opc) {
6279 default: assert(0 && "Cannot expand this yet!");
6280 case ISD::CTPOP: {
6281 static const uint64_t mask[6] = {
6282 0x5555555555555555ULL, 0x3333333333333333ULL,
6283 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6284 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6285 };
Duncan Sands92c43912008-06-06 12:08:01 +00006286 MVT VT = Op.getValueType();
6287 MVT ShVT = TLI.getShiftAmountTy();
6288 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006289 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6290 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman8181bd12008-07-27 21:46:04 +00006291 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6292 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006293 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6294 DAG.getNode(ISD::AND, VT,
6295 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6296 }
6297 return Op;
6298 }
6299 case ISD::CTLZ: {
6300 // for now, we do this:
6301 // x = x | (x >> 1);
6302 // x = x | (x >> 2);
6303 // ...
6304 // x = x | (x >>16);
6305 // x = x | (x >>32); // for 64-bit input
6306 // return popcount(~x);
6307 //
6308 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006309 MVT VT = Op.getValueType();
6310 MVT ShVT = TLI.getShiftAmountTy();
6311 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006312 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006313 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006314 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6315 }
6316 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6317 return DAG.getNode(ISD::CTPOP, VT, Op);
6318 }
6319 case ISD::CTTZ: {
6320 // for now, we use: { return popcount(~x & (x - 1)); }
6321 // unless the target has ctlz but not ctpop, in which case we use:
6322 // { return 32 - nlz(~x & (x-1)); }
6323 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006324 MVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006325 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6326 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006327 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6328 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6329 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6330 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6331 TLI.isOperationLegal(ISD::CTLZ, VT))
6332 return DAG.getNode(ISD::SUB, VT,
Duncan Sands92c43912008-06-06 12:08:01 +00006333 DAG.getConstant(VT.getSizeInBits(), VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006334 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6335 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6336 }
6337 }
6338}
6339
Dan Gohman8181bd12008-07-27 21:46:04 +00006340/// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006341/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman4fc03742008-10-01 15:07:49 +00006342/// LegalizedNodes map is filled in for any results that are not expanded, the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006343/// ExpandedNodes map is filled in for any results that are expanded, and the
6344/// Lo/Hi values are returned.
Dan Gohman8181bd12008-07-27 21:46:04 +00006345void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands92c43912008-06-06 12:08:01 +00006346 MVT VT = Op.getValueType();
6347 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greif1c80d112008-08-28 21:40:38 +00006348 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006349 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00006350 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands92c43912008-06-06 12:08:01 +00006351 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006352
6353 // See if we already expanded it.
Dan Gohman8181bd12008-07-27 21:46:04 +00006354 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006355 = ExpandedNodes.find(Op);
6356 if (I != ExpandedNodes.end()) {
6357 Lo = I->second.first;
6358 Hi = I->second.second;
6359 return;
6360 }
6361
6362 switch (Node->getOpcode()) {
6363 case ISD::CopyFromReg:
6364 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006365 case ISD::FP_ROUND_INREG:
6366 if (VT == MVT::ppcf128 &&
6367 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6368 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006369 SDValue SrcLo, SrcHi, Src;
Dale Johannesend3b6af32007-10-11 23:32:15 +00006370 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6371 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006372 SDValue Result = TLI.LowerOperation(
Dale Johannesend3b6af32007-10-11 23:32:15 +00006373 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006374 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6375 Lo = Result.getNode()->getOperand(0);
6376 Hi = Result.getNode()->getOperand(1);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006377 break;
6378 }
6379 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006380 default:
6381#ifndef NDEBUG
6382 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
6383#endif
6384 assert(0 && "Do not know how to expand this operator!");
6385 abort();
Dan Gohman550c8462008-02-27 01:52:30 +00006386 case ISD::EXTRACT_ELEMENT:
6387 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006388 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman550c8462008-02-27 01:52:30 +00006389 return ExpandOp(Hi, Lo, Hi);
Dan Gohman7e7aa2c2008-02-27 19:44:57 +00006390 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006391 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006392 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6393 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6394 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006395 case ISD::UNDEF:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006396 Lo = DAG.getNode(ISD::UNDEF, NVT);
6397 Hi = DAG.getNode(ISD::UNDEF, NVT);
6398 break;
6399 case ISD::Constant: {
Duncan Sands92c43912008-06-06 12:08:01 +00006400 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman97f1f8e2008-03-03 22:20:46 +00006401 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6402 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6403 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006404 break;
6405 }
6406 case ISD::ConstantFP: {
6407 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00006408 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00006409 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen2aef5692007-10-11 18:07:22 +00006410 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6411 MVT::f64);
6412 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6413 MVT::f64);
6414 break;
6415 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006416 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
6417 if (getTypeAction(Lo.getValueType()) == Expand)
6418 ExpandOp(Lo, Lo, Hi);
6419 break;
6420 }
6421 case ISD::BUILD_PAIR:
6422 // Return the operands.
6423 Lo = Node->getOperand(0);
6424 Hi = Node->getOperand(1);
6425 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006426
6427 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00006428 if (Node->getNumValues() == 1) {
6429 ExpandOp(Op.getOperand(0), Lo, Hi);
6430 break;
6431 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006432 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif46bf5472008-08-26 22:36:50 +00006433 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006434 Op.getValue(1).getValueType() == MVT::Other &&
6435 "unhandled MERGE_VALUES");
6436 ExpandOp(Op.getOperand(0), Lo, Hi);
6437 // Remember that we legalized the chain.
6438 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6439 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006440
6441 case ISD::SIGN_EXTEND_INREG:
6442 ExpandOp(Node->getOperand(0), Lo, Hi);
6443 // sext_inreg the low part if needed.
6444 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6445
6446 // The high part gets the sign extension from the lo-part. This handles
6447 // things like sextinreg V:i64 from i8.
6448 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands92c43912008-06-06 12:08:01 +00006449 DAG.getConstant(NVT.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006450 TLI.getShiftAmountTy()));
6451 break;
6452
6453 case ISD::BSWAP: {
6454 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006455 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006456 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6457 Lo = TempLo;
6458 break;
6459 }
6460
6461 case ISD::CTPOP:
6462 ExpandOp(Node->getOperand(0), Lo, Hi);
6463 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6464 DAG.getNode(ISD::CTPOP, NVT, Lo),
6465 DAG.getNode(ISD::CTPOP, NVT, Hi));
6466 Hi = DAG.getConstant(0, NVT);
6467 break;
6468
6469 case ISD::CTLZ: {
6470 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
6471 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006472 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6473 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Duncan Sands4a361272009-01-01 15:52:00 +00006474 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), HLZ, BitsC,
6475 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006476 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006477 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6478
6479 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6480 Hi = DAG.getConstant(0, NVT);
6481 break;
6482 }
6483
6484 case ISD::CTTZ: {
6485 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
6486 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006487 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6488 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Duncan Sands4a361272009-01-01 15:52:00 +00006489 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), LTZ, BitsC,
6490 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006491 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006492 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6493
6494 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6495 Hi = DAG.getConstant(0, NVT);
6496 break;
6497 }
6498
6499 case ISD::VAARG: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006500 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6501 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006502 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6503 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6504
6505 // Remember that we legalized the chain.
6506 Hi = LegalizeOp(Hi);
6507 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006508 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006509 std::swap(Lo, Hi);
6510 break;
6511 }
6512
6513 case ISD::LOAD: {
6514 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00006515 SDValue Ch = LD->getChain(); // Legalize the chain.
6516 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006517 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman29c3cef2008-08-14 20:04:46 +00006518 const Value *SV = LD->getSrcValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006519 int SVOffset = LD->getSrcValueOffset();
6520 unsigned Alignment = LD->getAlignment();
6521 bool isVolatile = LD->isVolatile();
6522
6523 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman29c3cef2008-08-14 20:04:46 +00006524 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006525 isVolatile, Alignment);
6526 if (VT == MVT::f32 || VT == MVT::f64) {
6527 // f32->i32 or f64->i64 one to one expansion.
6528 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006529 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006530 // Recursively expand the new load.
6531 if (getTypeAction(NVT) == Expand)
6532 ExpandOp(Lo, Lo, Hi);
6533 break;
6534 }
6535
6536 // Increment the pointer to the other half.
Duncan Sands92c43912008-06-06 12:08:01 +00006537 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006538 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006539 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006540 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006541 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00006542 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006543 isVolatile, Alignment);
6544
6545 // Build a factor node to remember that this load is independent of the
6546 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00006547 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006548 Hi.getValue(1));
6549
6550 // Remember that we legalized the chain.
6551 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006552 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006553 std::swap(Lo, Hi);
6554 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00006555 MVT EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006556
Dale Johannesen2550e3a2007-10-19 20:29:00 +00006557 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6558 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006559 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman29c3cef2008-08-14 20:04:46 +00006560 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006561 SVOffset, isVolatile, Alignment);
6562 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006563 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006564 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6565 break;
6566 }
6567
6568 if (EVT == NVT)
Dan Gohman29c3cef2008-08-14 20:04:46 +00006569 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006570 SVOffset, isVolatile, Alignment);
6571 else
Dan Gohman29c3cef2008-08-14 20:04:46 +00006572 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006573 SVOffset, EVT, isVolatile,
6574 Alignment);
6575
6576 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006577 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006578
6579 if (ExtType == ISD::SEXTLOAD) {
6580 // The high part is obtained by SRA'ing all but one of the bits of the
6581 // lo part.
Duncan Sands92c43912008-06-06 12:08:01 +00006582 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006583 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6584 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6585 } else if (ExtType == ISD::ZEXTLOAD) {
6586 // The high part is just a zero.
6587 Hi = DAG.getConstant(0, NVT);
6588 } else /* if (ExtType == ISD::EXTLOAD) */ {
6589 // The high part is undefined.
6590 Hi = DAG.getNode(ISD::UNDEF, NVT);
6591 }
6592 }
6593 break;
6594 }
6595 case ISD::AND:
6596 case ISD::OR:
6597 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +00006598 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006599 ExpandOp(Node->getOperand(0), LL, LH);
6600 ExpandOp(Node->getOperand(1), RL, RH);
6601 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6602 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6603 break;
6604 }
6605 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006606 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006607 ExpandOp(Node->getOperand(1), LL, LH);
6608 ExpandOp(Node->getOperand(2), RL, RH);
6609 if (getTypeAction(NVT) == Expand)
6610 NVT = TLI.getTypeToExpandTo(NVT);
6611 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
6612 if (VT != MVT::f32)
6613 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
6614 break;
6615 }
6616 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006617 SDValue TL, TH, FL, FH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006618 ExpandOp(Node->getOperand(2), TL, TH);
6619 ExpandOp(Node->getOperand(3), FL, FH);
6620 if (getTypeAction(NVT) == Expand)
6621 NVT = TLI.getTypeToExpandTo(NVT);
6622 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6623 Node->getOperand(1), TL, FL, Node->getOperand(4));
6624 if (VT != MVT::f32)
6625 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6626 Node->getOperand(1), TH, FH, Node->getOperand(4));
6627 break;
6628 }
6629 case ISD::ANY_EXTEND:
6630 // The low part is any extension of the input (which degenerates to a copy).
6631 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6632 // The high part is undefined.
6633 Hi = DAG.getNode(ISD::UNDEF, NVT);
6634 break;
6635 case ISD::SIGN_EXTEND: {
6636 // The low part is just a sign extension of the input (which degenerates to
6637 // a copy).
6638 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
6639
6640 // The high part is obtained by SRA'ing all but one of the bits of the lo
6641 // part.
Duncan Sands92c43912008-06-06 12:08:01 +00006642 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006643 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6644 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6645 break;
6646 }
6647 case ISD::ZERO_EXTEND:
6648 // The low part is just a zero extension of the input (which degenerates to
6649 // a copy).
6650 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
6651
6652 // The high part is just a zero.
6653 Hi = DAG.getConstant(0, NVT);
6654 break;
6655
6656 case ISD::TRUNCATE: {
6657 // The input value must be larger than this value. Expand *it*.
Dan Gohman8181bd12008-07-27 21:46:04 +00006658 SDValue NewLo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006659 ExpandOp(Node->getOperand(0), NewLo, Hi);
6660
6661 // The low part is now either the right size, or it is closer. If not the
6662 // right size, make an illegal truncate so we recursively expand it.
6663 if (NewLo.getValueType() != Node->getValueType(0))
6664 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6665 ExpandOp(NewLo, Lo, Hi);
6666 break;
6667 }
6668
6669 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006670 SDValue Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006671 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6672 // If the target wants to, allow it to lower this itself.
6673 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6674 case Expand: assert(0 && "cannot expand FP!");
6675 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6676 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6677 }
6678 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6679 }
6680
6681 // f32 / f64 must be expanded to i32 / i64.
6682 if (VT == MVT::f32 || VT == MVT::f64) {
6683 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6684 if (getTypeAction(NVT) == Expand)
6685 ExpandOp(Lo, Lo, Hi);
6686 break;
6687 }
6688
6689 // If source operand will be expanded to the same type as VT, i.e.
6690 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands92c43912008-06-06 12:08:01 +00006691 MVT VT0 = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006692 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6693 ExpandOp(Node->getOperand(0), Lo, Hi);
6694 break;
6695 }
6696
6697 // Turn this into a load/store pair by default.
Gabor Greif1c80d112008-08-28 21:40:38 +00006698 if (Tmp.getNode() == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00006699 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006700
6701 ExpandOp(Tmp, Lo, Hi);
6702 break;
6703 }
6704
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006705 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006706 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6707 TargetLowering::Custom &&
6708 "Must custom expand ReadCycleCounter");
Dan Gohman8181bd12008-07-27 21:46:04 +00006709 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006710 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006711 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006712 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006713 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006714 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006715 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006716
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006717 case ISD::ATOMIC_CMP_SWAP: {
Dale Johannesen44eb5372008-10-03 19:41:08 +00006718 // This operation does not need a loop.
6719 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6720 assert(Tmp.getNode() && "Node must be custom expanded!");
6721 ExpandOp(Tmp.getValue(0), Lo, Hi);
6722 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6723 LegalizeOp(Tmp.getValue(1)));
6724 break;
6725 }
6726
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006727 case ISD::ATOMIC_LOAD_ADD:
6728 case ISD::ATOMIC_LOAD_SUB:
6729 case ISD::ATOMIC_LOAD_AND:
6730 case ISD::ATOMIC_LOAD_OR:
6731 case ISD::ATOMIC_LOAD_XOR:
6732 case ISD::ATOMIC_LOAD_NAND:
6733 case ISD::ATOMIC_SWAP: {
Dale Johannesen44eb5372008-10-03 19:41:08 +00006734 // These operations require a loop to be generated. We can't do that yet,
6735 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesenf160d802008-10-02 18:53:47 +00006736 SDValue In2Lo, In2Hi, In2;
6737 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6738 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006739 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6740 SDValue Replace =
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006741 DAG.getAtomic(Op.getOpcode(), Anode->getMemoryVT(),
6742 Op.getOperand(0), Op.getOperand(1), In2,
Dale Johannesen44eb5372008-10-03 19:41:08 +00006743 Anode->getSrcValue(), Anode->getAlignment());
6744 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006745 ExpandOp(Result.getValue(0), Lo, Hi);
6746 // Remember that we legalized the chain.
6747 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharth81580822008-03-05 01:15:49 +00006748 break;
6749 }
6750
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006751 // These operators cannot be expanded directly, emit them as calls to
6752 // library functions.
6753 case ISD::FP_TO_SINT: {
6754 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006755 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006756 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6757 case Expand: assert(0 && "cannot expand FP!");
6758 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6759 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6760 }
6761
6762 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6763
6764 // Now that the custom expander is done, expand the result, which is still
6765 // VT.
Gabor Greif1c80d112008-08-28 21:40:38 +00006766 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006767 ExpandOp(Op, Lo, Hi);
6768 break;
6769 }
6770 }
6771
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006772 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6773 VT);
6774 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6775 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006776 break;
6777 }
6778
6779 case ISD::FP_TO_UINT: {
6780 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006781 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006782 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6783 case Expand: assert(0 && "cannot expand FP!");
6784 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6785 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6786 }
6787
6788 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6789
6790 // Now that the custom expander is done, expand the result.
Gabor Greif1c80d112008-08-28 21:40:38 +00006791 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006792 ExpandOp(Op, Lo, Hi);
6793 break;
6794 }
6795 }
6796
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006797 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6798 VT);
6799 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6800 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006801 break;
6802 }
6803
6804 case ISD::SHL: {
6805 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006806 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006807 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006808 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006809 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006810 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006811 // Now that the custom expander is done, expand the result, which is
6812 // still VT.
6813 ExpandOp(Op, Lo, Hi);
6814 break;
6815 }
6816 }
6817
6818 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6819 // this X << 1 as X+X.
6820 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00006821 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006822 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006823 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006824 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6825 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6826 LoOps[1] = LoOps[0];
6827 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6828
6829 HiOps[1] = HiOps[0];
6830 HiOps[2] = Lo.getValue(1);
6831 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6832 break;
6833 }
6834 }
6835
6836 // If we can emit an efficient shift operation, do so now.
6837 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6838 break;
6839
6840 // If this target supports SHL_PARTS, use it.
6841 TargetLowering::LegalizeAction Action =
6842 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6843 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6844 Action == TargetLowering::Custom) {
6845 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6846 break;
6847 }
6848
6849 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006850 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006851 break;
6852 }
6853
6854 case ISD::SRA: {
6855 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006856 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006857 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006858 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006859 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006860 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006861 // Now that the custom expander is done, expand the result, which is
6862 // still VT.
6863 ExpandOp(Op, Lo, Hi);
6864 break;
6865 }
6866 }
6867
6868 // If we can emit an efficient shift operation, do so now.
6869 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6870 break;
6871
6872 // If this target supports SRA_PARTS, use it.
6873 TargetLowering::LegalizeAction Action =
6874 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6875 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6876 Action == TargetLowering::Custom) {
6877 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6878 break;
6879 }
6880
6881 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006882 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006883 break;
6884 }
6885
6886 case ISD::SRL: {
6887 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006888 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006889 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006890 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006891 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006892 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006893 // Now that the custom expander is done, expand the result, which is
6894 // still VT.
6895 ExpandOp(Op, Lo, Hi);
6896 break;
6897 }
6898 }
6899
6900 // If we can emit an efficient shift operation, do so now.
6901 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6902 break;
6903
6904 // If this target supports SRL_PARTS, use it.
6905 TargetLowering::LegalizeAction Action =
6906 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6907 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6908 Action == TargetLowering::Custom) {
6909 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6910 break;
6911 }
6912
6913 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006914 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006915 break;
6916 }
6917
6918 case ISD::ADD:
6919 case ISD::SUB: {
6920 // If the target wants to custom expand this, let them.
6921 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6922 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006923 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006924 if (Result.getNode()) {
Duncan Sands4c3885b2008-06-22 09:42:16 +00006925 ExpandOp(Result, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006926 break;
6927 }
6928 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006929 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006930 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006931 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6932 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman8181bd12008-07-27 21:46:04 +00006933 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006934 LoOps[0] = LHSL;
6935 LoOps[1] = RHSL;
6936 HiOps[0] = LHSH;
6937 HiOps[1] = RHSH;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006938
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006939 //cascaded check to see if any smaller size has a a carry flag.
6940 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6941 bool hasCarry = false;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006942 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6943 MVT AVT = MVT::getIntegerVT(BitSize);
6944 if (TLI.isOperationLegal(OpV, AVT)) {
6945 hasCarry = true;
6946 break;
6947 }
6948 }
6949
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006950 if(hasCarry) {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00006951 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006952 if (Node->getOpcode() == ISD::ADD) {
6953 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6954 HiOps[2] = Lo.getValue(1);
6955 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6956 } else {
6957 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6958 HiOps[2] = Lo.getValue(1);
6959 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6960 }
6961 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006962 } else {
Andrew Lenharth5e814462008-10-07 14:15:42 +00006963 if (Node->getOpcode() == ISD::ADD) {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00006964 Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
6965 Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
Duncan Sands4a361272009-01-01 15:52:00 +00006966 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(NVT),
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006967 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006968 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6969 DAG.getConstant(1, NVT),
6970 DAG.getConstant(0, NVT));
Duncan Sands4a361272009-01-01 15:52:00 +00006971 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(NVT),
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006972 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006973 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6974 DAG.getConstant(1, NVT),
6975 Carry1);
6976 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6977 } else {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00006978 Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
6979 Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006980 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6981 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6982 DAG.getConstant(1, NVT),
6983 DAG.getConstant(0, NVT));
6984 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6985 }
6986 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006987 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006988 }
6989
6990 case ISD::ADDC:
6991 case ISD::SUBC: {
6992 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006993 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006994 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6995 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6996 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006997 SDValue LoOps[2] = { LHSL, RHSL };
6998 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006999
7000 if (Node->getOpcode() == ISD::ADDC) {
7001 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
7002 HiOps[2] = Lo.getValue(1);
7003 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
7004 } else {
7005 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
7006 HiOps[2] = Lo.getValue(1);
7007 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
7008 }
7009 // Remember that we legalized the flag.
7010 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7011 break;
7012 }
7013 case ISD::ADDE:
7014 case ISD::SUBE: {
7015 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00007016 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007017 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7018 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7019 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00007020 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
7021 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007022
7023 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
7024 HiOps[2] = Lo.getValue(1);
7025 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
7026
7027 // Remember that we legalized the flag.
7028 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7029 break;
7030 }
7031 case ISD::MUL: {
7032 // If the target wants to custom expand this, let them.
7033 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007034 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00007035 if (New.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007036 ExpandOp(New, Lo, Hi);
7037 break;
7038 }
7039 }
7040
7041 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
7042 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00007043 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
7044 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
7045 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007046 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007047 ExpandOp(Node->getOperand(0), LL, LH);
7048 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman07961cd2008-02-25 21:11:39 +00007049 unsigned OuterBitSize = Op.getValueSizeInBits();
7050 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman5a199552007-10-08 18:33:35 +00007051 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
7052 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman2594d942008-03-10 20:42:19 +00007053 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
7054 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
7055 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman5a199552007-10-08 18:33:35 +00007056 // The inputs are both zero-extended.
7057 if (HasUMUL_LOHI) {
7058 // We can emit a umul_lohi.
7059 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00007060 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00007061 break;
7062 }
7063 if (HasMULHU) {
7064 // We can emit a mulhu+mul.
7065 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7066 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7067 break;
7068 }
Dan Gohman5a199552007-10-08 18:33:35 +00007069 }
Dan Gohman07961cd2008-02-25 21:11:39 +00007070 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman5a199552007-10-08 18:33:35 +00007071 // The input values are both sign-extended.
7072 if (HasSMUL_LOHI) {
7073 // We can emit a smul_lohi.
7074 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00007075 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00007076 break;
7077 }
7078 if (HasMULHS) {
7079 // We can emit a mulhs+mul.
7080 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7081 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
7082 break;
7083 }
7084 }
7085 if (HasUMUL_LOHI) {
7086 // Lo,Hi = umul LHS, RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00007087 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman5a199552007-10-08 18:33:35 +00007088 DAG.getVTList(NVT, NVT), LL, RL);
7089 Lo = UMulLOHI;
7090 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007091 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7092 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7093 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7094 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7095 break;
7096 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00007097 if (HasMULHU) {
7098 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7099 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7100 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7101 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7102 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7103 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7104 break;
7105 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007106 }
7107
Dan Gohman5a199552007-10-08 18:33:35 +00007108 // If nothing else, we can make a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007109 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007110 break;
7111 }
7112 case ISD::SDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007113 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007114 break;
7115 case ISD::UDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007116 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007117 break;
7118 case ISD::SREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007119 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007120 break;
7121 case ISD::UREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007122 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007123 break;
7124
7125 case ISD::FADD:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007126 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7127 RTLIB::ADD_F64,
7128 RTLIB::ADD_F80,
7129 RTLIB::ADD_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007130 Node, false, Hi);
7131 break;
7132 case ISD::FSUB:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007133 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7134 RTLIB::SUB_F64,
7135 RTLIB::SUB_F80,
7136 RTLIB::SUB_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007137 Node, false, Hi);
7138 break;
7139 case ISD::FMUL:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007140 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7141 RTLIB::MUL_F64,
7142 RTLIB::MUL_F80,
7143 RTLIB::MUL_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007144 Node, false, Hi);
7145 break;
7146 case ISD::FDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007147 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7148 RTLIB::DIV_F64,
7149 RTLIB::DIV_F80,
7150 RTLIB::DIV_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007151 Node, false, Hi);
7152 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007153 case ISD::FP_EXTEND: {
Dale Johannesen4c14d512007-10-12 01:37:08 +00007154 if (VT == MVT::ppcf128) {
7155 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7156 Node->getOperand(0).getValueType()==MVT::f64);
7157 const uint64_t zero = 0;
7158 if (Node->getOperand(0).getValueType()==MVT::f32)
7159 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
7160 else
7161 Hi = Node->getOperand(0);
7162 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7163 break;
7164 }
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007165 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7166 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7167 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007168 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007169 }
7170 case ISD::FP_ROUND: {
7171 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7172 VT);
7173 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7174 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007175 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007176 }
Evan Cheng5316b392008-09-09 23:02:14 +00007177 case ISD::FSQRT:
7178 case ISD::FSIN:
7179 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007180 case ISD::FLOG:
7181 case ISD::FLOG2:
7182 case ISD::FLOG10:
7183 case ISD::FEXP:
7184 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00007185 case ISD::FTRUNC:
7186 case ISD::FFLOOR:
7187 case ISD::FCEIL:
7188 case ISD::FRINT:
7189 case ISD::FNEARBYINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00007190 case ISD::FPOW:
7191 case ISD::FPOWI: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007192 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
7193 switch(Node->getOpcode()) {
7194 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00007195 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7196 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007197 break;
7198 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00007199 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7200 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007201 break;
7202 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00007203 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7204 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007205 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00007206 case ISD::FLOG:
7207 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7208 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7209 break;
7210 case ISD::FLOG2:
7211 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7212 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7213 break;
7214 case ISD::FLOG10:
7215 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7216 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7217 break;
7218 case ISD::FEXP:
7219 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7220 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7221 break;
7222 case ISD::FEXP2:
7223 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7224 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7225 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00007226 case ISD::FTRUNC:
7227 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7228 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7229 break;
7230 case ISD::FFLOOR:
7231 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7232 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7233 break;
7234 case ISD::FCEIL:
7235 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7236 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7237 break;
7238 case ISD::FRINT:
7239 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7240 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7241 break;
7242 case ISD::FNEARBYINT:
7243 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7244 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7245 break;
Evan Cheng5316b392008-09-09 23:02:14 +00007246 case ISD::FPOW:
7247 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7248 RTLIB::POW_PPCF128);
7249 break;
7250 case ISD::FPOWI:
7251 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7252 RTLIB::POWI_PPCF128);
7253 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007254 default: assert(0 && "Unreachable!");
7255 }
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007256 Lo = ExpandLibCall(LC, Node, false, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007257 break;
7258 }
7259 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007260 if (VT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007261 SDValue Tmp;
Dale Johannesen5707ef82007-10-12 19:02:17 +00007262 ExpandOp(Node->getOperand(0), Lo, Tmp);
7263 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7264 // lo = hi==fabs(hi) ? lo : -lo;
7265 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7266 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7267 DAG.getCondCode(ISD::SETEQ));
7268 break;
7269 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007270 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007271 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7272 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7273 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7274 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7275 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7276 if (getTypeAction(NVT) == Expand)
7277 ExpandOp(Lo, Lo, Hi);
7278 break;
7279 }
7280 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007281 if (VT == MVT::ppcf128) {
7282 ExpandOp(Node->getOperand(0), Lo, Hi);
7283 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7284 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7285 break;
7286 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007287 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007288 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7289 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7290 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7291 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7292 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7293 if (getTypeAction(NVT) == Expand)
7294 ExpandOp(Lo, Lo, Hi);
7295 break;
7296 }
7297 case ISD::FCOPYSIGN: {
7298 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7299 if (getTypeAction(NVT) == Expand)
7300 ExpandOp(Lo, Lo, Hi);
7301 break;
7302 }
7303 case ISD::SINT_TO_FP:
7304 case ISD::UINT_TO_FP: {
7305 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands92c43912008-06-06 12:08:01 +00007306 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007307
7308 // Promote the operand if needed. Do this before checking for
7309 // ppcf128 so conversions of i16 and i8 work.
7310 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007311 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesen6a779c82008-03-18 17:28:38 +00007312 Tmp = isSigned
7313 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7314 DAG.getValueType(SrcVT))
7315 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greif1c80d112008-08-28 21:40:38 +00007316 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007317 SrcVT = Node->getOperand(0).getValueType();
7318 }
7319
Dan Gohmanec51f642008-03-10 23:03:31 +00007320 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman84d00962008-02-25 21:39:34 +00007321 static const uint64_t zero = 0;
Dale Johannesen4c14d512007-10-12 01:37:08 +00007322 if (isSigned) {
7323 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7324 Node->getOperand(0)));
7325 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7326 } else {
Dan Gohman84d00962008-02-25 21:39:34 +00007327 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen4c14d512007-10-12 01:37:08 +00007328 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7329 Node->getOperand(0)));
7330 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7331 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007332 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00007333 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7334 DAG.getConstant(0, MVT::i32),
7335 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7336 DAG.getConstantFP(
7337 APFloat(APInt(128, 2, TwoE32)),
7338 MVT::ppcf128)),
7339 Hi,
7340 DAG.getCondCode(ISD::SETLT)),
7341 Lo, Hi);
7342 }
7343 break;
7344 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007345 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7346 // si64->ppcf128 done by libcall, below
Dan Gohman84d00962008-02-25 21:39:34 +00007347 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007348 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7349 Lo, Hi);
7350 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7351 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7352 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7353 DAG.getConstant(0, MVT::i64),
7354 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7355 DAG.getConstantFP(
7356 APFloat(APInt(128, 2, TwoE64)),
7357 MVT::ppcf128)),
7358 Hi,
7359 DAG.getCondCode(ISD::SETLT)),
7360 Lo, Hi);
7361 break;
7362 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007363
Dan Gohmanec51f642008-03-10 23:03:31 +00007364 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7365 Node->getOperand(0));
Evan Chenga8740032008-04-01 01:50:16 +00007366 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng4a2f6df2008-04-01 01:51:26 +00007367 // float to i32 etc. can be 'expanded' to a single node.
Evan Chenga8740032008-04-01 01:50:16 +00007368 ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007369 break;
7370 }
7371 }
7372
7373 // Make sure the resultant values have been legalized themselves, unless this
7374 // is a type that requires multi-step expansion.
7375 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7376 Lo = LegalizeOp(Lo);
Gabor Greif1c80d112008-08-28 21:40:38 +00007377 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007378 // Don't legalize the high part if it is expanded to a single node.
7379 Hi = LegalizeOp(Hi);
7380 }
7381
7382 // Remember in a map if the values will be reused later.
Dan Gohman55d19662008-07-07 17:46:23 +00007383 bool isNew =
7384 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007385 assert(isNew && "Value already expanded?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007386 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007387}
7388
7389/// SplitVectorOp - Given an operand of vector type, break it down into
7390/// two smaller values, still of vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00007391void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7392 SDValue &Hi) {
Duncan Sands92c43912008-06-06 12:08:01 +00007393 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007394 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007395 unsigned NumElements = Op.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007396 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00007397
Duncan Sands92c43912008-06-06 12:08:01 +00007398 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman4a365ad2007-11-15 21:15:26 +00007399
7400 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7401 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7402
Duncan Sands92c43912008-06-06 12:08:01 +00007403 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7404 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007405
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007406 // See if we already split it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007407 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007408 = SplitNodes.find(Op);
7409 if (I != SplitNodes.end()) {
7410 Lo = I->second.first;
7411 Hi = I->second.second;
7412 return;
7413 }
7414
7415 switch (Node->getOpcode()) {
7416 default:
7417#ifndef NDEBUG
7418 Node->dump(&DAG);
7419#endif
7420 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00007421 case ISD::UNDEF:
7422 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7423 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7424 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007425 case ISD::BUILD_PAIR:
7426 Lo = Node->getOperand(0);
7427 Hi = Node->getOperand(1);
7428 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007429 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007430 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7431 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007432 unsigned Index = Idx->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007433 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007434 if (Index < NewNumElts_Lo)
7435 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7436 DAG.getIntPtrConstant(Index));
7437 else
7438 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7439 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7440 break;
7441 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007442 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007443 Node->getOperand(1),
7444 Node->getOperand(2));
7445 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007446 break;
7447 }
Chris Lattner587c46d2007-11-19 21:16:54 +00007448 case ISD::VECTOR_SHUFFLE: {
7449 // Build the low part.
Dan Gohman8181bd12008-07-27 21:46:04 +00007450 SDValue Mask = Node->getOperand(2);
7451 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +00007452 MVT PtrVT = TLI.getPointerTy();
Chris Lattner587c46d2007-11-19 21:16:54 +00007453
7454 // Insert all of the elements from the input that are needed. We use
7455 // buildvector of extractelement here because the input vectors will have
7456 // to be legalized, so this makes the code simpler.
7457 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007458 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007459 if (IdxNode.getOpcode() == ISD::UNDEF) {
7460 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7461 continue;
7462 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007463 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007464 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007465 if (Idx >= NumElements) {
7466 InVec = Node->getOperand(1);
7467 Idx -= NumElements;
7468 }
7469 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7470 DAG.getConstant(Idx, PtrVT)));
7471 }
7472 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7473 Ops.clear();
7474
7475 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007476 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007477 if (IdxNode.getOpcode() == ISD::UNDEF) {
7478 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7479 continue;
7480 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007481 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007482 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007483 if (Idx >= NumElements) {
7484 InVec = Node->getOperand(1);
7485 Idx -= NumElements;
7486 }
7487 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7488 DAG.getConstant(Idx, PtrVT)));
7489 }
Mon P Wang2e89b112008-07-25 01:30:26 +00007490 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner587c46d2007-11-19 21:16:54 +00007491 break;
7492 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007493 case ISD::BUILD_VECTOR: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007494 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00007495 Node->op_begin()+NewNumElts_Lo);
7496 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007497
Dan Gohman8181bd12008-07-27 21:46:04 +00007498 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007499 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007500 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007501 break;
7502 }
7503 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00007504 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007505 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7506 if (NewNumSubvectors == 1) {
7507 Lo = Node->getOperand(0);
7508 Hi = Node->getOperand(1);
7509 } else {
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007510 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7511 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007512 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007513
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007514 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007515 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007516 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007517 }
7518 break;
7519 }
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007520 case ISD::EXTRACT_SUBVECTOR: {
7521 SDValue Vec = Op.getOperand(0);
7522 SDValue Idx = Op.getOperand(1);
7523 MVT IdxVT = Idx.getValueType();
7524
7525 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7526 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7527 if (CIdx) {
7528 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7529 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7530 IdxVT));
7531 } else {
7532 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7533 DAG.getConstant(NewNumElts_Lo, IdxVT));
7534 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7535 }
7536 break;
7537 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00007538 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007539 SDValue Cond = Node->getOperand(0);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007540
Dan Gohman8181bd12008-07-27 21:46:04 +00007541 SDValue LL, LH, RL, RH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007542 SplitVectorOp(Node->getOperand(1), LL, LH);
7543 SplitVectorOp(Node->getOperand(2), RL, RH);
7544
Duncan Sands92c43912008-06-06 12:08:01 +00007545 if (Cond.getValueType().isVector()) {
Dan Gohmand5d4c872007-10-17 14:48:28 +00007546 // Handle a vector merge.
Dan Gohman8181bd12008-07-27 21:46:04 +00007547 SDValue CL, CH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007548 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007549 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7550 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007551 } else {
7552 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00007553 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7554 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007555 }
7556 break;
7557 }
Chris Lattnerc7471452008-06-30 02:43:01 +00007558 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007559 SDValue CondLHS = Node->getOperand(0);
7560 SDValue CondRHS = Node->getOperand(1);
7561 SDValue CondCode = Node->getOperand(4);
Chris Lattnerc7471452008-06-30 02:43:01 +00007562
Dan Gohman8181bd12008-07-27 21:46:04 +00007563 SDValue LL, LH, RL, RH;
Chris Lattnerc7471452008-06-30 02:43:01 +00007564 SplitVectorOp(Node->getOperand(2), LL, LH);
7565 SplitVectorOp(Node->getOperand(3), RL, RH);
7566
7567 // Handle a simple select with vector operands.
7568 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7569 LL, RL, CondCode);
7570 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7571 LH, RH, CondCode);
7572 break;
7573 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00007574 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007575 SDValue LL, LH, RL, RH;
Nate Begeman9a1ce152008-05-12 19:40:03 +00007576 SplitVectorOp(Node->getOperand(0), LL, LH);
7577 SplitVectorOp(Node->getOperand(1), RL, RH);
7578 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7579 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7580 break;
7581 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007582 case ISD::ADD:
7583 case ISD::SUB:
7584 case ISD::MUL:
7585 case ISD::FADD:
7586 case ISD::FSUB:
7587 case ISD::FMUL:
7588 case ISD::SDIV:
7589 case ISD::UDIV:
7590 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007591 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007592 case ISD::AND:
7593 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00007594 case ISD::XOR:
7595 case ISD::UREM:
7596 case ISD::SREM:
Mon P Wang26342922008-12-18 20:03:17 +00007597 case ISD::FREM:
7598 case ISD::SHL:
7599 case ISD::SRA:
7600 case ISD::SRL: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007601 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007602 SplitVectorOp(Node->getOperand(0), LL, LH);
7603 SplitVectorOp(Node->getOperand(1), RL, RH);
7604
Nate Begeman4a365ad2007-11-15 21:15:26 +00007605 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7606 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007607 break;
7608 }
Dan Gohman29c3cef2008-08-14 20:04:46 +00007609 case ISD::FP_ROUND:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007610 case ISD::FPOWI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007611 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007612 SplitVectorOp(Node->getOperand(0), L, H);
7613
Nate Begeman4a365ad2007-11-15 21:15:26 +00007614 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7615 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00007616 break;
7617 }
7618 case ISD::CTTZ:
7619 case ISD::CTLZ:
7620 case ISD::CTPOP:
7621 case ISD::FNEG:
7622 case ISD::FABS:
7623 case ISD::FSQRT:
7624 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00007625 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007626 case ISD::FLOG:
7627 case ISD::FLOG2:
7628 case ISD::FLOG10:
7629 case ISD::FEXP:
7630 case ISD::FEXP2:
Nate Begeman78246ca2007-11-17 03:58:34 +00007631 case ISD::FP_TO_SINT:
7632 case ISD::FP_TO_UINT:
7633 case ISD::SINT_TO_FP:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007634 case ISD::UINT_TO_FP:
7635 case ISD::TRUNCATE:
7636 case ISD::ANY_EXTEND:
7637 case ISD::SIGN_EXTEND:
7638 case ISD::ZERO_EXTEND:
7639 case ISD::FP_EXTEND: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007640 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007641 SplitVectorOp(Node->getOperand(0), L, H);
7642
Nate Begeman4a365ad2007-11-15 21:15:26 +00007643 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7644 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00007645 break;
7646 }
Mon P Wang73d31542008-11-10 20:54:11 +00007647 case ISD::CONVERT_RNDSAT: {
7648 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7649 SDValue L, H;
7650 SplitVectorOp(Node->getOperand(0), L, H);
7651 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7652 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7653 SDValue STyOpL = DAG.getValueType(L.getValueType());
7654 SDValue STyOpH = DAG.getValueType(H.getValueType());
7655
7656 SDValue RndOp = Node->getOperand(3);
7657 SDValue SatOp = Node->getOperand(4);
7658
7659 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7660 RndOp, SatOp, CvtCode);
7661 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7662 RndOp, SatOp, CvtCode);
7663 break;
7664 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007665 case ISD::LOAD: {
7666 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007667 SDValue Ch = LD->getChain();
7668 SDValue Ptr = LD->getBasePtr();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007669 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007670 const Value *SV = LD->getSrcValue();
7671 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007672 MVT MemoryVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007673 unsigned Alignment = LD->getAlignment();
7674 bool isVolatile = LD->isVolatile();
7675
Dan Gohman29c3cef2008-08-14 20:04:46 +00007676 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7677 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7678
7679 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7680 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7681 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7682
7683 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7684 NewVT_Lo, Ch, Ptr, Offset,
7685 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7686 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007687 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00007688 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007689 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00007690 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00007691 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7692 NewVT_Hi, Ch, Ptr, Offset,
7693 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007694
7695 // Build a factor node to remember that this load is independent of the
7696 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00007697 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007698 Hi.getValue(1));
7699
7700 // Remember that we legalized the chain.
7701 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
7702 break;
7703 }
7704 case ISD::BIT_CONVERT: {
7705 // We know the result is a vector. The input may be either a vector or a
7706 // scalar value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007707 SDValue InOp = Node->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007708 if (!InOp.getValueType().isVector() ||
7709 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007710 // The input is a scalar or single-element vector.
7711 // Lower to a store/load so that it can be split.
7712 // FIXME: this could be improved probably.
Mon P Wang36b59ac2008-07-15 05:28:34 +00007713 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7714 Op.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00007715 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greif1c80d112008-08-28 21:40:38 +00007716 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007717
Dan Gohman8181bd12008-07-27 21:46:04 +00007718 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00007719 InOp, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007720 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00007721 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007722 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007723 }
7724 // Split the vector and convert each of the pieces now.
7725 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007726 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7727 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007728 break;
7729 }
7730 }
7731
7732 // Remember in a map if the values will be reused later.
7733 bool isNew =
7734 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
7735 assert(isNew && "Value already split?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007736 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007737}
7738
7739
7740/// ScalarizeVectorOp - Given an operand of single-element vector type
7741/// (e.g. v1f32), convert it into the equivalent operation that returns a
7742/// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007743SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00007744 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007745 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007746 MVT NewVT = Op.getValueType().getVectorElementType();
7747 assert(Op.getValueType().getVectorNumElements() == 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007748
7749 // See if we already scalarized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007750 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007751 if (I != ScalarizedNodes.end()) return I->second;
7752
Dan Gohman8181bd12008-07-27 21:46:04 +00007753 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007754 switch (Node->getOpcode()) {
7755 default:
7756#ifndef NDEBUG
7757 Node->dump(&DAG); cerr << "\n";
7758#endif
7759 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7760 case ISD::ADD:
7761 case ISD::FADD:
7762 case ISD::SUB:
7763 case ISD::FSUB:
7764 case ISD::MUL:
7765 case ISD::FMUL:
7766 case ISD::SDIV:
7767 case ISD::UDIV:
7768 case ISD::FDIV:
7769 case ISD::SREM:
7770 case ISD::UREM:
7771 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007772 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007773 case ISD::AND:
7774 case ISD::OR:
7775 case ISD::XOR:
7776 Result = DAG.getNode(Node->getOpcode(),
7777 NewVT,
7778 ScalarizeVectorOp(Node->getOperand(0)),
7779 ScalarizeVectorOp(Node->getOperand(1)));
7780 break;
7781 case ISD::FNEG:
7782 case ISD::FABS:
7783 case ISD::FSQRT:
7784 case ISD::FSIN:
7785 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007786 case ISD::FLOG:
7787 case ISD::FLOG2:
7788 case ISD::FLOG10:
7789 case ISD::FEXP:
7790 case ISD::FEXP2:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007791 case ISD::FP_TO_SINT:
7792 case ISD::FP_TO_UINT:
7793 case ISD::SINT_TO_FP:
7794 case ISD::UINT_TO_FP:
7795 case ISD::SIGN_EXTEND:
7796 case ISD::ZERO_EXTEND:
7797 case ISD::ANY_EXTEND:
7798 case ISD::TRUNCATE:
7799 case ISD::FP_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007800 Result = DAG.getNode(Node->getOpcode(),
7801 NewVT,
7802 ScalarizeVectorOp(Node->getOperand(0)));
7803 break;
Mon P Wang73d31542008-11-10 20:54:11 +00007804 case ISD::CONVERT_RNDSAT: {
7805 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7806 Result = DAG.getConvertRndSat(NewVT, Op0,
7807 DAG.getValueType(NewVT),
7808 DAG.getValueType(Op0.getValueType()),
7809 Node->getOperand(3),
7810 Node->getOperand(4),
7811 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7812 break;
7813 }
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007814 case ISD::FPOWI:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007815 case ISD::FP_ROUND:
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007816 Result = DAG.getNode(Node->getOpcode(),
7817 NewVT,
7818 ScalarizeVectorOp(Node->getOperand(0)),
7819 Node->getOperand(1));
7820 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007821 case ISD::LOAD: {
7822 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007823 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7824 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman29c3cef2008-08-14 20:04:46 +00007825 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007826 const Value *SV = LD->getSrcValue();
7827 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007828 MVT MemoryVT = LD->getMemoryVT();
7829 unsigned Alignment = LD->getAlignment();
7830 bool isVolatile = LD->isVolatile();
7831
7832 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7833 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7834
7835 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7836 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7837 MemoryVT.getVectorElementType(),
7838 isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007839
7840 // Remember that we legalized the chain.
7841 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7842 break;
7843 }
7844 case ISD::BUILD_VECTOR:
7845 Result = Node->getOperand(0);
7846 break;
7847 case ISD::INSERT_VECTOR_ELT:
7848 // Returning the inserted scalar element.
7849 Result = Node->getOperand(1);
7850 break;
7851 case ISD::CONCAT_VECTORS:
7852 assert(Node->getOperand(0).getValueType() == NewVT &&
7853 "Concat of non-legal vectors not yet supported!");
7854 Result = Node->getOperand(0);
7855 break;
7856 case ISD::VECTOR_SHUFFLE: {
7857 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007858 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007859 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007860 Result = ScalarizeVectorOp(Node->getOperand(1));
7861 else
7862 Result = ScalarizeVectorOp(Node->getOperand(0));
7863 break;
7864 }
7865 case ISD::EXTRACT_SUBVECTOR:
Mon P Wang927daf52008-11-06 22:52:21 +00007866 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007867 Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007868 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007869 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007870 SDValue Op0 = Op.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007871 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng2cc16e72008-05-16 17:19:05 +00007872 Op0 = ScalarizeVectorOp(Op0);
7873 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007874 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007875 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007876 case ISD::SELECT:
7877 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
7878 ScalarizeVectorOp(Op.getOperand(1)),
7879 ScalarizeVectorOp(Op.getOperand(2)));
7880 break;
Chris Lattnerc7471452008-06-30 02:43:01 +00007881 case ISD::SELECT_CC:
7882 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7883 Node->getOperand(1),
7884 ScalarizeVectorOp(Op.getOperand(2)),
7885 ScalarizeVectorOp(Op.getOperand(3)),
7886 Node->getOperand(4));
7887 break;
Nate Begeman78ca4f92008-05-12 23:09:43 +00007888 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007889 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7890 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Duncan Sands4a361272009-01-01 15:52:00 +00007891 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0.getValueType()),
7892 Op0, Op1, Op.getOperand(2));
Nate Begeman78ca4f92008-05-12 23:09:43 +00007893 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7894 DAG.getConstant(-1ULL, NewVT),
7895 DAG.getConstant(0ULL, NewVT));
7896 break;
7897 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007898 }
7899
7900 if (TLI.isTypeLegal(NewVT))
7901 Result = LegalizeOp(Result);
7902 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7903 assert(isNew && "Value already scalarized?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007904 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007905 return Result;
7906}
7907
7908
Mon P Wang1448aad2008-10-30 08:01:45 +00007909SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7910 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7911 if (I != WidenNodes.end()) return I->second;
7912
7913 MVT VT = Op.getValueType();
7914 assert(VT.isVector() && "Cannot widen non-vector type!");
7915
7916 SDValue Result;
7917 SDNode *Node = Op.getNode();
7918 MVT EVT = VT.getVectorElementType();
7919
7920 unsigned NumElts = VT.getVectorNumElements();
7921 unsigned NewNumElts = WidenVT.getVectorNumElements();
7922 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7923 assert(NewNumElts < 17);
7924
7925 // When widen is called, it is assumed that it is more efficient to use a
7926 // wide type. The default action is to widen to operation to a wider legal
7927 // vector type and then do the operation if it is legal by calling LegalizeOp
7928 // again. If there is no vector equivalent, we will unroll the operation, do
7929 // it, and rebuild the vector. If most of the operations are vectorizible to
7930 // the legal type, the resulting code will be more efficient. If this is not
7931 // the case, the resulting code will preform badly as we end up generating
7932 // code to pack/unpack the results. It is the function that calls widen
Mon P Wanga5a239f2008-11-06 05:31:54 +00007933 // that is responsible for seeing this doesn't happen.
Mon P Wang1448aad2008-10-30 08:01:45 +00007934 switch (Node->getOpcode()) {
7935 default:
7936#ifndef NDEBUG
7937 Node->dump(&DAG);
7938#endif
7939 assert(0 && "Unexpected operation in WidenVectorOp!");
7940 break;
7941 case ISD::CopyFromReg:
Mon P Wang257e1c72008-11-15 06:05:52 +00007942 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang1448aad2008-10-30 08:01:45 +00007943 case ISD::Constant:
7944 case ISD::ConstantFP:
7945 // To build a vector of these elements, clients should call BuildVector
7946 // and with each element instead of creating a node with a vector type
7947 assert(0 && "Unexpected operation in WidenVectorOp!");
7948 case ISD::VAARG:
7949 // Variable Arguments with vector types doesn't make any sense to me
7950 assert(0 && "Unexpected operation in WidenVectorOp!");
7951 break;
Mon P Wang257e1c72008-11-15 06:05:52 +00007952 case ISD::UNDEF:
7953 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7954 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00007955 case ISD::BUILD_VECTOR: {
7956 // Build a vector with undefined for the new nodes
7957 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7958 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7959 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7960 }
7961 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7962 break;
7963 }
7964 case ISD::INSERT_VECTOR_ELT: {
7965 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7966 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7967 Node->getOperand(1), Node->getOperand(2));
7968 break;
7969 }
7970 case ISD::VECTOR_SHUFFLE: {
7971 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7972 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7973 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7974 // used as permutation array. We build the vector here instead of widening
7975 // because we don't want to legalize and have it turned to something else.
7976 SDValue PermOp = Node->getOperand(2);
7977 SDValueVector NewOps;
7978 MVT PVT = PermOp.getValueType().getVectorElementType();
7979 for (unsigned i = 0; i < NumElts; ++i) {
7980 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7981 NewOps.push_back(PermOp.getOperand(i));
7982 } else {
7983 unsigned Idx =
Mon P Wangec428ad2008-12-13 08:15:14 +00007984 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
Mon P Wang1448aad2008-10-30 08:01:45 +00007985 if (Idx < NumElts) {
7986 NewOps.push_back(PermOp.getOperand(i));
7987 }
7988 else {
7989 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7990 PermOp.getOperand(i).getValueType()));
7991 }
7992 }
7993 }
7994 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7995 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
7996 }
7997
7998 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
7999 MVT::getVectorVT(PVT, NewOps.size()),
8000 &NewOps[0], NewOps.size());
8001
8002 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
8003 break;
8004 }
8005 case ISD::LOAD: {
8006 // If the load widen returns true, we can use a single load for the
8007 // vector. Otherwise, it is returning a token factor for multiple
8008 // loads.
8009 SDValue TFOp;
8010 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
8011 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
8012 else
8013 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
8014 break;
8015 }
8016
8017 case ISD::BIT_CONVERT: {
8018 SDValue Tmp1 = Node->getOperand(0);
8019 // Converts between two different types so we need to determine
8020 // the correct widen type for the input operand.
Mon P Wang26342922008-12-18 20:03:17 +00008021 MVT InVT = Tmp1.getValueType();
8022 unsigned WidenSize = WidenVT.getSizeInBits();
8023 if (InVT.isVector()) {
8024 MVT InEltVT = InVT.getVectorElementType();
8025 unsigned InEltSize = InEltVT.getSizeInBits();
8026 assert(WidenSize % InEltSize == 0 &&
8027 "can not widen bit convert that are not multiple of element type");
8028 MVT NewInWidenVT = MVT::getVectorVT(InEltVT, WidenSize / InEltSize);
8029 Tmp1 = WidenVectorOp(Tmp1, NewInWidenVT);
8030 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
8031 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Tmp1);
8032 } else {
8033 // If the result size is a multiple of the input size, widen the input
8034 // and then convert.
8035 unsigned InSize = InVT.getSizeInBits();
8036 assert(WidenSize % InSize == 0 &&
8037 "can not widen bit convert that are not multiple of element type");
8038 unsigned NewNumElts = WidenSize / InSize;
8039 SmallVector<SDValue, 16> Ops(NewNumElts);
8040 SDValue UndefVal = DAG.getNode(ISD::UNDEF, InVT);
8041 Ops[0] = Tmp1;
8042 for (unsigned i = 1; i < NewNumElts; ++i)
8043 Ops[i] = UndefVal;
Mon P Wang1448aad2008-10-30 08:01:45 +00008044
Mon P Wang26342922008-12-18 20:03:17 +00008045 MVT NewInVT = MVT::getVectorVT(InVT, NewNumElts);
8046 Result = DAG.getNode(ISD::BUILD_VECTOR, NewInVT, &Ops[0], NewNumElts);
8047 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Result);
Mon P Wang1448aad2008-10-30 08:01:45 +00008048 }
8049 break;
8050 }
8051
8052 case ISD::SINT_TO_FP:
8053 case ISD::UINT_TO_FP:
8054 case ISD::FP_TO_SINT:
Mon P Wang26342922008-12-18 20:03:17 +00008055 case ISD::FP_TO_UINT:
8056 case ISD::FP_ROUND: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008057 SDValue Tmp1 = Node->getOperand(0);
8058 // Converts between two different types so we need to determine
8059 // the correct widen type for the input operand.
8060 MVT TVT = Tmp1.getValueType();
8061 assert(TVT.isVector() && "can not widen non vector type");
8062 MVT TEVT = TVT.getVectorElementType();
8063 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
8064 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
8065 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
8066 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang1448aad2008-10-30 08:01:45 +00008067 break;
8068 }
8069
8070 case ISD::FP_EXTEND:
8071 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
8072 case ISD::TRUNCATE:
8073 case ISD::SIGN_EXTEND:
8074 case ISD::ZERO_EXTEND:
8075 case ISD::ANY_EXTEND:
Mon P Wang1448aad2008-10-30 08:01:45 +00008076 case ISD::SIGN_EXTEND_INREG:
8077 case ISD::FABS:
8078 case ISD::FNEG:
8079 case ISD::FSQRT:
8080 case ISD::FSIN:
Mon P Wang257e1c72008-11-15 06:05:52 +00008081 case ISD::FCOS:
8082 case ISD::CTPOP:
8083 case ISD::CTTZ:
8084 case ISD::CTLZ: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008085 // Unary op widening
Mon P Wang26342922008-12-18 20:03:17 +00008086 SDValue Tmp1;
Mon P Wang1448aad2008-10-30 08:01:45 +00008087 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8088 assert(Tmp1.getValueType() == WidenVT);
8089 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang1448aad2008-10-30 08:01:45 +00008090 break;
8091 }
Mon P Wang73d31542008-11-10 20:54:11 +00008092 case ISD::CONVERT_RNDSAT: {
8093 SDValue RndOp = Node->getOperand(3);
8094 SDValue SatOp = Node->getOperand(4);
Mon P Wang73d31542008-11-10 20:54:11 +00008095 SDValue SrcOp = Node->getOperand(0);
8096
8097 // Converts between two different types so we need to determine
8098 // the correct widen type for the input operand.
8099 MVT SVT = SrcOp.getValueType();
8100 assert(SVT.isVector() && "can not widen non vector type");
8101 MVT SEVT = SVT.getVectorElementType();
8102 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8103
8104 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8105 assert(SrcOp.getValueType() == WidenVT);
8106 SDValue DTyOp = DAG.getValueType(WidenVT);
8107 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8108 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8109
8110 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
8111 RndOp, SatOp, CvtCode);
Mon P Wang73d31542008-11-10 20:54:11 +00008112 break;
8113 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008114 case ISD::FPOW:
8115 case ISD::FPOWI:
8116 case ISD::ADD:
8117 case ISD::SUB:
8118 case ISD::MUL:
8119 case ISD::MULHS:
8120 case ISD::MULHU:
8121 case ISD::AND:
8122 case ISD::OR:
8123 case ISD::XOR:
8124 case ISD::FADD:
8125 case ISD::FSUB:
8126 case ISD::FMUL:
8127 case ISD::SDIV:
8128 case ISD::SREM:
8129 case ISD::FDIV:
8130 case ISD::FREM:
8131 case ISD::FCOPYSIGN:
8132 case ISD::UDIV:
8133 case ISD::UREM:
8134 case ISD::BSWAP: {
8135 // Binary op widening
Mon P Wang1448aad2008-10-30 08:01:45 +00008136 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8137 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8138 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8139 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
Mon P Wang1448aad2008-10-30 08:01:45 +00008140 break;
8141 }
8142
8143 case ISD::SHL:
8144 case ISD::SRA:
8145 case ISD::SRL: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008146 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8147 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangd5638262008-12-02 07:35:08 +00008148 SDValue ShOp = Node->getOperand(1);
8149 MVT ShVT = ShOp.getValueType();
8150 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8151 WidenVT.getVectorNumElements());
8152 ShOp = WidenVectorOp(ShOp, NewShVT);
8153 assert(ShOp.getValueType() == NewShVT);
8154 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, ShOp);
Mon P Wang1448aad2008-10-30 08:01:45 +00008155 break;
8156 }
Mon P Wangd5638262008-12-02 07:35:08 +00008157
Mon P Wang1448aad2008-10-30 08:01:45 +00008158 case ISD::EXTRACT_VECTOR_ELT: {
8159 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8160 assert(Tmp1.getValueType() == WidenVT);
8161 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8162 break;
8163 }
8164 case ISD::CONCAT_VECTORS: {
8165 // We concurrently support only widen on a multiple of the incoming vector.
8166 // We could widen on a multiple of the incoming operand if necessary.
8167 unsigned NumConcat = NewNumElts / NumElts;
8168 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Mon P Wangd5638262008-12-02 07:35:08 +00008169 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
Mon P Wang1448aad2008-10-30 08:01:45 +00008170 SmallVector<SDValue, 8> MOps;
8171 MOps.push_back(Op);
8172 for (unsigned i = 1; i != NumConcat; ++i) {
8173 MOps.push_back(UndefVal);
8174 }
8175 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8176 &MOps[0], MOps.size()));
8177 break;
8178 }
8179 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang257e1c72008-11-15 06:05:52 +00008180 SDValue Tmp1 = Node->getOperand(0);
8181 SDValue Idx = Node->getOperand(1);
8182 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8183 if (CIdx && CIdx->getZExtValue() == 0) {
8184 // Since we are access the start of the vector, the incoming
8185 // vector type might be the proper.
8186 MVT Tmp1VT = Tmp1.getValueType();
8187 if (Tmp1VT == WidenVT)
8188 return Tmp1;
8189 else {
8190 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8191 if (Tmp1VTNumElts < NewNumElts)
8192 Result = WidenVectorOp(Tmp1, WidenVT);
8193 else
8194 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8195 }
8196 } else if (NewNumElts % NumElts == 0) {
8197 // Widen the extracted subvector.
8198 unsigned NumConcat = NewNumElts / NumElts;
8199 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8200 SmallVector<SDValue, 8> MOps;
8201 MOps.push_back(Op);
8202 for (unsigned i = 1; i != NumConcat; ++i) {
8203 MOps.push_back(UndefVal);
8204 }
8205 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8206 &MOps[0], MOps.size()));
8207 } else {
8208 assert(0 && "can not widen extract subvector");
8209 // This could be implemented using insert and build vector but I would
8210 // like to see when this happens.
8211 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008212 break;
8213 }
8214
8215 case ISD::SELECT: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008216 // Determine new condition widen type and widen
8217 SDValue Cond1 = Node->getOperand(0);
8218 MVT CondVT = Cond1.getValueType();
8219 assert(CondVT.isVector() && "can not widen non vector type");
8220 MVT CondEVT = CondVT.getVectorElementType();
8221 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8222 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8223 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8224
8225 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8226 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8227 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8228 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang1448aad2008-10-30 08:01:45 +00008229 break;
8230 }
8231
8232 case ISD::SELECT_CC: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008233 // Determine new condition widen type and widen
8234 SDValue Cond1 = Node->getOperand(0);
8235 SDValue Cond2 = Node->getOperand(1);
8236 MVT CondVT = Cond1.getValueType();
8237 assert(CondVT.isVector() && "can not widen non vector type");
8238 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8239 MVT CondEVT = CondVT.getVectorElementType();
8240 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8241 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8242 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8243 assert(Cond1.getValueType() == CondWidenVT &&
8244 Cond2.getValueType() == CondWidenVT && "condition not widen");
8245
8246 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8247 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8248 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8249 "operands not widen");
8250 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8251 Tmp2, Node->getOperand(4));
Mon P Wang1448aad2008-10-30 08:01:45 +00008252 break;
Mon P Wang42ac14e2008-10-30 18:21:52 +00008253 }
8254 case ISD::VSETCC: {
8255 // Determine widen for the operand
8256 SDValue Tmp1 = Node->getOperand(0);
8257 MVT TmpVT = Tmp1.getValueType();
8258 assert(TmpVT.isVector() && "can not widen non vector type");
8259 MVT TmpEVT = TmpVT.getVectorElementType();
8260 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8261 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8262 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
Mon P Wang26342922008-12-18 20:03:17 +00008263 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
Mon P Wang42ac14e2008-10-30 18:21:52 +00008264 Node->getOperand(2));
Mon P Wang1448aad2008-10-30 08:01:45 +00008265 break;
8266 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00008267 case ISD::ATOMIC_CMP_SWAP:
8268 case ISD::ATOMIC_LOAD_ADD:
8269 case ISD::ATOMIC_LOAD_SUB:
8270 case ISD::ATOMIC_LOAD_AND:
8271 case ISD::ATOMIC_LOAD_OR:
8272 case ISD::ATOMIC_LOAD_XOR:
8273 case ISD::ATOMIC_LOAD_NAND:
8274 case ISD::ATOMIC_LOAD_MIN:
8275 case ISD::ATOMIC_LOAD_MAX:
8276 case ISD::ATOMIC_LOAD_UMIN:
8277 case ISD::ATOMIC_LOAD_UMAX:
8278 case ISD::ATOMIC_SWAP: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008279 // For now, we assume that using vectors for these operations don't make
8280 // much sense so we just split it. We return an empty result
8281 SDValue X, Y;
8282 SplitVectorOp(Op, X, Y);
8283 return Result;
8284 break;
8285 }
8286
8287 } // end switch (Node->getOpcode())
8288
8289 assert(Result.getNode() && "Didn't set a result!");
8290 if (Result != Op)
8291 Result = LegalizeOp(Result);
8292
Mon P Wanga5a239f2008-11-06 05:31:54 +00008293 AddWidenedOperand(Op, Result);
Mon P Wang1448aad2008-10-30 08:01:45 +00008294 return Result;
8295}
8296
8297// Utility function to find a legal vector type and its associated element
8298// type from a preferred width and whose vector type must be the same size
8299// as the VVT.
8300// TLI: Target lowering used to determine legal types
8301// Width: Preferred width of element type
8302// VVT: Vector value type whose size we must match.
8303// Returns VecEVT and EVT - the vector type and its associated element type
8304static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8305 MVT& EVT, MVT& VecEVT) {
8306 // We start with the preferred width, make it a power of 2 and see if
8307 // we can find a vector type of that width. If not, we reduce it by
8308 // another power of 2. If we have widen the type, a vector of bytes should
8309 // always be legal.
8310 assert(TLI.isTypeLegal(VVT));
8311 unsigned EWidth = Width + 1;
8312 do {
8313 assert(EWidth > 0);
8314 EWidth = (1 << Log2_32(EWidth-1));
8315 EVT = MVT::getIntegerVT(EWidth);
8316 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8317 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8318 } while (!TLI.isTypeLegal(VecEVT) ||
8319 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8320}
8321
8322SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8323 SDValue Chain,
8324 SDValue BasePtr,
8325 const Value *SV,
8326 int SVOffset,
8327 unsigned Alignment,
8328 bool isVolatile,
8329 unsigned LdWidth,
8330 MVT ResType) {
8331 // We assume that we have good rules to handle loading power of two loads so
8332 // we break down the operations to power of 2 loads. The strategy is to
8333 // load the largest power of 2 that we can easily transform to a legal vector
8334 // and then insert into that vector, and the cast the result into the legal
8335 // vector that we want. This avoids unnecessary stack converts.
8336 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8337 // the load is nonvolatile, we an use a wider load for the value.
8338 // Find a vector length we can load a large chunk
8339 MVT EVT, VecEVT;
8340 unsigned EVTWidth;
8341 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8342 EVTWidth = EVT.getSizeInBits();
8343
8344 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8345 isVolatile, Alignment);
8346 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8347 LdChain.push_back(LdOp.getValue(1));
8348
8349 // Check if we can load the element with one instruction
8350 if (LdWidth == EVTWidth) {
8351 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8352 }
8353
8354 // The vector element order is endianness dependent.
8355 unsigned Idx = 1;
8356 LdWidth -= EVTWidth;
8357 unsigned Offset = 0;
8358
8359 while (LdWidth > 0) {
8360 unsigned Increment = EVTWidth / 8;
8361 Offset += Increment;
8362 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8363 DAG.getIntPtrConstant(Increment));
8364
8365 if (LdWidth < EVTWidth) {
8366 // Our current type we are using is too large, use a smaller size by
8367 // using a smaller power of 2
8368 unsigned oEVTWidth = EVTWidth;
8369 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8370 EVTWidth = EVT.getSizeInBits();
8371 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008372 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang1448aad2008-10-30 08:01:45 +00008373 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8374 }
8375
8376 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8377 SVOffset+Offset, isVolatile,
8378 MinAlign(Alignment, Offset));
8379 LdChain.push_back(LdOp.getValue(1));
8380 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8381 DAG.getIntPtrConstant(Idx++));
8382
8383 LdWidth -= EVTWidth;
8384 }
8385
8386 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8387}
8388
8389bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8390 SDValue& TFOp,
8391 SDValue Op,
8392 MVT NVT) {
8393 // TODO: Add support for ConcatVec and the ability to load many vector
8394 // types (e.g., v4i8). This will not work when a vector register
8395 // to memory mapping is strange (e.g., vector elements are not
8396 // stored in some sequential order).
8397
8398 // It must be true that the widen vector type is bigger than where
8399 // we need to load from.
8400 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8401 MVT LdVT = LD->getMemoryVT();
8402 assert(LdVT.isVector() && NVT.isVector());
8403 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8404
8405 // Load information
8406 SDValue Chain = LD->getChain();
8407 SDValue BasePtr = LD->getBasePtr();
8408 int SVOffset = LD->getSrcValueOffset();
8409 unsigned Alignment = LD->getAlignment();
8410 bool isVolatile = LD->isVolatile();
8411 const Value *SV = LD->getSrcValue();
8412 unsigned int LdWidth = LdVT.getSizeInBits();
8413
8414 // Load value as a large register
8415 SDValueVector LdChain;
8416 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8417 Alignment, isVolatile, LdWidth, NVT);
8418
8419 if (LdChain.size() == 1) {
8420 TFOp = LdChain[0];
8421 return true;
8422 }
8423 else {
8424 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8425 return false;
8426 }
8427}
8428
8429
8430void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8431 SDValue Chain,
8432 SDValue BasePtr,
8433 const Value *SV,
8434 int SVOffset,
8435 unsigned Alignment,
8436 bool isVolatile,
Mon P Wang257e1c72008-11-15 06:05:52 +00008437 SDValue ValOp,
Mon P Wang1448aad2008-10-30 08:01:45 +00008438 unsigned StWidth) {
8439 // Breaks the stores into a series of power of 2 width stores. For any
8440 // width, we convert the vector to the vector of element size that we
8441 // want to store. This avoids requiring a stack convert.
8442
8443 // Find a width of the element type we can store with
8444 MVT VVT = ValOp.getValueType();
8445 MVT EVT, VecEVT;
8446 unsigned EVTWidth;
8447 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8448 EVTWidth = EVT.getSizeInBits();
8449
8450 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8451 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang927daf52008-11-06 22:52:21 +00008452 DAG.getIntPtrConstant(0));
Mon P Wang1448aad2008-10-30 08:01:45 +00008453 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8454 isVolatile, Alignment);
8455 StChain.push_back(StOp);
8456
8457 // Check if we are done
8458 if (StWidth == EVTWidth) {
8459 return;
8460 }
8461
8462 unsigned Idx = 1;
8463 StWidth -= EVTWidth;
8464 unsigned Offset = 0;
8465
8466 while (StWidth > 0) {
8467 unsigned Increment = EVTWidth / 8;
8468 Offset += Increment;
8469 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8470 DAG.getIntPtrConstant(Increment));
8471
8472 if (StWidth < EVTWidth) {
8473 // Our current type we are using is too large, use a smaller size by
8474 // using a smaller power of 2
8475 unsigned oEVTWidth = EVTWidth;
8476 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8477 EVTWidth = EVT.getSizeInBits();
8478 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008479 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang1448aad2008-10-30 08:01:45 +00008480 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8481 }
8482
8483 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang257e1c72008-11-15 06:05:52 +00008484 DAG.getIntPtrConstant(Idx++));
Mon P Wang1448aad2008-10-30 08:01:45 +00008485 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8486 SVOffset + Offset, isVolatile,
8487 MinAlign(Alignment, Offset)));
8488 StWidth -= EVTWidth;
8489 }
8490}
8491
8492
8493SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8494 SDValue Chain,
8495 SDValue BasePtr) {
8496 // TODO: It might be cleaner if we can use SplitVector and have more legal
8497 // vector types that can be stored into memory (e.g., v4xi8 can
8498 // be stored as a word). This will not work when a vector register
8499 // to memory mapping is strange (e.g., vector elements are not
8500 // stored in some sequential order).
8501
8502 MVT StVT = ST->getMemoryVT();
8503 SDValue ValOp = ST->getValue();
8504
8505 // Check if we have widen this node with another value
8506 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8507 if (I != WidenNodes.end())
8508 ValOp = I->second;
8509
8510 MVT VVT = ValOp.getValueType();
8511
8512 // It must be true that we the widen vector type is bigger than where
8513 // we need to store.
8514 assert(StVT.isVector() && VVT.isVector());
8515 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8516 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8517
8518 // Store value
8519 SDValueVector StChain;
8520 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8521 ST->getSrcValueOffset(), ST->getAlignment(),
8522 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8523 if (StChain.size() == 1)
8524 return StChain[0];
8525 else
8526 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8527}
8528
8529
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008530// SelectionDAG::Legalize - This is the entry point for the file.
8531//
Duncan Sandse016a2e2008-12-14 09:43:15 +00008532void SelectionDAG::Legalize(bool TypesNeedLegalizing) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008533 /// run - This is the main entry point to this class.
8534 ///
Duncan Sandse016a2e2008-12-14 09:43:15 +00008535 SelectionDAGLegalize(*this, TypesNeedLegalizing).LegalizeDAG();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008536}
8537