blob: 26a02babd2c50c74e06ac581f85a3989b20bcdde [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 Gohman0275b132009-01-15 16:43:02 +0000545 SelectionDAG &DAG, const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 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,
Dan Gohman0275b132009-01-15 16:43:02 +0000594 SelectionDAG &DAG,
595 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +0000596 MVT VT = Node->getValueType(0);
597 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
599 "fcopysign expansion only supported for f32 and f64");
Duncan Sands92c43912008-06-06 12:08:01 +0000600 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601
602 // First get the sign bit of second operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000603 SDValue Mask1 = (SrcVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
605 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
606 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000607 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
609 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands92c43912008-06-06 12:08:01 +0000610 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 if (SizeDiff > 0) {
612 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
613 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
614 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnere6fa1452008-07-10 23:46:13 +0000615 } else if (SizeDiff < 0) {
616 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
617 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
618 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
619 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620
621 // Clear the sign bit of first operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000622 SDValue Mask2 = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
624 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
625 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman8181bd12008-07-27 21:46:04 +0000626 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
628
629 // Or the value with the sign bit.
630 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
631 return Result;
632}
633
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000634/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
635static
Dan Gohman8181bd12008-07-27 21:46:04 +0000636SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0275b132009-01-15 16:43:02 +0000637 const TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000638 SDValue Chain = ST->getChain();
639 SDValue Ptr = ST->getBasePtr();
640 SDValue Val = ST->getValue();
Duncan Sands92c43912008-06-06 12:08:01 +0000641 MVT VT = Val.getValueType();
Dale Johannesen08275382007-09-08 19:29:23 +0000642 int Alignment = ST->getAlignment();
643 int SVOffset = ST->getSrcValueOffset();
Duncan Sands92c43912008-06-06 12:08:01 +0000644 if (ST->getMemoryVT().isFloatingPoint() ||
645 ST->getMemoryVT().isVector()) {
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000646 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
647 if (TLI.isTypeLegal(intVT)) {
648 // Expand to a bitconvert of the value to the integer type of the
649 // same size, then a (misaligned) int store.
650 // FIXME: Does not handle truncating floating point stores!
651 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
652 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
653 SVOffset, ST->isVolatile(), Alignment);
654 } else {
655 // Do a (aligned) store to a stack slot, then copy from the stack slot
656 // to the final destination using (unaligned) integer loads and stores.
657 MVT StoredVT = ST->getMemoryVT();
658 MVT RegVT =
659 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
660 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
661 unsigned RegBytes = RegVT.getSizeInBits() / 8;
662 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen08275382007-09-08 19:29:23 +0000663
Duncan Sands734f49b2008-12-13 07:18:38 +0000664 // Make sure the stack slot is also aligned for the register type.
665 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
666
667 // Perform the original store, only redirected to the stack slot.
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000668 SDValue Store = DAG.getTruncStore(Chain, Val, StackPtr, NULL, 0,StoredVT);
669 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
670 SmallVector<SDValue, 8> Stores;
671 unsigned Offset = 0;
672
673 // Do all but one copies using the full register width.
674 for (unsigned i = 1; i < NumRegs; i++) {
675 // Load one integer register's worth from the stack slot.
676 SDValue Load = DAG.getLoad(RegVT, Store, StackPtr, NULL, 0);
677 // Store it to the final location. Remember the store.
678 Stores.push_back(DAG.getStore(Load.getValue(1), Load, Ptr,
679 ST->getSrcValue(), SVOffset + Offset,
680 ST->isVolatile(),
681 MinAlign(ST->getAlignment(), Offset)));
682 // Increment the pointers.
683 Offset += RegBytes;
684 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
685 Increment);
686 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
687 }
688
Duncan Sands734f49b2008-12-13 07:18:38 +0000689 // The last store may be partial. Do a truncating store. On big-endian
690 // machines this requires an extending load from the stack slot to ensure
691 // that the bits are in the right place.
692 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000693
Duncan Sands734f49b2008-12-13 07:18:38 +0000694 // Load from the stack slot.
695 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Store, StackPtr,
696 NULL, 0, MemVT);
697
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000698 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, Ptr,
699 ST->getSrcValue(), SVOffset + Offset,
Duncan Sands734f49b2008-12-13 07:18:38 +0000700 MemVT, ST->isVolatile(),
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000701 MinAlign(ST->getAlignment(), Offset)));
702 // The order of the stores doesn't matter - say it with a TokenFactor.
703 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
704 Stores.size());
705 }
Dale Johannesen08275382007-09-08 19:29:23 +0000706 }
Duncan Sands92c43912008-06-06 12:08:01 +0000707 assert(ST->getMemoryVT().isInteger() &&
708 !ST->getMemoryVT().isVector() &&
Dale Johannesen08275382007-09-08 19:29:23 +0000709 "Unaligned store of unknown type.");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000710 // Get the half-size VT
Duncan Sands92c43912008-06-06 12:08:01 +0000711 MVT NewStoredVT =
712 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
713 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000714 int IncrementSize = NumBits / 8;
715
716 // Divide the stored value in two parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000717 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
718 SDValue Lo = Val;
719 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000720
721 // Store the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000722 SDValue Store1, Store2;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000723 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
724 ST->getSrcValue(), SVOffset, NewStoredVT,
725 ST->isVolatile(), Alignment);
726 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
727 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsa3691432007-10-28 12:59:45 +0000728 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000729 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
730 ST->getSrcValue(), SVOffset + IncrementSize,
731 NewStoredVT, ST->isVolatile(), Alignment);
732
733 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
734}
735
736/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
737static
Dan Gohman8181bd12008-07-27 21:46:04 +0000738SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
739 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000740 int SVOffset = LD->getSrcValueOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +0000741 SDValue Chain = LD->getChain();
742 SDValue Ptr = LD->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +0000743 MVT VT = LD->getValueType(0);
744 MVT LoadedVT = LD->getMemoryVT();
745 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000746 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
747 if (TLI.isTypeLegal(intVT)) {
748 // Expand to a (misaligned) integer load of the same size,
749 // then bitconvert to floating point or vector.
750 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
751 SVOffset, LD->isVolatile(),
Dale Johannesen08275382007-09-08 19:29:23 +0000752 LD->getAlignment());
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000753 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
754 if (VT.isFloatingPoint() && LoadedVT != VT)
755 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
Dale Johannesen08275382007-09-08 19:29:23 +0000756
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000757 SDValue Ops[] = { Result, Chain };
758 return DAG.getMergeValues(Ops, 2);
759 } else {
760 // Copy the value to a (aligned) stack slot using (unaligned) integer
761 // loads and stores, then do a (aligned) load from the stack slot.
762 MVT RegVT = TLI.getRegisterType(intVT);
763 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
764 unsigned RegBytes = RegVT.getSizeInBits() / 8;
765 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
766
Duncan Sands734f49b2008-12-13 07:18:38 +0000767 // Make sure the stack slot is also aligned for the register type.
768 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
769
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000770 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
771 SmallVector<SDValue, 8> Stores;
772 SDValue StackPtr = StackBase;
773 unsigned Offset = 0;
774
775 // Do all but one copies using the full register width.
776 for (unsigned i = 1; i < NumRegs; i++) {
777 // Load one integer register's worth from the original location.
778 SDValue Load = DAG.getLoad(RegVT, Chain, Ptr, LD->getSrcValue(),
779 SVOffset + Offset, LD->isVolatile(),
780 MinAlign(LD->getAlignment(), Offset));
781 // Follow the load with a store to the stack slot. Remember the store.
782 Stores.push_back(DAG.getStore(Load.getValue(1), Load, StackPtr,
783 NULL, 0));
784 // Increment the pointers.
785 Offset += RegBytes;
786 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
787 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
788 Increment);
789 }
790
791 // The last copy may be partial. Do an extending load.
Duncan Sands734f49b2008-12-13 07:18:38 +0000792 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000793 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Chain, Ptr,
794 LD->getSrcValue(), SVOffset + Offset,
Duncan Sands734f49b2008-12-13 07:18:38 +0000795 MemVT, LD->isVolatile(),
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000796 MinAlign(LD->getAlignment(), Offset));
797 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sands734f49b2008-12-13 07:18:38 +0000798 // On big-endian machines this requires a truncating store to ensure
799 // that the bits end up in the right place.
800 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, StackPtr,
801 NULL, 0, MemVT));
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000802
803 // The order of the stores doesn't matter - say it with a TokenFactor.
804 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
805 Stores.size());
806
807 // Finally, perform the original load only redirected to the stack slot.
808 Load = DAG.getExtLoad(LD->getExtensionType(), VT, TF, StackBase,
809 NULL, 0, LoadedVT);
810
811 // Callers expect a MERGE_VALUES node.
812 SDValue Ops[] = { Load, TF };
813 return DAG.getMergeValues(Ops, 2);
814 }
Dale Johannesen08275382007-09-08 19:29:23 +0000815 }
Duncan Sands92c43912008-06-06 12:08:01 +0000816 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000817 "Unaligned load of unsupported type.");
818
Dale Johannesendc0ee192008-02-27 22:36:00 +0000819 // Compute the new VT that is half the size of the old one. This is an
820 // integer MVT.
Duncan Sands92c43912008-06-06 12:08:01 +0000821 unsigned NumBits = LoadedVT.getSizeInBits();
822 MVT NewLoadedVT;
823 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000824 NumBits >>= 1;
825
826 unsigned Alignment = LD->getAlignment();
827 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000828 ISD::LoadExtType HiExtType = LD->getExtensionType();
829
830 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
831 if (HiExtType == ISD::NON_EXTLOAD)
832 HiExtType = ISD::ZEXTLOAD;
833
834 // Load the value in two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000835 SDValue Lo, Hi;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000836 if (TLI.isLittleEndian()) {
837 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
838 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
839 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
840 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
841 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
842 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000843 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000844 } else {
845 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
846 NewLoadedVT,LD->isVolatile(), Alignment);
847 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
848 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
849 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
850 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000851 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000852 }
853
854 // aggregate the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000855 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
856 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000857 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
858
Dan Gohman8181bd12008-07-27 21:46:04 +0000859 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000860 Hi.getValue(1));
861
Dan Gohman8181bd12008-07-27 21:46:04 +0000862 SDValue Ops[] = { Result, TF };
Duncan Sands698842f2008-07-02 17:40:58 +0000863 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000864}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865
Dan Gohman6d05cac2007-10-11 23:57:53 +0000866/// UnrollVectorOp - We know that the given vector has a legal type, however
867/// the operation it performs is not legal and is an operation that we have
868/// no way of lowering. "Unroll" the vector, splitting out the scalars and
869/// operating on each element individually.
Dan Gohman8181bd12008-07-27 21:46:04 +0000870SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000871 MVT VT = Op.getValueType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000872 assert(isTypeLegal(VT) &&
873 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000874 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman6d05cac2007-10-11 23:57:53 +0000875 "Can't unroll a vector with multiple results!");
Duncan Sands92c43912008-06-06 12:08:01 +0000876 unsigned NE = VT.getVectorNumElements();
877 MVT EltVT = VT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000878
Dan Gohman8181bd12008-07-27 21:46:04 +0000879 SmallVector<SDValue, 8> Scalars;
880 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman6d05cac2007-10-11 23:57:53 +0000881 for (unsigned i = 0; i != NE; ++i) {
882 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000883 SDValue Operand = Op.getOperand(j);
Duncan Sands92c43912008-06-06 12:08:01 +0000884 MVT OperandVT = Operand.getValueType();
885 if (OperandVT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +0000886 // A vector operand; extract a single element.
Duncan Sands92c43912008-06-06 12:08:01 +0000887 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000888 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
889 OperandEltVT,
890 Operand,
891 DAG.getConstant(i, MVT::i32));
892 } else {
893 // A scalar operand; just use it as is.
894 Operands[j] = Operand;
895 }
896 }
Mon P Wang9901e732008-12-09 05:46:39 +0000897
898 switch (Op.getOpcode()) {
899 default:
900 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
901 &Operands[0], Operands.size()));
902 break;
903 case ISD::SHL:
904 case ISD::SRA:
905 case ISD::SRL:
906 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT, Operands[0],
907 LegalizeShiftAmount(Operands[1])));
908 break;
909 }
Dan Gohman6d05cac2007-10-11 23:57:53 +0000910 }
911
912 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
913}
914
Duncan Sands37a3f472008-01-10 10:28:30 +0000915/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands92c43912008-06-06 12:08:01 +0000916static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands37a3f472008-01-10 10:28:30 +0000917 RTLIB::Libcall Call_F32,
918 RTLIB::Libcall Call_F64,
919 RTLIB::Libcall Call_F80,
920 RTLIB::Libcall Call_PPCF128) {
921 return
922 VT == MVT::f32 ? Call_F32 :
923 VT == MVT::f64 ? Call_F64 :
924 VT == MVT::f80 ? Call_F80 :
925 VT == MVT::ppcf128 ? Call_PPCF128 :
926 RTLIB::UNKNOWN_LIBCALL;
927}
928
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000929/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
930/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
931/// is necessary to spill the vector being inserted into to memory, perform
932/// the insert there, and then read the result back.
Dan Gohman8181bd12008-07-27 21:46:04 +0000933SDValue SelectionDAGLegalize::
934PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
935 SDValue Tmp1 = Vec;
936 SDValue Tmp2 = Val;
937 SDValue Tmp3 = Idx;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000938
939 // If the target doesn't support this, we have to spill the input vector
940 // to a temporary stack slot, update the element, then reload it. This is
941 // badness. We could also load the value into a vector register (either
942 // with a "move to register" or "extload into register" instruction, then
943 // permute it into place, if the idx is a constant and if the idx is
944 // supported by the target.
Duncan Sands92c43912008-06-06 12:08:01 +0000945 MVT VT = Tmp1.getValueType();
946 MVT EltVT = VT.getVectorElementType();
947 MVT IdxVT = Tmp3.getValueType();
948 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +0000949 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000950
Gabor Greif1c80d112008-08-28 21:40:38 +0000951 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000952
953 // Store the vector.
Dan Gohman8181bd12008-07-27 21:46:04 +0000954 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang1448aad2008-10-30 08:01:45 +0000955 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000956
957 // Truncate or zero extend offset to target pointer type.
Duncan Sandsec142ee2008-06-08 20:54:56 +0000958 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000959 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
960 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +0000961 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000962 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman8181bd12008-07-27 21:46:04 +0000963 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000964 // Store the scalar value.
965 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000966 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000967 // Load the updated vector.
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000968 return DAG.getLoad(VT, Ch, StackPtr,
969 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000970}
971
Mon P Wang9901e732008-12-09 05:46:39 +0000972SDValue SelectionDAGLegalize::LegalizeShiftAmount(SDValue ShiftAmt) {
973 if (TLI.getShiftAmountTy().bitsLT(ShiftAmt.getValueType()))
974 return DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), ShiftAmt);
975
976 if (TLI.getShiftAmountTy().bitsGT(ShiftAmt.getValueType()))
977 return DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), ShiftAmt);
978
979 return ShiftAmt;
980}
981
982
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983/// LegalizeOp - We know that the specified value has a legal type, and
984/// that its operands are legal. Now ensure that the operation itself
985/// is legal, recursively ensuring that the operands' operations remain
986/// legal.
Dan Gohman8181bd12008-07-27 21:46:04 +0000987SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattnerdad577b2007-08-25 01:00:22 +0000988 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
989 return Op;
990
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 assert(isTypeLegal(Op.getValueType()) &&
992 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000993 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994
995 // If this operation defines any values that cannot be represented in a
996 // register on this target, make sure to expand or promote them.
997 if (Node->getNumValues() > 1) {
998 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
999 if (getTypeAction(Node->getValueType(i)) != Legal) {
1000 HandleOp(Op.getValue(i));
1001 assert(LegalizedNodes.count(Op) &&
1002 "Handling didn't add legal operands!");
1003 return LegalizedNodes[Op];
1004 }
1005 }
1006
1007 // Note that LegalizeOp may be reentered even from single-use nodes, which
1008 // means that we always must cache transformed nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00001009 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 if (I != LegalizedNodes.end()) return I->second;
1011
Dan Gohman8181bd12008-07-27 21:46:04 +00001012 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
1013 SDValue Result = Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014 bool isCustom = false;
1015
1016 switch (Node->getOpcode()) {
1017 case ISD::FrameIndex:
1018 case ISD::EntryToken:
1019 case ISD::Register:
1020 case ISD::BasicBlock:
1021 case ISD::TargetFrameIndex:
1022 case ISD::TargetJumpTable:
1023 case ISD::TargetConstant:
1024 case ISD::TargetConstantFP:
1025 case ISD::TargetConstantPool:
1026 case ISD::TargetGlobalAddress:
1027 case ISD::TargetGlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +00001028 case ISD::TargetExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029 case ISD::VALUETYPE:
1030 case ISD::SRCVALUE:
Dan Gohman12a9c082008-02-06 22:27:42 +00001031 case ISD::MEMOPERAND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032 case ISD::CONDCODE:
Duncan Sandsc93fae32008-03-21 09:14:45 +00001033 case ISD::ARG_FLAGS:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001034 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00001035 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001036 "This must be legal!");
1037 break;
1038 default:
1039 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1040 // If this is a target node, legalize it by legalizing the operands then
1041 // passing it through.
Dan Gohman8181bd12008-07-27 21:46:04 +00001042 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001043 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1044 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1045
1046 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
1047
1048 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1049 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00001050 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001051 }
1052 // Otherwise this is an unhandled builtin node. splat.
1053#ifndef NDEBUG
1054 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
1055#endif
1056 assert(0 && "Do not know how to legalize this operator!");
1057 abort();
1058 case ISD::GLOBAL_OFFSET_TABLE:
1059 case ISD::GlobalAddress:
1060 case ISD::GlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +00001061 case ISD::ExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062 case ISD::ConstantPool:
1063 case ISD::JumpTable: // Nothing to do.
1064 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1065 default: assert(0 && "This action is not supported yet!");
1066 case TargetLowering::Custom:
1067 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001068 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001069 // FALLTHROUGH if the target doesn't want to lower this op after all.
1070 case TargetLowering::Legal:
1071 break;
1072 }
1073 break;
1074 case ISD::FRAMEADDR:
1075 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001076 // The only option for these nodes is to custom lower them. If the target
1077 // does not custom lower them, then return zero.
1078 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001079 if (Tmp1.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001080 Result = Tmp1;
1081 else
1082 Result = DAG.getConstant(0, TLI.getPointerTy());
1083 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +00001084 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands92c43912008-06-06 12:08:01 +00001085 MVT VT = Node->getValueType(0);
Anton Korobeynikov09386bd2007-08-29 19:28:29 +00001086 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1087 default: assert(0 && "This action is not supported yet!");
1088 case TargetLowering::Custom:
1089 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001090 if (Result.getNode()) break;
Anton Korobeynikov09386bd2007-08-29 19:28:29 +00001091 // Fall Thru
1092 case TargetLowering::Legal:
1093 Result = DAG.getConstant(0, VT);
1094 break;
1095 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +00001096 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +00001097 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001098 case ISD::EXCEPTIONADDR: {
1099 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00001100 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001101 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1102 default: assert(0 && "This action is not supported yet!");
1103 case TargetLowering::Expand: {
1104 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001105 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001106 }
1107 break;
1108 case TargetLowering::Custom:
1109 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001110 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001111 // Fall Thru
1112 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001113 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands698842f2008-07-02 17:40:58 +00001114 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001115 break;
1116 }
1117 }
1118 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001119 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001120
Gabor Greif1c80d112008-08-28 21:40:38 +00001121 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001122 "Cannot return more than two values!");
1123
1124 // Since we produced two values, make sure to remember that we
1125 // legalized both of them.
1126 Tmp1 = LegalizeOp(Result);
1127 Tmp2 = LegalizeOp(Result.getValue(1));
1128 AddLegalizedOperand(Op.getValue(0), Tmp1);
1129 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001130 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001131 case ISD::EHSELECTION: {
1132 Tmp1 = LegalizeOp(Node->getOperand(0));
1133 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00001134 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001135 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1136 default: assert(0 && "This action is not supported yet!");
1137 case TargetLowering::Expand: {
1138 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001139 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001140 }
1141 break;
1142 case TargetLowering::Custom:
1143 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001144 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145 // Fall Thru
1146 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001147 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands698842f2008-07-02 17:40:58 +00001148 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001149 break;
1150 }
1151 }
1152 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001153 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001154
Gabor Greif1c80d112008-08-28 21:40:38 +00001155 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001156 "Cannot return more than two values!");
1157
1158 // Since we produced two values, make sure to remember that we
1159 // legalized both of them.
1160 Tmp1 = LegalizeOp(Result);
1161 Tmp2 = LegalizeOp(Result.getValue(1));
1162 AddLegalizedOperand(Op.getValue(0), Tmp1);
1163 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001164 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 case ISD::EH_RETURN: {
Duncan Sands92c43912008-06-06 12:08:01 +00001166 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001167 // The only "good" option for this node is to custom lower it.
1168 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1169 default: assert(0 && "This action is not supported at all!");
1170 case TargetLowering::Custom:
1171 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001172 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001173 // Fall Thru
1174 case TargetLowering::Legal:
1175 // Target does not know, how to lower this, lower to noop
1176 Result = LegalizeOp(Node->getOperand(0));
1177 break;
1178 }
1179 }
1180 break;
1181 case ISD::AssertSext:
1182 case ISD::AssertZext:
1183 Tmp1 = LegalizeOp(Node->getOperand(0));
1184 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1185 break;
1186 case ISD::MERGE_VALUES:
1187 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif46bf5472008-08-26 22:36:50 +00001188 Result = Node->getOperand(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001189 break;
1190 case ISD::CopyFromReg:
1191 Tmp1 = LegalizeOp(Node->getOperand(0));
1192 Result = Op.getValue(0);
1193 if (Node->getNumValues() == 2) {
1194 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1195 } else {
1196 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
1197 if (Node->getNumOperands() == 3) {
1198 Tmp2 = LegalizeOp(Node->getOperand(2));
1199 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1200 } else {
1201 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1202 }
1203 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1204 }
1205 // Since CopyFromReg produces two values, make sure to remember that we
1206 // legalized both of them.
1207 AddLegalizedOperand(Op.getValue(0), Result);
1208 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001209 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001210 case ISD::UNDEF: {
Duncan Sands92c43912008-06-06 12:08:01 +00001211 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001212 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
1213 default: assert(0 && "This action is not supported yet!");
1214 case TargetLowering::Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00001215 if (VT.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001216 Result = DAG.getConstant(0, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00001217 else if (VT.isFloatingPoint())
1218 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesen20b76352007-09-26 17:26:49 +00001219 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001220 else
1221 assert(0 && "Unknown value type!");
1222 break;
1223 case TargetLowering::Legal:
1224 break;
1225 }
1226 break;
1227 }
1228
1229 case ISD::INTRINSIC_W_CHAIN:
1230 case ISD::INTRINSIC_WO_CHAIN:
1231 case ISD::INTRINSIC_VOID: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001232 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001233 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1234 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1235 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1236
1237 // Allow the target to custom lower its intrinsics if it wants to.
1238 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1239 TargetLowering::Custom) {
1240 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001241 if (Tmp3.getNode()) Result = Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001242 }
1243
Gabor Greif1c80d112008-08-28 21:40:38 +00001244 if (Result.getNode()->getNumValues() == 1) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001245
1246 // Must have return value and chain result.
Gabor Greif1c80d112008-08-28 21:40:38 +00001247 assert(Result.getNode()->getNumValues() == 2 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001248 "Cannot return more than two values!");
1249
1250 // Since loads produce two values, make sure to remember that we
1251 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001252 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1253 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001254 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001255 }
1256
Dan Gohman472d12c2008-06-30 20:59:49 +00001257 case ISD::DBG_STOPPOINT:
1258 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1260
Dan Gohman472d12c2008-06-30 20:59:49 +00001261 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001262 case TargetLowering::Promote:
1263 default: assert(0 && "This action is not supported yet!");
1264 case TargetLowering::Expand: {
Devang Patelfcf1c752009-01-13 00:35:13 +00001265 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001266 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001267 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001268
Dan Gohman472d12c2008-06-30 20:59:49 +00001269 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Devang Patelfcf1c752009-01-13 00:35:13 +00001270 GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
1271 if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
1272 DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
1273 unsigned SrcFile = DW->RecordSource(CU.getDirectory(),
1274 CU.getFilename());
1275
Dan Gohman472d12c2008-06-30 20:59:49 +00001276 unsigned Line = DSP->getLine();
1277 unsigned Col = DSP->getColumn();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001278
1279 if (useDEBUG_LOC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001280 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Chengd6f57682008-07-08 20:06:39 +00001281 DAG.getConstant(Col, MVT::i32),
1282 DAG.getConstant(SrcFile, MVT::i32) };
1283 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001284 } else {
Devang Patelfcf1c752009-01-13 00:35:13 +00001285 unsigned ID = DW->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001286 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001287 }
1288 } else {
1289 Result = Tmp1; // chain
1290 }
1291 break;
1292 }
Evan Chengd6f57682008-07-08 20:06:39 +00001293 case TargetLowering::Legal: {
1294 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1295 if (Action == Legal && Tmp1 == Node->getOperand(0))
1296 break;
1297
Dan Gohman8181bd12008-07-27 21:46:04 +00001298 SmallVector<SDValue, 8> Ops;
Evan Chengd6f57682008-07-08 20:06:39 +00001299 Ops.push_back(Tmp1);
1300 if (Action == Legal) {
1301 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1302 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1303 } else {
1304 // Otherwise promote them.
1305 Ops.push_back(PromoteOp(Node->getOperand(1)));
1306 Ops.push_back(PromoteOp(Node->getOperand(2)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001307 }
Evan Chengd6f57682008-07-08 20:06:39 +00001308 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1309 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1310 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001311 break;
1312 }
Evan Chengd6f57682008-07-08 20:06:39 +00001313 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001314 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001315
1316 case ISD::DECLARE:
1317 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1318 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1319 default: assert(0 && "This action is not supported yet!");
1320 case TargetLowering::Legal:
1321 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1322 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1323 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1324 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1325 break;
Chris Lattner203cd052008-02-28 05:53:40 +00001326 case TargetLowering::Expand:
1327 Result = LegalizeOp(Node->getOperand(0));
1328 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001329 }
1330 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001331
1332 case ISD::DEBUG_LOC:
1333 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1334 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1335 default: assert(0 && "This action is not supported yet!");
Evan Chengd6f57682008-07-08 20:06:39 +00001336 case TargetLowering::Legal: {
1337 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001338 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Chengd6f57682008-07-08 20:06:39 +00001339 if (Action == Legal && Tmp1 == Node->getOperand(0))
1340 break;
1341 if (Action == Legal) {
1342 Tmp2 = Node->getOperand(1);
1343 Tmp3 = Node->getOperand(2);
1344 Tmp4 = Node->getOperand(3);
1345 } else {
1346 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1347 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1348 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1349 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001350 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1351 break;
1352 }
Evan Chengd6f57682008-07-08 20:06:39 +00001353 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001354 break;
1355
Dan Gohmanfa607c92008-07-01 00:05:16 +00001356 case ISD::DBG_LABEL:
1357 case ISD::EH_LABEL:
1358 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1359 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001360 default: assert(0 && "This action is not supported yet!");
1361 case TargetLowering::Legal:
1362 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohmanfa607c92008-07-01 00:05:16 +00001363 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001364 break;
1365 case TargetLowering::Expand:
1366 Result = LegalizeOp(Node->getOperand(0));
1367 break;
1368 }
1369 break;
1370
Evan Chengd1d68072008-03-08 00:58:38 +00001371 case ISD::PREFETCH:
1372 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1373 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1374 default: assert(0 && "This action is not supported yet!");
1375 case TargetLowering::Legal:
1376 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1377 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1378 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1379 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1380 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1381 break;
1382 case TargetLowering::Expand:
1383 // It's a noop.
1384 Result = LegalizeOp(Node->getOperand(0));
1385 break;
1386 }
1387 break;
1388
Andrew Lenharth785610d2008-02-16 01:24:58 +00001389 case ISD::MEMBARRIER: {
1390 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001391 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1392 default: assert(0 && "This action is not supported yet!");
1393 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001394 SDValue Ops[6];
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001395 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands3ee041a2008-02-27 08:53:44 +00001396 for (int x = 1; x < 6; ++x) {
1397 Ops[x] = Node->getOperand(x);
1398 if (!isTypeLegal(Ops[x].getValueType()))
1399 Ops[x] = PromoteOp(Ops[x]);
1400 }
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001401 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1402 break;
1403 }
1404 case TargetLowering::Expand:
1405 //There is no libgcc call for this op
1406 Result = Node->getOperand(0); // Noop
1407 break;
1408 }
Andrew Lenharth785610d2008-02-16 01:24:58 +00001409 break;
1410 }
1411
Dan Gohmanbebba8d2008-12-23 21:37:04 +00001412 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001413 unsigned int num_operands = 4;
1414 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001415 SDValue Ops[4];
Mon P Wang078a62d2008-05-05 19:05:59 +00001416 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001417 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang078a62d2008-05-05 19:05:59 +00001418 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1419
1420 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1421 default: assert(0 && "This action is not supported yet!");
1422 case TargetLowering::Custom:
1423 Result = TLI.LowerOperation(Result, DAG);
1424 break;
1425 case TargetLowering::Legal:
1426 break;
1427 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001428 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1429 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001430 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001431 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00001432 case ISD::ATOMIC_LOAD_ADD:
1433 case ISD::ATOMIC_LOAD_SUB:
1434 case ISD::ATOMIC_LOAD_AND:
1435 case ISD::ATOMIC_LOAD_OR:
1436 case ISD::ATOMIC_LOAD_XOR:
1437 case ISD::ATOMIC_LOAD_NAND:
1438 case ISD::ATOMIC_LOAD_MIN:
1439 case ISD::ATOMIC_LOAD_MAX:
1440 case ISD::ATOMIC_LOAD_UMIN:
1441 case ISD::ATOMIC_LOAD_UMAX:
1442 case ISD::ATOMIC_SWAP: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001443 unsigned int num_operands = 3;
1444 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001445 SDValue Ops[3];
Mon P Wang078a62d2008-05-05 19:05:59 +00001446 for (unsigned int x = 0; x < num_operands; ++x)
1447 Ops[x] = LegalizeOp(Node->getOperand(x));
1448 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sandsac496a12008-07-04 11:47:58 +00001449
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001450 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001451 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001452 case TargetLowering::Custom:
1453 Result = TLI.LowerOperation(Result, DAG);
1454 break;
1455 case TargetLowering::Legal:
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001456 break;
1457 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001458 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1459 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001460 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001461 }
Scott Michelf2e2b702007-08-08 23:23:31 +00001462 case ISD::Constant: {
1463 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1464 unsigned opAction =
1465 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1466
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001467 // We know we don't need to expand constants here, constants only have one
1468 // value and we check that it is fine above.
1469
Scott Michelf2e2b702007-08-08 23:23:31 +00001470 if (opAction == TargetLowering::Custom) {
1471 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001472 if (Tmp1.getNode())
Scott Michelf2e2b702007-08-08 23:23:31 +00001473 Result = Tmp1;
1474 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001475 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001476 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001477 case ISD::ConstantFP: {
1478 // Spill FP immediates to the constant pool if the target cannot directly
1479 // codegen them. Targets often have some immediate values that can be
1480 // efficiently generated into an FP register without a load. We explicitly
1481 // leave these constants as ConstantFP nodes for the target to deal with.
1482 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1483
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001484 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1485 default: assert(0 && "This action is not supported yet!");
Nate Begemane2ba64f2008-02-14 08:57:00 +00001486 case TargetLowering::Legal:
1487 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001488 case TargetLowering::Custom:
1489 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001490 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491 Result = Tmp3;
1492 break;
1493 }
1494 // FALLTHROUGH
Nate Begemane2ba64f2008-02-14 08:57:00 +00001495 case TargetLowering::Expand: {
1496 // Check to see if this FP immediate is already legal.
1497 bool isLegal = false;
1498 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1499 E = TLI.legal_fpimm_end(); I != E; ++I) {
1500 if (CFP->isExactlyValue(*I)) {
1501 isLegal = true;
1502 break;
1503 }
1504 }
1505 // If this is a legal constant, turn it into a TargetConstantFP node.
1506 if (isLegal)
1507 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001508 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1509 }
Nate Begemane2ba64f2008-02-14 08:57:00 +00001510 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001511 break;
1512 }
1513 case ISD::TokenFactor:
1514 if (Node->getNumOperands() == 2) {
1515 Tmp1 = LegalizeOp(Node->getOperand(0));
1516 Tmp2 = LegalizeOp(Node->getOperand(1));
1517 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1518 } else if (Node->getNumOperands() == 3) {
1519 Tmp1 = LegalizeOp(Node->getOperand(0));
1520 Tmp2 = LegalizeOp(Node->getOperand(1));
1521 Tmp3 = LegalizeOp(Node->getOperand(2));
1522 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1523 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00001524 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001525 // Legalize the operands.
1526 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1527 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1528 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1529 }
1530 break;
1531
1532 case ISD::FORMAL_ARGUMENTS:
1533 case ISD::CALL:
1534 // The only option for this is to custom lower it.
1535 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001536 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesenac246272008-03-05 19:14:03 +00001537 // A call within a calling sequence must be legalized to something
1538 // other than the normal CALLSEQ_END. Violating this gets Legalize
1539 // into an infinite loop.
1540 assert ((!IsLegalizingCall ||
1541 Node->getOpcode() != ISD::CALL ||
Gabor Greif1c80d112008-08-28 21:40:38 +00001542 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesenac246272008-03-05 19:14:03 +00001543 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001544
1545 // The number of incoming and outgoing values should match; unless the final
1546 // outgoing value is a flag.
Gabor Greif1c80d112008-08-28 21:40:38 +00001547 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1548 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1549 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling22f8deb2007-11-13 00:44:25 +00001550 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001551 "Lowering call/formal_arguments produced unexpected # results!");
1552
1553 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1554 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greif1c80d112008-08-28 21:40:38 +00001555 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1556 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling22f8deb2007-11-13 00:44:25 +00001557 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001558 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00001559 if (Op.getResNo() == i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001560 Tmp2 = Tmp1;
Dan Gohman8181bd12008-07-27 21:46:04 +00001561 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001562 }
1563 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001564 case ISD::EXTRACT_SUBREG: {
1565 Tmp1 = LegalizeOp(Node->getOperand(0));
1566 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1567 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001568 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001569 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1570 }
1571 break;
1572 case ISD::INSERT_SUBREG: {
1573 Tmp1 = LegalizeOp(Node->getOperand(0));
1574 Tmp2 = LegalizeOp(Node->getOperand(1));
1575 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1576 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001577 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001578 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1579 }
1580 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001581 case ISD::BUILD_VECTOR:
1582 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1583 default: assert(0 && "This action is not supported yet!");
1584 case TargetLowering::Custom:
1585 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001586 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001587 Result = Tmp3;
1588 break;
1589 }
1590 // FALLTHROUGH
1591 case TargetLowering::Expand:
Gabor Greif1c80d112008-08-28 21:40:38 +00001592 Result = ExpandBUILD_VECTOR(Result.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001593 break;
1594 }
1595 break;
1596 case ISD::INSERT_VECTOR_ELT:
1597 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001598 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001599
1600 // The type of the value to insert may not be legal, even though the vector
1601 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1602 // here.
1603 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1604 default: assert(0 && "Cannot expand insert element operand");
1605 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1606 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang1448aad2008-10-30 08:01:45 +00001607 case Expand:
1608 // FIXME: An alternative would be to check to see if the target is not
1609 // going to custom lower this operation, we could bitcast to half elt
1610 // width and perform two inserts at that width, if that is legal.
1611 Tmp2 = Node->getOperand(1);
1612 break;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001613 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001614 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1615
1616 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1617 Node->getValueType(0))) {
1618 default: assert(0 && "This action is not supported yet!");
1619 case TargetLowering::Legal:
1620 break;
1621 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001622 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001623 if (Tmp4.getNode()) {
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001624 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001625 break;
1626 }
1627 // FALLTHROUGH
Mon P Wang1448aad2008-10-30 08:01:45 +00001628 case TargetLowering::Promote:
1629 // Fall thru for vector case
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001630 case TargetLowering::Expand: {
1631 // If the insert index is a constant, codegen this as a scalar_to_vector,
1632 // then a shuffle that inserts it into the right position in the vector.
1633 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001634 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1635 // match the element type of the vector being created.
1636 if (Tmp2.getValueType() ==
Duncan Sands92c43912008-06-06 12:08:01 +00001637 Op.getValueType().getVectorElementType()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001638 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001639 Tmp1.getValueType(), Tmp2);
1640
Duncan Sands92c43912008-06-06 12:08:01 +00001641 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1642 MVT ShufMaskVT =
1643 MVT::getIntVectorWithNumElements(NumElts);
1644 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001645
1646 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1647 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1648 // elt 0 of the RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00001649 SmallVector<SDValue, 8> ShufOps;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001650 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001651 if (i != InsertPos->getZExtValue())
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001652 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1653 else
1654 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1655 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001656 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001657 &ShufOps[0], ShufOps.size());
1658
1659 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1660 Tmp1, ScVec, ShufMask);
1661 Result = LegalizeOp(Result);
1662 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001663 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001664 }
Nate Begeman7c9e4b72008-04-25 18:07:40 +00001665 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001666 break;
1667 }
1668 }
1669 break;
1670 case ISD::SCALAR_TO_VECTOR:
1671 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1672 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1673 break;
1674 }
1675
1676 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1677 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1678 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1679 Node->getValueType(0))) {
1680 default: assert(0 && "This action is not supported yet!");
1681 case TargetLowering::Legal:
1682 break;
1683 case TargetLowering::Custom:
1684 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001685 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001686 Result = Tmp3;
1687 break;
1688 }
1689 // FALLTHROUGH
1690 case TargetLowering::Expand:
1691 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1692 break;
1693 }
1694 break;
1695 case ISD::VECTOR_SHUFFLE:
1696 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1697 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1698 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1699
1700 // Allow targets to custom lower the SHUFFLEs they support.
1701 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1702 default: assert(0 && "Unknown operation action!");
1703 case TargetLowering::Legal:
1704 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1705 "vector shuffle should not be created if not legal!");
1706 break;
1707 case TargetLowering::Custom:
1708 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001709 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001710 Result = Tmp3;
1711 break;
1712 }
1713 // FALLTHROUGH
1714 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00001715 MVT VT = Node->getValueType(0);
1716 MVT EltVT = VT.getVectorElementType();
1717 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00001718 SDValue Mask = Node->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001719 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00001720 SmallVector<SDValue,8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001721 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001722 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001723 if (Arg.getOpcode() == ISD::UNDEF) {
1724 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1725 } else {
1726 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001727 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001728 if (Idx < NumElems)
1729 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1730 DAG.getConstant(Idx, PtrVT)));
1731 else
1732 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1733 DAG.getConstant(Idx - NumElems, PtrVT)));
1734 }
1735 }
1736 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1737 break;
1738 }
1739 case TargetLowering::Promote: {
1740 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00001741 MVT OVT = Node->getValueType(0);
1742 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001743
1744 // Cast the two input vectors.
1745 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1746 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1747
1748 // Convert the shuffle mask to the right # elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00001749 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001750 assert(Tmp3.getNode() && "Shuffle not legal?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001751 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1752 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1753 break;
1754 }
1755 }
1756 break;
1757
1758 case ISD::EXTRACT_VECTOR_ELT:
1759 Tmp1 = Node->getOperand(0);
1760 Tmp2 = LegalizeOp(Node->getOperand(1));
1761 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1762 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1763 break;
1764
1765 case ISD::EXTRACT_SUBVECTOR:
1766 Tmp1 = Node->getOperand(0);
1767 Tmp2 = LegalizeOp(Node->getOperand(1));
1768 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1769 Result = ExpandEXTRACT_SUBVECTOR(Result);
1770 break;
1771
Mon P Wang1448aad2008-10-30 08:01:45 +00001772 case ISD::CONCAT_VECTORS: {
1773 // Use extract/insert/build vector for now. We might try to be
1774 // more clever later.
1775 MVT PtrVT = TLI.getPointerTy();
1776 SmallVector<SDValue, 8> Ops;
1777 unsigned NumOperands = Node->getNumOperands();
1778 for (unsigned i=0; i < NumOperands; ++i) {
1779 SDValue SubOp = Node->getOperand(i);
1780 MVT VVT = SubOp.getNode()->getValueType(0);
1781 MVT EltVT = VVT.getVectorElementType();
1782 unsigned NumSubElem = VVT.getVectorNumElements();
1783 for (unsigned j=0; j < NumSubElem; ++j) {
1784 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1785 DAG.getConstant(j, PtrVT)));
1786 }
1787 }
1788 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1789 &Ops[0], Ops.size()));
1790 }
1791
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001792 case ISD::CALLSEQ_START: {
1793 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1794
1795 // Recursively Legalize all of the inputs of the call end that do not lead
1796 // to this call start. This ensures that any libcalls that need be inserted
1797 // are inserted *before* the CALLSEQ_START.
1798 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1799 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greif1c80d112008-08-28 21:40:38 +00001800 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001801 NodesLeadingTo);
1802 }
1803
1804 // Now that we legalized all of the inputs (which may have inserted
1805 // libcalls) create the new CALLSEQ_START node.
1806 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1807
1808 // Merge in the last call, to ensure that this call start after the last
1809 // call ended.
1810 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1811 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1812 Tmp1 = LegalizeOp(Tmp1);
1813 }
1814
1815 // Do not try to legalize the target-specific arguments (#1+).
1816 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001817 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001818 Ops[0] = Tmp1;
1819 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1820 }
1821
1822 // Remember that the CALLSEQ_START is legalized.
1823 AddLegalizedOperand(Op.getValue(0), Result);
1824 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1825 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1826
1827 // Now that the callseq_start and all of the non-call nodes above this call
1828 // sequence have been legalized, legalize the call itself. During this
1829 // process, no libcalls can/will be inserted, guaranteeing that no calls
1830 // can overlap.
1831 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001832 // Note that we are selecting this call!
Dan Gohman8181bd12008-07-27 21:46:04 +00001833 LastCALLSEQ_END = SDValue(CallEnd, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001834 IsLegalizingCall = true;
1835
1836 // Legalize the call, starting from the CALLSEQ_END.
1837 LegalizeOp(LastCALLSEQ_END);
1838 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1839 return Result;
1840 }
1841 case ISD::CALLSEQ_END:
1842 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1843 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greif1c80d112008-08-28 21:40:38 +00001844 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001845 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1846 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001847 assert(I != LegalizedNodes.end() &&
1848 "Legalizing the call start should have legalized this node!");
1849 return I->second;
1850 }
1851
1852 // Otherwise, the call start has been legalized and everything is going
1853 // according to plan. Just legalize ourselves normally here.
1854 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1855 // Do not try to legalize the target-specific arguments (#1+), except for
1856 // an optional flag input.
1857 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1858 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001859 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001860 Ops[0] = Tmp1;
1861 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1862 }
1863 } else {
1864 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1865 if (Tmp1 != Node->getOperand(0) ||
1866 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001867 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001868 Ops[0] = Tmp1;
1869 Ops.back() = Tmp2;
1870 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1871 }
1872 }
1873 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1874 // This finishes up call legalization.
1875 IsLegalizingCall = false;
1876
1877 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001878 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001879 if (Node->getNumValues() == 2)
Dan Gohman8181bd12008-07-27 21:46:04 +00001880 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001881 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001882 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands92c43912008-06-06 12:08:01 +00001883 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001884 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1885 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1886 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1887 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1888
1889 Tmp1 = Result.getValue(0);
1890 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001891 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001892 default: assert(0 && "This action is not supported yet!");
1893 case TargetLowering::Expand: {
1894 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1895 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1896 " not tell us which reg is the stack pointer!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001897 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001898
1899 // Chain the dynamic stack allocation so that it doesn't modify the stack
1900 // pointer when other instructions are using the stack.
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001901 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling22f8deb2007-11-13 00:44:25 +00001902
Dan Gohman8181bd12008-07-27 21:46:04 +00001903 SDValue Size = Tmp2.getOperand(1);
1904 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Chenga448bc42007-08-16 23:50:06 +00001905 Chain = SP.getValue(1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001906 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Chenga448bc42007-08-16 23:50:06 +00001907 unsigned StackAlign =
1908 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1909 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001910 SP = DAG.getNode(ISD::AND, VT, SP,
1911 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001912 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling22f8deb2007-11-13 00:44:25 +00001913 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1914
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001915 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1916 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling22f8deb2007-11-13 00:44:25 +00001917
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001918 Tmp1 = LegalizeOp(Tmp1);
1919 Tmp2 = LegalizeOp(Tmp2);
1920 break;
1921 }
1922 case TargetLowering::Custom:
1923 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001924 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001925 Tmp1 = LegalizeOp(Tmp3);
1926 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1927 }
1928 break;
1929 case TargetLowering::Legal:
1930 break;
1931 }
1932 // Since this op produce two values, make sure to remember that we
1933 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001934 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1935 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001936 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001937 }
1938 case ISD::INLINEASM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001939 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001940 bool Changed = false;
1941 // Legalize all of the operands of the inline asm, in case they are nodes
1942 // that need to be expanded or something. Note we skip the asm string and
1943 // all of the TargetConstant flags.
Dan Gohman8181bd12008-07-27 21:46:04 +00001944 SDValue Op = LegalizeOp(Ops[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001945 Changed = Op != Ops[0];
1946 Ops[0] = Op;
1947
1948 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1949 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001950 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001951 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001952 SDValue Op = LegalizeOp(Ops[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001953 if (Op != Ops[i]) {
1954 Changed = true;
1955 Ops[i] = Op;
1956 }
1957 }
1958 }
1959
1960 if (HasInFlag) {
1961 Op = LegalizeOp(Ops.back());
1962 Changed |= Op != Ops.back();
1963 Ops.back() = Op;
1964 }
1965
1966 if (Changed)
1967 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1968
1969 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman8181bd12008-07-27 21:46:04 +00001970 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1971 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001972 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001973 }
1974 case ISD::BR:
1975 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1976 // Ensure that libcalls are emitted before a branch.
1977 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1978 Tmp1 = LegalizeOp(Tmp1);
1979 LastCALLSEQ_END = DAG.getEntryNode();
1980
1981 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1982 break;
1983 case ISD::BRIND:
1984 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1985 // Ensure that libcalls are emitted before a branch.
1986 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1987 Tmp1 = LegalizeOp(Tmp1);
1988 LastCALLSEQ_END = DAG.getEntryNode();
1989
1990 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1991 default: assert(0 && "Indirect target must be legal type (pointer)!");
1992 case Legal:
1993 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1994 break;
1995 }
1996 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1997 break;
1998 case ISD::BR_JT:
1999 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2000 // Ensure that libcalls are emitted before a branch.
2001 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2002 Tmp1 = LegalizeOp(Tmp1);
2003 LastCALLSEQ_END = DAG.getEntryNode();
2004
2005 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
2006 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2007
2008 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
2009 default: assert(0 && "This action is not supported yet!");
2010 case TargetLowering::Legal: break;
2011 case TargetLowering::Custom:
2012 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002013 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002014 break;
2015 case TargetLowering::Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002016 SDValue Chain = Result.getOperand(0);
2017 SDValue Table = Result.getOperand(1);
2018 SDValue Index = Result.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002019
Duncan Sands92c43912008-06-06 12:08:01 +00002020 MVT PTy = TLI.getPointerTy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002021 MachineFunction &MF = DAG.getMachineFunction();
2022 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
2023 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman8181bd12008-07-27 21:46:04 +00002024 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002025
Duncan Sands12ddc802008-12-12 08:13:38 +00002026 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
2027 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, Chain, Addr,
2028 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Cheng6fb06762007-11-09 01:32:10 +00002029 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002030 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2031 // For PIC, the sequence is:
2032 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00002033 // RelocBase can be JumpTable, GOT or some sort of global base.
Evan Cheng6fb06762007-11-09 01:32:10 +00002034 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
2035 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002036 }
Evan Cheng6fb06762007-11-09 01:32:10 +00002037 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002038 }
2039 }
2040 break;
2041 case ISD::BRCOND:
2042 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2043 // Ensure that libcalls are emitted before a return.
2044 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2045 Tmp1 = LegalizeOp(Tmp1);
2046 LastCALLSEQ_END = DAG.getEntryNode();
2047
2048 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2049 case Expand: assert(0 && "It's impossible to expand bools");
2050 case Legal:
2051 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2052 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002053 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002054 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
2055
2056 // The top bits of the promoted condition are not necessarily zero, ensure
2057 // that the value is properly zero extended.
Dan Gohman07961cd2008-02-25 21:11:39 +00002058 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002059 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman07961cd2008-02-25 21:11:39 +00002060 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002061 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
2062 break;
2063 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002064 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002065
2066 // Basic block destination (Op#2) is always legal.
2067 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2068
2069 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
2070 default: assert(0 && "This action is not supported yet!");
2071 case TargetLowering::Legal: break;
2072 case TargetLowering::Custom:
2073 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002074 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002075 break;
2076 case TargetLowering::Expand:
2077 // Expand brcond's setcc into its constituent parts and create a BR_CC
2078 // Node.
2079 if (Tmp2.getOpcode() == ISD::SETCC) {
2080 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
2081 Tmp2.getOperand(0), Tmp2.getOperand(1),
2082 Node->getOperand(2));
2083 } else {
2084 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
2085 DAG.getCondCode(ISD::SETNE), Tmp2,
2086 DAG.getConstant(0, Tmp2.getValueType()),
2087 Node->getOperand(2));
2088 }
2089 break;
2090 }
2091 break;
2092 case ISD::BR_CC:
2093 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2094 // Ensure that libcalls are emitted before a branch.
2095 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2096 Tmp1 = LegalizeOp(Tmp1);
2097 Tmp2 = Node->getOperand(2); // LHS
2098 Tmp3 = Node->getOperand(3); // RHS
2099 Tmp4 = Node->getOperand(1); // CC
2100
Duncan Sands4a361272009-01-01 15:52:00 +00002101 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()), Tmp2, Tmp3,Tmp4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002102 LastCALLSEQ_END = DAG.getEntryNode();
2103
Evan Cheng71343822008-10-15 02:05:31 +00002104 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002105 // the LHS is a legal SETCC itself. In this case, we need to compare
2106 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00002107 if (Tmp3.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002108 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2109 Tmp4 = DAG.getCondCode(ISD::SETNE);
2110 }
2111
2112 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2113 Node->getOperand(4));
2114
2115 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2116 default: assert(0 && "Unexpected action for BR_CC!");
2117 case TargetLowering::Legal: break;
2118 case TargetLowering::Custom:
2119 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002120 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002121 break;
2122 }
2123 break;
2124 case ISD::LOAD: {
2125 LoadSDNode *LD = cast<LoadSDNode>(Node);
2126 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2127 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
2128
2129 ISD::LoadExtType ExtType = LD->getExtensionType();
2130 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands92c43912008-06-06 12:08:01 +00002131 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002132 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2133 Tmp3 = Result.getValue(0);
2134 Tmp4 = Result.getValue(1);
2135
2136 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2137 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002138 case TargetLowering::Legal:
2139 // If this is an unaligned load and the target doesn't support it,
2140 // expand it.
2141 if (!TLI.allowsUnalignedMemoryAccesses()) {
2142 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002143 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002144 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002145 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002146 TLI);
2147 Tmp3 = Result.getOperand(0);
2148 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00002149 Tmp3 = LegalizeOp(Tmp3);
2150 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002151 }
2152 }
2153 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002154 case TargetLowering::Custom:
2155 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002156 if (Tmp1.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002157 Tmp3 = LegalizeOp(Tmp1);
2158 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2159 }
2160 break;
2161 case TargetLowering::Promote: {
2162 // Only promote a load of vector type to another.
Duncan Sands92c43912008-06-06 12:08:01 +00002163 assert(VT.isVector() && "Cannot promote this load!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002164 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002165 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002166
2167 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
2168 LD->getSrcValueOffset(),
2169 LD->isVolatile(), LD->getAlignment());
2170 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2171 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2172 break;
2173 }
2174 }
2175 // Since loads produce two values, make sure to remember that we
2176 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002177 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2178 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif46bf5472008-08-26 22:36:50 +00002179 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002180 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00002181 MVT SrcVT = LD->getMemoryVT();
2182 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sands082524c2008-01-23 20:39:46 +00002183 int SVOffset = LD->getSrcValueOffset();
2184 unsigned Alignment = LD->getAlignment();
2185 bool isVolatile = LD->isVolatile();
2186
Duncan Sands92c43912008-06-06 12:08:01 +00002187 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002188 // Some targets pretend to have an i1 loading operation, and actually
2189 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2190 // bits are guaranteed to be zero; it helps the optimizers understand
2191 // that these bits are zero. It is also useful for EXTLOAD, since it
2192 // tells the optimizers that those bits are undefined. It would be
2193 // nice to have an effective generic way of getting these benefits...
2194 // Until such a way is found, don't insist on promoting i1 here.
2195 (SrcVT != MVT::i1 ||
Evan Cheng08c171a2008-10-14 21:26:46 +00002196 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002197 // Promote to a byte-sized load if not loading an integral number of
2198 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands92c43912008-06-06 12:08:01 +00002199 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2200 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002201 SDValue Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002202
2203 // The extra bits are guaranteed to be zero, since we stored them that
2204 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2205
2206 ISD::LoadExtType NewExtType =
2207 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2208
2209 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2210 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2211 NVT, isVolatile, Alignment);
2212
2213 Ch = Result.getValue(1); // The chain.
2214
2215 if (ExtType == ISD::SEXTLOAD)
2216 // Having the top bits zero doesn't help when sign extending.
2217 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2218 Result, DAG.getValueType(SrcVT));
2219 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2220 // All the top bits are guaranteed to be zero - inform the optimizers.
2221 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2222 DAG.getValueType(SrcVT));
2223
2224 Tmp1 = LegalizeOp(Result);
2225 Tmp2 = LegalizeOp(Ch);
2226 } else if (SrcWidth & (SrcWidth - 1)) {
2227 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands92c43912008-06-06 12:08:01 +00002228 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002229 "Unsupported extload!");
2230 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2231 assert(RoundWidth < SrcWidth);
2232 unsigned ExtraWidth = SrcWidth - RoundWidth;
2233 assert(ExtraWidth < RoundWidth);
2234 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2235 "Load size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002236 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2237 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002238 SDValue Lo, Hi, Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002239 unsigned IncrementSize;
2240
2241 if (TLI.isLittleEndian()) {
2242 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2243 // Load the bottom RoundWidth bits.
2244 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2245 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2246 Alignment);
2247
2248 // Load the remaining ExtraWidth bits.
2249 IncrementSize = RoundWidth / 8;
2250 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2251 DAG.getIntPtrConstant(IncrementSize));
2252 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2253 LD->getSrcValue(), SVOffset + IncrementSize,
2254 ExtraVT, isVolatile,
2255 MinAlign(Alignment, IncrementSize));
2256
2257 // Build a factor node to remember that this load is independent of the
2258 // other one.
2259 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2260 Hi.getValue(1));
2261
2262 // Move the top bits to the right place.
2263 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2264 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2265
2266 // Join the hi and lo parts.
2267 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002268 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00002269 // Big endian - avoid unaligned loads.
2270 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2271 // Load the top RoundWidth bits.
2272 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2273 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2274 Alignment);
2275
2276 // Load the remaining ExtraWidth bits.
2277 IncrementSize = RoundWidth / 8;
2278 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2279 DAG.getIntPtrConstant(IncrementSize));
2280 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2281 LD->getSrcValue(), SVOffset + IncrementSize,
2282 ExtraVT, isVolatile,
2283 MinAlign(Alignment, IncrementSize));
2284
2285 // Build a factor node to remember that this load is independent of the
2286 // other one.
2287 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2288 Hi.getValue(1));
2289
2290 // Move the top bits to the right place.
2291 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2292 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2293
2294 // Join the hi and lo parts.
2295 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2296 }
2297
2298 Tmp1 = LegalizeOp(Result);
2299 Tmp2 = LegalizeOp(Ch);
2300 } else {
Evan Cheng08c171a2008-10-14 21:26:46 +00002301 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002302 default: assert(0 && "This action is not supported yet!");
2303 case TargetLowering::Custom:
2304 isCustom = true;
2305 // FALLTHROUGH
2306 case TargetLowering::Legal:
2307 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2308 Tmp1 = Result.getValue(0);
2309 Tmp2 = Result.getValue(1);
2310
2311 if (isCustom) {
2312 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002313 if (Tmp3.getNode()) {
Duncan Sands082524c2008-01-23 20:39:46 +00002314 Tmp1 = LegalizeOp(Tmp3);
2315 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2316 }
2317 } else {
2318 // If this is an unaligned load and the target doesn't support it,
2319 // expand it.
2320 if (!TLI.allowsUnalignedMemoryAccesses()) {
2321 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002322 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sands082524c2008-01-23 20:39:46 +00002323 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002324 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sands082524c2008-01-23 20:39:46 +00002325 TLI);
2326 Tmp1 = Result.getOperand(0);
2327 Tmp2 = Result.getOperand(1);
2328 Tmp1 = LegalizeOp(Tmp1);
2329 Tmp2 = LegalizeOp(Tmp2);
2330 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002331 }
2332 }
Duncan Sands082524c2008-01-23 20:39:46 +00002333 break;
2334 case TargetLowering::Expand:
2335 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2336 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002337 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sands082524c2008-01-23 20:39:46 +00002338 LD->getSrcValueOffset(),
2339 LD->isVolatile(), LD->getAlignment());
2340 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2341 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2342 Tmp2 = LegalizeOp(Load.getValue(1));
2343 break;
2344 }
2345 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2346 // Turn the unsupported load into an EXTLOAD followed by an explicit
2347 // zero/sign extend inreg.
2348 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2349 Tmp1, Tmp2, LD->getSrcValue(),
2350 LD->getSrcValueOffset(), SrcVT,
2351 LD->isVolatile(), LD->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00002352 SDValue ValRes;
Duncan Sands082524c2008-01-23 20:39:46 +00002353 if (ExtType == ISD::SEXTLOAD)
2354 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2355 Result, DAG.getValueType(SrcVT));
2356 else
2357 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2358 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2359 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002360 break;
2361 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002362 }
Duncan Sands082524c2008-01-23 20:39:46 +00002363
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002364 // Since loads produce two values, make sure to remember that we legalized
2365 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002366 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2367 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002368 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002369 }
2370 }
2371 case ISD::EXTRACT_ELEMENT: {
Duncan Sands92c43912008-06-06 12:08:01 +00002372 MVT OpTy = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002373 switch (getTypeAction(OpTy)) {
2374 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2375 case Legal:
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002376 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002377 // 1 -> Hi
2378 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands92c43912008-06-06 12:08:01 +00002379 DAG.getConstant(OpTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002380 TLI.getShiftAmountTy()));
2381 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2382 } else {
2383 // 0 -> Lo
2384 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2385 Node->getOperand(0));
2386 }
2387 break;
2388 case Expand:
2389 // Get both the low and high parts.
2390 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002391 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002392 Result = Tmp2; // 1 -> Hi
2393 else
2394 Result = Tmp1; // 0 -> Lo
2395 break;
2396 }
2397 break;
2398 }
2399
2400 case ISD::CopyToReg:
2401 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2402
2403 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2404 "Register type must be legal!");
2405 // Legalize the incoming value (must be a legal type).
2406 Tmp2 = LegalizeOp(Node->getOperand(2));
2407 if (Node->getNumValues() == 1) {
2408 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2409 } else {
2410 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2411 if (Node->getNumOperands() == 4) {
2412 Tmp3 = LegalizeOp(Node->getOperand(3));
2413 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2414 Tmp3);
2415 } else {
2416 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2417 }
2418
2419 // Since this produces two values, make sure to remember that we legalized
2420 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002421 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2422 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002423 return Result;
2424 }
2425 break;
2426
2427 case ISD::RET:
2428 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2429
2430 // Ensure that libcalls are emitted before a return.
2431 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2432 Tmp1 = LegalizeOp(Tmp1);
2433 LastCALLSEQ_END = DAG.getEntryNode();
2434
2435 switch (Node->getNumOperands()) {
2436 case 3: // ret val
2437 Tmp2 = Node->getOperand(1);
2438 Tmp3 = Node->getOperand(2); // Signness
2439 switch (getTypeAction(Tmp2.getValueType())) {
2440 case Legal:
2441 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2442 break;
2443 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00002444 if (!Tmp2.getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002445 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002446 ExpandOp(Tmp2, Lo, Hi);
2447
2448 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002449 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002450 std::swap(Lo, Hi);
2451
Gabor Greif1c80d112008-08-28 21:40:38 +00002452 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002453 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2454 else
2455 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2456 Result = LegalizeOp(Result);
2457 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +00002458 SDNode *InVal = Tmp2.getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002459 int InIx = Tmp2.getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002460 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2461 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002462
2463 // Figure out if there is a simple type corresponding to this Vector
2464 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002465 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002466 if (TLI.isTypeLegal(TVT)) {
2467 // Turn this into a return of the vector type.
2468 Tmp2 = LegalizeOp(Tmp2);
2469 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2470 } else if (NumElems == 1) {
2471 // Turn this into a return of the scalar type.
2472 Tmp2 = ScalarizeVectorOp(Tmp2);
2473 Tmp2 = LegalizeOp(Tmp2);
2474 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2475
2476 // FIXME: Returns of gcc generic vectors smaller than a legal type
2477 // should be returned in integer registers!
2478
2479 // The scalarized value type may not be legal, e.g. it might require
2480 // promotion or expansion. Relegalize the return.
2481 Result = LegalizeOp(Result);
2482 } else {
2483 // FIXME: Returns of gcc generic vectors larger than a legal vector
2484 // type should be returned by reference!
Dan Gohman8181bd12008-07-27 21:46:04 +00002485 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002486 SplitVectorOp(Tmp2, Lo, Hi);
2487 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2488 Result = LegalizeOp(Result);
2489 }
2490 }
2491 break;
2492 case Promote:
2493 Tmp2 = PromoteOp(Node->getOperand(1));
2494 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2495 Result = LegalizeOp(Result);
2496 break;
2497 }
2498 break;
2499 case 1: // ret void
2500 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2501 break;
2502 default: { // ret <values>
Dan Gohman8181bd12008-07-27 21:46:04 +00002503 SmallVector<SDValue, 8> NewValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002504 NewValues.push_back(Tmp1);
2505 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2506 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2507 case Legal:
2508 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2509 NewValues.push_back(Node->getOperand(i+1));
2510 break;
2511 case Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002512 SDValue Lo, Hi;
Duncan Sands92c43912008-06-06 12:08:01 +00002513 assert(!Node->getOperand(i).getValueType().isExtended() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002514 "FIXME: TODO: implement returning non-legal vector types!");
2515 ExpandOp(Node->getOperand(i), Lo, Hi);
2516 NewValues.push_back(Lo);
2517 NewValues.push_back(Node->getOperand(i+1));
Gabor Greif1c80d112008-08-28 21:40:38 +00002518 if (Hi.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002519 NewValues.push_back(Hi);
2520 NewValues.push_back(Node->getOperand(i+1));
2521 }
2522 break;
2523 }
2524 case Promote:
2525 assert(0 && "Can't promote multiple return value yet!");
2526 }
2527
2528 if (NewValues.size() == Node->getNumOperands())
2529 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2530 else
2531 Result = DAG.getNode(ISD::RET, MVT::Other,
2532 &NewValues[0], NewValues.size());
2533 break;
2534 }
2535 }
2536
2537 if (Result.getOpcode() == ISD::RET) {
2538 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2539 default: assert(0 && "This action is not supported yet!");
2540 case TargetLowering::Legal: break;
2541 case TargetLowering::Custom:
2542 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002543 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002544 break;
2545 }
2546 }
2547 break;
2548 case ISD::STORE: {
2549 StoreSDNode *ST = cast<StoreSDNode>(Node);
2550 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2551 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2552 int SVOffset = ST->getSrcValueOffset();
2553 unsigned Alignment = ST->getAlignment();
2554 bool isVolatile = ST->isVolatile();
2555
2556 if (!ST->isTruncatingStore()) {
2557 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2558 // FIXME: We shouldn't do this for TargetConstantFP's.
2559 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2560 // to phase ordering between legalized code and the dag combiner. This
2561 // probably means that we need to integrate dag combiner and legalizer
2562 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002563 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002564 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002565 if (CFP->getValueType(0) == MVT::f32 &&
2566 getTypeAction(MVT::i32) == Legal) {
Dan Gohman39509762008-03-11 00:11:06 +00002567 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002568 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen1616e902007-09-11 18:32:33 +00002569 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002570 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2571 SVOffset, isVolatile, Alignment);
2572 break;
2573 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002574 // If this target supports 64-bit registers, do a single 64-bit store.
2575 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002576 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman39509762008-03-11 00:11:06 +00002577 zextOrTrunc(64), MVT::i64);
Chris Lattner19f229a2007-10-15 05:46:06 +00002578 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2579 SVOffset, isVolatile, Alignment);
2580 break;
Duncan Sands2418bec2008-06-13 19:07:40 +00002581 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002582 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2583 // stores. If the target supports neither 32- nor 64-bits, this
2584 // xform is certainly not worth it.
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002585 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman8181bd12008-07-27 21:46:04 +00002586 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2587 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002588 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002589
2590 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2591 SVOffset, isVolatile, Alignment);
2592 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002593 DAG.getIntPtrConstant(4));
Chris Lattner19f229a2007-10-15 05:46:06 +00002594 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002595 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002596
2597 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2598 break;
2599 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002600 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002601 }
2602
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002603 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002604 case Legal: {
2605 Tmp3 = LegalizeOp(ST->getValue());
2606 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2607 ST->getOffset());
2608
Duncan Sands92c43912008-06-06 12:08:01 +00002609 MVT VT = Tmp3.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002610 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2611 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002612 case TargetLowering::Legal:
2613 // If this is an unaligned store and the target doesn't support it,
2614 // expand it.
2615 if (!TLI.allowsUnalignedMemoryAccesses()) {
2616 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002617 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002618 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002619 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002620 TLI);
2621 }
2622 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002623 case TargetLowering::Custom:
2624 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002625 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002626 break;
2627 case TargetLowering::Promote:
Duncan Sands92c43912008-06-06 12:08:01 +00002628 assert(VT.isVector() && "Unknown legal promote case!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002629 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2630 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2631 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2632 ST->getSrcValue(), SVOffset, isVolatile,
2633 Alignment);
2634 break;
2635 }
2636 break;
2637 }
2638 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002639 if (!ST->getMemoryVT().isVector()) {
2640 // Truncate the value and store the result.
2641 Tmp3 = PromoteOp(ST->getValue());
2642 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2643 SVOffset, ST->getMemoryVT(),
2644 isVolatile, Alignment);
2645 break;
2646 }
2647 // Fall thru to expand for vector
2648 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002649 unsigned IncrementSize = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002650 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002651
2652 // If this is a vector type, then we have to calculate the increment as
2653 // the product of the element size in bytes, and the number of elements
2654 // in the high half of the vector.
Duncan Sands92c43912008-06-06 12:08:01 +00002655 if (ST->getValue().getValueType().isVector()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002656 SDNode *InVal = ST->getValue().getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002657 int InIx = ST->getValue().getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002658 MVT InVT = InVal->getValueType(InIx);
2659 unsigned NumElems = InVT.getVectorNumElements();
2660 MVT EVT = InVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002661
2662 // Figure out if there is a simple type corresponding to this Vector
2663 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002664 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665 if (TLI.isTypeLegal(TVT)) {
2666 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002667 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002668 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2669 SVOffset, isVolatile, Alignment);
2670 Result = LegalizeOp(Result);
2671 break;
2672 } else if (NumElems == 1) {
2673 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002674 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002675 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2676 SVOffset, isVolatile, Alignment);
2677 // The scalarized value type may not be legal, e.g. it might require
2678 // promotion or expansion. Relegalize the scalar store.
2679 Result = LegalizeOp(Result);
2680 break;
2681 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +00002682 // Check if we have widen this node with another value
2683 std::map<SDValue, SDValue>::iterator I =
2684 WidenNodes.find(ST->getValue());
2685 if (I != WidenNodes.end()) {
2686 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2687 break;
2688 }
2689 else {
2690 SplitVectorOp(ST->getValue(), Lo, Hi);
2691 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2692 EVT.getSizeInBits()/8;
2693 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002694 }
2695 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002696 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greif1c80d112008-08-28 21:40:38 +00002697 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002698
Richard Pennington73ae9e42008-09-25 16:15:10 +00002699 if (Hi.getNode() && TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002700 std::swap(Lo, Hi);
2701 }
2702
2703 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2704 SVOffset, isVolatile, Alignment);
2705
Gabor Greif1c80d112008-08-28 21:40:38 +00002706 if (Hi.getNode() == NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002707 // Must be int <-> float one-to-one expansion.
2708 Result = Lo;
2709 break;
2710 }
2711
2712 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002713 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002714 assert(isTypeLegal(Tmp2.getValueType()) &&
2715 "Pointers must be legal!");
2716 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002717 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002718 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2719 SVOffset, isVolatile, Alignment);
2720 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2721 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00002722 } // case Expand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002723 }
2724 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002725 switch (getTypeAction(ST->getValue().getValueType())) {
2726 case Legal:
2727 Tmp3 = LegalizeOp(ST->getValue());
2728 break;
2729 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002730 if (!ST->getValue().getValueType().isVector()) {
2731 // We can promote the value, the truncstore will still take care of it.
2732 Tmp3 = PromoteOp(ST->getValue());
2733 break;
2734 }
2735 // Vector case falls through to expand
Chris Lattner3bc08502008-01-17 19:59:44 +00002736 case Expand:
2737 // Just store the low part. This may become a non-trunc store, so make
2738 // sure to use getTruncStore, not UpdateNodeOperands below.
2739 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2740 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2741 SVOffset, MVT::i8, isVolatile, Alignment);
2742 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002743
Duncan Sands92c43912008-06-06 12:08:01 +00002744 MVT StVT = ST->getMemoryVT();
2745 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands40676662008-01-22 07:17:34 +00002746
Duncan Sands92c43912008-06-06 12:08:01 +00002747 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands40676662008-01-22 07:17:34 +00002748 // Promote to a byte-sized store with upper bits zero if not
2749 // storing an integral number of bytes. For example, promote
2750 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands92c43912008-06-06 12:08:01 +00002751 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands40676662008-01-22 07:17:34 +00002752 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2753 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2754 SVOffset, NVT, isVolatile, Alignment);
2755 } else if (StWidth & (StWidth - 1)) {
2756 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands92c43912008-06-06 12:08:01 +00002757 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands40676662008-01-22 07:17:34 +00002758 "Unsupported truncstore!");
2759 unsigned RoundWidth = 1 << Log2_32(StWidth);
2760 assert(RoundWidth < StWidth);
2761 unsigned ExtraWidth = StWidth - RoundWidth;
2762 assert(ExtraWidth < RoundWidth);
2763 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2764 "Store size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002765 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2766 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002767 SDValue Lo, Hi;
Duncan Sands40676662008-01-22 07:17:34 +00002768 unsigned IncrementSize;
2769
2770 if (TLI.isLittleEndian()) {
2771 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2772 // Store the bottom RoundWidth bits.
2773 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2774 SVOffset, RoundVT,
2775 isVolatile, Alignment);
2776
2777 // Store the remaining ExtraWidth bits.
2778 IncrementSize = RoundWidth / 8;
2779 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2780 DAG.getIntPtrConstant(IncrementSize));
2781 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2782 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2783 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2784 SVOffset + IncrementSize, ExtraVT, isVolatile,
2785 MinAlign(Alignment, IncrementSize));
2786 } else {
2787 // Big endian - avoid unaligned stores.
2788 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2789 // Store the top RoundWidth bits.
2790 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2791 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2792 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2793 RoundVT, isVolatile, Alignment);
2794
2795 // Store the remaining ExtraWidth bits.
2796 IncrementSize = RoundWidth / 8;
2797 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2798 DAG.getIntPtrConstant(IncrementSize));
2799 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2800 SVOffset + IncrementSize, ExtraVT, isVolatile,
2801 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002802 }
Duncan Sands40676662008-01-22 07:17:34 +00002803
2804 // The order of the stores doesn't matter.
2805 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2806 } else {
2807 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2808 Tmp2 != ST->getBasePtr())
2809 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2810 ST->getOffset());
2811
2812 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2813 default: assert(0 && "This action is not supported yet!");
2814 case TargetLowering::Legal:
2815 // If this is an unaligned store and the target doesn't support it,
2816 // expand it.
2817 if (!TLI.allowsUnalignedMemoryAccesses()) {
2818 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002819 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands40676662008-01-22 07:17:34 +00002820 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002821 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands40676662008-01-22 07:17:34 +00002822 TLI);
2823 }
2824 break;
2825 case TargetLowering::Custom:
2826 Result = TLI.LowerOperation(Result, DAG);
2827 break;
2828 case Expand:
2829 // TRUNCSTORE:i16 i32 -> STORE i16
2830 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2831 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2832 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2833 isVolatile, Alignment);
2834 break;
2835 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002836 }
2837 }
2838 break;
2839 }
2840 case ISD::PCMARKER:
2841 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2842 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2843 break;
2844 case ISD::STACKSAVE:
2845 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2846 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2847 Tmp1 = Result.getValue(0);
2848 Tmp2 = Result.getValue(1);
2849
2850 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2851 default: assert(0 && "This action is not supported yet!");
2852 case TargetLowering::Legal: break;
2853 case TargetLowering::Custom:
2854 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002855 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002856 Tmp1 = LegalizeOp(Tmp3);
2857 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2858 }
2859 break;
2860 case TargetLowering::Expand:
2861 // Expand to CopyFromReg if the target set
2862 // StackPointerRegisterToSaveRestore.
2863 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2864 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2865 Node->getValueType(0));
2866 Tmp2 = Tmp1.getValue(1);
2867 } else {
2868 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2869 Tmp2 = Node->getOperand(0);
2870 }
2871 break;
2872 }
2873
2874 // Since stacksave produce two values, make sure to remember that we
2875 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002876 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2877 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002878 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002879
2880 case ISD::STACKRESTORE:
2881 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2882 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2883 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2884
2885 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2886 default: assert(0 && "This action is not supported yet!");
2887 case TargetLowering::Legal: break;
2888 case TargetLowering::Custom:
2889 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002890 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002891 break;
2892 case TargetLowering::Expand:
2893 // Expand to CopyToReg if the target set
2894 // StackPointerRegisterToSaveRestore.
2895 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2896 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2897 } else {
2898 Result = Tmp1;
2899 }
2900 break;
2901 }
2902 break;
2903
2904 case ISD::READCYCLECOUNTER:
2905 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2906 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2907 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2908 Node->getValueType(0))) {
2909 default: assert(0 && "This action is not supported yet!");
2910 case TargetLowering::Legal:
2911 Tmp1 = Result.getValue(0);
2912 Tmp2 = Result.getValue(1);
2913 break;
2914 case TargetLowering::Custom:
2915 Result = TLI.LowerOperation(Result, DAG);
2916 Tmp1 = LegalizeOp(Result.getValue(0));
2917 Tmp2 = LegalizeOp(Result.getValue(1));
2918 break;
2919 }
2920
2921 // Since rdcc produce two values, make sure to remember that we legalized
2922 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002923 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2924 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002925 return Result;
2926
2927 case ISD::SELECT:
2928 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2929 case Expand: assert(0 && "It's impossible to expand bools");
2930 case Legal:
2931 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2932 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002933 case Promote: {
Mon P Wang1448aad2008-10-30 08:01:45 +00002934 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002935 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2936 // Make sure the condition is either zero or one.
Dan Gohman07961cd2008-02-25 21:11:39 +00002937 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002938 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman07961cd2008-02-25 21:11:39 +00002939 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002940 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2941 break;
2942 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002943 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002944 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2945 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2946
2947 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2948
2949 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2950 default: assert(0 && "This action is not supported yet!");
2951 case TargetLowering::Legal: break;
2952 case TargetLowering::Custom: {
2953 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002954 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002955 break;
2956 }
2957 case TargetLowering::Expand:
2958 if (Tmp1.getOpcode() == ISD::SETCC) {
2959 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2960 Tmp2, Tmp3,
2961 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2962 } else {
2963 Result = DAG.getSelectCC(Tmp1,
2964 DAG.getConstant(0, Tmp1.getValueType()),
2965 Tmp2, Tmp3, ISD::SETNE);
2966 }
2967 break;
2968 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00002969 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002970 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2971 unsigned ExtOp, TruncOp;
Duncan Sands92c43912008-06-06 12:08:01 +00002972 if (Tmp2.getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002973 ExtOp = ISD::BIT_CONVERT;
2974 TruncOp = ISD::BIT_CONVERT;
Duncan Sands92c43912008-06-06 12:08:01 +00002975 } else if (Tmp2.getValueType().isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002976 ExtOp = ISD::ANY_EXTEND;
2977 TruncOp = ISD::TRUNCATE;
2978 } else {
2979 ExtOp = ISD::FP_EXTEND;
2980 TruncOp = ISD::FP_ROUND;
2981 }
2982 // Promote each of the values to the new type.
2983 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2984 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2985 // Perform the larger operation, then round down.
2986 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00002987 if (TruncOp != ISD::FP_ROUND)
2988 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2989 else
2990 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2991 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002992 break;
2993 }
2994 }
2995 break;
2996 case ISD::SELECT_CC: {
2997 Tmp1 = Node->getOperand(0); // LHS
2998 Tmp2 = Node->getOperand(1); // RHS
2999 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
3000 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman8181bd12008-07-27 21:46:04 +00003001 SDValue CC = Node->getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003002
Duncan Sands4a361272009-01-01 15:52:00 +00003003 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003004
Evan Cheng71343822008-10-15 02:05:31 +00003005 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003006 // the LHS is a legal SETCC itself. In this case, we need to compare
3007 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00003008 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003009 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3010 CC = DAG.getCondCode(ISD::SETNE);
3011 }
3012 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
3013
3014 // Everything is legal, see if we should expand this op or something.
3015 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
3016 default: assert(0 && "This action is not supported yet!");
3017 case TargetLowering::Legal: break;
3018 case TargetLowering::Custom:
3019 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003020 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003021 break;
3022 }
3023 break;
3024 }
3025 case ISD::SETCC:
3026 Tmp1 = Node->getOperand(0);
3027 Tmp2 = Node->getOperand(1);
3028 Tmp3 = Node->getOperand(2);
Evan Cheng71343822008-10-15 02:05:31 +00003029 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003030
3031 // If we had to Expand the SetCC operands into a SELECT node, then it may
3032 // not always be possible to return a true LHS & RHS. In this case, just
3033 // return the value we legalized, returned in the LHS
Gabor Greif1c80d112008-08-28 21:40:38 +00003034 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003035 Result = Tmp1;
3036 break;
3037 }
3038
3039 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
3040 default: assert(0 && "Cannot handle this action for SETCC yet!");
3041 case TargetLowering::Custom:
3042 isCustom = true;
3043 // FALLTHROUGH.
3044 case TargetLowering::Legal:
3045 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3046 if (isCustom) {
3047 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003048 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003049 }
3050 break;
3051 case TargetLowering::Promote: {
3052 // First step, figure out the appropriate operation to use.
3053 // Allow SETCC to not be supported for all legal data types
3054 // Mostly this targets FP
Duncan Sands92c43912008-06-06 12:08:01 +00003055 MVT NewInTy = Node->getOperand(0).getValueType();
3056 MVT OldVT = NewInTy; OldVT = OldVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003057
3058 // Scan for the appropriate larger type to use.
3059 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00003060 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003061
Duncan Sands92c43912008-06-06 12:08:01 +00003062 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003063 "Fell off of the edge of the integer world");
Duncan Sands92c43912008-06-06 12:08:01 +00003064 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065 "Fell off of the edge of the floating point world");
3066
3067 // If the target supports SETCC of this type, use it.
3068 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
3069 break;
3070 }
Duncan Sands92c43912008-06-06 12:08:01 +00003071 if (NewInTy.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003072 assert(0 && "Cannot promote Legal Integer SETCC yet");
3073 else {
3074 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
3075 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
3076 }
3077 Tmp1 = LegalizeOp(Tmp1);
3078 Tmp2 = LegalizeOp(Tmp2);
3079 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3080 Result = LegalizeOp(Result);
3081 break;
3082 }
3083 case TargetLowering::Expand:
3084 // Expand a setcc node into a select_cc of the same condition, lhs, and
3085 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands92c43912008-06-06 12:08:01 +00003086 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003087 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3088 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3089 Tmp3);
3090 break;
3091 }
3092 break;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003093 case ISD::VSETCC: {
3094 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3095 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman8181bd12008-07-27 21:46:04 +00003096 SDValue CC = Node->getOperand(2);
Nate Begeman9a1ce152008-05-12 19:40:03 +00003097
3098 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3099
3100 // Everything is legal, see if we should expand this op or something.
3101 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3102 default: assert(0 && "This action is not supported yet!");
3103 case TargetLowering::Legal: break;
3104 case TargetLowering::Custom:
3105 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003106 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003107 break;
Mon P Wangec428ad2008-12-13 08:15:14 +00003108 case TargetLowering::Expand: {
3109 // Unroll into a nasty set of scalar code for now.
3110 MVT VT = Node->getValueType(0);
3111 unsigned NumElems = VT.getVectorNumElements();
3112 MVT EltVT = VT.getVectorElementType();
3113 MVT TmpEltVT = Tmp1.getValueType().getVectorElementType();
3114 SmallVector<SDValue, 8> Ops(NumElems);
3115 for (unsigned i = 0; i < NumElems; ++i) {
3116 SDValue In1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3117 Tmp1, DAG.getIntPtrConstant(i));
Duncan Sands4a361272009-01-01 15:52:00 +00003118 Ops[i] = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(TmpEltVT), In1,
Mon P Wang77bc9cd2008-12-17 08:49:47 +00003119 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3120 Tmp2, DAG.getIntPtrConstant(i)),
3121 CC);
3122 Ops[i] = DAG.getNode(ISD::SELECT, EltVT, Ops[i],
3123 DAG.getConstant(EltVT.getIntegerVTBitMask(),EltVT),
3124 DAG.getConstant(0, EltVT));
Mon P Wangec428ad2008-12-13 08:15:14 +00003125 }
3126 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], NumElems);
3127 break;
3128 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00003129 }
3130 break;
3131 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003132
3133 case ISD::SHL_PARTS:
3134 case ISD::SRA_PARTS:
3135 case ISD::SRL_PARTS: {
Dan Gohman8181bd12008-07-27 21:46:04 +00003136 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003137 bool Changed = false;
3138 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3139 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3140 Changed |= Ops.back() != Node->getOperand(i);
3141 }
3142 if (Changed)
3143 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
3144
3145 switch (TLI.getOperationAction(Node->getOpcode(),
3146 Node->getValueType(0))) {
3147 default: assert(0 && "This action is not supported yet!");
3148 case TargetLowering::Legal: break;
3149 case TargetLowering::Custom:
3150 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003151 if (Tmp1.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003152 SDValue Tmp2, RetVal(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003153 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
3154 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman8181bd12008-07-27 21:46:04 +00003155 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00003156 if (i == Op.getResNo())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003157 RetVal = Tmp2;
3158 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003159 assert(RetVal.getNode() && "Illegal result number");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003160 return RetVal;
3161 }
3162 break;
3163 }
3164
3165 // Since these produce multiple values, make sure to remember that we
3166 // legalized all of them.
3167 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman8181bd12008-07-27 21:46:04 +00003168 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00003169 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003170 }
3171
3172 // Binary operators
3173 case ISD::ADD:
3174 case ISD::SUB:
3175 case ISD::MUL:
3176 case ISD::MULHS:
3177 case ISD::MULHU:
3178 case ISD::UDIV:
3179 case ISD::SDIV:
3180 case ISD::AND:
3181 case ISD::OR:
3182 case ISD::XOR:
3183 case ISD::SHL:
3184 case ISD::SRL:
3185 case ISD::SRA:
3186 case ISD::FADD:
3187 case ISD::FSUB:
3188 case ISD::FMUL:
3189 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00003190 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003191 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3192 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3193 case Expand: assert(0 && "Not possible");
3194 case Legal:
3195 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3196 break;
3197 case Promote:
3198 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3199 break;
3200 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003201
3202 if ((Node->getOpcode() == ISD::SHL ||
3203 Node->getOpcode() == ISD::SRL ||
3204 Node->getOpcode() == ISD::SRA) &&
3205 !Node->getValueType(0).isVector()) {
Mon P Wang9901e732008-12-09 05:46:39 +00003206 Tmp2 = LegalizeShiftAmount(Tmp2);
Mon P Wangec428ad2008-12-13 08:15:14 +00003207 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003208
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003209 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangbff5d9c2008-11-10 04:46:22 +00003210
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003211 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3212 default: assert(0 && "BinOp legalize operation not supported");
3213 case TargetLowering::Legal: break;
3214 case TargetLowering::Custom:
3215 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003216 if (Tmp1.getNode()) {
Nate Begemanbb1ce942008-07-29 15:49:41 +00003217 Result = Tmp1;
3218 break;
Nate Begeman7569e762008-07-29 19:07:27 +00003219 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003220 // Fall through if the custom lower can't deal with the operation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003221 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00003222 MVT VT = Op.getValueType();
Mon P Wang1448aad2008-10-30 08:01:45 +00003223
Dan Gohman5a199552007-10-08 18:33:35 +00003224 // See if multiply or divide can be lowered using two-result operations.
3225 SDVTList VTs = DAG.getVTList(VT, VT);
3226 if (Node->getOpcode() == ISD::MUL) {
3227 // We just need the low half of the multiply; try both the signed
3228 // and unsigned forms. If the target supports both SMUL_LOHI and
3229 // UMUL_LOHI, form a preference by checking which forms of plain
3230 // MULH it supports.
3231 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3232 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3233 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3234 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3235 unsigned OpToUse = 0;
3236 if (HasSMUL_LOHI && !HasMULHS) {
3237 OpToUse = ISD::SMUL_LOHI;
3238 } else if (HasUMUL_LOHI && !HasMULHU) {
3239 OpToUse = ISD::UMUL_LOHI;
3240 } else if (HasSMUL_LOHI) {
3241 OpToUse = ISD::SMUL_LOHI;
3242 } else if (HasUMUL_LOHI) {
3243 OpToUse = ISD::UMUL_LOHI;
3244 }
3245 if (OpToUse) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003246 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003247 break;
3248 }
3249 }
3250 if (Node->getOpcode() == ISD::MULHS &&
3251 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003252 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3253 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003254 break;
3255 }
3256 if (Node->getOpcode() == ISD::MULHU &&
3257 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003258 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3259 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003260 break;
3261 }
3262 if (Node->getOpcode() == ISD::SDIV &&
3263 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003264 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3265 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003266 break;
3267 }
3268 if (Node->getOpcode() == ISD::UDIV &&
3269 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner48188652008-10-04 21:27:46 +00003270 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3271 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003272 break;
3273 }
Mon P Wang26342922008-12-18 20:03:17 +00003274
Dan Gohman6d05cac2007-10-11 23:57:53 +00003275 // Check to see if we have a libcall for this operator.
3276 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3277 bool isSigned = false;
3278 switch (Node->getOpcode()) {
3279 case ISD::UDIV:
3280 case ISD::SDIV:
3281 if (VT == MVT::i32) {
3282 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang1448aad2008-10-30 08:01:45 +00003283 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003284 isSigned = Node->getOpcode() == ISD::SDIV;
3285 }
3286 break;
Chris Lattner48188652008-10-04 21:27:46 +00003287 case ISD::MUL:
3288 if (VT == MVT::i32)
3289 LC = RTLIB::MUL_I32;
Scott Michel81215042008-12-29 03:21:37 +00003290 else if (VT == MVT::i64)
3291 LC = RTLIB::MUL_I64;
Chris Lattner48188652008-10-04 21:27:46 +00003292 break;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003293 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00003294 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3295 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003296 break;
3297 default: break;
3298 }
3299 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003300 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003301 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003302 break;
3303 }
Mon P Wang1448aad2008-10-30 08:01:45 +00003304
Duncan Sands92c43912008-06-06 12:08:01 +00003305 assert(Node->getValueType(0).isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003306 "Cannot expand this binary operator!");
3307 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003308 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003309 break;
3310 }
3311 case TargetLowering::Promote: {
3312 switch (Node->getOpcode()) {
3313 default: assert(0 && "Do not know how to promote this BinOp!");
3314 case ISD::AND:
3315 case ISD::OR:
3316 case ISD::XOR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003317 MVT OVT = Node->getValueType(0);
3318 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3319 assert(OVT.isVector() && "Cannot promote this BinOp!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003320 // Bit convert each of the values to the new type.
3321 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3322 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3323 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3324 // Bit convert the result back the original type.
3325 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3326 break;
3327 }
3328 }
3329 }
3330 }
3331 break;
3332
Dan Gohman475cd732007-10-05 14:17:22 +00003333 case ISD::SMUL_LOHI:
3334 case ISD::UMUL_LOHI:
3335 case ISD::SDIVREM:
3336 case ISD::UDIVREM:
3337 // These nodes will only be produced by target-specific lowering, so
3338 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003339 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003340 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003341
3342 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3343 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3344 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003345 break;
3346
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003347 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3348 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3349 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3350 case Expand: assert(0 && "Not possible");
3351 case Legal:
3352 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3353 break;
3354 case Promote:
3355 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3356 break;
3357 }
3358
3359 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3360
3361 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3362 default: assert(0 && "Operation not supported");
3363 case TargetLowering::Custom:
3364 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003365 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003366 break;
3367 case TargetLowering::Legal: break;
3368 case TargetLowering::Expand: {
3369 // If this target supports fabs/fneg natively and select is cheap,
3370 // do this efficiently.
3371 if (!TLI.isSelectExpensive() &&
3372 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3373 TargetLowering::Legal &&
3374 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3375 TargetLowering::Legal) {
3376 // Get the sign bit of the RHS.
Duncan Sands92c43912008-06-06 12:08:01 +00003377 MVT IVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003378 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman8181bd12008-07-27 21:46:04 +00003379 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Duncan Sands4a361272009-01-01 15:52:00 +00003380 SignBit = DAG.getSetCC(TLI.getSetCCResultType(IVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003381 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3382 // Get the absolute value of the result.
Dan Gohman8181bd12008-07-27 21:46:04 +00003383 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003384 // Select between the nabs and abs value based on the sign bit of
3385 // the input.
3386 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3387 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3388 AbsVal),
3389 AbsVal);
3390 Result = LegalizeOp(Result);
3391 break;
3392 }
3393
3394 // Otherwise, do bitwise ops!
Duncan Sands92c43912008-06-06 12:08:01 +00003395 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003396 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3397 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3398 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3399 Result = LegalizeOp(Result);
3400 break;
3401 }
3402 }
3403 break;
3404
3405 case ISD::ADDC:
3406 case ISD::SUBC:
3407 Tmp1 = LegalizeOp(Node->getOperand(0));
3408 Tmp2 = LegalizeOp(Node->getOperand(1));
3409 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003410 Tmp3 = Result.getValue(0);
3411 Tmp4 = Result.getValue(1);
3412
3413 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3414 default: assert(0 && "This action is not supported yet!");
3415 case TargetLowering::Legal:
3416 break;
3417 case TargetLowering::Custom:
3418 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3419 if (Tmp1.getNode() != NULL) {
Sanjiv Guptad57f2e12008-11-27 05:58:04 +00003420 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003421 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3422 }
3423 break;
3424 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003425 // Since this produces two values, make sure to remember that we legalized
3426 // both of them.
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003427 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3428 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3429 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003430
3431 case ISD::ADDE:
3432 case ISD::SUBE:
3433 Tmp1 = LegalizeOp(Node->getOperand(0));
3434 Tmp2 = LegalizeOp(Node->getOperand(1));
3435 Tmp3 = LegalizeOp(Node->getOperand(2));
3436 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003437 Tmp3 = Result.getValue(0);
3438 Tmp4 = Result.getValue(1);
3439
3440 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3441 default: assert(0 && "This action is not supported yet!");
3442 case TargetLowering::Legal:
3443 break;
3444 case TargetLowering::Custom:
3445 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3446 if (Tmp1.getNode() != NULL) {
Sanjiv Guptad57f2e12008-11-27 05:58:04 +00003447 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003448 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3449 }
3450 break;
3451 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003452 // Since this produces two values, make sure to remember that we legalized
3453 // both of them.
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003454 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3455 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3456 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003457
3458 case ISD::BUILD_PAIR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003459 MVT PairTy = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003460 // TODO: handle the case where the Lo and Hi operands are not of legal type
3461 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3462 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3463 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3464 case TargetLowering::Promote:
3465 case TargetLowering::Custom:
3466 assert(0 && "Cannot promote/custom this yet!");
3467 case TargetLowering::Legal:
3468 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3469 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3470 break;
3471 case TargetLowering::Expand:
3472 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3473 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3474 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003475 DAG.getConstant(PairTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003476 TLI.getShiftAmountTy()));
3477 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3478 break;
3479 }
3480 break;
3481 }
3482
3483 case ISD::UREM:
3484 case ISD::SREM:
3485 case ISD::FREM:
3486 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3487 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3488
3489 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3490 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3491 case TargetLowering::Custom:
3492 isCustom = true;
3493 // FALLTHROUGH
3494 case TargetLowering::Legal:
3495 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3496 if (isCustom) {
3497 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003498 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003499 }
3500 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003501 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003502 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3503 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands92c43912008-06-06 12:08:01 +00003504 MVT VT = Node->getValueType(0);
Dan Gohman5a199552007-10-08 18:33:35 +00003505
3506 // See if remainder can be lowered using two-result operations.
3507 SDVTList VTs = DAG.getVTList(VT, VT);
3508 if (Node->getOpcode() == ISD::SREM &&
3509 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003510 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003511 break;
3512 }
3513 if (Node->getOpcode() == ISD::UREM &&
3514 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003515 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003516 break;
3517 }
3518
Duncan Sands92c43912008-06-06 12:08:01 +00003519 if (VT.isInteger()) {
Dan Gohman5a199552007-10-08 18:33:35 +00003520 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003521 TargetLowering::Legal) {
3522 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003523 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3524 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3525 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands92c43912008-06-06 12:08:01 +00003526 } else if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003527 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003528 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003529 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003530 "Cannot expand this binary operator!");
3531 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3532 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman8181bd12008-07-27 21:46:04 +00003533 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003534 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003535 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003536 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00003537 assert(VT.isFloatingPoint() &&
Dan Gohman59b4b102007-11-06 22:11:54 +00003538 "remainder op must have integer or floating-point type");
Duncan Sands92c43912008-06-06 12:08:01 +00003539 if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003540 Result = LegalizeOp(UnrollVectorOp(Op));
3541 } else {
3542 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003543 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3544 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003545 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003546 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003547 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003548 }
3549 break;
3550 }
Dan Gohman5a199552007-10-08 18:33:35 +00003551 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003552 break;
3553 case ISD::VAARG: {
3554 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3555 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3556
Duncan Sands92c43912008-06-06 12:08:01 +00003557 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003558 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3559 default: assert(0 && "This action is not supported yet!");
3560 case TargetLowering::Custom:
3561 isCustom = true;
3562 // FALLTHROUGH
3563 case TargetLowering::Legal:
3564 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3565 Result = Result.getValue(0);
3566 Tmp1 = Result.getValue(1);
3567
3568 if (isCustom) {
3569 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003570 if (Tmp2.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003571 Result = LegalizeOp(Tmp2);
3572 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3573 }
3574 }
3575 break;
3576 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003577 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003578 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003579 // Increment the pointer, VAList, to the next vaarg
Duncan Sands55a4c232008-11-03 11:51:11 +00003580 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sandsd68f13b2009-01-12 20:38:59 +00003581 DAG.getConstant(TLI.getTargetData()->
3582 getTypePaddedSize(VT.getTypeForMVT()),
3583 TLI.getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003584 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00003585 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003586 // Load the actual argument out of the pointer VAList
3587 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3588 Tmp1 = LegalizeOp(Result.getValue(1));
3589 Result = LegalizeOp(Result);
3590 break;
3591 }
3592 }
3593 // Since VAARG produces two values, make sure to remember that we
3594 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003595 AddLegalizedOperand(SDValue(Node, 0), Result);
3596 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00003597 return Op.getResNo() ? Tmp1 : Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003598 }
3599
3600 case ISD::VACOPY:
3601 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3602 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3603 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3604
3605 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3606 default: assert(0 && "This action is not supported yet!");
3607 case TargetLowering::Custom:
3608 isCustom = true;
3609 // FALLTHROUGH
3610 case TargetLowering::Legal:
3611 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3612 Node->getOperand(3), Node->getOperand(4));
3613 if (isCustom) {
3614 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003615 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003616 }
3617 break;
3618 case TargetLowering::Expand:
3619 // This defaults to loading a pointer from the input and storing it to the
3620 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003621 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3622 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman6b9a08e2008-04-17 02:09:26 +00003623 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3624 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003625 break;
3626 }
3627 break;
3628
3629 case ISD::VAEND:
3630 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3631 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3632
3633 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3634 default: assert(0 && "This action is not supported yet!");
3635 case TargetLowering::Custom:
3636 isCustom = true;
3637 // FALLTHROUGH
3638 case TargetLowering::Legal:
3639 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3640 if (isCustom) {
3641 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003642 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003643 }
3644 break;
3645 case TargetLowering::Expand:
3646 Result = Tmp1; // Default to a no-op, return the chain
3647 break;
3648 }
3649 break;
3650
3651 case ISD::VASTART:
3652 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3653 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3654
3655 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3656
3657 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3658 default: assert(0 && "This action is not supported yet!");
3659 case TargetLowering::Legal: break;
3660 case TargetLowering::Custom:
3661 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003662 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003663 break;
3664 }
3665 break;
3666
3667 case ISD::ROTL:
3668 case ISD::ROTR:
3669 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3670 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3671 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3672 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3673 default:
3674 assert(0 && "ROTL/ROTR legalize operation not supported");
3675 break;
3676 case TargetLowering::Legal:
3677 break;
3678 case TargetLowering::Custom:
3679 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003680 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003681 break;
3682 case TargetLowering::Promote:
3683 assert(0 && "Do not know how to promote ROTL/ROTR");
3684 break;
3685 case TargetLowering::Expand:
3686 assert(0 && "Do not know how to expand ROTL/ROTR");
3687 break;
3688 }
3689 break;
3690
3691 case ISD::BSWAP:
3692 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3693 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3694 case TargetLowering::Custom:
3695 assert(0 && "Cannot custom legalize this yet!");
3696 case TargetLowering::Legal:
3697 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3698 break;
3699 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003700 MVT OVT = Tmp1.getValueType();
3701 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3702 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003703
3704 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3705 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3706 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3707 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3708 break;
3709 }
3710 case TargetLowering::Expand:
3711 Result = ExpandBSWAP(Tmp1);
3712 break;
3713 }
3714 break;
3715
3716 case ISD::CTPOP:
3717 case ISD::CTTZ:
3718 case ISD::CTLZ:
3719 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3720 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003721 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003722 case TargetLowering::Legal:
3723 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003724 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003725 TargetLowering::Custom) {
3726 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003727 if (Tmp1.getNode()) {
Scott Michelbc62b412007-08-02 02:22:46 +00003728 Result = Tmp1;
3729 }
Scott Michel48b63e62007-07-30 21:00:31 +00003730 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003731 break;
3732 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003733 MVT OVT = Tmp1.getValueType();
3734 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003735
3736 // Zero extend the argument.
3737 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3738 // Perform the larger operation, then subtract if needed.
3739 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3740 switch (Node->getOpcode()) {
3741 case ISD::CTPOP:
3742 Result = Tmp1;
3743 break;
3744 case ISD::CTTZ:
3745 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Duncan Sands4a361272009-01-01 15:52:00 +00003746 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003747 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003748 ISD::SETEQ);
3749 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003750 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003751 break;
3752 case ISD::CTLZ:
3753 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3754 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003755 DAG.getConstant(NVT.getSizeInBits() -
3756 OVT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003757 break;
3758 }
3759 break;
3760 }
3761 case TargetLowering::Expand:
3762 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3763 break;
3764 }
3765 break;
3766
3767 // Unary operators
3768 case ISD::FABS:
3769 case ISD::FNEG:
3770 case ISD::FSQRT:
3771 case ISD::FSIN:
3772 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003773 case ISD::FLOG:
3774 case ISD::FLOG2:
3775 case ISD::FLOG10:
3776 case ISD::FEXP:
3777 case ISD::FEXP2:
Dan Gohmanc8b20e22008-08-21 17:55:02 +00003778 case ISD::FTRUNC:
3779 case ISD::FFLOOR:
3780 case ISD::FCEIL:
3781 case ISD::FRINT:
3782 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003783 Tmp1 = LegalizeOp(Node->getOperand(0));
3784 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3785 case TargetLowering::Promote:
3786 case TargetLowering::Custom:
3787 isCustom = true;
3788 // FALLTHROUGH
3789 case TargetLowering::Legal:
3790 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3791 if (isCustom) {
3792 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003793 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003794 }
3795 break;
3796 case TargetLowering::Expand:
3797 switch (Node->getOpcode()) {
3798 default: assert(0 && "Unreachable!");
3799 case ISD::FNEG:
3800 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3801 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3802 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3803 break;
3804 case ISD::FABS: {
3805 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands92c43912008-06-06 12:08:01 +00003806 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003807 Tmp2 = DAG.getConstantFP(0.0, VT);
Duncan Sands4a361272009-01-01 15:52:00 +00003808 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
3809 Tmp1, Tmp2, ISD::SETUGT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003810 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3811 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3812 break;
3813 }
Evan Cheng1fac6952008-09-09 23:35:53 +00003814 case ISD::FSQRT:
3815 case ISD::FSIN:
3816 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003817 case ISD::FLOG:
3818 case ISD::FLOG2:
3819 case ISD::FLOG10:
3820 case ISD::FEXP:
3821 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00003822 case ISD::FTRUNC:
3823 case ISD::FFLOOR:
3824 case ISD::FCEIL:
3825 case ISD::FRINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00003826 case ISD::FNEARBYINT: {
Duncan Sands92c43912008-06-06 12:08:01 +00003827 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003828
3829 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003830 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003831 Result = LegalizeOp(UnrollVectorOp(Op));
3832 break;
3833 }
3834
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003835 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3836 switch(Node->getOpcode()) {
3837 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003838 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3839 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003840 break;
3841 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003842 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3843 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003844 break;
3845 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003846 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3847 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003848 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00003849 case ISD::FLOG:
3850 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3851 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3852 break;
3853 case ISD::FLOG2:
3854 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3855 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3856 break;
3857 case ISD::FLOG10:
3858 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3859 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3860 break;
3861 case ISD::FEXP:
3862 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3863 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3864 break;
3865 case ISD::FEXP2:
3866 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3867 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3868 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00003869 case ISD::FTRUNC:
3870 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3871 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3872 break;
3873 case ISD::FFLOOR:
3874 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3875 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3876 break;
3877 case ISD::FCEIL:
3878 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3879 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3880 break;
3881 case ISD::FRINT:
3882 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3883 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3884 break;
3885 case ISD::FNEARBYINT:
3886 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3887 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3888 break;
Evan Cheng1fac6952008-09-09 23:35:53 +00003889 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003890 default: assert(0 && "Unreachable!");
3891 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003892 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003893 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003894 break;
3895 }
3896 }
3897 break;
3898 }
3899 break;
3900 case ISD::FPOWI: {
Duncan Sands92c43912008-06-06 12:08:01 +00003901 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003902
3903 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003904 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003905 Result = LegalizeOp(UnrollVectorOp(Op));
3906 break;
3907 }
3908
3909 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003910 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3911 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003912 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003913 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003914 break;
3915 }
3916 case ISD::BIT_CONVERT:
3917 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003918 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3919 Node->getValueType(0));
Duncan Sands92c43912008-06-06 12:08:01 +00003920 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003921 // The input has to be a vector type, we have to either scalarize it, pack
3922 // it, or convert it based on whether the input vector type is legal.
Gabor Greif1c80d112008-08-28 21:40:38 +00003923 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00003924 int InIx = Node->getOperand(0).getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00003925 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3926 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003927
3928 // Figure out if there is a simple type corresponding to this Vector
3929 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00003930 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003931 if (TLI.isTypeLegal(TVT)) {
3932 // Turn this into a bit convert of the vector input.
3933 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3934 LegalizeOp(Node->getOperand(0)));
3935 break;
3936 } else if (NumElems == 1) {
3937 // Turn this into a bit convert of the scalar input.
3938 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3939 ScalarizeVectorOp(Node->getOperand(0)));
3940 break;
3941 } else {
3942 // FIXME: UNIMP! Store then reload
3943 assert(0 && "Cast from unsupported vector type not implemented yet!");
3944 }
3945 } else {
3946 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3947 Node->getOperand(0).getValueType())) {
3948 default: assert(0 && "Unknown operation action!");
3949 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003950 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3951 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003952 break;
3953 case TargetLowering::Legal:
3954 Tmp1 = LegalizeOp(Node->getOperand(0));
3955 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3956 break;
3957 }
3958 }
3959 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003960 case ISD::CONVERT_RNDSAT: {
3961 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3962 switch (CvtCode) {
3963 default: assert(0 && "Unknown cvt code!");
3964 case ISD::CVT_SF:
3965 case ISD::CVT_UF:
Mon P Wang73d31542008-11-10 20:54:11 +00003966 case ISD::CVT_FF:
Mon P Wang2fc3f9e2008-12-09 07:27:39 +00003967 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003968 case ISD::CVT_FS:
3969 case ISD::CVT_FU:
3970 case ISD::CVT_SS:
3971 case ISD::CVT_SU:
3972 case ISD::CVT_US:
3973 case ISD::CVT_UU: {
3974 SDValue DTyOp = Node->getOperand(1);
3975 SDValue STyOp = Node->getOperand(2);
3976 SDValue RndOp = Node->getOperand(3);
3977 SDValue SatOp = Node->getOperand(4);
3978 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3979 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3980 case Legal:
3981 Tmp1 = LegalizeOp(Node->getOperand(0));
3982 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3983 RndOp, SatOp);
3984 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3985 TargetLowering::Custom) {
3986 Tmp1 = TLI.LowerOperation(Result, DAG);
3987 if (Tmp1.getNode()) Result = Tmp1;
3988 }
3989 break;
3990 case Promote:
3991 Result = PromoteOp(Node->getOperand(0));
3992 // For FP, make Op1 a i32
3993
Mon P Wang2fc3f9e2008-12-09 07:27:39 +00003994 Result = DAG.getConvertRndSat(Op.getValueType(), Result,
Mon P Wang73d31542008-11-10 20:54:11 +00003995 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3996 break;
3997 }
3998 break;
3999 }
4000 } // end switch CvtCode
4001 break;
4002 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004003 // Conversion operators. The source and destination have different types.
4004 case ISD::SINT_TO_FP:
4005 case ISD::UINT_TO_FP: {
4006 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman29c3cef2008-08-14 20:04:46 +00004007 Result = LegalizeINT_TO_FP(Result, isSigned,
4008 Node->getValueType(0), Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004009 break;
4010 }
4011 case ISD::TRUNCATE:
4012 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4013 case Legal:
4014 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michele9b8a402008-12-02 19:55:08 +00004015 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
4016 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
4017 case TargetLowering::Custom:
Mon P Wang72fe5462008-12-11 00:44:22 +00004018 isCustom = true;
4019 // FALLTHROUGH
Scott Michele9b8a402008-12-02 19:55:08 +00004020 case TargetLowering::Legal:
Mon P Wang72fe5462008-12-11 00:44:22 +00004021 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4022 if (isCustom) {
4023 Tmp1 = TLI.LowerOperation(Result, DAG);
4024 if (Tmp1.getNode()) Result = Tmp1;
4025 }
4026 break;
Mon P Wang83edba52008-12-12 01:25:51 +00004027 case TargetLowering::Expand:
4028 assert(Result.getValueType().isVector() && "must be vector type");
4029 // Unroll the truncate. We should do better.
4030 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Schellerbfc55ee2008-12-02 12:12:25 +00004031 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004032 break;
4033 case Expand:
4034 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4035
4036 // Since the result is legal, we should just be able to truncate the low
4037 // part of the source.
4038 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
4039 break;
4040 case Promote:
4041 Result = PromoteOp(Node->getOperand(0));
4042 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
4043 break;
4044 }
4045 break;
4046
4047 case ISD::FP_TO_SINT:
4048 case ISD::FP_TO_UINT:
4049 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4050 case Legal:
4051 Tmp1 = LegalizeOp(Node->getOperand(0));
4052
4053 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
4054 default: assert(0 && "Unknown operation action!");
4055 case TargetLowering::Custom:
4056 isCustom = true;
4057 // FALLTHROUGH
4058 case TargetLowering::Legal:
4059 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4060 if (isCustom) {
4061 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004062 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004063 }
4064 break;
4065 case TargetLowering::Promote:
4066 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
4067 Node->getOpcode() == ISD::FP_TO_SINT);
4068 break;
4069 case TargetLowering::Expand:
4070 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004071 SDValue True, False;
Duncan Sands92c43912008-06-06 12:08:01 +00004072 MVT VT = Node->getOperand(0).getValueType();
4073 MVT NVT = Node->getValueType(0);
Dale Johannesen958b08b2007-09-19 23:55:34 +00004074 const uint64_t zero[] = {0, 0};
Duncan Sands92c43912008-06-06 12:08:01 +00004075 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
4076 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohman88ae8c52008-02-29 01:44:25 +00004077 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00004078 Tmp2 = DAG.getConstantFP(apf, VT);
Duncan Sands4a361272009-01-01 15:52:00 +00004079 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(VT), Node->getOperand(0),
4080 Tmp2, ISD::SETLT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004081 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
4082 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
4083 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
4084 Tmp2));
4085 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman88ae8c52008-02-29 01:44:25 +00004086 DAG.getConstant(x, NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004087 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
4088 break;
4089 } else {
4090 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
4091 }
4092 break;
4093 }
4094 break;
4095 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00004096 MVT VT = Op.getValueType();
4097 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00004098 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004099 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00004100 if (Node->getOpcode() == ISD::FP_TO_SINT) {
4101 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
4102 Node->getOperand(0), DAG.getValueType(MVT::f64));
4103 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
4104 DAG.getIntPtrConstant(1));
4105 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
4106 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00004107 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4108 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4109 Tmp2 = DAG.getConstantFP(apf, OVT);
4110 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4111 // FIXME: generated code sucks.
4112 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
4113 DAG.getNode(ISD::ADD, MVT::i32,
4114 DAG.getNode(ISD::FP_TO_SINT, VT,
4115 DAG.getNode(ISD::FSUB, OVT,
4116 Node->getOperand(0), Tmp2)),
4117 DAG.getConstant(0x80000000, MVT::i32)),
4118 DAG.getNode(ISD::FP_TO_SINT, VT,
4119 Node->getOperand(0)),
4120 DAG.getCondCode(ISD::SETGE));
4121 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004122 break;
4123 }
Dan Gohmanec51f642008-03-10 23:03:31 +00004124 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsf68dffb2008-07-17 02:36:29 +00004125 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4126 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4127 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman8181bd12008-07-27 21:46:04 +00004128 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00004129 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004130 break;
4131 }
4132 case Promote:
4133 Tmp1 = PromoteOp(Node->getOperand(0));
4134 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4135 Result = LegalizeOp(Result);
4136 break;
4137 }
4138 break;
4139
Chris Lattner56ecde32008-01-16 06:57:07 +00004140 case ISD::FP_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004141 MVT DstVT = Op.getValueType();
4142 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004143 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4144 // The only other way we can lower this is to turn it into a STORE,
4145 // LOAD pair, targetting a temporary location (a stack slot).
4146 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
4147 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00004148 }
4149 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4150 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4151 case Legal:
4152 Tmp1 = LegalizeOp(Node->getOperand(0));
4153 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4154 break;
4155 case Promote:
4156 Tmp1 = PromoteOp(Node->getOperand(0));
4157 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4158 break;
4159 }
4160 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004161 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004162 case ISD::FP_ROUND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004163 MVT DstVT = Op.getValueType();
4164 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004165 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4166 if (SrcVT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004167 SDValue Lo;
Dale Johannesena0d36082008-01-20 01:18:38 +00004168 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00004169 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00004170 if (DstVT!=MVT::f64)
4171 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00004172 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004173 }
Chris Lattner5872a362008-01-17 07:00:52 +00004174 // The only other way we can lower this is to turn it into a STORE,
4175 // LOAD pair, targetting a temporary location (a stack slot).
4176 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4177 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004178 }
Chris Lattner56ecde32008-01-16 06:57:07 +00004179 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4180 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4181 case Legal:
4182 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004183 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004184 break;
4185 case Promote:
4186 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004187 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4188 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004189 break;
4190 }
4191 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004192 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004193 case ISD::ANY_EXTEND:
4194 case ISD::ZERO_EXTEND:
4195 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004196 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4197 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4198 case Legal:
4199 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac54d002008-04-30 00:26:38 +00004200 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michelac7091c2008-02-15 23:05:48 +00004201 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4202 TargetLowering::Custom) {
Scott Michelac54d002008-04-30 00:26:38 +00004203 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004204 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelac7091c2008-02-15 23:05:48 +00004205 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004206 break;
4207 case Promote:
4208 switch (Node->getOpcode()) {
4209 case ISD::ANY_EXTEND:
4210 Tmp1 = PromoteOp(Node->getOperand(0));
4211 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
4212 break;
4213 case ISD::ZERO_EXTEND:
4214 Result = PromoteOp(Node->getOperand(0));
4215 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4216 Result = DAG.getZeroExtendInReg(Result,
4217 Node->getOperand(0).getValueType());
4218 break;
4219 case ISD::SIGN_EXTEND:
4220 Result = PromoteOp(Node->getOperand(0));
4221 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
4222 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4223 Result,
4224 DAG.getValueType(Node->getOperand(0).getValueType()));
4225 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004226 }
4227 }
4228 break;
4229 case ISD::FP_ROUND_INREG:
4230 case ISD::SIGN_EXTEND_INREG: {
4231 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004232 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004233
4234 // If this operation is not supported, convert it to a shl/shr or load/store
4235 // pair.
4236 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4237 default: assert(0 && "This action not supported for this op yet!");
4238 case TargetLowering::Legal:
4239 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
4240 break;
4241 case TargetLowering::Expand:
4242 // If this is an integer extend and shifts are supported, do that.
4243 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
4244 // NOTE: we could fall back on load/store here too for targets without
4245 // SAR. However, it is doubtful that any exist.
Duncan Sands92c43912008-06-06 12:08:01 +00004246 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4247 ExtraVT.getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00004248 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004249 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4250 Node->getOperand(0), ShiftCst);
4251 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4252 Result, ShiftCst);
4253 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
4254 // The only way we can lower this is to turn it into a TRUNCSTORE,
4255 // EXTLOAD pair, targetting a temporary location (a stack slot).
4256
4257 // NOTE: there is a choice here between constantly creating new stack
4258 // slots and always reusing the same one. We currently always create
4259 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00004260 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4261 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004262 } else {
4263 assert(0 && "Unknown op");
4264 }
4265 break;
4266 }
4267 break;
4268 }
Duncan Sands38947cd2007-07-27 12:58:54 +00004269 case ISD::TRAMPOLINE: {
Dan Gohman8181bd12008-07-27 21:46:04 +00004270 SDValue Ops[6];
Duncan Sands38947cd2007-07-27 12:58:54 +00004271 for (unsigned i = 0; i != 6; ++i)
4272 Ops[i] = LegalizeOp(Node->getOperand(i));
4273 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4274 // The only option for this node is to custom lower it.
4275 Result = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004276 assert(Result.getNode() && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00004277
4278 // Since trampoline produces two values, make sure to remember that we
4279 // legalized both of them.
4280 Tmp1 = LegalizeOp(Result.getValue(1));
4281 Result = LegalizeOp(Result);
Dan Gohman8181bd12008-07-27 21:46:04 +00004282 AddLegalizedOperand(SDValue(Node, 0), Result);
4283 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00004284 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00004285 }
Dan Gohmane8e4a412008-05-14 00:43:10 +00004286 case ISD::FLT_ROUNDS_: {
Duncan Sands92c43912008-06-06 12:08:01 +00004287 MVT VT = Node->getValueType(0);
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004288 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4289 default: assert(0 && "This action not supported for this op yet!");
4290 case TargetLowering::Custom:
4291 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004292 if (Result.getNode()) break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004293 // Fall Thru
4294 case TargetLowering::Legal:
4295 // If this operation is not supported, lower it to constant 1
4296 Result = DAG.getConstant(1, VT);
4297 break;
4298 }
Dan Gohmane09dc8c2008-05-12 16:07:15 +00004299 break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004300 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004301 case ISD::TRAP: {
Duncan Sands92c43912008-06-06 12:08:01 +00004302 MVT VT = Node->getValueType(0);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004303 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4304 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00004305 case TargetLowering::Legal:
4306 Tmp1 = LegalizeOp(Node->getOperand(0));
4307 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4308 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004309 case TargetLowering::Custom:
4310 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004311 if (Result.getNode()) break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004312 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00004313 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004314 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00004315 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004316 TargetLowering::ArgListTy Args;
Dan Gohman8181bd12008-07-27 21:46:04 +00004317 std::pair<SDValue,SDValue> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004318 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00004319 false, false, false, false, CallingConv::C, false,
Bill Wendlingfef06052008-09-16 21:48:12 +00004320 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner88e03932008-01-15 22:09:33 +00004321 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004322 Result = CallResult.second;
4323 break;
4324 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004325 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004326 }
Bill Wendling913dcf32008-11-22 00:22:52 +00004327
Bill Wendling7e04be62008-12-09 22:08:41 +00004328 case ISD::SADDO:
4329 case ISD::SSUBO: {
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004330 MVT VT = Node->getValueType(0);
4331 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4332 default: assert(0 && "This action not supported for this op yet!");
4333 case TargetLowering::Custom:
4334 Result = TLI.LowerOperation(Op, DAG);
4335 if (Result.getNode()) break;
4336 // FALLTHROUGH
4337 case TargetLowering::Legal: {
4338 SDValue LHS = LegalizeOp(Node->getOperand(0));
4339 SDValue RHS = LegalizeOp(Node->getOperand(1));
4340
Bill Wendling7e04be62008-12-09 22:08:41 +00004341 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
4342 ISD::ADD : ISD::SUB, LHS.getValueType(),
4343 LHS, RHS);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004344 MVT OType = Node->getValueType(1);
4345
Bill Wendlingc65e6e42008-11-25 08:19:22 +00004346 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004347
Bill Wendlingcf4de122008-11-25 19:40:17 +00004348 // LHSSign -> LHS >= 0
4349 // RHSSign -> RHS >= 0
4350 // SumSign -> Sum >= 0
4351 //
Bill Wendling7e04be62008-12-09 22:08:41 +00004352 // Add:
Bill Wendlingcf4de122008-11-25 19:40:17 +00004353 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling7e04be62008-12-09 22:08:41 +00004354 // Sub:
4355 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendlingcf4de122008-11-25 19:40:17 +00004356 //
4357 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4358 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
Bill Wendling7e04be62008-12-09 22:08:41 +00004359 SDValue SignsMatch = DAG.getSetCC(OType, LHSSign, RHSSign,
4360 Node->getOpcode() == ISD::SADDO ?
4361 ISD::SETEQ : ISD::SETNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004362
Bill Wendlingcf4de122008-11-25 19:40:17 +00004363 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4364 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004365
Bill Wendling7e04be62008-12-09 22:08:41 +00004366 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsMatch, SumSignNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004367
4368 MVT ValueVTs[] = { LHS.getValueType(), OType };
4369 SDValue Ops[] = { Sum, Cmp };
4370
Duncan Sands42d7bb82008-12-01 11:41:29 +00004371 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4372 &Ops[0], 2);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004373 SDNode *RNode = Result.getNode();
4374 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4375 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4376 break;
4377 }
4378 }
4379
4380 break;
4381 }
Bill Wendling7e04be62008-12-09 22:08:41 +00004382 case ISD::UADDO:
4383 case ISD::USUBO: {
Bill Wendling4c134df2008-11-24 19:21:46 +00004384 MVT VT = Node->getValueType(0);
4385 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4386 default: assert(0 && "This action not supported for this op yet!");
4387 case TargetLowering::Custom:
4388 Result = TLI.LowerOperation(Op, DAG);
4389 if (Result.getNode()) break;
4390 // FALLTHROUGH
4391 case TargetLowering::Legal: {
4392 SDValue LHS = LegalizeOp(Node->getOperand(0));
4393 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling913dcf32008-11-22 00:22:52 +00004394
Bill Wendling7e04be62008-12-09 22:08:41 +00004395 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
4396 ISD::ADD : ISD::SUB, LHS.getValueType(),
4397 LHS, RHS);
Bill Wendling4c134df2008-11-24 19:21:46 +00004398 MVT OType = Node->getValueType(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00004399 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS,
4400 Node->getOpcode () == ISD::UADDO ?
4401 ISD::SETULT : ISD::SETUGT);
Bill Wendling913dcf32008-11-22 00:22:52 +00004402
Bill Wendling4c134df2008-11-24 19:21:46 +00004403 MVT ValueVTs[] = { LHS.getValueType(), OType };
4404 SDValue Ops[] = { Sum, Cmp };
Bill Wendling913dcf32008-11-22 00:22:52 +00004405
Duncan Sands42d7bb82008-12-01 11:41:29 +00004406 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4407 &Ops[0], 2);
Bill Wendling4c134df2008-11-24 19:21:46 +00004408 SDNode *RNode = Result.getNode();
4409 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4410 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4411 break;
4412 }
4413 }
4414
Bill Wendling913dcf32008-11-22 00:22:52 +00004415 break;
4416 }
Bill Wendling7e04be62008-12-09 22:08:41 +00004417 case ISD::SMULO:
4418 case ISD::UMULO: {
4419 MVT VT = Node->getValueType(0);
4420 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4421 default: assert(0 && "This action is not supported at all!");
4422 case TargetLowering::Custom:
4423 Result = TLI.LowerOperation(Op, DAG);
4424 if (Result.getNode()) break;
4425 // Fall Thru
4426 case TargetLowering::Legal:
4427 // FIXME: According to Hacker's Delight, this can be implemented in
4428 // target independent lowering, but it would be inefficient, since it
Bill Wendling35f1a9d2008-12-10 02:01:32 +00004429 // requires a division + a branch.
Bill Wendling7e04be62008-12-09 22:08:41 +00004430 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4431 break;
4432 }
4433 break;
4434 }
4435
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004436 }
4437
4438 assert(Result.getValueType() == Op.getValueType() &&
4439 "Bad legalization!");
4440
4441 // Make sure that the generated code is itself legal.
4442 if (Result != Op)
4443 Result = LegalizeOp(Result);
4444
4445 // Note that LegalizeOp may be reentered even from single-use nodes, which
4446 // means that we always must cache transformed nodes.
4447 AddLegalizedOperand(Op, Result);
4448 return Result;
4449}
4450
4451/// PromoteOp - Given an operation that produces a value in an invalid type,
4452/// promote it to compute the value into a larger type. The produced value will
4453/// have the correct bits for the low portion of the register, but no guarantee
4454/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +00004455SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00004456 MVT VT = Op.getValueType();
4457 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004458 assert(getTypeAction(VT) == Promote &&
4459 "Caller should expand or legalize operands that are not promotable!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004460 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004461 "Cannot promote to smaller type!");
4462
Dan Gohman8181bd12008-07-27 21:46:04 +00004463 SDValue Tmp1, Tmp2, Tmp3;
4464 SDValue Result;
Gabor Greif1c80d112008-08-28 21:40:38 +00004465 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004466
Dan Gohman8181bd12008-07-27 21:46:04 +00004467 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004468 if (I != PromotedNodes.end()) return I->second;
4469
4470 switch (Node->getOpcode()) {
4471 case ISD::CopyFromReg:
4472 assert(0 && "CopyFromReg must be legal!");
4473 default:
4474#ifndef NDEBUG
4475 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4476#endif
4477 assert(0 && "Do not know how to promote this operator!");
4478 abort();
4479 case ISD::UNDEF:
4480 Result = DAG.getNode(ISD::UNDEF, NVT);
4481 break;
4482 case ISD::Constant:
4483 if (VT != MVT::i1)
4484 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4485 else
4486 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
4487 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4488 break;
4489 case ISD::ConstantFP:
4490 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4491 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4492 break;
4493
Duncan Sands4a361272009-01-01 15:52:00 +00004494 case ISD::SETCC: {
4495 MVT VT0 = Node->getOperand(0).getValueType();
4496 assert(isTypeLegal(TLI.getSetCCResultType(VT0))
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004497 && "SetCC type is not legal??");
Duncan Sands4a361272009-01-01 15:52:00 +00004498 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(VT0),
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004499 Node->getOperand(0), Node->getOperand(1),
4500 Node->getOperand(2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004501 break;
Duncan Sands4a361272009-01-01 15:52:00 +00004502 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004503 case ISD::TRUNCATE:
4504 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4505 case Legal:
4506 Result = LegalizeOp(Node->getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00004507 assert(Result.getValueType().bitsGE(NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004508 "This truncation doesn't make sense!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004509 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004510 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4511 break;
4512 case Promote:
4513 // The truncation is not required, because we don't guarantee anything
4514 // about high bits anyway.
4515 Result = PromoteOp(Node->getOperand(0));
4516 break;
4517 case Expand:
4518 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4519 // Truncate the low part of the expanded value to the result type
4520 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4521 }
4522 break;
4523 case ISD::SIGN_EXTEND:
4524 case ISD::ZERO_EXTEND:
4525 case ISD::ANY_EXTEND:
4526 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4527 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4528 case Legal:
4529 // Input is legal? Just do extend all the way to the larger type.
4530 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4531 break;
4532 case Promote:
4533 // Promote the reg if it's smaller.
4534 Result = PromoteOp(Node->getOperand(0));
4535 // The high bits are not guaranteed to be anything. Insert an extend.
4536 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4537 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4538 DAG.getValueType(Node->getOperand(0).getValueType()));
4539 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4540 Result = DAG.getZeroExtendInReg(Result,
4541 Node->getOperand(0).getValueType());
4542 break;
4543 }
4544 break;
Mon P Wang73d31542008-11-10 20:54:11 +00004545 case ISD::CONVERT_RNDSAT: {
4546 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4547 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4548 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4549 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4550 "can only promote integers");
4551 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4552 Node->getOperand(1), Node->getOperand(2),
4553 Node->getOperand(3), Node->getOperand(4),
4554 CvtCode);
4555 break;
4556
4557 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004558 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004559 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4560 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004561 Result = PromoteOp(Result);
4562 break;
4563
4564 case ISD::FP_EXTEND:
4565 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4566 case ISD::FP_ROUND:
4567 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4568 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4569 case Promote: assert(0 && "Unreachable with 2 FP types!");
4570 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004571 if (Node->getConstantOperandVal(1) == 0) {
4572 // Input is legal? Do an FP_ROUND_INREG.
4573 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4574 DAG.getValueType(VT));
4575 } else {
4576 // Just remove the truncate, it isn't affecting the value.
4577 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4578 Node->getOperand(1));
4579 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004580 break;
4581 }
4582 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004583 case ISD::SINT_TO_FP:
4584 case ISD::UINT_TO_FP:
4585 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4586 case Legal:
4587 // No extra round required here.
4588 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4589 break;
4590
4591 case Promote:
4592 Result = PromoteOp(Node->getOperand(0));
4593 if (Node->getOpcode() == ISD::SINT_TO_FP)
4594 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4595 Result,
4596 DAG.getValueType(Node->getOperand(0).getValueType()));
4597 else
4598 Result = DAG.getZeroExtendInReg(Result,
4599 Node->getOperand(0).getValueType());
4600 // No extra round required here.
4601 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4602 break;
4603 case Expand:
4604 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4605 Node->getOperand(0));
4606 // Round if we cannot tolerate excess precision.
4607 if (NoExcessFPPrecision)
4608 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4609 DAG.getValueType(VT));
4610 break;
4611 }
4612 break;
4613
4614 case ISD::SIGN_EXTEND_INREG:
4615 Result = PromoteOp(Node->getOperand(0));
4616 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4617 Node->getOperand(1));
4618 break;
4619 case ISD::FP_TO_SINT:
4620 case ISD::FP_TO_UINT:
4621 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4622 case Legal:
4623 case Expand:
4624 Tmp1 = Node->getOperand(0);
4625 break;
4626 case Promote:
4627 // The input result is prerounded, so we don't have to do anything
4628 // special.
4629 Tmp1 = PromoteOp(Node->getOperand(0));
4630 break;
4631 }
4632 // If we're promoting a UINT to a larger size, check to see if the new node
4633 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4634 // we can use that instead. This allows us to generate better code for
4635 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4636 // legal, such as PowerPC.
4637 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4638 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4639 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4640 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4641 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4642 } else {
4643 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4644 }
4645 break;
4646
4647 case ISD::FABS:
4648 case ISD::FNEG:
4649 Tmp1 = PromoteOp(Node->getOperand(0));
4650 assert(Tmp1.getValueType() == NVT);
4651 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4652 // NOTE: we do not have to do any extra rounding here for
4653 // NoExcessFPPrecision, because we know the input will have the appropriate
4654 // precision, and these operations don't modify precision at all.
4655 break;
4656
Dale Johannesen92b33082008-09-04 00:47:13 +00004657 case ISD::FLOG:
4658 case ISD::FLOG2:
4659 case ISD::FLOG10:
4660 case ISD::FEXP:
4661 case ISD::FEXP2:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004662 case ISD::FSQRT:
4663 case ISD::FSIN:
4664 case ISD::FCOS:
Dan Gohmanb2158232008-08-21 18:38:14 +00004665 case ISD::FTRUNC:
4666 case ISD::FFLOOR:
4667 case ISD::FCEIL:
4668 case ISD::FRINT:
4669 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004670 Tmp1 = PromoteOp(Node->getOperand(0));
4671 assert(Tmp1.getValueType() == NVT);
4672 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4673 if (NoExcessFPPrecision)
4674 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4675 DAG.getValueType(VT));
4676 break;
4677
Evan Cheng1fac6952008-09-09 23:35:53 +00004678 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004679 case ISD::FPOWI: {
Evan Cheng1fac6952008-09-09 23:35:53 +00004680 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004681 // directly as well, which may be better.
4682 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng1fac6952008-09-09 23:35:53 +00004683 Tmp2 = Node->getOperand(1);
4684 if (Node->getOpcode() == ISD::FPOW)
4685 Tmp2 = PromoteOp(Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004686 assert(Tmp1.getValueType() == NVT);
Evan Cheng1fac6952008-09-09 23:35:53 +00004687 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004688 if (NoExcessFPPrecision)
4689 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4690 DAG.getValueType(VT));
4691 break;
4692 }
4693
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004694 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004695 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004696 Tmp2 = PromoteOp(Node->getOperand(2));
4697 Tmp3 = PromoteOp(Node->getOperand(3));
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004698 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getMemoryVT(),
4699 AtomNode->getChain(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004700 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004701 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004702 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004703 // Remember that we legalized the chain.
4704 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4705 break;
4706 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004707 case ISD::ATOMIC_LOAD_ADD:
4708 case ISD::ATOMIC_LOAD_SUB:
4709 case ISD::ATOMIC_LOAD_AND:
4710 case ISD::ATOMIC_LOAD_OR:
4711 case ISD::ATOMIC_LOAD_XOR:
4712 case ISD::ATOMIC_LOAD_NAND:
4713 case ISD::ATOMIC_LOAD_MIN:
4714 case ISD::ATOMIC_LOAD_MAX:
4715 case ISD::ATOMIC_LOAD_UMIN:
4716 case ISD::ATOMIC_LOAD_UMAX:
4717 case ISD::ATOMIC_SWAP: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004718 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004719 Tmp2 = PromoteOp(Node->getOperand(2));
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004720 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getMemoryVT(),
4721 AtomNode->getChain(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004722 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004723 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004724 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004725 // Remember that we legalized the chain.
4726 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4727 break;
4728 }
4729
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004730 case ISD::AND:
4731 case ISD::OR:
4732 case ISD::XOR:
4733 case ISD::ADD:
4734 case ISD::SUB:
4735 case ISD::MUL:
4736 // The input may have strange things in the top bits of the registers, but
4737 // these operations don't care. They may have weird bits going out, but
4738 // that too is okay if they are integer operations.
4739 Tmp1 = PromoteOp(Node->getOperand(0));
4740 Tmp2 = PromoteOp(Node->getOperand(1));
4741 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4742 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4743 break;
4744 case ISD::FADD:
4745 case ISD::FSUB:
4746 case ISD::FMUL:
4747 Tmp1 = PromoteOp(Node->getOperand(0));
4748 Tmp2 = PromoteOp(Node->getOperand(1));
4749 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4750 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4751
4752 // Floating point operations will give excess precision that we may not be
4753 // able to tolerate. If we DO allow excess precision, just leave it,
4754 // otherwise excise it.
4755 // FIXME: Why would we need to round FP ops more than integer ones?
4756 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4757 if (NoExcessFPPrecision)
4758 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4759 DAG.getValueType(VT));
4760 break;
4761
4762 case ISD::SDIV:
4763 case ISD::SREM:
4764 // These operators require that their input be sign extended.
4765 Tmp1 = PromoteOp(Node->getOperand(0));
4766 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004767 if (NVT.isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004768 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4769 DAG.getValueType(VT));
4770 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4771 DAG.getValueType(VT));
4772 }
4773 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4774
4775 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands92c43912008-06-06 12:08:01 +00004776 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004777 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4778 DAG.getValueType(VT));
4779 break;
4780 case ISD::FDIV:
4781 case ISD::FREM:
4782 case ISD::FCOPYSIGN:
4783 // These operators require that their input be fp extended.
4784 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004785 case Expand: assert(0 && "not implemented");
4786 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4787 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004788 }
4789 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004790 case Expand: assert(0 && "not implemented");
4791 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4792 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004793 }
4794 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4795
4796 // Perform FP_ROUND: this is probably overly pessimistic.
4797 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4798 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4799 DAG.getValueType(VT));
4800 break;
4801
4802 case ISD::UDIV:
4803 case ISD::UREM:
4804 // These operators require that their input be zero extended.
4805 Tmp1 = PromoteOp(Node->getOperand(0));
4806 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004807 assert(NVT.isInteger() && "Operators don't apply to FP!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004808 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4809 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4810 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4811 break;
4812
4813 case ISD::SHL:
4814 Tmp1 = PromoteOp(Node->getOperand(0));
4815 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4816 break;
4817 case ISD::SRA:
4818 // The input value must be properly sign extended.
4819 Tmp1 = PromoteOp(Node->getOperand(0));
4820 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4821 DAG.getValueType(VT));
4822 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4823 break;
4824 case ISD::SRL:
4825 // The input value must be properly zero extended.
4826 Tmp1 = PromoteOp(Node->getOperand(0));
4827 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4828 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4829 break;
4830
4831 case ISD::VAARG:
4832 Tmp1 = Node->getOperand(0); // Get the chain.
4833 Tmp2 = Node->getOperand(1); // Get the pointer.
4834 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4835 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sandsac496a12008-07-04 11:47:58 +00004836 Result = TLI.LowerOperation(Tmp3, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004837 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004838 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00004839 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004840 // Increment the pointer, VAList, to the next vaarg
4841 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands92c43912008-06-06 12:08:01 +00004842 DAG.getConstant(VT.getSizeInBits()/8,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004843 TLI.getPointerTy()));
4844 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00004845 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004846 // Load the actual argument out of the pointer VAList
4847 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4848 }
4849 // Remember that we legalized the chain.
4850 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4851 break;
4852
4853 case ISD::LOAD: {
4854 LoadSDNode *LD = cast<LoadSDNode>(Node);
4855 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4856 ? ISD::EXTLOAD : LD->getExtensionType();
4857 Result = DAG.getExtLoad(ExtType, NVT,
4858 LD->getChain(), LD->getBasePtr(),
4859 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004860 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004861 LD->isVolatile(),
4862 LD->getAlignment());
4863 // Remember that we legalized the chain.
4864 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4865 break;
4866 }
Scott Michel67224b22008-06-02 22:18:03 +00004867 case ISD::SELECT: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004868 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4869 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel67224b22008-06-02 22:18:03 +00004870
Duncan Sands92c43912008-06-06 12:08:01 +00004871 MVT VT2 = Tmp2.getValueType();
Scott Michel67224b22008-06-02 22:18:03 +00004872 assert(VT2 == Tmp3.getValueType()
Scott Michel7b54de02008-06-03 19:13:20 +00004873 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4874 // Ensure that the resulting node is at least the same size as the operands'
4875 // value types, because we cannot assume that TLI.getSetCCValueType() is
4876 // constant.
4877 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004878 break;
Scott Michel67224b22008-06-02 22:18:03 +00004879 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004880 case ISD::SELECT_CC:
4881 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4882 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4883 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4884 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4885 break;
4886 case ISD::BSWAP:
4887 Tmp1 = Node->getOperand(0);
4888 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4889 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4890 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004891 DAG.getConstant(NVT.getSizeInBits() -
4892 VT.getSizeInBits(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004893 TLI.getShiftAmountTy()));
4894 break;
4895 case ISD::CTPOP:
4896 case ISD::CTTZ:
4897 case ISD::CTLZ:
4898 // Zero extend the argument
4899 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4900 // Perform the larger operation, then subtract if needed.
4901 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4902 switch(Node->getOpcode()) {
4903 case ISD::CTPOP:
4904 Result = Tmp1;
4905 break;
4906 case ISD::CTTZ:
4907 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Duncan Sands4a361272009-01-01 15:52:00 +00004908 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004909 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004910 ISD::SETEQ);
4911 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00004912 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004913 break;
4914 case ISD::CTLZ:
4915 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4916 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004917 DAG.getConstant(NVT.getSizeInBits() -
4918 VT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004919 break;
4920 }
4921 break;
4922 case ISD::EXTRACT_SUBVECTOR:
4923 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4924 break;
4925 case ISD::EXTRACT_VECTOR_ELT:
4926 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4927 break;
4928 }
4929
Gabor Greif1c80d112008-08-28 21:40:38 +00004930 assert(Result.getNode() && "Didn't set a result!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004931
4932 // Make sure the result is itself legal.
4933 Result = LegalizeOp(Result);
4934
4935 // Remember that we promoted this!
4936 AddPromotedOperand(Op, Result);
4937 return Result;
4938}
4939
4940/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4941/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4942/// based on the vector type. The return type of this matches the element type
4943/// of the vector, which may not be legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00004944SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004945 // We know that operand #0 is the Vec vector. If the index is a constant
4946 // or if the invec is a supported hardware type, we can use it. Otherwise,
4947 // lower to a store then an indexed load.
Dan Gohman8181bd12008-07-27 21:46:04 +00004948 SDValue Vec = Op.getOperand(0);
4949 SDValue Idx = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004950
Duncan Sands92c43912008-06-06 12:08:01 +00004951 MVT TVT = Vec.getValueType();
4952 unsigned NumElems = TVT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004953
4954 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4955 default: assert(0 && "This action is not supported yet!");
4956 case TargetLowering::Custom: {
4957 Vec = LegalizeOp(Vec);
4958 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004959 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004960 if (Tmp3.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004961 return Tmp3;
4962 break;
4963 }
4964 case TargetLowering::Legal:
4965 if (isTypeLegal(TVT)) {
4966 Vec = LegalizeOp(Vec);
4967 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004968 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004969 }
4970 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00004971 case TargetLowering::Promote:
4972 assert(TVT.isVector() && "not vector type");
4973 // fall thru to expand since vectors are by default are promote
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004974 case TargetLowering::Expand:
4975 break;
4976 }
4977
4978 if (NumElems == 1) {
4979 // This must be an access of the only element. Return it.
4980 Op = ScalarizeVectorOp(Vec);
4981 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00004982 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004983 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004984 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004985 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004986 if (CIdx->getZExtValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004987 Vec = Lo;
4988 } else {
4989 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004990 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004991 Idx.getValueType());
4992 }
4993
4994 // It's now an extract from the appropriate high or low part. Recurse.
4995 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4996 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4997 } else {
4998 // Store the value to a temporary stack slot, then LOAD the scalar
4999 // element back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00005000 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
5001 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002
5003 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +00005004 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005005 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
5006 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00005007
Duncan Sandsec142ee2008-06-08 20:54:56 +00005008 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00005009 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00005010 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00005011 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00005012
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005013 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
5014
5015 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
5016 }
5017 return Op;
5018}
5019
5020/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
5021/// we assume the operation can be split if it is not already legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00005022SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005023 // We know that operand #0 is the Vec vector. For now we assume the index
5024 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman8181bd12008-07-27 21:46:04 +00005025 SDValue Vec = Op.getOperand(0);
5026 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005027
Duncan Sands92c43912008-06-06 12:08:01 +00005028 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005029
Duncan Sands92c43912008-06-06 12:08:01 +00005030 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005031 // This must be an access of the desired vector length. Return it.
5032 return Vec;
5033 }
5034
5035 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00005036 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005037 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005038 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005039 Vec = Lo;
5040 } else {
5041 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005042 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
5043 Idx.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005044 }
5045
5046 // It's now an extract from the appropriate high or low part. Recurse.
5047 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
5048 return ExpandEXTRACT_SUBVECTOR(Op);
5049}
5050
5051/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
5052/// with condition CC on the current target. This usually involves legalizing
5053/// or promoting the arguments. In the case where LHS and RHS must be expanded,
5054/// there may be no choice but to create a new SetCC node to represent the
5055/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman8181bd12008-07-27 21:46:04 +00005056/// LHS, and the SDValue returned in RHS has a nil SDNode value.
5057void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
5058 SDValue &RHS,
5059 SDValue &CC) {
5060 SDValue Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005061
5062 switch (getTypeAction(LHS.getValueType())) {
5063 case Legal:
5064 Tmp1 = LegalizeOp(LHS); // LHS
5065 Tmp2 = LegalizeOp(RHS); // RHS
5066 break;
5067 case Promote:
5068 Tmp1 = PromoteOp(LHS); // LHS
5069 Tmp2 = PromoteOp(RHS); // RHS
5070
5071 // If this is an FP compare, the operands have already been extended.
Duncan Sands92c43912008-06-06 12:08:01 +00005072 if (LHS.getValueType().isInteger()) {
5073 MVT VT = LHS.getValueType();
5074 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005075
5076 // Otherwise, we have to insert explicit sign or zero extends. Note
5077 // that we could insert sign extends for ALL conditions, but zero extend
5078 // is cheaper on many machines (an AND instead of two shifts), so prefer
5079 // it.
5080 switch (cast<CondCodeSDNode>(CC)->get()) {
5081 default: assert(0 && "Unknown integer comparison!");
5082 case ISD::SETEQ:
5083 case ISD::SETNE:
5084 case ISD::SETUGE:
5085 case ISD::SETUGT:
5086 case ISD::SETULE:
5087 case ISD::SETULT:
5088 // ALL of these operations will work if we either sign or zero extend
5089 // the operands (including the unsigned comparisons!). Zero extend is
5090 // usually a simpler/cheaper operation, so prefer it.
5091 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
5092 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
5093 break;
5094 case ISD::SETGE:
5095 case ISD::SETGT:
5096 case ISD::SETLT:
5097 case ISD::SETLE:
5098 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
5099 DAG.getValueType(VT));
5100 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
5101 DAG.getValueType(VT));
Evan Chengd901b662008-10-13 18:46:18 +00005102 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5103 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005104 break;
5105 }
5106 }
5107 break;
5108 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00005109 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005110 if (VT == MVT::f32 || VT == MVT::f64) {
5111 // Expand into one or more soft-fp libcall(s).
Evan Cheng24108632008-07-01 21:35:46 +00005112 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005113 switch (cast<CondCodeSDNode>(CC)->get()) {
5114 case ISD::SETEQ:
5115 case ISD::SETOEQ:
5116 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5117 break;
5118 case ISD::SETNE:
5119 case ISD::SETUNE:
5120 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
5121 break;
5122 case ISD::SETGE:
5123 case ISD::SETOGE:
5124 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5125 break;
5126 case ISD::SETLT:
5127 case ISD::SETOLT:
5128 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5129 break;
5130 case ISD::SETLE:
5131 case ISD::SETOLE:
5132 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5133 break;
5134 case ISD::SETGT:
5135 case ISD::SETOGT:
5136 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
5137 break;
5138 case ISD::SETUO:
5139 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5140 break;
5141 case ISD::SETO:
5142 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
5143 break;
5144 default:
5145 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5146 switch (cast<CondCodeSDNode>(CC)->get()) {
5147 case ISD::SETONE:
5148 // SETONE = SETOLT | SETOGT
5149 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5150 // Fallthrough
5151 case ISD::SETUGT:
5152 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
5153 break;
5154 case ISD::SETUGE:
5155 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5156 break;
5157 case ISD::SETULT:
5158 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5159 break;
5160 case ISD::SETULE:
5161 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5162 break;
5163 case ISD::SETUEQ:
5164 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5165 break;
5166 default: assert(0 && "Unsupported FP setcc!");
5167 }
5168 }
Duncan Sandsf19591c2008-06-30 10:19:09 +00005169
Dan Gohman8181bd12008-07-27 21:46:04 +00005170 SDValue Dummy;
5171 SDValue Ops[2] = { LHS, RHS };
Gabor Greif1c80d112008-08-28 21:40:38 +00005172 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005173 false /*sign irrelevant*/, Dummy);
5174 Tmp2 = DAG.getConstant(0, MVT::i32);
5175 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
5176 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Duncan Sands4a361272009-01-01 15:52:00 +00005177 Tmp1 = DAG.getNode(ISD::SETCC,
5178 TLI.getSetCCResultType(Tmp1.getValueType()),
5179 Tmp1, Tmp2, CC);
Gabor Greif1c80d112008-08-28 21:40:38 +00005180 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005181 false /*sign irrelevant*/, Dummy);
Duncan Sands4a361272009-01-01 15:52:00 +00005182 Tmp2 = DAG.getNode(ISD::SETCC,
5183 TLI.getSetCCResultType(LHS.getValueType()), LHS,
5184 Tmp2, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005185 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005186 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005187 }
Evan Cheng18a1ab12008-07-07 07:18:09 +00005188 LHS = LegalizeOp(Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005189 RHS = Tmp2;
5190 return;
5191 }
5192
Dan Gohman8181bd12008-07-27 21:46:04 +00005193 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005194 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005195 ExpandOp(RHS, RHSLo, RHSHi);
5196 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5197
5198 if (VT==MVT::ppcf128) {
5199 // FIXME: This generated code sucks. We want to generate
Dale Johannesen26317b62008-09-12 00:30:56 +00005200 // FCMPU crN, hi1, hi2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005201 // BNE crN, L:
Dale Johannesen26317b62008-09-12 00:30:56 +00005202 // FCMPU crN, lo1, lo2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005203 // The following can be improved, but not that much.
Duncan Sands4a361272009-01-01 15:52:00 +00005204 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5205 LHSHi, RHSHi, ISD::SETOEQ);
5206 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5207 LHSLo, RHSLo, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005208 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Duncan Sands4a361272009-01-01 15:52:00 +00005209 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5210 LHSHi, RHSHi, ISD::SETUNE);
5211 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5212 LHSHi, RHSHi, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005213 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5214 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00005215 Tmp2 = SDValue();
Dale Johannesen472d15d2007-10-06 01:24:11 +00005216 break;
5217 }
5218
5219 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005220 case ISD::SETEQ:
5221 case ISD::SETNE:
5222 if (RHSLo == RHSHi)
5223 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5224 if (RHSCST->isAllOnesValue()) {
5225 // Comparison to -1.
5226 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5227 Tmp2 = RHSLo;
5228 break;
5229 }
5230
5231 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5232 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5233 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5234 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5235 break;
5236 default:
5237 // If this is a comparison of the sign bit, just look at the top part.
5238 // X > -1, x < 0
5239 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5240 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00005241 CST->isNullValue()) || // X < 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005242 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5243 CST->isAllOnesValue())) { // X > -1
5244 Tmp1 = LHSHi;
5245 Tmp2 = RHSHi;
5246 break;
5247 }
5248
5249 // FIXME: This generated code sucks.
5250 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005251 switch (CCCode) {
5252 default: assert(0 && "Unknown integer setcc!");
5253 case ISD::SETLT:
5254 case ISD::SETULT: LowCC = ISD::SETULT; break;
5255 case ISD::SETGT:
5256 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5257 case ISD::SETLE:
5258 case ISD::SETULE: LowCC = ISD::SETULE; break;
5259 case ISD::SETGE:
5260 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5261 }
5262
5263 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5264 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5265 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5266
5267 // NOTE: on targets without efficient SELECT of bools, we can always use
5268 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
5269 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Duncan Sands4a361272009-01-01 15:52:00 +00005270 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5271 LHSLo, RHSLo, LowCC, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005272 if (!Tmp1.getNode())
Duncan Sands4a361272009-01-01 15:52:00 +00005273 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5274 LHSLo, RHSLo, LowCC);
5275 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5276 LHSHi, RHSHi, CCCode, false, DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005277 if (!Tmp2.getNode())
Duncan Sands4a361272009-01-01 15:52:00 +00005278 Tmp2 = DAG.getNode(ISD::SETCC,
5279 TLI.getSetCCResultType(LHSHi.getValueType()),
5280 LHSHi, RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005281
Gabor Greif1c80d112008-08-28 21:40:38 +00005282 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5283 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman9d24dc72008-03-13 22:13:53 +00005284 if ((Tmp1C && Tmp1C->isNullValue()) ||
5285 (Tmp2C && Tmp2C->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005286 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5287 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman9d24dc72008-03-13 22:13:53 +00005288 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005289 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5290 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5291 // low part is known false, returns high part.
5292 // For LE / GE, if high part is known false, ignore the low part.
5293 // For LT / GT, if high part is known true, ignore the low part.
5294 Tmp1 = Tmp2;
Dan Gohman8181bd12008-07-27 21:46:04 +00005295 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005296 } else {
Duncan Sands4a361272009-01-01 15:52:00 +00005297 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5298 LHSHi, RHSHi, ISD::SETEQ, false,
5299 DagCombineInfo);
Gabor Greif1c80d112008-08-28 21:40:38 +00005300 if (!Result.getNode())
Duncan Sands4a361272009-01-01 15:52:00 +00005301 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5302 LHSHi, RHSHi, ISD::SETEQ);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005303 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5304 Result, Tmp1, Tmp2));
5305 Tmp1 = Result;
Dan Gohman8181bd12008-07-27 21:46:04 +00005306 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005307 }
5308 }
5309 }
5310 }
5311 LHS = Tmp1;
5312 RHS = Tmp2;
5313}
5314
Evan Cheng71343822008-10-15 02:05:31 +00005315/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5316/// condition code CC on the current target. This routine assumes LHS and rHS
5317/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5318/// illegal condition code into AND / OR of multiple SETCC values.
5319void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5320 SDValue &LHS, SDValue &RHS,
5321 SDValue &CC) {
5322 MVT OpVT = LHS.getValueType();
5323 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5324 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5325 default: assert(0 && "Unknown condition code action!");
5326 case TargetLowering::Legal:
5327 // Nothing to do.
5328 break;
5329 case TargetLowering::Expand: {
5330 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5331 unsigned Opc = 0;
5332 switch (CCCode) {
5333 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohman2b5b9ca2008-10-21 03:12:54 +00005334 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5335 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5336 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5337 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5338 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5339 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5340 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5341 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5342 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5343 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5344 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5345 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng71343822008-10-15 02:05:31 +00005346 // FIXME: Implement more expansions.
5347 }
5348
5349 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5350 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5351 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5352 RHS = SDValue();
5353 CC = SDValue();
5354 break;
5355 }
5356 }
5357}
5358
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005359/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5360/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5361/// a load from the stack slot to DestVT, extending it if needed.
5362/// The resultant code need not be legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00005363SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5364 MVT SlotVT,
5365 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005366 // Create the stack frame object.
Mon P Wang55854cc2008-07-05 20:40:31 +00005367 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5368 SrcOp.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00005369 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang55854cc2008-07-05 20:40:31 +00005370
Dan Gohman20e37962008-02-11 18:58:42 +00005371 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005372 int SPFI = StackPtrFI->getIndex();
Mon P Wang55854cc2008-07-05 20:40:31 +00005373
Duncan Sands92c43912008-06-06 12:08:01 +00005374 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5375 unsigned SlotSize = SlotVT.getSizeInBits();
5376 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang55854cc2008-07-05 20:40:31 +00005377 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5378 DestVT.getTypeForMVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005379
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005380 // Emit a store to the stack slot. Use a truncstore if the input value is
5381 // later than DestVT.
Dan Gohman8181bd12008-07-27 21:46:04 +00005382 SDValue Store;
Mon P Wang55854cc2008-07-05 20:40:31 +00005383
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005384 if (SrcSize > SlotSize)
Dan Gohman12a9c082008-02-06 22:27:42 +00005385 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005386 PseudoSourceValue::getFixedStack(SPFI), 0,
5387 SlotVT, false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005388 else {
5389 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman12a9c082008-02-06 22:27:42 +00005390 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005391 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang55854cc2008-07-05 20:40:31 +00005392 false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005393 }
5394
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005395 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005396 if (SlotSize == DestSize)
Mon P Wang55854cc2008-07-05 20:40:31 +00005397 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005398
5399 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang55854cc2008-07-05 20:40:31 +00005400 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5401 false, DestAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005402}
5403
Dan Gohman8181bd12008-07-27 21:46:04 +00005404SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005405 // Create a vector sized/aligned stack slot, store the value to element #0,
5406 // then load the whole vector back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00005407 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman12a9c082008-02-06 22:27:42 +00005408
Dan Gohman20e37962008-02-11 18:58:42 +00005409 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005410 int SPFI = StackPtrFI->getIndex();
5411
Dan Gohman8181bd12008-07-27 21:46:04 +00005412 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005413 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00005414 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005415 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005416}
5417
5418
5419/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
5420/// support the operation, but do support the resultant vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00005421SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005422
5423 // If the only non-undef value is the low element, turn this into a
5424 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
5425 unsigned NumElems = Node->getNumOperands();
5426 bool isOnlyLowElement = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005427 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005428
Dan Gohman8181bd12008-07-27 21:46:04 +00005429 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerd8cee732008-03-09 00:29:42 +00005430 // and use a bitmask instead of a list of elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00005431 std::map<SDValue, std::vector<unsigned> > Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005432 Values[SplatValue].push_back(0);
5433 bool isConstant = true;
5434 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5435 SplatValue.getOpcode() != ISD::UNDEF)
5436 isConstant = false;
5437
5438 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005439 SDValue V = Node->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005440 Values[V].push_back(i);
5441 if (V.getOpcode() != ISD::UNDEF)
5442 isOnlyLowElement = false;
5443 if (SplatValue != V)
Dan Gohman8181bd12008-07-27 21:46:04 +00005444 SplatValue = SDValue(0,0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005445
5446 // If this isn't a constant element or an undef, we can't use a constant
5447 // pool load.
5448 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5449 V.getOpcode() != ISD::UNDEF)
5450 isConstant = false;
5451 }
5452
5453 if (isOnlyLowElement) {
5454 // If the low element is an undef too, then this whole things is an undef.
5455 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5456 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5457 // Otherwise, turn this into a scalar_to_vector node.
5458 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5459 Node->getOperand(0));
5460 }
5461
5462 // If all elements are constants, create a load from the constant pool.
5463 if (isConstant) {
Duncan Sands92c43912008-06-06 12:08:01 +00005464 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005465 std::vector<Constant*> CV;
5466 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5467 if (ConstantFPSDNode *V =
5468 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005469 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005470 } else if (ConstantSDNode *V =
Chris Lattner5e0610f2008-04-20 00:41:09 +00005471 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005472 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005473 } else {
5474 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner5e0610f2008-04-20 00:41:09 +00005475 const Type *OpNTy =
Duncan Sands92c43912008-06-06 12:08:01 +00005476 Node->getOperand(0).getValueType().getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005477 CV.push_back(UndefValue::get(OpNTy));
5478 }
5479 }
5480 Constant *CP = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005481 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005482 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman12a9c082008-02-06 22:27:42 +00005483 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005484 PseudoSourceValue::getConstantPool(), 0,
5485 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005486 }
5487
Gabor Greif1c80d112008-08-28 21:40:38 +00005488 if (SplatValue.getNode()) { // Splat of one value?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005489 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands92c43912008-06-06 12:08:01 +00005490 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman8181bd12008-07-27 21:46:04 +00005491 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5492 std::vector<SDValue> ZeroVec(NumElems, Zero);
5493 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005494 &ZeroVec[0], ZeroVec.size());
5495
5496 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
5497 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
5498 // Get the splatted value into the low element of a vector register.
Dan Gohman8181bd12008-07-27 21:46:04 +00005499 SDValue LowValVec =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005500 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5501
5502 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5503 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5504 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5505 SplatMask);
5506 }
5507 }
5508
5509 // If there are only two unique elements, we may be able to turn this into a
5510 // vector shuffle.
5511 if (Values.size() == 2) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005512 // Get the two values in deterministic order.
Dan Gohman8181bd12008-07-27 21:46:04 +00005513 SDValue Val1 = Node->getOperand(1);
5514 SDValue Val2;
5515 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerd8cee732008-03-09 00:29:42 +00005516 if (MI->first != Val1)
5517 Val2 = MI->first;
5518 else
5519 Val2 = (++MI)->first;
5520
5521 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5522 // vector shuffle has the undef vector on the RHS.
5523 if (Val1.getOpcode() == ISD::UNDEF)
5524 std::swap(Val1, Val2);
5525
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005526 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands92c43912008-06-06 12:08:01 +00005527 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5528 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005529 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005530
5531 // Set elements of the shuffle mask for Val1.
5532 std::vector<unsigned> &Val1Elts = Values[Val1];
5533 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5534 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5535
5536 // Set elements of the shuffle mask for Val2.
5537 std::vector<unsigned> &Val2Elts = Values[Val2];
5538 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5539 if (Val2.getOpcode() != ISD::UNDEF)
5540 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5541 else
5542 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5543
Dan Gohman8181bd12008-07-27 21:46:04 +00005544 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005545 &MaskVec[0], MaskVec.size());
5546
Chris Lattnerd8cee732008-03-09 00:29:42 +00005547 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005548 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5549 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005550 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5551 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005552 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005553
5554 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerd8cee732008-03-09 00:29:42 +00005555 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005556 }
5557 }
5558
5559 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5560 // aligned object on the stack, store each element into it, then load
5561 // the result as a vector.
Duncan Sands92c43912008-06-06 12:08:01 +00005562 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005563 // Create the stack frame object.
Dan Gohman8181bd12008-07-27 21:46:04 +00005564 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005565
5566 // Emit a store of each element to the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00005567 SmallVector<SDValue, 8> Stores;
Duncan Sands92c43912008-06-06 12:08:01 +00005568 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005569 // Store (in the right endianness) the elements to memory.
5570 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5571 // Ignore undef elements.
5572 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5573
5574 unsigned Offset = TypeByteSize*i;
5575
Dan Gohman8181bd12008-07-27 21:46:04 +00005576 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005577 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5578
5579 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
5580 NULL, 0));
5581 }
5582
Dan Gohman8181bd12008-07-27 21:46:04 +00005583 SDValue StoreChain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005584 if (!Stores.empty()) // Not all undef elements?
5585 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5586 &Stores[0], Stores.size());
5587 else
5588 StoreChain = DAG.getEntryNode();
5589
5590 // Result is a load from the stack slot.
5591 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
5592}
5593
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005594void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman8181bd12008-07-27 21:46:04 +00005595 SDValue Op, SDValue Amt,
5596 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005597 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00005598 SDValue LHSL, LHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005599 ExpandOp(Op, LHSL, LHSH);
5600
Dan Gohman8181bd12008-07-27 21:46:04 +00005601 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands92c43912008-06-06 12:08:01 +00005602 MVT VT = LHSL.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005603 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5604 Hi = Lo.getValue(1);
5605}
5606
5607
5608/// ExpandShift - Try to find a clever way to expand this shift operation out to
5609/// smaller elements. If we can't find a way that is more efficient than a
5610/// libcall on this target, return false. Otherwise, return true with the
5611/// low-parts expanded into Lo and Hi.
Dan Gohman8181bd12008-07-27 21:46:04 +00005612bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5613 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005614 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5615 "This is not a shift!");
5616
Duncan Sands92c43912008-06-06 12:08:01 +00005617 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman8181bd12008-07-27 21:46:04 +00005618 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands92c43912008-06-06 12:08:01 +00005619 MVT ShTy = ShAmt.getValueType();
5620 unsigned ShBits = ShTy.getSizeInBits();
5621 unsigned VTBits = Op.getValueType().getSizeInBits();
5622 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005623
Chris Lattner8c931452007-10-14 20:35:12 +00005624 // Handle the case when Amt is an immediate.
Gabor Greif1c80d112008-08-28 21:40:38 +00005625 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005626 unsigned Cst = CN->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005627 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005628 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005629 ExpandOp(Op, InL, InH);
5630 switch(Opc) {
5631 case ISD::SHL:
5632 if (Cst > VTBits) {
5633 Lo = DAG.getConstant(0, NVT);
5634 Hi = DAG.getConstant(0, NVT);
5635 } else if (Cst > NVTBits) {
5636 Lo = DAG.getConstant(0, NVT);
5637 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5638 } else if (Cst == NVTBits) {
5639 Lo = DAG.getConstant(0, NVT);
5640 Hi = InL;
5641 } else {
5642 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5643 Hi = DAG.getNode(ISD::OR, NVT,
5644 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5645 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5646 }
5647 return true;
5648 case ISD::SRL:
5649 if (Cst > VTBits) {
5650 Lo = DAG.getConstant(0, NVT);
5651 Hi = DAG.getConstant(0, NVT);
5652 } else if (Cst > NVTBits) {
5653 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5654 Hi = DAG.getConstant(0, NVT);
5655 } else if (Cst == NVTBits) {
5656 Lo = InH;
5657 Hi = DAG.getConstant(0, NVT);
5658 } else {
5659 Lo = DAG.getNode(ISD::OR, NVT,
5660 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5661 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5662 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5663 }
5664 return true;
5665 case ISD::SRA:
5666 if (Cst > VTBits) {
5667 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5668 DAG.getConstant(NVTBits-1, ShTy));
5669 } else if (Cst > NVTBits) {
5670 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5671 DAG.getConstant(Cst-NVTBits, ShTy));
5672 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5673 DAG.getConstant(NVTBits-1, ShTy));
5674 } else if (Cst == NVTBits) {
5675 Lo = InH;
5676 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5677 DAG.getConstant(NVTBits-1, ShTy));
5678 } else {
5679 Lo = DAG.getNode(ISD::OR, NVT,
5680 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5681 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5682 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5683 }
5684 return true;
5685 }
5686 }
5687
5688 // Okay, the shift amount isn't constant. However, if we can tell that it is
5689 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005690 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5691 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005692 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5693
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005694 // If we know that if any of the high bits of the shift amount are one, then
5695 // we can do this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005696 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005697 // Mask out the high bit, which we know is set.
5698 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005699 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005700
5701 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005702 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005703 ExpandOp(Op, InL, InH);
5704 switch(Opc) {
5705 case ISD::SHL:
5706 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5707 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5708 return true;
5709 case ISD::SRL:
5710 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5711 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5712 return true;
5713 case ISD::SRA:
5714 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5715 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5716 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5717 return true;
5718 }
5719 }
5720
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005721 // If we know that the high bits of the shift amount are all zero, then we can
5722 // do this as a couple of simple shifts.
5723 if ((KnownZero & Mask) == Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005724 // Compute 32-amt.
Dan Gohman8181bd12008-07-27 21:46:04 +00005725 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005726 DAG.getConstant(NVTBits, Amt.getValueType()),
5727 Amt);
5728
5729 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005730 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005731 ExpandOp(Op, InL, InH);
5732 switch(Opc) {
5733 case ISD::SHL:
5734 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5735 Hi = DAG.getNode(ISD::OR, NVT,
5736 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5737 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5738 return true;
5739 case ISD::SRL:
5740 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5741 Lo = DAG.getNode(ISD::OR, NVT,
5742 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5743 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5744 return true;
5745 case ISD::SRA:
5746 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5747 Lo = DAG.getNode(ISD::OR, NVT,
5748 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5749 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5750 return true;
5751 }
5752 }
5753
5754 return false;
5755}
5756
5757
5758// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5759// does not fit into a register, return the lo part and set the hi part to the
5760// by-reg argument. If it does fit into a single register, return the result
5761// and leave the Hi part unset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005762SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5763 bool isSigned, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005764 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5765 // The input chain to this libcall is the entry node of the function.
5766 // Legalizing the call will automatically add the previous call to the
5767 // dependence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005768 SDValue InChain = DAG.getEntryNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005769
5770 TargetLowering::ArgListTy Args;
5771 TargetLowering::ArgListEntry Entry;
5772 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00005773 MVT ArgVT = Node->getOperand(i).getValueType();
5774 const Type *ArgTy = ArgVT.getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005775 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5776 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005777 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005778 Args.push_back(Entry);
5779 }
Bill Wendlingfef06052008-09-16 21:48:12 +00005780 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang1448aad2008-10-30 08:01:45 +00005781 TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005782
5783 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands92c43912008-06-06 12:08:01 +00005784 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman8181bd12008-07-27 21:46:04 +00005785 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen67cc9b62008-09-26 19:31:26 +00005786 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5787 CallingConv::C, false, Callee, Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005788
5789 // Legalize the call sequence, starting with the chain. This will advance
5790 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5791 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5792 LegalizeOp(CallInfo.second);
Dan Gohman8181bd12008-07-27 21:46:04 +00005793 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005794 switch (getTypeAction(CallInfo.first.getValueType())) {
5795 default: assert(0 && "Unknown thing");
5796 case Legal:
5797 Result = CallInfo.first;
5798 break;
5799 case Expand:
5800 ExpandOp(CallInfo.first, Result, Hi);
5801 break;
5802 }
5803 return Result;
5804}
5805
Dan Gohman29c3cef2008-08-14 20:04:46 +00005806/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5807///
5808SDValue SelectionDAGLegalize::
5809LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5810 bool isCustom = false;
5811 SDValue Tmp1;
5812 switch (getTypeAction(Op.getValueType())) {
5813 case Legal:
5814 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5815 Op.getValueType())) {
5816 default: assert(0 && "Unknown operation action!");
5817 case TargetLowering::Custom:
5818 isCustom = true;
5819 // FALLTHROUGH
5820 case TargetLowering::Legal:
5821 Tmp1 = LegalizeOp(Op);
Gabor Greif1c80d112008-08-28 21:40:38 +00005822 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005823 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5824 else
5825 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5826 DestTy, Tmp1);
5827 if (isCustom) {
5828 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005829 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman29c3cef2008-08-14 20:04:46 +00005830 }
5831 break;
5832 case TargetLowering::Expand:
5833 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5834 break;
5835 case TargetLowering::Promote:
5836 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5837 break;
5838 }
5839 break;
5840 case Expand:
5841 Result = ExpandIntToFP(isSigned, DestTy, Op);
5842 break;
5843 case Promote:
5844 Tmp1 = PromoteOp(Op);
5845 if (isSigned) {
5846 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5847 Tmp1, DAG.getValueType(Op.getValueType()));
5848 } else {
5849 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5850 Op.getValueType());
5851 }
Gabor Greif1c80d112008-08-28 21:40:38 +00005852 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005853 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5854 else
5855 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5856 DestTy, Tmp1);
5857 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5858 break;
5859 }
5860 return Result;
5861}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005862
5863/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5864///
Dan Gohman8181bd12008-07-27 21:46:04 +00005865SDValue SelectionDAGLegalize::
5866ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands92c43912008-06-06 12:08:01 +00005867 MVT SourceVT = Source.getValueType();
Dan Gohman8b232ff2008-03-11 01:59:03 +00005868 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005869
Dan Gohman29c3cef2008-08-14 20:04:46 +00005870 // Expand unsupported int-to-fp vector casts by unrolling them.
5871 if (DestTy.isVector()) {
5872 if (!ExpandSource)
5873 return LegalizeOp(UnrollVectorOp(Source));
5874 MVT DestEltTy = DestTy.getVectorElementType();
5875 if (DestTy.getVectorNumElements() == 1) {
5876 SDValue Scalar = ScalarizeVectorOp(Source);
5877 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5878 DestEltTy, Scalar);
5879 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5880 }
5881 SDValue Lo, Hi;
5882 SplitVectorOp(Source, Lo, Hi);
5883 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5884 DestTy.getVectorNumElements() / 2);
5885 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5886 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengd901b662008-10-13 18:46:18 +00005887 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5888 HiResult));
Dan Gohman29c3cef2008-08-14 20:04:46 +00005889 }
5890
Evan Chengf99a7752008-04-01 02:18:22 +00005891 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5892 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmana193dba2008-03-05 02:07:31 +00005893 // The integer value loaded will be incorrectly if the 'sign bit' of the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005894 // incoming integer is set. To handle this, we dynamically test to see if
5895 // it is set, and, if so, add a fudge factor.
Dan Gohman8181bd12008-07-27 21:46:04 +00005896 SDValue Hi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005897 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005898 SDValue Lo;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005899 ExpandOp(Source, Lo, Hi);
5900 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5901 } else {
5902 // The comparison for the sign bit will use the entire operand.
5903 Hi = Source;
5904 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005905
Dale Johannesen96db7962008-11-04 20:52:49 +00005906 // Check to see if the target has a custom way to lower this. If so, use
5907 // it. (Note we've already expanded the operand in this case.)
Dale Johannesena359b8b2008-10-21 20:50:01 +00005908 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5909 default: assert(0 && "This action not implemented for this operation!");
5910 case TargetLowering::Legal:
5911 case TargetLowering::Expand:
5912 break; // This case is handled below.
5913 case TargetLowering::Custom: {
5914 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5915 Source), DAG);
5916 if (NV.getNode())
5917 return LegalizeOp(NV);
5918 break; // The target decided this was legal after all
5919 }
5920 }
5921
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005922 // If this is unsigned, and not supported, first perform the conversion to
5923 // signed, then adjust the result if the sign bit is set.
Dan Gohman8181bd12008-07-27 21:46:04 +00005924 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005925
Duncan Sands4a361272009-01-01 15:52:00 +00005926 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi.getValueType()),
5927 Hi, DAG.getConstant(0, Hi.getValueType()),
5928 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005929 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5930 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005931 SignSet, Four, Zero);
5932 uint64_t FF = 0x5f800000ULL;
5933 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmana193dba2008-03-05 02:07:31 +00005934 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005935
Dan Gohman8181bd12008-07-27 21:46:04 +00005936 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005937 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005938 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00005939 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00005940 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005941 if (DestTy == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005942 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005943 PseudoSourceValue::getConstantPool(), 0,
5944 false, Alignment);
Duncan Sandsec142ee2008-06-08 20:54:56 +00005945 else if (DestTy.bitsGT(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005946 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005947 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00005948 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005949 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00005950 MVT::f32, false, Alignment);
Dale Johannesen2fc20782007-09-14 22:26:36 +00005951 else
5952 assert(0 && "Unexpected conversion");
5953
Duncan Sands92c43912008-06-06 12:08:01 +00005954 MVT SCVT = SignedConv.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005955 if (SCVT != DestTy) {
5956 // Destination type needs to be expanded as well. The FADD now we are
5957 // constructing will be expanded into a libcall.
Duncan Sands92c43912008-06-06 12:08:01 +00005958 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5959 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmanc98645c2008-03-05 01:08:17 +00005960 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005961 SignedConv, SignedConv.getValue(1));
5962 }
5963 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5964 }
5965 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5966 }
5967
5968 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmanc98645c2008-03-05 01:08:17 +00005969 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005970 default: assert(0 && "This action not implemented for this operation!");
5971 case TargetLowering::Legal:
5972 case TargetLowering::Expand:
5973 break; // This case is handled below.
5974 case TargetLowering::Custom: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005975 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005976 Source), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005977 if (NV.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005978 return LegalizeOp(NV);
5979 break; // The target decided this was legal after all
5980 }
5981 }
5982
5983 // Expand the source, then glue it back together for the call. We must expand
5984 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman8b232ff2008-03-11 01:59:03 +00005985 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005986 SDValue SrcLo, SrcHi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005987 ExpandOp(Source, SrcLo, SrcHi);
5988 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5989 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005990
Duncan Sandsf68dffb2008-07-17 02:36:29 +00005991 RTLIB::Libcall LC = isSigned ?
5992 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5993 RTLIB::getUINTTOFP(SourceVT, DestTy);
5994 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5995
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005996 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman8181bd12008-07-27 21:46:04 +00005997 SDValue HiPart;
Gabor Greif1c80d112008-08-28 21:40:38 +00005998 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5999 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmanec51f642008-03-10 23:03:31 +00006000 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
6001 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006002}
6003
6004/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
6005/// INT_TO_FP operation of the specified operand when the target requests that
6006/// we expand it. At this point, we know that the result and operand types are
6007/// legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00006008SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
6009 SDValue Op0,
6010 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006011 if (Op0.getValueType() == MVT::i32) {
6012 // simple 32-bit [signed|unsigned] integer to float/double expansion
6013
Chris Lattner0aeb1d02008-01-16 07:03:22 +00006014 // Get the stack frame index of a 8 byte buffer.
Dan Gohman8181bd12008-07-27 21:46:04 +00006015 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner0aeb1d02008-01-16 07:03:22 +00006016
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006017 // word offset constant for Hi/Lo address computation
Dan Gohman8181bd12008-07-27 21:46:04 +00006018 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006019 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman8181bd12008-07-27 21:46:04 +00006020 SDValue Hi = StackSlot;
6021 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006022 if (TLI.isLittleEndian())
6023 std::swap(Hi, Lo);
6024
6025 // if signed map to unsigned space
Dan Gohman8181bd12008-07-27 21:46:04 +00006026 SDValue Op0Mapped;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006027 if (isSigned) {
6028 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman8181bd12008-07-27 21:46:04 +00006029 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006030 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
6031 } else {
6032 Op0Mapped = Op0;
6033 }
6034 // store the lo of the constructed double - based on integer input
Dan Gohman8181bd12008-07-27 21:46:04 +00006035 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006036 Op0Mapped, Lo, NULL, 0);
6037 // initial hi portion of constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00006038 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006039 // store the hi of the constructed double - biased exponent
Dan Gohman8181bd12008-07-27 21:46:04 +00006040 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006041 // load the constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00006042 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006043 // FP constant to bias correct the final result
Dan Gohman8181bd12008-07-27 21:46:04 +00006044 SDValue Bias = DAG.getConstantFP(isSigned ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006045 BitsToDouble(0x4330000080000000ULL)
6046 : BitsToDouble(0x4330000000000000ULL),
6047 MVT::f64);
6048 // subtract the bias
Dan Gohman8181bd12008-07-27 21:46:04 +00006049 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006050 // final result
Dan Gohman8181bd12008-07-27 21:46:04 +00006051 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006052 // handle final rounding
6053 if (DestVT == MVT::f64) {
6054 // do nothing
6055 Result = Sub;
Duncan Sandsec142ee2008-06-08 20:54:56 +00006056 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner5872a362008-01-17 07:00:52 +00006057 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
6058 DAG.getIntPtrConstant(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00006059 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenb17a7a22007-09-16 16:51:49 +00006060 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006061 }
6062 return Result;
6063 }
6064 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman8181bd12008-07-27 21:46:04 +00006065 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006066
Duncan Sands4a361272009-01-01 15:52:00 +00006067 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0.getValueType()),
6068 Op0, DAG.getConstant(0, Op0.getValueType()),
6069 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00006070 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
6071 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006072 SignSet, Four, Zero);
6073
6074 // If the sign bit of the integer is set, the large number will be treated
6075 // as a negative number. To counteract this, the dynamic code adds an
6076 // offset depending on the data type.
6077 uint64_t FF;
Duncan Sands92c43912008-06-06 12:08:01 +00006078 switch (Op0.getValueType().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006079 default: assert(0 && "Unsupported integer type!");
6080 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6081 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6082 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6083 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6084 }
6085 if (TLI.isLittleEndian()) FF <<= 32;
6086 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
6087
Dan Gohman8181bd12008-07-27 21:46:04 +00006088 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00006089 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006090 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00006091 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00006092 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006093 if (DestVT == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00006094 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00006095 PseudoSourceValue::getConstantPool(), 0,
6096 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006097 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00006098 FudgeInReg =
6099 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
6100 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006101 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00006102 MVT::f32, false, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006103 }
6104
6105 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
6106}
6107
6108/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6109/// *INT_TO_FP operation of the specified operand when the target requests that
6110/// we promote it. At this point, we know that the result and operand types are
6111/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6112/// operation that takes a larger input.
Dan Gohman8181bd12008-07-27 21:46:04 +00006113SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6114 MVT DestVT,
6115 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006116 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00006117 MVT NewInTy = LegalOp.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006118
6119 unsigned OpToUse = 0;
6120
6121 // Scan for the appropriate larger type to use.
6122 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00006123 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6124 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006125
6126 // If the target supports SINT_TO_FP of this type, use it.
6127 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6128 default: break;
6129 case TargetLowering::Legal:
6130 if (!TLI.isTypeLegal(NewInTy))
6131 break; // Can't use this datatype.
6132 // FALL THROUGH.
6133 case TargetLowering::Custom:
6134 OpToUse = ISD::SINT_TO_FP;
6135 break;
6136 }
6137 if (OpToUse) break;
6138 if (isSigned) continue;
6139
6140 // If the target supports UINT_TO_FP of this type, use it.
6141 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6142 default: break;
6143 case TargetLowering::Legal:
6144 if (!TLI.isTypeLegal(NewInTy))
6145 break; // Can't use this datatype.
6146 // FALL THROUGH.
6147 case TargetLowering::Custom:
6148 OpToUse = ISD::UINT_TO_FP;
6149 break;
6150 }
6151 if (OpToUse) break;
6152
6153 // Otherwise, try a larger type.
6154 }
6155
6156 // Okay, we found the operation and type to use. Zero extend our input to the
6157 // desired type then run the operation on it.
6158 return DAG.getNode(OpToUse, DestVT,
6159 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
6160 NewInTy, LegalOp));
6161}
6162
6163/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6164/// FP_TO_*INT operation of the specified operand when the target requests that
6165/// we promote it. At this point, we know that the result and operand types are
6166/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6167/// operation that returns a larger result.
Dan Gohman8181bd12008-07-27 21:46:04 +00006168SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6169 MVT DestVT,
6170 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006171 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00006172 MVT NewOutTy = DestVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006173
6174 unsigned OpToUse = 0;
6175
6176 // Scan for the appropriate larger type to use.
6177 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00006178 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6179 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006180
6181 // If the target supports FP_TO_SINT returning this type, use it.
6182 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6183 default: break;
6184 case TargetLowering::Legal:
6185 if (!TLI.isTypeLegal(NewOutTy))
6186 break; // Can't use this datatype.
6187 // FALL THROUGH.
6188 case TargetLowering::Custom:
6189 OpToUse = ISD::FP_TO_SINT;
6190 break;
6191 }
6192 if (OpToUse) break;
6193
6194 // If the target supports FP_TO_UINT of this type, use it.
6195 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6196 default: break;
6197 case TargetLowering::Legal:
6198 if (!TLI.isTypeLegal(NewOutTy))
6199 break; // Can't use this datatype.
6200 // FALL THROUGH.
6201 case TargetLowering::Custom:
6202 OpToUse = ISD::FP_TO_UINT;
6203 break;
6204 }
6205 if (OpToUse) break;
6206
6207 // Otherwise, try a larger type.
6208 }
6209
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006210
6211 // Okay, we found the operation and type to use.
Dan Gohman8181bd12008-07-27 21:46:04 +00006212 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sandsac496a12008-07-04 11:47:58 +00006213
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006214 // If the operation produces an invalid type, it must be custom lowered. Use
6215 // the target lowering hooks to expand it. Just keep the low part of the
6216 // expanded operation, we know that we're truncating anyway.
6217 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006218 SmallVector<SDValue, 2> Results;
6219 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6220 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6221 Operation = Results[0];
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006222 }
Duncan Sandsac496a12008-07-04 11:47:58 +00006223
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006224 // Truncate the result of the extended FP_TO_*INT operation to the desired
6225 // size.
6226 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006227}
6228
6229/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6230///
Dan Gohman8181bd12008-07-27 21:46:04 +00006231SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00006232 MVT VT = Op.getValueType();
6233 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00006234 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands92c43912008-06-06 12:08:01 +00006235 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006236 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6237 case MVT::i16:
6238 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6239 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6240 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6241 case MVT::i32:
6242 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6243 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6244 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6245 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6246 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6247 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6248 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6249 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6250 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6251 case MVT::i64:
6252 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6253 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6254 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6255 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6256 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6257 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6258 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6259 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6260 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6261 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6262 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6263 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6264 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6265 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6266 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6267 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6268 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6269 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6270 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6271 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6272 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6273 }
6274}
6275
6276/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6277///
Dan Gohman8181bd12008-07-27 21:46:04 +00006278SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006279 switch (Opc) {
6280 default: assert(0 && "Cannot expand this yet!");
6281 case ISD::CTPOP: {
6282 static const uint64_t mask[6] = {
6283 0x5555555555555555ULL, 0x3333333333333333ULL,
6284 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6285 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6286 };
Duncan Sands92c43912008-06-06 12:08:01 +00006287 MVT VT = Op.getValueType();
6288 MVT ShVT = TLI.getShiftAmountTy();
6289 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006290 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6291 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman8181bd12008-07-27 21:46:04 +00006292 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6293 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006294 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6295 DAG.getNode(ISD::AND, VT,
6296 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6297 }
6298 return Op;
6299 }
6300 case ISD::CTLZ: {
6301 // for now, we do this:
6302 // x = x | (x >> 1);
6303 // x = x | (x >> 2);
6304 // ...
6305 // x = x | (x >>16);
6306 // x = x | (x >>32); // for 64-bit input
6307 // return popcount(~x);
6308 //
6309 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006310 MVT VT = Op.getValueType();
6311 MVT ShVT = TLI.getShiftAmountTy();
6312 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006313 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006314 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006315 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6316 }
6317 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6318 return DAG.getNode(ISD::CTPOP, VT, Op);
6319 }
6320 case ISD::CTTZ: {
6321 // for now, we use: { return popcount(~x & (x - 1)); }
6322 // unless the target has ctlz but not ctpop, in which case we use:
6323 // { return 32 - nlz(~x & (x-1)); }
6324 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006325 MVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006326 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6327 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006328 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6329 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6330 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6331 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6332 TLI.isOperationLegal(ISD::CTLZ, VT))
6333 return DAG.getNode(ISD::SUB, VT,
Duncan Sands92c43912008-06-06 12:08:01 +00006334 DAG.getConstant(VT.getSizeInBits(), VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006335 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6336 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6337 }
6338 }
6339}
6340
Dan Gohman8181bd12008-07-27 21:46:04 +00006341/// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006342/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman4fc03742008-10-01 15:07:49 +00006343/// LegalizedNodes map is filled in for any results that are not expanded, the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006344/// ExpandedNodes map is filled in for any results that are expanded, and the
6345/// Lo/Hi values are returned.
Dan Gohman8181bd12008-07-27 21:46:04 +00006346void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands92c43912008-06-06 12:08:01 +00006347 MVT VT = Op.getValueType();
6348 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greif1c80d112008-08-28 21:40:38 +00006349 SDNode *Node = Op.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006350 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00006351 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands92c43912008-06-06 12:08:01 +00006352 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006353
6354 // See if we already expanded it.
Dan Gohman8181bd12008-07-27 21:46:04 +00006355 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006356 = ExpandedNodes.find(Op);
6357 if (I != ExpandedNodes.end()) {
6358 Lo = I->second.first;
6359 Hi = I->second.second;
6360 return;
6361 }
6362
6363 switch (Node->getOpcode()) {
6364 case ISD::CopyFromReg:
6365 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006366 case ISD::FP_ROUND_INREG:
6367 if (VT == MVT::ppcf128 &&
6368 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6369 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006370 SDValue SrcLo, SrcHi, Src;
Dale Johannesend3b6af32007-10-11 23:32:15 +00006371 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6372 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006373 SDValue Result = TLI.LowerOperation(
Dale Johannesend3b6af32007-10-11 23:32:15 +00006374 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006375 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6376 Lo = Result.getNode()->getOperand(0);
6377 Hi = Result.getNode()->getOperand(1);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006378 break;
6379 }
6380 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006381 default:
6382#ifndef NDEBUG
6383 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
6384#endif
6385 assert(0 && "Do not know how to expand this operator!");
6386 abort();
Dan Gohman550c8462008-02-27 01:52:30 +00006387 case ISD::EXTRACT_ELEMENT:
6388 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006389 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman550c8462008-02-27 01:52:30 +00006390 return ExpandOp(Hi, Lo, Hi);
Dan Gohman7e7aa2c2008-02-27 19:44:57 +00006391 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006392 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006393 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6394 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6395 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006396 case ISD::UNDEF:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006397 Lo = DAG.getNode(ISD::UNDEF, NVT);
6398 Hi = DAG.getNode(ISD::UNDEF, NVT);
6399 break;
6400 case ISD::Constant: {
Duncan Sands92c43912008-06-06 12:08:01 +00006401 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman97f1f8e2008-03-03 22:20:46 +00006402 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6403 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6404 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006405 break;
6406 }
6407 case ISD::ConstantFP: {
6408 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00006409 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00006410 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen2aef5692007-10-11 18:07:22 +00006411 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6412 MVT::f64);
6413 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6414 MVT::f64);
6415 break;
6416 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006417 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
6418 if (getTypeAction(Lo.getValueType()) == Expand)
6419 ExpandOp(Lo, Lo, Hi);
6420 break;
6421 }
6422 case ISD::BUILD_PAIR:
6423 // Return the operands.
6424 Lo = Node->getOperand(0);
6425 Hi = Node->getOperand(1);
6426 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006427
6428 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00006429 if (Node->getNumValues() == 1) {
6430 ExpandOp(Op.getOperand(0), Lo, Hi);
6431 break;
6432 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006433 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif46bf5472008-08-26 22:36:50 +00006434 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006435 Op.getValue(1).getValueType() == MVT::Other &&
6436 "unhandled MERGE_VALUES");
6437 ExpandOp(Op.getOperand(0), Lo, Hi);
6438 // Remember that we legalized the chain.
6439 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6440 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006441
6442 case ISD::SIGN_EXTEND_INREG:
6443 ExpandOp(Node->getOperand(0), Lo, Hi);
6444 // sext_inreg the low part if needed.
6445 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6446
6447 // The high part gets the sign extension from the lo-part. This handles
6448 // things like sextinreg V:i64 from i8.
6449 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands92c43912008-06-06 12:08:01 +00006450 DAG.getConstant(NVT.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006451 TLI.getShiftAmountTy()));
6452 break;
6453
6454 case ISD::BSWAP: {
6455 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006456 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006457 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6458 Lo = TempLo;
6459 break;
6460 }
6461
6462 case ISD::CTPOP:
6463 ExpandOp(Node->getOperand(0), Lo, Hi);
6464 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6465 DAG.getNode(ISD::CTPOP, NVT, Lo),
6466 DAG.getNode(ISD::CTPOP, NVT, Hi));
6467 Hi = DAG.getConstant(0, NVT);
6468 break;
6469
6470 case ISD::CTLZ: {
6471 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
6472 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006473 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6474 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Duncan Sands4a361272009-01-01 15:52:00 +00006475 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), HLZ, BitsC,
6476 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006477 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006478 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6479
6480 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6481 Hi = DAG.getConstant(0, NVT);
6482 break;
6483 }
6484
6485 case ISD::CTTZ: {
6486 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
6487 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006488 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6489 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Duncan Sands4a361272009-01-01 15:52:00 +00006490 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), LTZ, BitsC,
6491 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00006492 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006493 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6494
6495 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6496 Hi = DAG.getConstant(0, NVT);
6497 break;
6498 }
6499
6500 case ISD::VAARG: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006501 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6502 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006503 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6504 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6505
6506 // Remember that we legalized the chain.
6507 Hi = LegalizeOp(Hi);
6508 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006509 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006510 std::swap(Lo, Hi);
6511 break;
6512 }
6513
6514 case ISD::LOAD: {
6515 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00006516 SDValue Ch = LD->getChain(); // Legalize the chain.
6517 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006518 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman29c3cef2008-08-14 20:04:46 +00006519 const Value *SV = LD->getSrcValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006520 int SVOffset = LD->getSrcValueOffset();
6521 unsigned Alignment = LD->getAlignment();
6522 bool isVolatile = LD->isVolatile();
6523
6524 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman29c3cef2008-08-14 20:04:46 +00006525 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006526 isVolatile, Alignment);
6527 if (VT == MVT::f32 || VT == MVT::f64) {
6528 // f32->i32 or f64->i64 one to one expansion.
6529 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006530 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006531 // Recursively expand the new load.
6532 if (getTypeAction(NVT) == Expand)
6533 ExpandOp(Lo, Lo, Hi);
6534 break;
6535 }
6536
6537 // Increment the pointer to the other half.
Duncan Sands92c43912008-06-06 12:08:01 +00006538 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006539 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006540 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006541 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006542 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00006543 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006544 isVolatile, Alignment);
6545
6546 // Build a factor node to remember that this load is independent of the
6547 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00006548 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006549 Hi.getValue(1));
6550
6551 // Remember that we legalized the chain.
6552 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006553 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006554 std::swap(Lo, Hi);
6555 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00006556 MVT EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006557
Dale Johannesen2550e3a2007-10-19 20:29:00 +00006558 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6559 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006560 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman29c3cef2008-08-14 20:04:46 +00006561 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006562 SVOffset, isVolatile, Alignment);
6563 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006564 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006565 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6566 break;
6567 }
6568
6569 if (EVT == NVT)
Dan Gohman29c3cef2008-08-14 20:04:46 +00006570 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006571 SVOffset, isVolatile, Alignment);
6572 else
Dan Gohman29c3cef2008-08-14 20:04:46 +00006573 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006574 SVOffset, EVT, isVolatile,
6575 Alignment);
6576
6577 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006578 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006579
6580 if (ExtType == ISD::SEXTLOAD) {
6581 // The high part is obtained by SRA'ing all but one of the bits of the
6582 // lo part.
Duncan Sands92c43912008-06-06 12:08:01 +00006583 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006584 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6585 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6586 } else if (ExtType == ISD::ZEXTLOAD) {
6587 // The high part is just a zero.
6588 Hi = DAG.getConstant(0, NVT);
6589 } else /* if (ExtType == ISD::EXTLOAD) */ {
6590 // The high part is undefined.
6591 Hi = DAG.getNode(ISD::UNDEF, NVT);
6592 }
6593 }
6594 break;
6595 }
6596 case ISD::AND:
6597 case ISD::OR:
6598 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +00006599 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006600 ExpandOp(Node->getOperand(0), LL, LH);
6601 ExpandOp(Node->getOperand(1), RL, RH);
6602 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6603 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6604 break;
6605 }
6606 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006607 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006608 ExpandOp(Node->getOperand(1), LL, LH);
6609 ExpandOp(Node->getOperand(2), RL, RH);
6610 if (getTypeAction(NVT) == Expand)
6611 NVT = TLI.getTypeToExpandTo(NVT);
6612 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
6613 if (VT != MVT::f32)
6614 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
6615 break;
6616 }
6617 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006618 SDValue TL, TH, FL, FH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006619 ExpandOp(Node->getOperand(2), TL, TH);
6620 ExpandOp(Node->getOperand(3), FL, FH);
6621 if (getTypeAction(NVT) == Expand)
6622 NVT = TLI.getTypeToExpandTo(NVT);
6623 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6624 Node->getOperand(1), TL, FL, Node->getOperand(4));
6625 if (VT != MVT::f32)
6626 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6627 Node->getOperand(1), TH, FH, Node->getOperand(4));
6628 break;
6629 }
6630 case ISD::ANY_EXTEND:
6631 // The low part is any extension of the input (which degenerates to a copy).
6632 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6633 // The high part is undefined.
6634 Hi = DAG.getNode(ISD::UNDEF, NVT);
6635 break;
6636 case ISD::SIGN_EXTEND: {
6637 // The low part is just a sign extension of the input (which degenerates to
6638 // a copy).
6639 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
6640
6641 // The high part is obtained by SRA'ing all but one of the bits of the lo
6642 // part.
Duncan Sands92c43912008-06-06 12:08:01 +00006643 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006644 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6645 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6646 break;
6647 }
6648 case ISD::ZERO_EXTEND:
6649 // The low part is just a zero extension of the input (which degenerates to
6650 // a copy).
6651 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
6652
6653 // The high part is just a zero.
6654 Hi = DAG.getConstant(0, NVT);
6655 break;
6656
6657 case ISD::TRUNCATE: {
6658 // The input value must be larger than this value. Expand *it*.
Dan Gohman8181bd12008-07-27 21:46:04 +00006659 SDValue NewLo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006660 ExpandOp(Node->getOperand(0), NewLo, Hi);
6661
6662 // The low part is now either the right size, or it is closer. If not the
6663 // right size, make an illegal truncate so we recursively expand it.
6664 if (NewLo.getValueType() != Node->getValueType(0))
6665 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6666 ExpandOp(NewLo, Lo, Hi);
6667 break;
6668 }
6669
6670 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006671 SDValue Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006672 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6673 // If the target wants to, allow it to lower this itself.
6674 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6675 case Expand: assert(0 && "cannot expand FP!");
6676 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6677 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6678 }
6679 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6680 }
6681
6682 // f32 / f64 must be expanded to i32 / i64.
6683 if (VT == MVT::f32 || VT == MVT::f64) {
6684 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6685 if (getTypeAction(NVT) == Expand)
6686 ExpandOp(Lo, Lo, Hi);
6687 break;
6688 }
6689
6690 // If source operand will be expanded to the same type as VT, i.e.
6691 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands92c43912008-06-06 12:08:01 +00006692 MVT VT0 = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006693 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6694 ExpandOp(Node->getOperand(0), Lo, Hi);
6695 break;
6696 }
6697
6698 // Turn this into a load/store pair by default.
Gabor Greif1c80d112008-08-28 21:40:38 +00006699 if (Tmp.getNode() == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00006700 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006701
6702 ExpandOp(Tmp, Lo, Hi);
6703 break;
6704 }
6705
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006706 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006707 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6708 TargetLowering::Custom &&
6709 "Must custom expand ReadCycleCounter");
Dan Gohman8181bd12008-07-27 21:46:04 +00006710 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006711 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006712 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006713 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006714 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006715 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006716 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006717
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006718 case ISD::ATOMIC_CMP_SWAP: {
Dale Johannesen44eb5372008-10-03 19:41:08 +00006719 // This operation does not need a loop.
6720 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6721 assert(Tmp.getNode() && "Node must be custom expanded!");
6722 ExpandOp(Tmp.getValue(0), Lo, Hi);
6723 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6724 LegalizeOp(Tmp.getValue(1)));
6725 break;
6726 }
6727
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006728 case ISD::ATOMIC_LOAD_ADD:
6729 case ISD::ATOMIC_LOAD_SUB:
6730 case ISD::ATOMIC_LOAD_AND:
6731 case ISD::ATOMIC_LOAD_OR:
6732 case ISD::ATOMIC_LOAD_XOR:
6733 case ISD::ATOMIC_LOAD_NAND:
6734 case ISD::ATOMIC_SWAP: {
Dale Johannesen44eb5372008-10-03 19:41:08 +00006735 // These operations require a loop to be generated. We can't do that yet,
6736 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesenf160d802008-10-02 18:53:47 +00006737 SDValue In2Lo, In2Hi, In2;
6738 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6739 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006740 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6741 SDValue Replace =
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006742 DAG.getAtomic(Op.getOpcode(), Anode->getMemoryVT(),
6743 Op.getOperand(0), Op.getOperand(1), In2,
Dale Johannesen44eb5372008-10-03 19:41:08 +00006744 Anode->getSrcValue(), Anode->getAlignment());
6745 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006746 ExpandOp(Result.getValue(0), Lo, Hi);
6747 // Remember that we legalized the chain.
6748 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharth81580822008-03-05 01:15:49 +00006749 break;
6750 }
6751
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006752 // These operators cannot be expanded directly, emit them as calls to
6753 // library functions.
6754 case ISD::FP_TO_SINT: {
6755 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006756 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006757 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6758 case Expand: assert(0 && "cannot expand FP!");
6759 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6760 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6761 }
6762
6763 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6764
6765 // Now that the custom expander is done, expand the result, which is still
6766 // VT.
Gabor Greif1c80d112008-08-28 21:40:38 +00006767 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006768 ExpandOp(Op, Lo, Hi);
6769 break;
6770 }
6771 }
6772
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006773 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6774 VT);
6775 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6776 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006777 break;
6778 }
6779
6780 case ISD::FP_TO_UINT: {
6781 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006782 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006783 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6784 case Expand: assert(0 && "cannot expand FP!");
6785 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6786 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6787 }
6788
6789 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6790
6791 // Now that the custom expander is done, expand the result.
Gabor Greif1c80d112008-08-28 21:40:38 +00006792 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006793 ExpandOp(Op, Lo, Hi);
6794 break;
6795 }
6796 }
6797
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006798 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6799 VT);
6800 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6801 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006802 break;
6803 }
6804
6805 case ISD::SHL: {
6806 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006807 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006808 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006809 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006810 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006811 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006812 // Now that the custom expander is done, expand the result, which is
6813 // still VT.
6814 ExpandOp(Op, Lo, Hi);
6815 break;
6816 }
6817 }
6818
6819 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6820 // this X << 1 as X+X.
6821 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00006822 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006823 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006824 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006825 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6826 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6827 LoOps[1] = LoOps[0];
6828 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6829
6830 HiOps[1] = HiOps[0];
6831 HiOps[2] = Lo.getValue(1);
6832 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6833 break;
6834 }
6835 }
6836
6837 // If we can emit an efficient shift operation, do so now.
6838 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6839 break;
6840
6841 // If this target supports SHL_PARTS, use it.
6842 TargetLowering::LegalizeAction Action =
6843 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6844 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6845 Action == TargetLowering::Custom) {
6846 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6847 break;
6848 }
6849
6850 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006851 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006852 break;
6853 }
6854
6855 case ISD::SRA: {
6856 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006857 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006858 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006859 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006860 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006861 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006862 // Now that the custom expander is done, expand the result, which is
6863 // still VT.
6864 ExpandOp(Op, Lo, Hi);
6865 break;
6866 }
6867 }
6868
6869 // If we can emit an efficient shift operation, do so now.
6870 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6871 break;
6872
6873 // If this target supports SRA_PARTS, use it.
6874 TargetLowering::LegalizeAction Action =
6875 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6876 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6877 Action == TargetLowering::Custom) {
6878 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6879 break;
6880 }
6881
6882 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006883 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006884 break;
6885 }
6886
6887 case ISD::SRL: {
6888 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006889 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006890 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006891 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006892 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006893 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006894 // Now that the custom expander is done, expand the result, which is
6895 // still VT.
6896 ExpandOp(Op, Lo, Hi);
6897 break;
6898 }
6899 }
6900
6901 // If we can emit an efficient shift operation, do so now.
6902 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6903 break;
6904
6905 // If this target supports SRL_PARTS, use it.
6906 TargetLowering::LegalizeAction Action =
6907 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6908 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6909 Action == TargetLowering::Custom) {
6910 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6911 break;
6912 }
6913
6914 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006915 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006916 break;
6917 }
6918
6919 case ISD::ADD:
6920 case ISD::SUB: {
6921 // If the target wants to custom expand this, let them.
6922 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6923 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006924 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006925 if (Result.getNode()) {
Duncan Sands4c3885b2008-06-22 09:42:16 +00006926 ExpandOp(Result, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006927 break;
6928 }
6929 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006930 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006931 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006932 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6933 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman8181bd12008-07-27 21:46:04 +00006934 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006935 LoOps[0] = LHSL;
6936 LoOps[1] = RHSL;
6937 HiOps[0] = LHSH;
6938 HiOps[1] = RHSH;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006939
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006940 //cascaded check to see if any smaller size has a a carry flag.
6941 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6942 bool hasCarry = false;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00006943 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6944 MVT AVT = MVT::getIntegerVT(BitSize);
6945 if (TLI.isOperationLegal(OpV, AVT)) {
6946 hasCarry = true;
6947 break;
6948 }
6949 }
6950
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006951 if(hasCarry) {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00006952 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006953 if (Node->getOpcode() == ISD::ADD) {
6954 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6955 HiOps[2] = Lo.getValue(1);
6956 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6957 } else {
6958 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6959 HiOps[2] = Lo.getValue(1);
6960 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6961 }
6962 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006963 } else {
Andrew Lenharth5e814462008-10-07 14:15:42 +00006964 if (Node->getOpcode() == ISD::ADD) {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00006965 Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
6966 Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
Duncan Sands4a361272009-01-01 15:52:00 +00006967 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(NVT),
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006968 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006969 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6970 DAG.getConstant(1, NVT),
6971 DAG.getConstant(0, NVT));
Duncan Sands4a361272009-01-01 15:52:00 +00006972 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(NVT),
Andrew Lenhartha23d6992008-10-07 17:03:15 +00006973 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006974 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6975 DAG.getConstant(1, NVT),
6976 Carry1);
6977 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6978 } else {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00006979 Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
6980 Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
Andrew Lenharth5e814462008-10-07 14:15:42 +00006981 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6982 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6983 DAG.getConstant(1, NVT),
6984 DAG.getConstant(0, NVT));
6985 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6986 }
6987 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006988 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006989 }
6990
6991 case ISD::ADDC:
6992 case ISD::SUBC: {
6993 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006994 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006995 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6996 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6997 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006998 SDValue LoOps[2] = { LHSL, RHSL };
6999 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007000
7001 if (Node->getOpcode() == ISD::ADDC) {
7002 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
7003 HiOps[2] = Lo.getValue(1);
7004 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
7005 } else {
7006 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
7007 HiOps[2] = Lo.getValue(1);
7008 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
7009 }
7010 // Remember that we legalized the flag.
7011 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7012 break;
7013 }
7014 case ISD::ADDE:
7015 case ISD::SUBE: {
7016 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00007017 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007018 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7019 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7020 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00007021 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
7022 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007023
7024 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
7025 HiOps[2] = Lo.getValue(1);
7026 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
7027
7028 // Remember that we legalized the flag.
7029 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7030 break;
7031 }
7032 case ISD::MUL: {
7033 // If the target wants to custom expand this, let them.
7034 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007035 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00007036 if (New.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007037 ExpandOp(New, Lo, Hi);
7038 break;
7039 }
7040 }
7041
7042 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
7043 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00007044 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
7045 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
7046 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007047 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007048 ExpandOp(Node->getOperand(0), LL, LH);
7049 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman07961cd2008-02-25 21:11:39 +00007050 unsigned OuterBitSize = Op.getValueSizeInBits();
7051 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman5a199552007-10-08 18:33:35 +00007052 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
7053 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman2594d942008-03-10 20:42:19 +00007054 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
7055 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
7056 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman5a199552007-10-08 18:33:35 +00007057 // The inputs are both zero-extended.
7058 if (HasUMUL_LOHI) {
7059 // We can emit a umul_lohi.
7060 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00007061 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00007062 break;
7063 }
7064 if (HasMULHU) {
7065 // We can emit a mulhu+mul.
7066 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7067 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7068 break;
7069 }
Dan Gohman5a199552007-10-08 18:33:35 +00007070 }
Dan Gohman07961cd2008-02-25 21:11:39 +00007071 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman5a199552007-10-08 18:33:35 +00007072 // The input values are both sign-extended.
7073 if (HasSMUL_LOHI) {
7074 // We can emit a smul_lohi.
7075 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00007076 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00007077 break;
7078 }
7079 if (HasMULHS) {
7080 // We can emit a mulhs+mul.
7081 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7082 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
7083 break;
7084 }
7085 }
7086 if (HasUMUL_LOHI) {
7087 // Lo,Hi = umul LHS, RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00007088 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman5a199552007-10-08 18:33:35 +00007089 DAG.getVTList(NVT, NVT), LL, RL);
7090 Lo = UMulLOHI;
7091 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007092 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7093 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7094 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7095 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7096 break;
7097 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00007098 if (HasMULHU) {
7099 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7100 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7101 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7102 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7103 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7104 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7105 break;
7106 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007107 }
7108
Dan Gohman5a199552007-10-08 18:33:35 +00007109 // If nothing else, we can make a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007110 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007111 break;
7112 }
7113 case ISD::SDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007114 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007115 break;
7116 case ISD::UDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007117 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007118 break;
7119 case ISD::SREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007120 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007121 break;
7122 case ISD::UREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007123 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007124 break;
7125
7126 case ISD::FADD:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007127 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7128 RTLIB::ADD_F64,
7129 RTLIB::ADD_F80,
7130 RTLIB::ADD_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007131 Node, false, Hi);
7132 break;
7133 case ISD::FSUB:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007134 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7135 RTLIB::SUB_F64,
7136 RTLIB::SUB_F80,
7137 RTLIB::SUB_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007138 Node, false, Hi);
7139 break;
7140 case ISD::FMUL:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007141 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7142 RTLIB::MUL_F64,
7143 RTLIB::MUL_F80,
7144 RTLIB::MUL_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007145 Node, false, Hi);
7146 break;
7147 case ISD::FDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007148 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7149 RTLIB::DIV_F64,
7150 RTLIB::DIV_F80,
7151 RTLIB::DIV_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007152 Node, false, Hi);
7153 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007154 case ISD::FP_EXTEND: {
Dale Johannesen4c14d512007-10-12 01:37:08 +00007155 if (VT == MVT::ppcf128) {
7156 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7157 Node->getOperand(0).getValueType()==MVT::f64);
7158 const uint64_t zero = 0;
7159 if (Node->getOperand(0).getValueType()==MVT::f32)
7160 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
7161 else
7162 Hi = Node->getOperand(0);
7163 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7164 break;
7165 }
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007166 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7167 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7168 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007169 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007170 }
7171 case ISD::FP_ROUND: {
7172 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7173 VT);
7174 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7175 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007176 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007177 }
Evan Cheng5316b392008-09-09 23:02:14 +00007178 case ISD::FSQRT:
7179 case ISD::FSIN:
7180 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007181 case ISD::FLOG:
7182 case ISD::FLOG2:
7183 case ISD::FLOG10:
7184 case ISD::FEXP:
7185 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00007186 case ISD::FTRUNC:
7187 case ISD::FFLOOR:
7188 case ISD::FCEIL:
7189 case ISD::FRINT:
7190 case ISD::FNEARBYINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00007191 case ISD::FPOW:
7192 case ISD::FPOWI: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007193 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
7194 switch(Node->getOpcode()) {
7195 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00007196 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7197 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007198 break;
7199 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00007200 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7201 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007202 break;
7203 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00007204 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7205 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007206 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00007207 case ISD::FLOG:
7208 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7209 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7210 break;
7211 case ISD::FLOG2:
7212 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7213 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7214 break;
7215 case ISD::FLOG10:
7216 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7217 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7218 break;
7219 case ISD::FEXP:
7220 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7221 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7222 break;
7223 case ISD::FEXP2:
7224 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7225 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7226 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00007227 case ISD::FTRUNC:
7228 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7229 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7230 break;
7231 case ISD::FFLOOR:
7232 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7233 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7234 break;
7235 case ISD::FCEIL:
7236 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7237 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7238 break;
7239 case ISD::FRINT:
7240 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7241 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7242 break;
7243 case ISD::FNEARBYINT:
7244 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7245 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7246 break;
Evan Cheng5316b392008-09-09 23:02:14 +00007247 case ISD::FPOW:
7248 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7249 RTLIB::POW_PPCF128);
7250 break;
7251 case ISD::FPOWI:
7252 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7253 RTLIB::POWI_PPCF128);
7254 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007255 default: assert(0 && "Unreachable!");
7256 }
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007257 Lo = ExpandLibCall(LC, Node, false, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007258 break;
7259 }
7260 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007261 if (VT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007262 SDValue Tmp;
Dale Johannesen5707ef82007-10-12 19:02:17 +00007263 ExpandOp(Node->getOperand(0), Lo, Tmp);
7264 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7265 // lo = hi==fabs(hi) ? lo : -lo;
7266 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7267 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7268 DAG.getCondCode(ISD::SETEQ));
7269 break;
7270 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007271 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007272 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7273 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7274 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7275 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7276 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7277 if (getTypeAction(NVT) == Expand)
7278 ExpandOp(Lo, Lo, Hi);
7279 break;
7280 }
7281 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007282 if (VT == MVT::ppcf128) {
7283 ExpandOp(Node->getOperand(0), Lo, Hi);
7284 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7285 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7286 break;
7287 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007288 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007289 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7290 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7291 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7292 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7293 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7294 if (getTypeAction(NVT) == Expand)
7295 ExpandOp(Lo, Lo, Hi);
7296 break;
7297 }
7298 case ISD::FCOPYSIGN: {
7299 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7300 if (getTypeAction(NVT) == Expand)
7301 ExpandOp(Lo, Lo, Hi);
7302 break;
7303 }
7304 case ISD::SINT_TO_FP:
7305 case ISD::UINT_TO_FP: {
7306 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands92c43912008-06-06 12:08:01 +00007307 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007308
7309 // Promote the operand if needed. Do this before checking for
7310 // ppcf128 so conversions of i16 and i8 work.
7311 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007312 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesen6a779c82008-03-18 17:28:38 +00007313 Tmp = isSigned
7314 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7315 DAG.getValueType(SrcVT))
7316 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greif1c80d112008-08-28 21:40:38 +00007317 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007318 SrcVT = Node->getOperand(0).getValueType();
7319 }
7320
Dan Gohmanec51f642008-03-10 23:03:31 +00007321 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman84d00962008-02-25 21:39:34 +00007322 static const uint64_t zero = 0;
Dale Johannesen4c14d512007-10-12 01:37:08 +00007323 if (isSigned) {
7324 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7325 Node->getOperand(0)));
7326 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7327 } else {
Dan Gohman84d00962008-02-25 21:39:34 +00007328 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen4c14d512007-10-12 01:37:08 +00007329 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7330 Node->getOperand(0)));
7331 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7332 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007333 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00007334 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7335 DAG.getConstant(0, MVT::i32),
7336 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7337 DAG.getConstantFP(
7338 APFloat(APInt(128, 2, TwoE32)),
7339 MVT::ppcf128)),
7340 Hi,
7341 DAG.getCondCode(ISD::SETLT)),
7342 Lo, Hi);
7343 }
7344 break;
7345 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007346 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7347 // si64->ppcf128 done by libcall, below
Dan Gohman84d00962008-02-25 21:39:34 +00007348 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007349 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7350 Lo, Hi);
7351 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7352 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7353 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7354 DAG.getConstant(0, MVT::i64),
7355 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7356 DAG.getConstantFP(
7357 APFloat(APInt(128, 2, TwoE64)),
7358 MVT::ppcf128)),
7359 Hi,
7360 DAG.getCondCode(ISD::SETLT)),
7361 Lo, Hi);
7362 break;
7363 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007364
Dan Gohmanec51f642008-03-10 23:03:31 +00007365 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7366 Node->getOperand(0));
Evan Chenga8740032008-04-01 01:50:16 +00007367 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng4a2f6df2008-04-01 01:51:26 +00007368 // float to i32 etc. can be 'expanded' to a single node.
Evan Chenga8740032008-04-01 01:50:16 +00007369 ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007370 break;
7371 }
7372 }
7373
7374 // Make sure the resultant values have been legalized themselves, unless this
7375 // is a type that requires multi-step expansion.
7376 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7377 Lo = LegalizeOp(Lo);
Gabor Greif1c80d112008-08-28 21:40:38 +00007378 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007379 // Don't legalize the high part if it is expanded to a single node.
7380 Hi = LegalizeOp(Hi);
7381 }
7382
7383 // Remember in a map if the values will be reused later.
Dan Gohman55d19662008-07-07 17:46:23 +00007384 bool isNew =
7385 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007386 assert(isNew && "Value already expanded?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007387 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007388}
7389
7390/// SplitVectorOp - Given an operand of vector type, break it down into
7391/// two smaller values, still of vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00007392void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7393 SDValue &Hi) {
Duncan Sands92c43912008-06-06 12:08:01 +00007394 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007395 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007396 unsigned NumElements = Op.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007397 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00007398
Duncan Sands92c43912008-06-06 12:08:01 +00007399 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman4a365ad2007-11-15 21:15:26 +00007400
7401 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7402 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7403
Duncan Sands92c43912008-06-06 12:08:01 +00007404 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7405 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007406
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007407 // See if we already split it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007408 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007409 = SplitNodes.find(Op);
7410 if (I != SplitNodes.end()) {
7411 Lo = I->second.first;
7412 Hi = I->second.second;
7413 return;
7414 }
7415
7416 switch (Node->getOpcode()) {
7417 default:
7418#ifndef NDEBUG
7419 Node->dump(&DAG);
7420#endif
7421 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00007422 case ISD::UNDEF:
7423 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7424 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7425 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007426 case ISD::BUILD_PAIR:
7427 Lo = Node->getOperand(0);
7428 Hi = Node->getOperand(1);
7429 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007430 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007431 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7432 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007433 unsigned Index = Idx->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007434 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007435 if (Index < NewNumElts_Lo)
7436 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7437 DAG.getIntPtrConstant(Index));
7438 else
7439 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7440 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7441 break;
7442 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007443 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007444 Node->getOperand(1),
7445 Node->getOperand(2));
7446 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007447 break;
7448 }
Chris Lattner587c46d2007-11-19 21:16:54 +00007449 case ISD::VECTOR_SHUFFLE: {
7450 // Build the low part.
Dan Gohman8181bd12008-07-27 21:46:04 +00007451 SDValue Mask = Node->getOperand(2);
7452 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +00007453 MVT PtrVT = TLI.getPointerTy();
Chris Lattner587c46d2007-11-19 21:16:54 +00007454
7455 // Insert all of the elements from the input that are needed. We use
7456 // buildvector of extractelement here because the input vectors will have
7457 // to be legalized, so this makes the code simpler.
7458 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007459 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007460 if (IdxNode.getOpcode() == ISD::UNDEF) {
7461 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7462 continue;
7463 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007464 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007465 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007466 if (Idx >= NumElements) {
7467 InVec = Node->getOperand(1);
7468 Idx -= NumElements;
7469 }
7470 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7471 DAG.getConstant(Idx, PtrVT)));
7472 }
7473 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7474 Ops.clear();
7475
7476 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007477 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007478 if (IdxNode.getOpcode() == ISD::UNDEF) {
7479 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7480 continue;
7481 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007482 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007483 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007484 if (Idx >= NumElements) {
7485 InVec = Node->getOperand(1);
7486 Idx -= NumElements;
7487 }
7488 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7489 DAG.getConstant(Idx, PtrVT)));
7490 }
Mon P Wang2e89b112008-07-25 01:30:26 +00007491 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner587c46d2007-11-19 21:16:54 +00007492 break;
7493 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007494 case ISD::BUILD_VECTOR: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007495 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00007496 Node->op_begin()+NewNumElts_Lo);
7497 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007498
Dan Gohman8181bd12008-07-27 21:46:04 +00007499 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007500 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007501 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007502 break;
7503 }
7504 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00007505 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007506 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7507 if (NewNumSubvectors == 1) {
7508 Lo = Node->getOperand(0);
7509 Hi = Node->getOperand(1);
7510 } else {
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007511 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7512 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007513 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007514
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007515 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007516 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00007517 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007518 }
7519 break;
7520 }
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007521 case ISD::EXTRACT_SUBVECTOR: {
7522 SDValue Vec = Op.getOperand(0);
7523 SDValue Idx = Op.getOperand(1);
7524 MVT IdxVT = Idx.getValueType();
7525
7526 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7527 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7528 if (CIdx) {
7529 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7530 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7531 IdxVT));
7532 } else {
7533 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7534 DAG.getConstant(NewNumElts_Lo, IdxVT));
7535 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7536 }
7537 break;
7538 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00007539 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007540 SDValue Cond = Node->getOperand(0);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007541
Dan Gohman8181bd12008-07-27 21:46:04 +00007542 SDValue LL, LH, RL, RH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007543 SplitVectorOp(Node->getOperand(1), LL, LH);
7544 SplitVectorOp(Node->getOperand(2), RL, RH);
7545
Duncan Sands92c43912008-06-06 12:08:01 +00007546 if (Cond.getValueType().isVector()) {
Dan Gohmand5d4c872007-10-17 14:48:28 +00007547 // Handle a vector merge.
Dan Gohman8181bd12008-07-27 21:46:04 +00007548 SDValue CL, CH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007549 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007550 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7551 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007552 } else {
7553 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00007554 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7555 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007556 }
7557 break;
7558 }
Chris Lattnerc7471452008-06-30 02:43:01 +00007559 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007560 SDValue CondLHS = Node->getOperand(0);
7561 SDValue CondRHS = Node->getOperand(1);
7562 SDValue CondCode = Node->getOperand(4);
Chris Lattnerc7471452008-06-30 02:43:01 +00007563
Dan Gohman8181bd12008-07-27 21:46:04 +00007564 SDValue LL, LH, RL, RH;
Chris Lattnerc7471452008-06-30 02:43:01 +00007565 SplitVectorOp(Node->getOperand(2), LL, LH);
7566 SplitVectorOp(Node->getOperand(3), RL, RH);
7567
7568 // Handle a simple select with vector operands.
7569 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7570 LL, RL, CondCode);
7571 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7572 LH, RH, CondCode);
7573 break;
7574 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00007575 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007576 SDValue LL, LH, RL, RH;
Nate Begeman9a1ce152008-05-12 19:40:03 +00007577 SplitVectorOp(Node->getOperand(0), LL, LH);
7578 SplitVectorOp(Node->getOperand(1), RL, RH);
7579 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7580 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7581 break;
7582 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007583 case ISD::ADD:
7584 case ISD::SUB:
7585 case ISD::MUL:
7586 case ISD::FADD:
7587 case ISD::FSUB:
7588 case ISD::FMUL:
7589 case ISD::SDIV:
7590 case ISD::UDIV:
7591 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007592 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007593 case ISD::AND:
7594 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00007595 case ISD::XOR:
7596 case ISD::UREM:
7597 case ISD::SREM:
Mon P Wang26342922008-12-18 20:03:17 +00007598 case ISD::FREM:
7599 case ISD::SHL:
7600 case ISD::SRA:
7601 case ISD::SRL: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007602 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007603 SplitVectorOp(Node->getOperand(0), LL, LH);
7604 SplitVectorOp(Node->getOperand(1), RL, RH);
7605
Nate Begeman4a365ad2007-11-15 21:15:26 +00007606 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7607 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007608 break;
7609 }
Dan Gohman29c3cef2008-08-14 20:04:46 +00007610 case ISD::FP_ROUND:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007611 case ISD::FPOWI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007612 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007613 SplitVectorOp(Node->getOperand(0), L, H);
7614
Nate Begeman4a365ad2007-11-15 21:15:26 +00007615 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7616 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00007617 break;
7618 }
7619 case ISD::CTTZ:
7620 case ISD::CTLZ:
7621 case ISD::CTPOP:
7622 case ISD::FNEG:
7623 case ISD::FABS:
7624 case ISD::FSQRT:
7625 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00007626 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007627 case ISD::FLOG:
7628 case ISD::FLOG2:
7629 case ISD::FLOG10:
7630 case ISD::FEXP:
7631 case ISD::FEXP2:
Nate Begeman78246ca2007-11-17 03:58:34 +00007632 case ISD::FP_TO_SINT:
7633 case ISD::FP_TO_UINT:
7634 case ISD::SINT_TO_FP:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007635 case ISD::UINT_TO_FP:
7636 case ISD::TRUNCATE:
7637 case ISD::ANY_EXTEND:
7638 case ISD::SIGN_EXTEND:
7639 case ISD::ZERO_EXTEND:
7640 case ISD::FP_EXTEND: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007641 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007642 SplitVectorOp(Node->getOperand(0), L, H);
7643
Nate Begeman4a365ad2007-11-15 21:15:26 +00007644 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7645 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00007646 break;
7647 }
Mon P Wang73d31542008-11-10 20:54:11 +00007648 case ISD::CONVERT_RNDSAT: {
7649 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7650 SDValue L, H;
7651 SplitVectorOp(Node->getOperand(0), L, H);
7652 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7653 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7654 SDValue STyOpL = DAG.getValueType(L.getValueType());
7655 SDValue STyOpH = DAG.getValueType(H.getValueType());
7656
7657 SDValue RndOp = Node->getOperand(3);
7658 SDValue SatOp = Node->getOperand(4);
7659
7660 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7661 RndOp, SatOp, CvtCode);
7662 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7663 RndOp, SatOp, CvtCode);
7664 break;
7665 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007666 case ISD::LOAD: {
7667 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007668 SDValue Ch = LD->getChain();
7669 SDValue Ptr = LD->getBasePtr();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007670 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007671 const Value *SV = LD->getSrcValue();
7672 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007673 MVT MemoryVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007674 unsigned Alignment = LD->getAlignment();
7675 bool isVolatile = LD->isVolatile();
7676
Dan Gohman29c3cef2008-08-14 20:04:46 +00007677 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7678 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7679
7680 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7681 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7682 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7683
7684 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7685 NewVT_Lo, Ch, Ptr, Offset,
7686 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7687 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007688 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00007689 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007690 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00007691 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00007692 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7693 NewVT_Hi, Ch, Ptr, Offset,
7694 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007695
7696 // Build a factor node to remember that this load is independent of the
7697 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00007698 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007699 Hi.getValue(1));
7700
7701 // Remember that we legalized the chain.
7702 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
7703 break;
7704 }
7705 case ISD::BIT_CONVERT: {
7706 // We know the result is a vector. The input may be either a vector or a
7707 // scalar value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007708 SDValue InOp = Node->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007709 if (!InOp.getValueType().isVector() ||
7710 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007711 // The input is a scalar or single-element vector.
7712 // Lower to a store/load so that it can be split.
7713 // FIXME: this could be improved probably.
Mon P Wang36b59ac2008-07-15 05:28:34 +00007714 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7715 Op.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00007716 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greif1c80d112008-08-28 21:40:38 +00007717 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007718
Dan Gohman8181bd12008-07-27 21:46:04 +00007719 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00007720 InOp, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007721 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00007722 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007723 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007724 }
7725 // Split the vector and convert each of the pieces now.
7726 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007727 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7728 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007729 break;
7730 }
7731 }
7732
7733 // Remember in a map if the values will be reused later.
7734 bool isNew =
7735 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
7736 assert(isNew && "Value already split?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007737 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007738}
7739
7740
7741/// ScalarizeVectorOp - Given an operand of single-element vector type
7742/// (e.g. v1f32), convert it into the equivalent operation that returns a
7743/// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007744SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00007745 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007746 SDNode *Node = Op.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00007747 MVT NewVT = Op.getValueType().getVectorElementType();
7748 assert(Op.getValueType().getVectorNumElements() == 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007749
7750 // See if we already scalarized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007751 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007752 if (I != ScalarizedNodes.end()) return I->second;
7753
Dan Gohman8181bd12008-07-27 21:46:04 +00007754 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007755 switch (Node->getOpcode()) {
7756 default:
7757#ifndef NDEBUG
7758 Node->dump(&DAG); cerr << "\n";
7759#endif
7760 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7761 case ISD::ADD:
7762 case ISD::FADD:
7763 case ISD::SUB:
7764 case ISD::FSUB:
7765 case ISD::MUL:
7766 case ISD::FMUL:
7767 case ISD::SDIV:
7768 case ISD::UDIV:
7769 case ISD::FDIV:
7770 case ISD::SREM:
7771 case ISD::UREM:
7772 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007773 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007774 case ISD::AND:
7775 case ISD::OR:
7776 case ISD::XOR:
7777 Result = DAG.getNode(Node->getOpcode(),
7778 NewVT,
7779 ScalarizeVectorOp(Node->getOperand(0)),
7780 ScalarizeVectorOp(Node->getOperand(1)));
7781 break;
7782 case ISD::FNEG:
7783 case ISD::FABS:
7784 case ISD::FSQRT:
7785 case ISD::FSIN:
7786 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007787 case ISD::FLOG:
7788 case ISD::FLOG2:
7789 case ISD::FLOG10:
7790 case ISD::FEXP:
7791 case ISD::FEXP2:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007792 case ISD::FP_TO_SINT:
7793 case ISD::FP_TO_UINT:
7794 case ISD::SINT_TO_FP:
7795 case ISD::UINT_TO_FP:
7796 case ISD::SIGN_EXTEND:
7797 case ISD::ZERO_EXTEND:
7798 case ISD::ANY_EXTEND:
7799 case ISD::TRUNCATE:
7800 case ISD::FP_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007801 Result = DAG.getNode(Node->getOpcode(),
7802 NewVT,
7803 ScalarizeVectorOp(Node->getOperand(0)));
7804 break;
Mon P Wang73d31542008-11-10 20:54:11 +00007805 case ISD::CONVERT_RNDSAT: {
7806 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7807 Result = DAG.getConvertRndSat(NewVT, Op0,
7808 DAG.getValueType(NewVT),
7809 DAG.getValueType(Op0.getValueType()),
7810 Node->getOperand(3),
7811 Node->getOperand(4),
7812 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7813 break;
7814 }
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007815 case ISD::FPOWI:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007816 case ISD::FP_ROUND:
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007817 Result = DAG.getNode(Node->getOpcode(),
7818 NewVT,
7819 ScalarizeVectorOp(Node->getOperand(0)),
7820 Node->getOperand(1));
7821 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007822 case ISD::LOAD: {
7823 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007824 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7825 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman29c3cef2008-08-14 20:04:46 +00007826 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007827 const Value *SV = LD->getSrcValue();
7828 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007829 MVT MemoryVT = LD->getMemoryVT();
7830 unsigned Alignment = LD->getAlignment();
7831 bool isVolatile = LD->isVolatile();
7832
7833 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7834 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7835
7836 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7837 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7838 MemoryVT.getVectorElementType(),
7839 isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007840
7841 // Remember that we legalized the chain.
7842 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7843 break;
7844 }
7845 case ISD::BUILD_VECTOR:
7846 Result = Node->getOperand(0);
7847 break;
7848 case ISD::INSERT_VECTOR_ELT:
7849 // Returning the inserted scalar element.
7850 Result = Node->getOperand(1);
7851 break;
7852 case ISD::CONCAT_VECTORS:
7853 assert(Node->getOperand(0).getValueType() == NewVT &&
7854 "Concat of non-legal vectors not yet supported!");
7855 Result = Node->getOperand(0);
7856 break;
7857 case ISD::VECTOR_SHUFFLE: {
7858 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007859 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007860 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007861 Result = ScalarizeVectorOp(Node->getOperand(1));
7862 else
7863 Result = ScalarizeVectorOp(Node->getOperand(0));
7864 break;
7865 }
7866 case ISD::EXTRACT_SUBVECTOR:
Mon P Wang927daf52008-11-06 22:52:21 +00007867 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007868 Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007869 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007870 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007871 SDValue Op0 = Op.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007872 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng2cc16e72008-05-16 17:19:05 +00007873 Op0 = ScalarizeVectorOp(Op0);
7874 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007875 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007876 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007877 case ISD::SELECT:
7878 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
7879 ScalarizeVectorOp(Op.getOperand(1)),
7880 ScalarizeVectorOp(Op.getOperand(2)));
7881 break;
Chris Lattnerc7471452008-06-30 02:43:01 +00007882 case ISD::SELECT_CC:
7883 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7884 Node->getOperand(1),
7885 ScalarizeVectorOp(Op.getOperand(2)),
7886 ScalarizeVectorOp(Op.getOperand(3)),
7887 Node->getOperand(4));
7888 break;
Nate Begeman78ca4f92008-05-12 23:09:43 +00007889 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007890 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7891 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Duncan Sands4a361272009-01-01 15:52:00 +00007892 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0.getValueType()),
7893 Op0, Op1, Op.getOperand(2));
Nate Begeman78ca4f92008-05-12 23:09:43 +00007894 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7895 DAG.getConstant(-1ULL, NewVT),
7896 DAG.getConstant(0ULL, NewVT));
7897 break;
7898 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007899 }
7900
7901 if (TLI.isTypeLegal(NewVT))
7902 Result = LegalizeOp(Result);
7903 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7904 assert(isNew && "Value already scalarized?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007905 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007906 return Result;
7907}
7908
7909
Mon P Wang1448aad2008-10-30 08:01:45 +00007910SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7911 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7912 if (I != WidenNodes.end()) return I->second;
7913
7914 MVT VT = Op.getValueType();
7915 assert(VT.isVector() && "Cannot widen non-vector type!");
7916
7917 SDValue Result;
7918 SDNode *Node = Op.getNode();
7919 MVT EVT = VT.getVectorElementType();
7920
7921 unsigned NumElts = VT.getVectorNumElements();
7922 unsigned NewNumElts = WidenVT.getVectorNumElements();
7923 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7924 assert(NewNumElts < 17);
7925
7926 // When widen is called, it is assumed that it is more efficient to use a
7927 // wide type. The default action is to widen to operation to a wider legal
7928 // vector type and then do the operation if it is legal by calling LegalizeOp
7929 // again. If there is no vector equivalent, we will unroll the operation, do
7930 // it, and rebuild the vector. If most of the operations are vectorizible to
7931 // the legal type, the resulting code will be more efficient. If this is not
7932 // the case, the resulting code will preform badly as we end up generating
7933 // code to pack/unpack the results. It is the function that calls widen
Mon P Wanga5a239f2008-11-06 05:31:54 +00007934 // that is responsible for seeing this doesn't happen.
Mon P Wang1448aad2008-10-30 08:01:45 +00007935 switch (Node->getOpcode()) {
7936 default:
7937#ifndef NDEBUG
7938 Node->dump(&DAG);
7939#endif
7940 assert(0 && "Unexpected operation in WidenVectorOp!");
7941 break;
7942 case ISD::CopyFromReg:
Mon P Wang257e1c72008-11-15 06:05:52 +00007943 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang1448aad2008-10-30 08:01:45 +00007944 case ISD::Constant:
7945 case ISD::ConstantFP:
7946 // To build a vector of these elements, clients should call BuildVector
7947 // and with each element instead of creating a node with a vector type
7948 assert(0 && "Unexpected operation in WidenVectorOp!");
7949 case ISD::VAARG:
7950 // Variable Arguments with vector types doesn't make any sense to me
7951 assert(0 && "Unexpected operation in WidenVectorOp!");
7952 break;
Mon P Wang257e1c72008-11-15 06:05:52 +00007953 case ISD::UNDEF:
7954 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7955 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00007956 case ISD::BUILD_VECTOR: {
7957 // Build a vector with undefined for the new nodes
7958 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7959 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7960 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7961 }
7962 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7963 break;
7964 }
7965 case ISD::INSERT_VECTOR_ELT: {
7966 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7967 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7968 Node->getOperand(1), Node->getOperand(2));
7969 break;
7970 }
7971 case ISD::VECTOR_SHUFFLE: {
7972 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7973 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7974 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7975 // used as permutation array. We build the vector here instead of widening
7976 // because we don't want to legalize and have it turned to something else.
7977 SDValue PermOp = Node->getOperand(2);
7978 SDValueVector NewOps;
7979 MVT PVT = PermOp.getValueType().getVectorElementType();
7980 for (unsigned i = 0; i < NumElts; ++i) {
7981 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7982 NewOps.push_back(PermOp.getOperand(i));
7983 } else {
7984 unsigned Idx =
Mon P Wangec428ad2008-12-13 08:15:14 +00007985 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
Mon P Wang1448aad2008-10-30 08:01:45 +00007986 if (Idx < NumElts) {
7987 NewOps.push_back(PermOp.getOperand(i));
7988 }
7989 else {
7990 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7991 PermOp.getOperand(i).getValueType()));
7992 }
7993 }
7994 }
7995 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7996 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
7997 }
7998
7999 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
8000 MVT::getVectorVT(PVT, NewOps.size()),
8001 &NewOps[0], NewOps.size());
8002
8003 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
8004 break;
8005 }
8006 case ISD::LOAD: {
8007 // If the load widen returns true, we can use a single load for the
8008 // vector. Otherwise, it is returning a token factor for multiple
8009 // loads.
8010 SDValue TFOp;
8011 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
8012 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
8013 else
8014 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
8015 break;
8016 }
8017
8018 case ISD::BIT_CONVERT: {
8019 SDValue Tmp1 = Node->getOperand(0);
8020 // Converts between two different types so we need to determine
8021 // the correct widen type for the input operand.
Mon P Wang26342922008-12-18 20:03:17 +00008022 MVT InVT = Tmp1.getValueType();
8023 unsigned WidenSize = WidenVT.getSizeInBits();
8024 if (InVT.isVector()) {
8025 MVT InEltVT = InVT.getVectorElementType();
8026 unsigned InEltSize = InEltVT.getSizeInBits();
8027 assert(WidenSize % InEltSize == 0 &&
8028 "can not widen bit convert that are not multiple of element type");
8029 MVT NewInWidenVT = MVT::getVectorVT(InEltVT, WidenSize / InEltSize);
8030 Tmp1 = WidenVectorOp(Tmp1, NewInWidenVT);
8031 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
8032 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Tmp1);
8033 } else {
8034 // If the result size is a multiple of the input size, widen the input
8035 // and then convert.
8036 unsigned InSize = InVT.getSizeInBits();
8037 assert(WidenSize % InSize == 0 &&
8038 "can not widen bit convert that are not multiple of element type");
8039 unsigned NewNumElts = WidenSize / InSize;
8040 SmallVector<SDValue, 16> Ops(NewNumElts);
8041 SDValue UndefVal = DAG.getNode(ISD::UNDEF, InVT);
8042 Ops[0] = Tmp1;
8043 for (unsigned i = 1; i < NewNumElts; ++i)
8044 Ops[i] = UndefVal;
Mon P Wang1448aad2008-10-30 08:01:45 +00008045
Mon P Wang26342922008-12-18 20:03:17 +00008046 MVT NewInVT = MVT::getVectorVT(InVT, NewNumElts);
8047 Result = DAG.getNode(ISD::BUILD_VECTOR, NewInVT, &Ops[0], NewNumElts);
8048 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Result);
Mon P Wang1448aad2008-10-30 08:01:45 +00008049 }
8050 break;
8051 }
8052
8053 case ISD::SINT_TO_FP:
8054 case ISD::UINT_TO_FP:
8055 case ISD::FP_TO_SINT:
Mon P Wang26342922008-12-18 20:03:17 +00008056 case ISD::FP_TO_UINT:
8057 case ISD::FP_ROUND: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008058 SDValue Tmp1 = Node->getOperand(0);
8059 // Converts between two different types so we need to determine
8060 // the correct widen type for the input operand.
8061 MVT TVT = Tmp1.getValueType();
8062 assert(TVT.isVector() && "can not widen non vector type");
8063 MVT TEVT = TVT.getVectorElementType();
8064 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
8065 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
8066 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
8067 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang1448aad2008-10-30 08:01:45 +00008068 break;
8069 }
8070
8071 case ISD::FP_EXTEND:
8072 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
8073 case ISD::TRUNCATE:
8074 case ISD::SIGN_EXTEND:
8075 case ISD::ZERO_EXTEND:
8076 case ISD::ANY_EXTEND:
Mon P Wang1448aad2008-10-30 08:01:45 +00008077 case ISD::SIGN_EXTEND_INREG:
8078 case ISD::FABS:
8079 case ISD::FNEG:
8080 case ISD::FSQRT:
8081 case ISD::FSIN:
Mon P Wang257e1c72008-11-15 06:05:52 +00008082 case ISD::FCOS:
8083 case ISD::CTPOP:
8084 case ISD::CTTZ:
8085 case ISD::CTLZ: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008086 // Unary op widening
Mon P Wang26342922008-12-18 20:03:17 +00008087 SDValue Tmp1;
Mon P Wang1448aad2008-10-30 08:01:45 +00008088 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8089 assert(Tmp1.getValueType() == WidenVT);
8090 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang1448aad2008-10-30 08:01:45 +00008091 break;
8092 }
Mon P Wang73d31542008-11-10 20:54:11 +00008093 case ISD::CONVERT_RNDSAT: {
8094 SDValue RndOp = Node->getOperand(3);
8095 SDValue SatOp = Node->getOperand(4);
Mon P Wang73d31542008-11-10 20:54:11 +00008096 SDValue SrcOp = Node->getOperand(0);
8097
8098 // Converts between two different types so we need to determine
8099 // the correct widen type for the input operand.
8100 MVT SVT = SrcOp.getValueType();
8101 assert(SVT.isVector() && "can not widen non vector type");
8102 MVT SEVT = SVT.getVectorElementType();
8103 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8104
8105 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8106 assert(SrcOp.getValueType() == WidenVT);
8107 SDValue DTyOp = DAG.getValueType(WidenVT);
8108 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8109 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8110
8111 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
8112 RndOp, SatOp, CvtCode);
Mon P Wang73d31542008-11-10 20:54:11 +00008113 break;
8114 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008115 case ISD::FPOW:
8116 case ISD::FPOWI:
8117 case ISD::ADD:
8118 case ISD::SUB:
8119 case ISD::MUL:
8120 case ISD::MULHS:
8121 case ISD::MULHU:
8122 case ISD::AND:
8123 case ISD::OR:
8124 case ISD::XOR:
8125 case ISD::FADD:
8126 case ISD::FSUB:
8127 case ISD::FMUL:
8128 case ISD::SDIV:
8129 case ISD::SREM:
8130 case ISD::FDIV:
8131 case ISD::FREM:
8132 case ISD::FCOPYSIGN:
8133 case ISD::UDIV:
8134 case ISD::UREM:
8135 case ISD::BSWAP: {
8136 // Binary op widening
Mon P Wang1448aad2008-10-30 08:01:45 +00008137 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8138 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8139 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8140 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
Mon P Wang1448aad2008-10-30 08:01:45 +00008141 break;
8142 }
8143
8144 case ISD::SHL:
8145 case ISD::SRA:
8146 case ISD::SRL: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008147 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8148 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangd5638262008-12-02 07:35:08 +00008149 SDValue ShOp = Node->getOperand(1);
8150 MVT ShVT = ShOp.getValueType();
8151 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8152 WidenVT.getVectorNumElements());
8153 ShOp = WidenVectorOp(ShOp, NewShVT);
8154 assert(ShOp.getValueType() == NewShVT);
8155 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, ShOp);
Mon P Wang1448aad2008-10-30 08:01:45 +00008156 break;
8157 }
Mon P Wangd5638262008-12-02 07:35:08 +00008158
Mon P Wang1448aad2008-10-30 08:01:45 +00008159 case ISD::EXTRACT_VECTOR_ELT: {
8160 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8161 assert(Tmp1.getValueType() == WidenVT);
8162 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8163 break;
8164 }
8165 case ISD::CONCAT_VECTORS: {
8166 // We concurrently support only widen on a multiple of the incoming vector.
8167 // We could widen on a multiple of the incoming operand if necessary.
8168 unsigned NumConcat = NewNumElts / NumElts;
8169 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Mon P Wangd5638262008-12-02 07:35:08 +00008170 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
Mon P Wang1448aad2008-10-30 08:01:45 +00008171 SmallVector<SDValue, 8> MOps;
8172 MOps.push_back(Op);
8173 for (unsigned i = 1; i != NumConcat; ++i) {
8174 MOps.push_back(UndefVal);
8175 }
8176 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8177 &MOps[0], MOps.size()));
8178 break;
8179 }
8180 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang257e1c72008-11-15 06:05:52 +00008181 SDValue Tmp1 = Node->getOperand(0);
8182 SDValue Idx = Node->getOperand(1);
8183 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8184 if (CIdx && CIdx->getZExtValue() == 0) {
8185 // Since we are access the start of the vector, the incoming
8186 // vector type might be the proper.
8187 MVT Tmp1VT = Tmp1.getValueType();
8188 if (Tmp1VT == WidenVT)
8189 return Tmp1;
8190 else {
8191 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8192 if (Tmp1VTNumElts < NewNumElts)
8193 Result = WidenVectorOp(Tmp1, WidenVT);
8194 else
8195 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8196 }
8197 } else if (NewNumElts % NumElts == 0) {
8198 // Widen the extracted subvector.
8199 unsigned NumConcat = NewNumElts / NumElts;
8200 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8201 SmallVector<SDValue, 8> MOps;
8202 MOps.push_back(Op);
8203 for (unsigned i = 1; i != NumConcat; ++i) {
8204 MOps.push_back(UndefVal);
8205 }
8206 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8207 &MOps[0], MOps.size()));
8208 } else {
8209 assert(0 && "can not widen extract subvector");
8210 // This could be implemented using insert and build vector but I would
8211 // like to see when this happens.
8212 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008213 break;
8214 }
8215
8216 case ISD::SELECT: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008217 // Determine new condition widen type and widen
8218 SDValue Cond1 = Node->getOperand(0);
8219 MVT CondVT = Cond1.getValueType();
8220 assert(CondVT.isVector() && "can not widen non vector type");
8221 MVT CondEVT = CondVT.getVectorElementType();
8222 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8223 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8224 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8225
8226 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8227 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8228 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8229 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang1448aad2008-10-30 08:01:45 +00008230 break;
8231 }
8232
8233 case ISD::SELECT_CC: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008234 // Determine new condition widen type and widen
8235 SDValue Cond1 = Node->getOperand(0);
8236 SDValue Cond2 = Node->getOperand(1);
8237 MVT CondVT = Cond1.getValueType();
8238 assert(CondVT.isVector() && "can not widen non vector type");
8239 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8240 MVT CondEVT = CondVT.getVectorElementType();
8241 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8242 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8243 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8244 assert(Cond1.getValueType() == CondWidenVT &&
8245 Cond2.getValueType() == CondWidenVT && "condition not widen");
8246
8247 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8248 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8249 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8250 "operands not widen");
8251 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8252 Tmp2, Node->getOperand(4));
Mon P Wang1448aad2008-10-30 08:01:45 +00008253 break;
Mon P Wang42ac14e2008-10-30 18:21:52 +00008254 }
8255 case ISD::VSETCC: {
8256 // Determine widen for the operand
8257 SDValue Tmp1 = Node->getOperand(0);
8258 MVT TmpVT = Tmp1.getValueType();
8259 assert(TmpVT.isVector() && "can not widen non vector type");
8260 MVT TmpEVT = TmpVT.getVectorElementType();
8261 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8262 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8263 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
Mon P Wang26342922008-12-18 20:03:17 +00008264 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
Mon P Wang42ac14e2008-10-30 18:21:52 +00008265 Node->getOperand(2));
Mon P Wang1448aad2008-10-30 08:01:45 +00008266 break;
8267 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00008268 case ISD::ATOMIC_CMP_SWAP:
8269 case ISD::ATOMIC_LOAD_ADD:
8270 case ISD::ATOMIC_LOAD_SUB:
8271 case ISD::ATOMIC_LOAD_AND:
8272 case ISD::ATOMIC_LOAD_OR:
8273 case ISD::ATOMIC_LOAD_XOR:
8274 case ISD::ATOMIC_LOAD_NAND:
8275 case ISD::ATOMIC_LOAD_MIN:
8276 case ISD::ATOMIC_LOAD_MAX:
8277 case ISD::ATOMIC_LOAD_UMIN:
8278 case ISD::ATOMIC_LOAD_UMAX:
8279 case ISD::ATOMIC_SWAP: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008280 // For now, we assume that using vectors for these operations don't make
8281 // much sense so we just split it. We return an empty result
8282 SDValue X, Y;
8283 SplitVectorOp(Op, X, Y);
8284 return Result;
8285 break;
8286 }
8287
8288 } // end switch (Node->getOpcode())
8289
8290 assert(Result.getNode() && "Didn't set a result!");
8291 if (Result != Op)
8292 Result = LegalizeOp(Result);
8293
Mon P Wanga5a239f2008-11-06 05:31:54 +00008294 AddWidenedOperand(Op, Result);
Mon P Wang1448aad2008-10-30 08:01:45 +00008295 return Result;
8296}
8297
8298// Utility function to find a legal vector type and its associated element
8299// type from a preferred width and whose vector type must be the same size
8300// as the VVT.
8301// TLI: Target lowering used to determine legal types
8302// Width: Preferred width of element type
8303// VVT: Vector value type whose size we must match.
8304// Returns VecEVT and EVT - the vector type and its associated element type
Dan Gohman0275b132009-01-15 16:43:02 +00008305static void FindWidenVecType(const TargetLowering &TLI, unsigned Width, MVT VVT,
Mon P Wang1448aad2008-10-30 08:01:45 +00008306 MVT& EVT, MVT& VecEVT) {
8307 // We start with the preferred width, make it a power of 2 and see if
8308 // we can find a vector type of that width. If not, we reduce it by
8309 // another power of 2. If we have widen the type, a vector of bytes should
8310 // always be legal.
8311 assert(TLI.isTypeLegal(VVT));
8312 unsigned EWidth = Width + 1;
8313 do {
8314 assert(EWidth > 0);
8315 EWidth = (1 << Log2_32(EWidth-1));
8316 EVT = MVT::getIntegerVT(EWidth);
8317 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8318 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8319 } while (!TLI.isTypeLegal(VecEVT) ||
8320 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8321}
8322
8323SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8324 SDValue Chain,
8325 SDValue BasePtr,
8326 const Value *SV,
8327 int SVOffset,
8328 unsigned Alignment,
8329 bool isVolatile,
8330 unsigned LdWidth,
8331 MVT ResType) {
8332 // We assume that we have good rules to handle loading power of two loads so
8333 // we break down the operations to power of 2 loads. The strategy is to
8334 // load the largest power of 2 that we can easily transform to a legal vector
8335 // and then insert into that vector, and the cast the result into the legal
8336 // vector that we want. This avoids unnecessary stack converts.
8337 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8338 // the load is nonvolatile, we an use a wider load for the value.
8339 // Find a vector length we can load a large chunk
8340 MVT EVT, VecEVT;
8341 unsigned EVTWidth;
8342 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8343 EVTWidth = EVT.getSizeInBits();
8344
8345 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8346 isVolatile, Alignment);
8347 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8348 LdChain.push_back(LdOp.getValue(1));
8349
8350 // Check if we can load the element with one instruction
8351 if (LdWidth == EVTWidth) {
8352 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8353 }
8354
8355 // The vector element order is endianness dependent.
8356 unsigned Idx = 1;
8357 LdWidth -= EVTWidth;
8358 unsigned Offset = 0;
8359
8360 while (LdWidth > 0) {
8361 unsigned Increment = EVTWidth / 8;
8362 Offset += Increment;
8363 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8364 DAG.getIntPtrConstant(Increment));
8365
8366 if (LdWidth < EVTWidth) {
8367 // Our current type we are using is too large, use a smaller size by
8368 // using a smaller power of 2
8369 unsigned oEVTWidth = EVTWidth;
8370 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8371 EVTWidth = EVT.getSizeInBits();
8372 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008373 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang1448aad2008-10-30 08:01:45 +00008374 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8375 }
8376
8377 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8378 SVOffset+Offset, isVolatile,
8379 MinAlign(Alignment, Offset));
8380 LdChain.push_back(LdOp.getValue(1));
8381 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8382 DAG.getIntPtrConstant(Idx++));
8383
8384 LdWidth -= EVTWidth;
8385 }
8386
8387 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8388}
8389
8390bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8391 SDValue& TFOp,
8392 SDValue Op,
8393 MVT NVT) {
8394 // TODO: Add support for ConcatVec and the ability to load many vector
8395 // types (e.g., v4i8). This will not work when a vector register
8396 // to memory mapping is strange (e.g., vector elements are not
8397 // stored in some sequential order).
8398
8399 // It must be true that the widen vector type is bigger than where
8400 // we need to load from.
8401 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8402 MVT LdVT = LD->getMemoryVT();
8403 assert(LdVT.isVector() && NVT.isVector());
8404 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8405
8406 // Load information
8407 SDValue Chain = LD->getChain();
8408 SDValue BasePtr = LD->getBasePtr();
8409 int SVOffset = LD->getSrcValueOffset();
8410 unsigned Alignment = LD->getAlignment();
8411 bool isVolatile = LD->isVolatile();
8412 const Value *SV = LD->getSrcValue();
8413 unsigned int LdWidth = LdVT.getSizeInBits();
8414
8415 // Load value as a large register
8416 SDValueVector LdChain;
8417 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8418 Alignment, isVolatile, LdWidth, NVT);
8419
8420 if (LdChain.size() == 1) {
8421 TFOp = LdChain[0];
8422 return true;
8423 }
8424 else {
8425 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8426 return false;
8427 }
8428}
8429
8430
8431void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8432 SDValue Chain,
8433 SDValue BasePtr,
8434 const Value *SV,
8435 int SVOffset,
8436 unsigned Alignment,
8437 bool isVolatile,
Mon P Wang257e1c72008-11-15 06:05:52 +00008438 SDValue ValOp,
Mon P Wang1448aad2008-10-30 08:01:45 +00008439 unsigned StWidth) {
8440 // Breaks the stores into a series of power of 2 width stores. For any
8441 // width, we convert the vector to the vector of element size that we
8442 // want to store. This avoids requiring a stack convert.
8443
8444 // Find a width of the element type we can store with
8445 MVT VVT = ValOp.getValueType();
8446 MVT EVT, VecEVT;
8447 unsigned EVTWidth;
8448 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8449 EVTWidth = EVT.getSizeInBits();
8450
8451 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8452 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang927daf52008-11-06 22:52:21 +00008453 DAG.getIntPtrConstant(0));
Mon P Wang1448aad2008-10-30 08:01:45 +00008454 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8455 isVolatile, Alignment);
8456 StChain.push_back(StOp);
8457
8458 // Check if we are done
8459 if (StWidth == EVTWidth) {
8460 return;
8461 }
8462
8463 unsigned Idx = 1;
8464 StWidth -= EVTWidth;
8465 unsigned Offset = 0;
8466
8467 while (StWidth > 0) {
8468 unsigned Increment = EVTWidth / 8;
8469 Offset += Increment;
8470 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8471 DAG.getIntPtrConstant(Increment));
8472
8473 if (StWidth < EVTWidth) {
8474 // Our current type we are using is too large, use a smaller size by
8475 // using a smaller power of 2
8476 unsigned oEVTWidth = EVTWidth;
8477 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8478 EVTWidth = EVT.getSizeInBits();
8479 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008480 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang1448aad2008-10-30 08:01:45 +00008481 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8482 }
8483
8484 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang257e1c72008-11-15 06:05:52 +00008485 DAG.getIntPtrConstant(Idx++));
Mon P Wang1448aad2008-10-30 08:01:45 +00008486 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8487 SVOffset + Offset, isVolatile,
8488 MinAlign(Alignment, Offset)));
8489 StWidth -= EVTWidth;
8490 }
8491}
8492
8493
8494SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8495 SDValue Chain,
8496 SDValue BasePtr) {
8497 // TODO: It might be cleaner if we can use SplitVector and have more legal
8498 // vector types that can be stored into memory (e.g., v4xi8 can
8499 // be stored as a word). This will not work when a vector register
8500 // to memory mapping is strange (e.g., vector elements are not
8501 // stored in some sequential order).
8502
8503 MVT StVT = ST->getMemoryVT();
8504 SDValue ValOp = ST->getValue();
8505
8506 // Check if we have widen this node with another value
8507 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8508 if (I != WidenNodes.end())
8509 ValOp = I->second;
8510
8511 MVT VVT = ValOp.getValueType();
8512
8513 // It must be true that we the widen vector type is bigger than where
8514 // we need to store.
8515 assert(StVT.isVector() && VVT.isVector());
8516 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8517 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8518
8519 // Store value
8520 SDValueVector StChain;
8521 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8522 ST->getSrcValueOffset(), ST->getAlignment(),
8523 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8524 if (StChain.size() == 1)
8525 return StChain[0];
8526 else
8527 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8528}
8529
8530
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008531// SelectionDAG::Legalize - This is the entry point for the file.
8532//
Duncan Sandse016a2e2008-12-14 09:43:15 +00008533void SelectionDAG::Legalize(bool TypesNeedLegalizing) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008534 /// run - This is the main entry point to this class.
8535 ///
Duncan Sandse016a2e2008-12-14 09:43:15 +00008536 SelectionDAGLegalize(*this, TypesNeedLegalizing).LegalizeDAG();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008537}
8538