blob: 8cba55fdb0fd35bbda3381a20cdd3f26e236e489 [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
Mon P Wang7cba3942009-02-04 19:38:14 +000071 /// IsLegalizingCallArguments - This member is used only for the purpose
72 /// of providing assert to check for LegalizeTypes because legalizing an
73 /// operation might introduce call nodes that might need type legalization.
74 bool IsLegalizingCallArgs;
75
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 enum LegalizeAction {
77 Legal, // The target natively supports this operation.
78 Promote, // This operation should be executed in a larger type.
79 Expand // Try to expand this to other ops, otherwise use a libcall.
80 };
81
82 /// ValueTypeActions - This is a bitvector that contains two bits for each
83 /// value type, where the two bits correspond to the LegalizeAction enum.
84 /// This can be queried with "getTypeAction(VT)".
85 TargetLowering::ValueTypeActionImpl ValueTypeActions;
86
87 /// LegalizedNodes - For nodes that are of legal width, and that have more
88 /// than one use, this map indicates what regularized operand to use. This
89 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000090 DenseMap<SDValue, SDValue> LegalizedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091
92 /// PromotedNodes - For nodes that are below legal width, and that have more
93 /// than one use, this map indicates what promoted value to use. This allows
94 /// us to avoid promoting the same thing more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +000095 DenseMap<SDValue, SDValue> PromotedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096
97 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang1448aad2008-10-30 08:01:45 +000098 /// which operands are the expanded version of the input. This allows
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 /// us to avoid expanding the same node more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +0000100 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101
102 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang1448aad2008-10-30 08:01:45 +0000103 /// which operands are the split version of the input. This allows us
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 /// to avoid splitting the same node more than once.
Dan Gohman8181bd12008-07-27 21:46:04 +0000105 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
107 /// ScalarizedNodes - For nodes that need to be converted from vector types to
108 /// scalar types, this contains the mapping of ones we have already
109 /// processed to the result.
Dan Gohman8181bd12008-07-27 21:46:04 +0000110 std::map<SDValue, SDValue> ScalarizedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111
Mon P Wanga5a239f2008-11-06 05:31:54 +0000112 /// WidenNodes - For nodes that need to be widened from one vector type to
113 /// another, this contains the mapping of those that we have already widen.
114 /// This allows us to avoid widening more than once.
Mon P Wang1448aad2008-10-30 08:01:45 +0000115 std::map<SDValue, SDValue> WidenNodes;
116
Dan Gohman8181bd12008-07-27 21:46:04 +0000117 void AddLegalizedOperand(SDValue From, SDValue To) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 LegalizedNodes.insert(std::make_pair(From, To));
119 // If someone requests legalization of the new node, return itself.
120 if (From != To)
121 LegalizedNodes.insert(std::make_pair(To, To));
122 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000123 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman55d19662008-07-07 17:46:23 +0000124 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 assert(isNew && "Got into the map somehow?");
Evan Chengcf576fd2008-11-24 07:09:49 +0000126 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 // If someone requests legalization of the new node, return itself.
128 LegalizedNodes.insert(std::make_pair(To, To));
129 }
Mon P Wanga5a239f2008-11-06 05:31:54 +0000130 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang1448aad2008-10-30 08:01:45 +0000131 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
132 assert(isNew && "Got into the map somehow?");
Evan Chengcf576fd2008-11-24 07:09:49 +0000133 isNew = isNew;
Mon P Wang1448aad2008-10-30 08:01:45 +0000134 // If someone requests legalization of the new node, return itself.
135 LegalizedNodes.insert(std::make_pair(To, To));
136 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137
138public:
Duncan Sandse016a2e2008-12-14 09:43:15 +0000139 explicit SelectionDAGLegalize(SelectionDAG &DAG, bool TypesNeedLegalizing);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140
141 /// getTypeAction - Return how we should legalize values of this type, either
142 /// it is already legal or we need to expand it into multiple registers of
143 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands92c43912008-06-06 12:08:01 +0000144 LegalizeAction getTypeAction(MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
146 }
147
148 /// isTypeLegal - Return true if this type is legal on this target.
149 ///
Duncan Sands92c43912008-06-06 12:08:01 +0000150 bool isTypeLegal(MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 return getTypeAction(VT) == Legal;
152 }
153
154 void LegalizeDAG();
155
156private:
157 /// HandleOp - Legalize, Promote, or Expand the specified operand as
158 /// appropriate for its type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000159 void HandleOp(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160
161 /// LegalizeOp - We know that the specified value has a legal type.
162 /// Recursively ensure that the operands have legal types, then return the
163 /// result.
Dan Gohman8181bd12008-07-27 21:46:04 +0000164 SDValue LegalizeOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165
Dan Gohman6d05cac2007-10-11 23:57:53 +0000166 /// UnrollVectorOp - We know that the given vector has a legal type, however
167 /// the operation it performs is not legal and is an operation that we have
168 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
169 /// operating on each element individually.
Dan Gohman8181bd12008-07-27 21:46:04 +0000170 SDValue UnrollVectorOp(SDValue O);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000171
172 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
173 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
174 /// is necessary to spill the vector being inserted into to memory, perform
175 /// the insert there, and then read the result back.
Dan Gohman8181bd12008-07-27 21:46:04 +0000176 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Dale Johannesen352c47e2009-02-02 20:41:04 +0000177 SDValue Idx, DebugLoc dl);
Dan Gohman6d05cac2007-10-11 23:57:53 +0000178
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 /// PromoteOp - Given an operation that produces a value in an invalid type,
180 /// promote it to compute the value into a larger type. The produced value
181 /// will have the correct bits for the low portion of the register, but no
182 /// guarantee is made about the top bits: it may be zero, sign-extended, or
183 /// garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +0000184 SDValue PromoteOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185
Dan Gohman8181bd12008-07-27 21:46:04 +0000186 /// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman4fc03742008-10-01 15:07:49 +0000188 /// the LegalizedNodes map is filled in for any results that are not expanded,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 /// the ExpandedNodes map is filled in for any results that are expanded, and
190 /// the Lo/Hi values are returned. This applies to integer types and Vector
191 /// types.
Dan Gohman8181bd12008-07-27 21:46:04 +0000192 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193
Mon P Wanga5a239f2008-11-06 05:31:54 +0000194 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
195 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
196 /// for the existing elements but no guarantee is made about the new elements
197 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
198 /// when we have an instruction operating on an illegal vector type and we
199 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang1448aad2008-10-30 08:01:45 +0000200 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
201
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 /// SplitVectorOp - Given an operand of vector type, break it down into
203 /// two smaller values.
Dan Gohman8181bd12008-07-27 21:46:04 +0000204 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205
206 /// ScalarizeVectorOp - Given an operand of single-element vector type
207 /// (e.g. v1f32), convert it into the equivalent operation that returns a
208 /// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +0000209 SDValue ScalarizeVectorOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210
Mon P Wanga5a239f2008-11-06 05:31:54 +0000211 /// Useful 16 element vector type that is used to pass operands for widening.
Mon P Wang1448aad2008-10-30 08:01:45 +0000212 typedef SmallVector<SDValue, 16> SDValueVector;
213
214 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
215 /// the LdChain contains a single load and false if it contains a token
216 /// factor for multiple loads. It takes
217 /// Result: location to return the result
218 /// LdChain: location to return the load chain
219 /// Op: load operation to widen
220 /// NVT: widen vector result type we want for the load
221 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
222 SDValue Op, MVT NVT);
223
224 /// Helper genWidenVectorLoads - Helper function to generate a set of
225 /// loads to load a vector with a resulting wider type. It takes
226 /// LdChain: list of chains for the load we have generated
227 /// Chain: incoming chain for the ld vector
228 /// BasePtr: base pointer to load from
229 /// SV: memory disambiguation source value
230 /// SVOffset: memory disambiugation offset
231 /// Alignment: alignment of the memory
232 /// isVolatile: volatile load
233 /// LdWidth: width of memory that we want to load
234 /// ResType: the wider result result type for the resulting loaded vector
235 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
236 SDValue BasePtr, const Value *SV,
237 int SVOffset, unsigned Alignment,
238 bool isVolatile, unsigned LdWidth,
Dale Johannesend8fd5342009-02-02 22:49:46 +0000239 MVT ResType, DebugLoc dl);
Mon P Wang1448aad2008-10-30 08:01:45 +0000240
241 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
242 /// location. It takes
243 /// ST: store node that we want to replace
244 /// Chain: incoming store chain
245 /// BasePtr: base address of where we want to store into
246 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
247 SDValue BasePtr);
248
249 /// Helper genWidenVectorStores - Helper function to generate a set of
250 /// stores to store a widen vector into non widen memory
251 // It takes
252 // StChain: list of chains for the stores we have generated
253 // Chain: incoming chain for the ld vector
254 // BasePtr: base pointer to load from
255 // SV: memory disambiguation source value
256 // SVOffset: memory disambiugation offset
257 // Alignment: alignment of the memory
258 // isVolatile: volatile lod
259 // ValOp: value to store
260 // StWidth: width of memory that we want to store
261 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
262 SDValue BasePtr, const Value *SV,
263 int SVOffset, unsigned Alignment,
264 bool isVolatile, SDValue ValOp,
Dale Johannesend8fd5342009-02-02 22:49:46 +0000265 unsigned StWidth, DebugLoc dl);
Mon P Wang1448aad2008-10-30 08:01:45 +0000266
Duncan Sandsd3ace282008-07-21 10:20:31 +0000267 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 /// specified mask and type. Targets can specify exactly which masks they
269 /// support and the code generator is tasked with not creating illegal masks.
270 ///
271 /// Note that this will also return true for shuffles that are promoted to a
272 /// different type.
273 ///
274 /// If this is a legal shuffle, this method returns the (possibly promoted)
275 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman8181bd12008-07-27 21:46:04 +0000276 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277
278 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
279 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
280
Dale Johannesen352c47e2009-02-02 20:41:04 +0000281 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC,
282 DebugLoc dl);
283 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
284 DebugLoc dl);
285 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
286 DebugLoc dl) {
287 LegalizeSetCCOperands(LHS, RHS, CC, dl);
288 LegalizeSetCCCondCode(VT, LHS, RHS, CC, dl);
Evan Cheng71343822008-10-15 02:05:31 +0000289 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290
Dan Gohman8181bd12008-07-27 21:46:04 +0000291 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
292 SDValue &Hi);
Dale Johannesen9972b632009-02-02 19:03:57 +0000293 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source, DebugLoc dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294
Dale Johannesen82b5b722009-02-02 22:12:50 +0000295 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT, DebugLoc dl);
Dan Gohman8181bd12008-07-27 21:46:04 +0000296 SDValue ExpandBUILD_VECTOR(SDNode *Node);
297 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dale Johannesen9972b632009-02-02 19:03:57 +0000298 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy,
299 SDValue Op, DebugLoc dl);
300 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT,
301 DebugLoc dl);
302 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned,
303 DebugLoc dl);
304 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned,
305 DebugLoc dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306
Dale Johannesen82b5b722009-02-02 22:12:50 +0000307 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
308 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Dan Gohman8181bd12008-07-27 21:46:04 +0000309 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
Dale Johannesen82b5b722009-02-02 22:12:50 +0000310 SDValue &Lo, SDValue &Hi, DebugLoc dl);
Dan Gohman8181bd12008-07-27 21:46:04 +0000311 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
Dale Johannesen82b5b722009-02-02 22:12:50 +0000312 SDValue &Lo, SDValue &Hi, DebugLoc dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313
Dan Gohman8181bd12008-07-27 21:46:04 +0000314 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
315 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316};
317}
318
319/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
320/// specified mask and type. Targets can specify exactly which masks they
321/// support and the code generator is tasked with not creating illegal masks.
322///
323/// Note that this will also return true for shuffles that are promoted to a
324/// different type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000325SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
327 default: return 0;
328 case TargetLowering::Legal:
329 case TargetLowering::Custom:
330 break;
331 case TargetLowering::Promote: {
332 // If this is promoted to a different type, convert the shuffle mask and
333 // ask if it is legal in the promoted type!
Duncan Sands92c43912008-06-06 12:08:01 +0000334 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd3ace282008-07-21 10:20:31 +0000335 MVT EltVT = NVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336
337 // If we changed # elements, change the shuffle mask.
338 unsigned NumEltsGrowth =
Duncan Sands92c43912008-06-06 12:08:01 +0000339 NVT.getVectorNumElements() / VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
341 if (NumEltsGrowth > 1) {
342 // Renumber the elements.
Dan Gohman8181bd12008-07-27 21:46:04 +0000343 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000345 SDValue InOp = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
347 if (InOp.getOpcode() == ISD::UNDEF)
Dale Johannesen9bfc0172009-02-06 23:05:02 +0000348 Ops.push_back(DAG.getUNDEF(EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000350 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd3ace282008-07-21 10:20:31 +0000351 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 }
353 }
354 }
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +0000355 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getDebugLoc(),
Dale Johannesen352c47e2009-02-02 20:41:04 +0000356 NVT, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 }
358 VT = NVT;
359 break;
360 }
361 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000362 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363}
364
Duncan Sandse016a2e2008-12-14 09:43:15 +0000365SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag, bool types)
366 : TLI(dag.getTargetLoweringInfo()), DAG(dag), TypesNeedLegalizing(types),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 ValueTypeActions(TLI.getValueTypeActions()) {
368 assert(MVT::LAST_VALUETYPE <= 32 &&
369 "Too many value types for ValueTypeActions to hold!");
370}
371
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372void SelectionDAGLegalize::LegalizeDAG() {
373 LastCALLSEQ_END = DAG.getEntryNode();
374 IsLegalizingCall = false;
Mon P Wang7cba3942009-02-04 19:38:14 +0000375 IsLegalizingCallArgs = false;
376
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 // The legalize process is inherently a bottom-up recursive process (users
378 // legalize their uses before themselves). Given infinite stack space, we
379 // could just start legalizing on the root and traverse the whole graph. In
380 // practice however, this causes us to run out of stack space on large basic
381 // blocks. To avoid this problem, compute an ordering of the nodes where each
382 // node is only legalized after all of its operands are legalized.
Dan Gohman2d2a7a32008-09-30 18:30:35 +0000383 DAG.AssignTopologicalOrder();
384 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
385 E = prior(DAG.allnodes_end()); I != next(E); ++I)
386 HandleOp(SDValue(I, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387
388 // Finally, it's possible the root changed. Get the new root.
Dan Gohman8181bd12008-07-27 21:46:04 +0000389 SDValue OldRoot = DAG.getRoot();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
391 DAG.setRoot(LegalizedNodes[OldRoot]);
392
393 ExpandedNodes.clear();
394 LegalizedNodes.clear();
395 PromotedNodes.clear();
396 SplitNodes.clear();
397 ScalarizedNodes.clear();
Mon P Wang1448aad2008-10-30 08:01:45 +0000398 WidenNodes.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399
400 // Remove dead nodes now.
401 DAG.RemoveDeadNodes();
402}
403
404
405/// FindCallEndFromCallStart - Given a chained node that is part of a call
406/// sequence, find the CALLSEQ_END node that terminates the call sequence.
407static SDNode *FindCallEndFromCallStart(SDNode *Node) {
408 if (Node->getOpcode() == ISD::CALLSEQ_END)
409 return Node;
410 if (Node->use_empty())
411 return 0; // No CallSeqEnd
412
413 // The chain is usually at the end.
Dan Gohman8181bd12008-07-27 21:46:04 +0000414 SDValue TheChain(Node, Node->getNumValues()-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 if (TheChain.getValueType() != MVT::Other) {
416 // Sometimes it's at the beginning.
Dan Gohman8181bd12008-07-27 21:46:04 +0000417 TheChain = SDValue(Node, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 if (TheChain.getValueType() != MVT::Other) {
419 // Otherwise, hunt for it.
420 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
421 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000422 TheChain = SDValue(Node, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 break;
424 }
425
426 // Otherwise, we walked into a node without a chain.
427 if (TheChain.getValueType() != MVT::Other)
428 return 0;
429 }
430 }
431
432 for (SDNode::use_iterator UI = Node->use_begin(),
433 E = Node->use_end(); UI != E; ++UI) {
434
435 // Make sure to only follow users of our token chain.
Dan Gohman0c97f1d2008-07-27 20:43:25 +0000436 SDNode *User = *UI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
438 if (User->getOperand(i) == TheChain)
439 if (SDNode *Result = FindCallEndFromCallStart(User))
440 return Result;
441 }
442 return 0;
443}
444
445/// FindCallStartFromCallEnd - Given a chained node that is part of a call
446/// sequence, find the CALLSEQ_START node that initiates the call sequence.
447static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
448 assert(Node && "Didn't find callseq_start for a call??");
449 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
450
451 assert(Node->getOperand(0).getValueType() == MVT::Other &&
452 "Node doesn't have a token chain argument!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000453 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454}
455
456/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
457/// see if any uses can reach Dest. If no dest operands can get to dest,
458/// legalize them, legalize ourself, and return false, otherwise, return true.
459///
460/// Keep track of the nodes we fine that actually do lead to Dest in
461/// NodesLeadingTo. This avoids retraversing them exponential number of times.
462///
463bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
464 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
465 if (N == Dest) return true; // N certainly leads to Dest :)
466
467 // If we've already processed this node and it does lead to Dest, there is no
468 // need to reprocess it.
469 if (NodesLeadingTo.count(N)) return true;
470
471 // If the first result of this node has been already legalized, then it cannot
472 // reach N.
473 switch (getTypeAction(N->getValueType(0))) {
474 case Legal:
Dan Gohman8181bd12008-07-27 21:46:04 +0000475 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476 break;
477 case Promote:
Dan Gohman8181bd12008-07-27 21:46:04 +0000478 if (PromotedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 break;
480 case Expand:
Dan Gohman8181bd12008-07-27 21:46:04 +0000481 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 break;
483 }
484
485 // Okay, this node has not already been legalized. Check and legalize all
486 // operands. If none lead to Dest, then we can legalize this node.
487 bool OperandsLeadToDest = false;
488 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
489 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greif1c80d112008-08-28 21:40:38 +0000490 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491
492 if (OperandsLeadToDest) {
493 NodesLeadingTo.insert(N);
494 return true;
495 }
496
497 // Okay, this node looks safe, legalize it and return false.
Dan Gohman8181bd12008-07-27 21:46:04 +0000498 HandleOp(SDValue(N, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 return false;
500}
501
Mon P Wang1448aad2008-10-30 08:01:45 +0000502/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503/// appropriate for its type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000504void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000505 MVT VT = Op.getValueType();
Duncan Sandse016a2e2008-12-14 09:43:15 +0000506 // If the type legalizer was run then we should never see any illegal result
507 // types here except for target constants (the type legalizer does not touch
Mon P Wang26342922008-12-18 20:03:17 +0000508 // those) or for build vector used as a mask for a vector shuffle.
509 // FIXME: We can removed the BUILD_VECTOR case when we fix PR2957.
Duncan Sandse016a2e2008-12-14 09:43:15 +0000510 assert((TypesNeedLegalizing || getTypeAction(VT) == Legal ||
Mon P Wang7cba3942009-02-04 19:38:14 +0000511 IsLegalizingCallArgs || Op.getOpcode() == ISD::TargetConstant ||
Mon P Wang26342922008-12-18 20:03:17 +0000512 Op.getOpcode() == ISD::BUILD_VECTOR) &&
Duncan Sandse016a2e2008-12-14 09:43:15 +0000513 "Illegal type introduced after type legalization?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 switch (getTypeAction(VT)) {
515 default: assert(0 && "Bad type action!");
516 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang1448aad2008-10-30 08:01:45 +0000517 case Promote:
518 if (!VT.isVector()) {
519 (void)PromoteOp(Op);
520 break;
521 }
522 else {
523 // See if we can widen otherwise use Expand to either scalarize or split
524 MVT WidenVT = TLI.getWidenVectorType(VT);
525 if (WidenVT != MVT::Other) {
526 (void) WidenVectorOp(Op, WidenVT);
527 break;
528 }
529 // else fall thru to expand since we can't widen the vector
530 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +0000532 if (!VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 // If this is an illegal scalar, expand it into its two component
534 // pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +0000535 SDValue X, Y;
Chris Lattnerdad577b2007-08-25 01:00:22 +0000536 if (Op.getOpcode() == ISD::TargetConstant)
537 break; // Allow illegal target nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 ExpandOp(Op, X, Y);
Duncan Sands92c43912008-06-06 12:08:01 +0000539 } else if (VT.getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000540 // If this is an illegal single element vector, convert it to a
541 // scalar operation.
542 (void)ScalarizeVectorOp(Op);
543 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +0000544 // This is an illegal multiple element vector.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 // Split it in half and legalize both parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000546 SDValue X, Y;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 SplitVectorOp(Op, X, Y);
548 }
549 break;
550 }
551}
552
553/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
554/// a load from the constant pool.
Dan Gohman8181bd12008-07-27 21:46:04 +0000555static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0275b132009-01-15 16:43:02 +0000556 SelectionDAG &DAG, const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 bool Extend = false;
Dale Johannesenea996922009-02-04 20:06:27 +0000558 DebugLoc dl = CFP->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559
560 // If a FP immediate is precise when represented as a float and if the
561 // target can do an extending load from float to double, we put it into
562 // the constant pool as a float, even if it's is statically typed as a
Chris Lattnere718cc52008-03-05 06:46:58 +0000563 // double. This shrinks FP constants and canonicalizes them for targets where
564 // an FP extending load is the same cost as a normal load (such as on the x87
565 // fp stack or PPC FP unit).
Duncan Sands92c43912008-06-06 12:08:01 +0000566 MVT VT = CFP->getValueType(0);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000567 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 if (!UseCP) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000569 if (VT!=MVT::f64 && VT!=MVT::f32)
570 assert(0 && "Invalid type expansion");
Dale Johannesen49cc7ce2008-10-09 18:53:47 +0000571 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Cheng354be062008-03-04 08:05:30 +0000572 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 }
574
Duncan Sands92c43912008-06-06 12:08:01 +0000575 MVT OrigVT = VT;
576 MVT SVT = VT;
Evan Cheng354be062008-03-04 08:05:30 +0000577 while (SVT != MVT::f32) {
Duncan Sands92c43912008-06-06 12:08:01 +0000578 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Cheng354be062008-03-04 08:05:30 +0000579 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
580 // Only do this if the target has a native EXTLOAD instruction from
581 // smaller type.
Evan Cheng08c171a2008-10-14 21:26:46 +0000582 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattnere718cc52008-03-05 06:46:58 +0000583 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000584 const Type *SType = SVT.getTypeForMVT();
Evan Cheng354be062008-03-04 08:05:30 +0000585 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
586 VT = SVT;
587 Extend = true;
588 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 }
590
Dan Gohman8181bd12008-07-27 21:46:04 +0000591 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +0000592 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Cheng354be062008-03-04 08:05:30 +0000593 if (Extend)
Dale Johannesenea996922009-02-04 20:06:27 +0000594 return DAG.getExtLoad(ISD::EXTLOAD, dl,
Dale Johannesen3c4fb222009-02-04 02:34:38 +0000595 OrigVT, DAG.getEntryNode(),
Dan Gohmanfb020b62008-02-07 18:41:25 +0000596 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman04637d12008-09-16 22:05:41 +0000597 0, VT, false, Alignment);
Dale Johannesenea996922009-02-04 20:06:27 +0000598 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +0000599 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600}
601
602
603/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
604/// operations.
605static
Dan Gohman8181bd12008-07-27 21:46:04 +0000606SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
Dan Gohman0275b132009-01-15 16:43:02 +0000607 SelectionDAG &DAG,
608 const TargetLowering &TLI) {
Dale Johannesen352c47e2009-02-02 20:41:04 +0000609 DebugLoc dl = Node->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +0000610 MVT VT = Node->getValueType(0);
611 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
613 "fcopysign expansion only supported for f32 and f64");
Duncan Sands92c43912008-06-06 12:08:01 +0000614 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615
616 // First get the sign bit of second operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000617 SDValue Mask1 = (SrcVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
619 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Dale Johannesen352c47e2009-02-02 20:41:04 +0000620 Mask1 = DAG.getNode(ISD::BIT_CONVERT, dl, SrcNVT, Mask1);
621 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, dl, SrcNVT,
622 Node->getOperand(1));
623 SignBit = DAG.getNode(ISD::AND, dl, SrcNVT, SignBit, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands92c43912008-06-06 12:08:01 +0000625 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 if (SizeDiff > 0) {
Dale Johannesen352c47e2009-02-02 20:41:04 +0000627 SignBit = DAG.getNode(ISD::SRL, dl, SrcNVT, SignBit,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000628 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
Dale Johannesen352c47e2009-02-02 20:41:04 +0000629 SignBit = DAG.getNode(ISD::TRUNCATE, dl, NVT, SignBit);
Chris Lattnere6fa1452008-07-10 23:46:13 +0000630 } else if (SizeDiff < 0) {
Dale Johannesen352c47e2009-02-02 20:41:04 +0000631 SignBit = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, SignBit);
632 SignBit = DAG.getNode(ISD::SHL, dl, NVT, SignBit,
Chris Lattnere6fa1452008-07-10 23:46:13 +0000633 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
634 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635
636 // Clear the sign bit of first operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000637 SDValue Mask2 = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
639 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
Dale Johannesen352c47e2009-02-02 20:41:04 +0000640 Mask2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask2);
641 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
642 Result = DAG.getNode(ISD::AND, dl, NVT, Result, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643
644 // Or the value with the sign bit.
Dale Johannesen352c47e2009-02-02 20:41:04 +0000645 Result = DAG.getNode(ISD::OR, dl, NVT, Result, SignBit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 return Result;
647}
648
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000649/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
650static
Dan Gohman8181bd12008-07-27 21:46:04 +0000651SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0275b132009-01-15 16:43:02 +0000652 const TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000653 SDValue Chain = ST->getChain();
654 SDValue Ptr = ST->getBasePtr();
655 SDValue Val = ST->getValue();
Duncan Sands92c43912008-06-06 12:08:01 +0000656 MVT VT = Val.getValueType();
Dale Johannesen08275382007-09-08 19:29:23 +0000657 int Alignment = ST->getAlignment();
658 int SVOffset = ST->getSrcValueOffset();
Dale Johannesen352c47e2009-02-02 20:41:04 +0000659 DebugLoc dl = ST->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +0000660 if (ST->getMemoryVT().isFloatingPoint() ||
661 ST->getMemoryVT().isVector()) {
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000662 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
663 if (TLI.isTypeLegal(intVT)) {
664 // Expand to a bitconvert of the value to the integer type of the
665 // same size, then a (misaligned) int store.
666 // FIXME: Does not handle truncating floating point stores!
Dale Johannesen352c47e2009-02-02 20:41:04 +0000667 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, intVT, Val);
668 return DAG.getStore(Chain, dl, Result, Ptr, ST->getSrcValue(),
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000669 SVOffset, ST->isVolatile(), Alignment);
670 } else {
671 // Do a (aligned) store to a stack slot, then copy from the stack slot
672 // to the final destination using (unaligned) integer loads and stores.
673 MVT StoredVT = ST->getMemoryVT();
674 MVT RegVT =
675 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
676 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
677 unsigned RegBytes = RegVT.getSizeInBits() / 8;
678 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen08275382007-09-08 19:29:23 +0000679
Duncan Sands734f49b2008-12-13 07:18:38 +0000680 // Make sure the stack slot is also aligned for the register type.
681 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
682
683 // Perform the original store, only redirected to the stack slot.
Dale Johannesen352c47e2009-02-02 20:41:04 +0000684 SDValue Store = DAG.getTruncStore(Chain, dl,
685 Val, StackPtr, NULL, 0,StoredVT);
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000686 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
687 SmallVector<SDValue, 8> Stores;
688 unsigned Offset = 0;
689
690 // Do all but one copies using the full register width.
691 for (unsigned i = 1; i < NumRegs; i++) {
692 // Load one integer register's worth from the stack slot.
Dale Johannesen352c47e2009-02-02 20:41:04 +0000693 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, NULL, 0);
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000694 // Store it to the final location. Remember the store.
Dale Johannesen352c47e2009-02-02 20:41:04 +0000695 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000696 ST->getSrcValue(), SVOffset + Offset,
697 ST->isVolatile(),
698 MinAlign(ST->getAlignment(), Offset)));
699 // Increment the pointers.
700 Offset += RegBytes;
Dale Johannesen352c47e2009-02-02 20:41:04 +0000701 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000702 Increment);
Dale Johannesen352c47e2009-02-02 20:41:04 +0000703 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000704 }
705
Duncan Sands734f49b2008-12-13 07:18:38 +0000706 // The last store may be partial. Do a truncating store. On big-endian
707 // machines this requires an extending load from the stack slot to ensure
708 // that the bits are in the right place.
709 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000710
Duncan Sands734f49b2008-12-13 07:18:38 +0000711 // Load from the stack slot.
Dale Johannesen352c47e2009-02-02 20:41:04 +0000712 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
Duncan Sands734f49b2008-12-13 07:18:38 +0000713 NULL, 0, MemVT);
714
Dale Johannesen352c47e2009-02-02 20:41:04 +0000715 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000716 ST->getSrcValue(), SVOffset + Offset,
Duncan Sands734f49b2008-12-13 07:18:38 +0000717 MemVT, ST->isVolatile(),
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000718 MinAlign(ST->getAlignment(), Offset)));
719 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesen352c47e2009-02-02 20:41:04 +0000720 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000721 Stores.size());
722 }
Dale Johannesen08275382007-09-08 19:29:23 +0000723 }
Duncan Sands92c43912008-06-06 12:08:01 +0000724 assert(ST->getMemoryVT().isInteger() &&
725 !ST->getMemoryVT().isVector() &&
Dale Johannesen08275382007-09-08 19:29:23 +0000726 "Unaligned store of unknown type.");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000727 // Get the half-size VT
Duncan Sands92c43912008-06-06 12:08:01 +0000728 MVT NewStoredVT =
729 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
730 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000731 int IncrementSize = NumBits / 8;
732
733 // Divide the stored value in two parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000734 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
735 SDValue Lo = Val;
Dale Johannesen352c47e2009-02-02 20:41:04 +0000736 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000737
738 // Store the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000739 SDValue Store1, Store2;
Dale Johannesen352c47e2009-02-02 20:41:04 +0000740 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000741 ST->getSrcValue(), SVOffset, NewStoredVT,
742 ST->isVolatile(), Alignment);
Dale Johannesen352c47e2009-02-02 20:41:04 +0000743 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000744 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsa3691432007-10-28 12:59:45 +0000745 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesen352c47e2009-02-02 20:41:04 +0000746 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000747 ST->getSrcValue(), SVOffset + IncrementSize,
748 NewStoredVT, ST->isVolatile(), Alignment);
749
Dale Johannesen352c47e2009-02-02 20:41:04 +0000750 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000751}
752
753/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
754static
Dan Gohman8181bd12008-07-27 21:46:04 +0000755SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmana0c429e2009-01-15 16:58:17 +0000756 const TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000757 int SVOffset = LD->getSrcValueOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +0000758 SDValue Chain = LD->getChain();
759 SDValue Ptr = LD->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +0000760 MVT VT = LD->getValueType(0);
761 MVT LoadedVT = LD->getMemoryVT();
Dale Johannesen352c47e2009-02-02 20:41:04 +0000762 DebugLoc dl = LD->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +0000763 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000764 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
765 if (TLI.isTypeLegal(intVT)) {
766 // Expand to a (misaligned) integer load of the same size,
767 // then bitconvert to floating point or vector.
Dale Johannesen352c47e2009-02-02 20:41:04 +0000768 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000769 SVOffset, LD->isVolatile(),
Dale Johannesen08275382007-09-08 19:29:23 +0000770 LD->getAlignment());
Dale Johannesen352c47e2009-02-02 20:41:04 +0000771 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, LoadedVT, newLoad);
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000772 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesen352c47e2009-02-02 20:41:04 +0000773 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen08275382007-09-08 19:29:23 +0000774
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000775 SDValue Ops[] = { Result, Chain };
Dale Johannesen352c47e2009-02-02 20:41:04 +0000776 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000777 } else {
778 // Copy the value to a (aligned) stack slot using (unaligned) integer
779 // loads and stores, then do a (aligned) load from the stack slot.
780 MVT RegVT = TLI.getRegisterType(intVT);
781 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
782 unsigned RegBytes = RegVT.getSizeInBits() / 8;
783 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
784
Duncan Sands734f49b2008-12-13 07:18:38 +0000785 // Make sure the stack slot is also aligned for the register type.
786 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
787
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000788 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
789 SmallVector<SDValue, 8> Stores;
790 SDValue StackPtr = StackBase;
791 unsigned Offset = 0;
792
793 // Do all but one copies using the full register width.
794 for (unsigned i = 1; i < NumRegs; i++) {
795 // Load one integer register's worth from the original location.
Dale Johannesen352c47e2009-02-02 20:41:04 +0000796 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000797 SVOffset + Offset, LD->isVolatile(),
798 MinAlign(LD->getAlignment(), Offset));
799 // Follow the load with a store to the stack slot. Remember the store.
Dale Johannesen352c47e2009-02-02 20:41:04 +0000800 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000801 NULL, 0));
802 // Increment the pointers.
803 Offset += RegBytes;
Dale Johannesen352c47e2009-02-02 20:41:04 +0000804 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
805 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000806 Increment);
807 }
808
809 // The last copy may be partial. Do an extending load.
Duncan Sands734f49b2008-12-13 07:18:38 +0000810 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Dale Johannesen352c47e2009-02-02 20:41:04 +0000811 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000812 LD->getSrcValue(), SVOffset + Offset,
Duncan Sands734f49b2008-12-13 07:18:38 +0000813 MemVT, LD->isVolatile(),
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000814 MinAlign(LD->getAlignment(), Offset));
815 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sands734f49b2008-12-13 07:18:38 +0000816 // On big-endian machines this requires a truncating store to ensure
817 // that the bits end up in the right place.
Dale Johannesen352c47e2009-02-02 20:41:04 +0000818 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sands734f49b2008-12-13 07:18:38 +0000819 NULL, 0, MemVT));
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000820
821 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesen352c47e2009-02-02 20:41:04 +0000822 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000823 Stores.size());
824
825 // Finally, perform the original load only redirected to the stack slot.
Dale Johannesen352c47e2009-02-02 20:41:04 +0000826 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000827 NULL, 0, LoadedVT);
828
829 // Callers expect a MERGE_VALUES node.
830 SDValue Ops[] = { Load, TF };
Dale Johannesen352c47e2009-02-02 20:41:04 +0000831 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb7ae4592008-12-12 21:47:02 +0000832 }
Dale Johannesen08275382007-09-08 19:29:23 +0000833 }
Duncan Sands92c43912008-06-06 12:08:01 +0000834 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000835 "Unaligned load of unsupported type.");
836
Dale Johannesendc0ee192008-02-27 22:36:00 +0000837 // Compute the new VT that is half the size of the old one. This is an
838 // integer MVT.
Duncan Sands92c43912008-06-06 12:08:01 +0000839 unsigned NumBits = LoadedVT.getSizeInBits();
840 MVT NewLoadedVT;
841 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000842 NumBits >>= 1;
843
844 unsigned Alignment = LD->getAlignment();
845 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000846 ISD::LoadExtType HiExtType = LD->getExtensionType();
847
848 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
849 if (HiExtType == ISD::NON_EXTLOAD)
850 HiExtType = ISD::ZEXTLOAD;
851
852 // Load the value in two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000853 SDValue Lo, Hi;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000854 if (TLI.isLittleEndian()) {
Dale Johannesen352c47e2009-02-02 20:41:04 +0000855 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000856 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesen352c47e2009-02-02 20:41:04 +0000857 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000858 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesen352c47e2009-02-02 20:41:04 +0000859 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000860 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000861 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000862 } else {
Dale Johannesen352c47e2009-02-02 20:41:04 +0000863 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
864 SVOffset, NewLoadedVT,LD->isVolatile(), Alignment);
865 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000866 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesen352c47e2009-02-02 20:41:04 +0000867 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000868 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000869 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000870 }
871
872 // aggregate the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000873 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
Dale Johannesen352c47e2009-02-02 20:41:04 +0000874 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
875 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000876
Dale Johannesen352c47e2009-02-02 20:41:04 +0000877 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000878 Hi.getValue(1));
879
Dan Gohman8181bd12008-07-27 21:46:04 +0000880 SDValue Ops[] = { Result, TF };
Dale Johannesen352c47e2009-02-02 20:41:04 +0000881 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000882}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883
Dan Gohman6d05cac2007-10-11 23:57:53 +0000884/// UnrollVectorOp - We know that the given vector has a legal type, however
885/// the operation it performs is not legal and is an operation that we have
886/// no way of lowering. "Unroll" the vector, splitting out the scalars and
887/// operating on each element individually.
Dan Gohman8181bd12008-07-27 21:46:04 +0000888SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000889 MVT VT = Op.getValueType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000890 assert(isTypeLegal(VT) &&
891 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +0000892 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman6d05cac2007-10-11 23:57:53 +0000893 "Can't unroll a vector with multiple results!");
Duncan Sands92c43912008-06-06 12:08:01 +0000894 unsigned NE = VT.getVectorNumElements();
895 MVT EltVT = VT.getVectorElementType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +0000896 DebugLoc dl = Op.getDebugLoc();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000897
Dan Gohman8181bd12008-07-27 21:46:04 +0000898 SmallVector<SDValue, 8> Scalars;
899 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman6d05cac2007-10-11 23:57:53 +0000900 for (unsigned i = 0; i != NE; ++i) {
901 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000902 SDValue Operand = Op.getOperand(j);
Duncan Sands92c43912008-06-06 12:08:01 +0000903 MVT OperandVT = Operand.getValueType();
904 if (OperandVT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +0000905 // A vector operand; extract a single element.
Duncan Sands92c43912008-06-06 12:08:01 +0000906 MVT OperandEltVT = OperandVT.getVectorElementType();
Dale Johannesen352c47e2009-02-02 20:41:04 +0000907 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dan Gohman6d05cac2007-10-11 23:57:53 +0000908 OperandEltVT,
909 Operand,
910 DAG.getConstant(i, MVT::i32));
911 } else {
912 // A scalar operand; just use it as is.
913 Operands[j] = Operand;
914 }
915 }
Mon P Wang9901e732008-12-09 05:46:39 +0000916
917 switch (Op.getOpcode()) {
918 default:
Dale Johannesen352c47e2009-02-02 20:41:04 +0000919 Scalars.push_back(DAG.getNode(Op.getOpcode(), dl, EltVT,
Mon P Wang9901e732008-12-09 05:46:39 +0000920 &Operands[0], Operands.size()));
921 break;
922 case ISD::SHL:
923 case ISD::SRA:
924 case ISD::SRL:
Duncan Sands7d9e3612009-01-31 15:50:11 +0000925 case ISD::ROTL:
926 case ISD::ROTR:
Dale Johannesen352c47e2009-02-02 20:41:04 +0000927 Scalars.push_back(DAG.getNode(Op.getOpcode(), dl, EltVT, Operands[0],
Duncan Sands7d9e3612009-01-31 15:50:11 +0000928 DAG.getShiftAmountOperand(Operands[1])));
Mon P Wang9901e732008-12-09 05:46:39 +0000929 break;
930 }
Dan Gohman6d05cac2007-10-11 23:57:53 +0000931 }
932
Dale Johannesen352c47e2009-02-02 20:41:04 +0000933 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Scalars[0], Scalars.size());
Dan Gohman6d05cac2007-10-11 23:57:53 +0000934}
935
Duncan Sands37a3f472008-01-10 10:28:30 +0000936/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands92c43912008-06-06 12:08:01 +0000937static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands37a3f472008-01-10 10:28:30 +0000938 RTLIB::Libcall Call_F32,
939 RTLIB::Libcall Call_F64,
940 RTLIB::Libcall Call_F80,
941 RTLIB::Libcall Call_PPCF128) {
942 return
943 VT == MVT::f32 ? Call_F32 :
944 VT == MVT::f64 ? Call_F64 :
945 VT == MVT::f80 ? Call_F80 :
946 VT == MVT::ppcf128 ? Call_PPCF128 :
947 RTLIB::UNKNOWN_LIBCALL;
948}
949
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000950/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
951/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
952/// is necessary to spill the vector being inserted into to memory, perform
953/// the insert there, and then read the result back.
Dan Gohman8181bd12008-07-27 21:46:04 +0000954SDValue SelectionDAGLegalize::
Dale Johannesen352c47e2009-02-02 20:41:04 +0000955PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
956 DebugLoc dl) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000957 SDValue Tmp1 = Vec;
958 SDValue Tmp2 = Val;
959 SDValue Tmp3 = Idx;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000960
961 // If the target doesn't support this, we have to spill the input vector
962 // to a temporary stack slot, update the element, then reload it. This is
963 // badness. We could also load the value into a vector register (either
964 // with a "move to register" or "extload into register" instruction, then
965 // permute it into place, if the idx is a constant and if the idx is
966 // supported by the target.
Duncan Sands92c43912008-06-06 12:08:01 +0000967 MVT VT = Tmp1.getValueType();
968 MVT EltVT = VT.getVectorElementType();
969 MVT IdxVT = Tmp3.getValueType();
970 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +0000971 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000972
Gabor Greif1c80d112008-08-28 21:40:38 +0000973 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000974
975 // Store the vector.
Dale Johannesen352c47e2009-02-02 20:41:04 +0000976 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Mon P Wang1448aad2008-10-30 08:01:45 +0000977 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000978
979 // Truncate or zero extend offset to target pointer type.
Duncan Sandsec142ee2008-06-08 20:54:56 +0000980 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesen352c47e2009-02-02 20:41:04 +0000981 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000982 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +0000983 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesen352c47e2009-02-02 20:41:04 +0000984 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
985 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000986 // Store the scalar value.
Dale Johannesen352c47e2009-02-02 20:41:04 +0000987 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2,
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000988 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000989 // Load the updated vector.
Dale Johannesen352c47e2009-02-02 20:41:04 +0000990 return DAG.getLoad(VT, dl, Ch, StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000991 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000992}
993
Mon P Wang9901e732008-12-09 05:46:39 +0000994
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995/// LegalizeOp - We know that the specified value has a legal type, and
996/// that its operands are legal. Now ensure that the operation itself
997/// is legal, recursively ensuring that the operands' operations remain
998/// legal.
Dan Gohman8181bd12008-07-27 21:46:04 +0000999SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattnerdad577b2007-08-25 01:00:22 +00001000 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
1001 return Op;
1002
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001003 assert(isTypeLegal(Op.getValueType()) &&
1004 "Caller should expand or promote operands that are not legal!");
Gabor Greif1c80d112008-08-28 21:40:38 +00001005 SDNode *Node = Op.getNode();
Dale Johannesenca6237b2009-01-30 23:10:59 +00001006 DebugLoc dl = Node->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001007
1008 // If this operation defines any values that cannot be represented in a
1009 // register on this target, make sure to expand or promote them.
1010 if (Node->getNumValues() > 1) {
1011 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1012 if (getTypeAction(Node->getValueType(i)) != Legal) {
1013 HandleOp(Op.getValue(i));
1014 assert(LegalizedNodes.count(Op) &&
1015 "Handling didn't add legal operands!");
1016 return LegalizedNodes[Op];
1017 }
1018 }
1019
1020 // Note that LegalizeOp may be reentered even from single-use nodes, which
1021 // means that we always must cache transformed nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00001022 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001023 if (I != LegalizedNodes.end()) return I->second;
1024
Dan Gohman8181bd12008-07-27 21:46:04 +00001025 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
1026 SDValue Result = Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001027 bool isCustom = false;
1028
1029 switch (Node->getOpcode()) {
1030 case ISD::FrameIndex:
1031 case ISD::EntryToken:
1032 case ISD::Register:
1033 case ISD::BasicBlock:
1034 case ISD::TargetFrameIndex:
1035 case ISD::TargetJumpTable:
1036 case ISD::TargetConstant:
1037 case ISD::TargetConstantFP:
1038 case ISD::TargetConstantPool:
1039 case ISD::TargetGlobalAddress:
1040 case ISD::TargetGlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +00001041 case ISD::TargetExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001042 case ISD::VALUETYPE:
1043 case ISD::SRCVALUE:
Dan Gohman12a9c082008-02-06 22:27:42 +00001044 case ISD::MEMOPERAND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001045 case ISD::CONDCODE:
Duncan Sandsc93fae32008-03-21 09:14:45 +00001046 case ISD::ARG_FLAGS:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00001048 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049 "This must be legal!");
1050 break;
1051 default:
1052 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1053 // If this is a target node, legalize it by legalizing the operands then
1054 // passing it through.
Dan Gohman8181bd12008-07-27 21:46:04 +00001055 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001056 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1057 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1058
1059 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
1060
1061 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1062 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00001063 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 }
1065 // Otherwise this is an unhandled builtin node. splat.
1066#ifndef NDEBUG
1067 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
1068#endif
1069 assert(0 && "Do not know how to legalize this operator!");
1070 abort();
1071 case ISD::GLOBAL_OFFSET_TABLE:
1072 case ISD::GlobalAddress:
1073 case ISD::GlobalTLSAddress:
Bill Wendlingfef06052008-09-16 21:48:12 +00001074 case ISD::ExternalSymbol:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075 case ISD::ConstantPool:
1076 case ISD::JumpTable: // Nothing to do.
1077 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1078 default: assert(0 && "This action is not supported yet!");
1079 case TargetLowering::Custom:
1080 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001081 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001082 // FALLTHROUGH if the target doesn't want to lower this op after all.
1083 case TargetLowering::Legal:
1084 break;
1085 }
1086 break;
1087 case ISD::FRAMEADDR:
1088 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089 // The only option for these nodes is to custom lower them. If the target
1090 // does not custom lower them, then return zero.
1091 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001092 if (Tmp1.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093 Result = Tmp1;
1094 else
1095 Result = DAG.getConstant(0, TLI.getPointerTy());
1096 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +00001097 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands92c43912008-06-06 12:08:01 +00001098 MVT VT = Node->getValueType(0);
Anton Korobeynikov09386bd2007-08-29 19:28:29 +00001099 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1100 default: assert(0 && "This action is not supported yet!");
1101 case TargetLowering::Custom:
1102 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001103 if (Result.getNode()) break;
Anton Korobeynikov09386bd2007-08-29 19:28:29 +00001104 // Fall Thru
1105 case TargetLowering::Legal:
1106 Result = DAG.getConstant(0, VT);
1107 break;
1108 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +00001109 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +00001110 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001111 case ISD::EXCEPTIONADDR: {
1112 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00001113 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001114 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1115 default: assert(0 && "This action is not supported yet!");
1116 case TargetLowering::Expand: {
1117 unsigned Reg = TLI.getExceptionAddressRegister();
Dale Johannesen564036c2009-02-04 00:13:36 +00001118 Result = DAG.getCopyFromReg(Tmp1, dl, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001119 }
1120 break;
1121 case TargetLowering::Custom:
1122 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001123 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001124 // Fall Thru
1125 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001126 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001127 Result = DAG.getMergeValues(Ops, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001128 break;
1129 }
1130 }
1131 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001132 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001133
Gabor Greif1c80d112008-08-28 21:40:38 +00001134 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001135 "Cannot return more than two values!");
1136
1137 // Since we produced two values, make sure to remember that we
1138 // legalized both of them.
1139 Tmp1 = LegalizeOp(Result);
1140 Tmp2 = LegalizeOp(Result.getValue(1));
1141 AddLegalizedOperand(Op.getValue(0), Tmp1);
1142 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001143 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001144 case ISD::EHSELECTION: {
1145 Tmp1 = LegalizeOp(Node->getOperand(0));
1146 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00001147 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001148 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1149 default: assert(0 && "This action is not supported yet!");
1150 case TargetLowering::Expand: {
1151 unsigned Reg = TLI.getExceptionSelectorRegister();
Dale Johannesen564036c2009-02-04 00:13:36 +00001152 Result = DAG.getCopyFromReg(Tmp2, dl, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001153 }
1154 break;
1155 case TargetLowering::Custom:
1156 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001157 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001158 // Fall Thru
1159 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001160 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001161 Result = DAG.getMergeValues(Ops, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001162 break;
1163 }
1164 }
1165 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001166 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001167
Gabor Greif1c80d112008-08-28 21:40:38 +00001168 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +00001169 "Cannot return more than two values!");
1170
1171 // Since we produced two values, make sure to remember that we
1172 // legalized both of them.
1173 Tmp1 = LegalizeOp(Result);
1174 Tmp2 = LegalizeOp(Result.getValue(1));
1175 AddLegalizedOperand(Op.getValue(0), Tmp1);
1176 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001177 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001178 case ISD::EH_RETURN: {
Duncan Sands92c43912008-06-06 12:08:01 +00001179 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001180 // The only "good" option for this node is to custom lower it.
1181 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1182 default: assert(0 && "This action is not supported at all!");
1183 case TargetLowering::Custom:
1184 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001185 if (Result.getNode()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001186 // Fall Thru
1187 case TargetLowering::Legal:
1188 // Target does not know, how to lower this, lower to noop
1189 Result = LegalizeOp(Node->getOperand(0));
1190 break;
1191 }
1192 }
1193 break;
1194 case ISD::AssertSext:
1195 case ISD::AssertZext:
1196 Tmp1 = LegalizeOp(Node->getOperand(0));
1197 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1198 break;
1199 case ISD::MERGE_VALUES:
1200 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif46bf5472008-08-26 22:36:50 +00001201 Result = Node->getOperand(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001202 break;
1203 case ISD::CopyFromReg:
1204 Tmp1 = LegalizeOp(Node->getOperand(0));
1205 Result = Op.getValue(0);
1206 if (Node->getNumValues() == 2) {
1207 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1208 } else {
1209 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
1210 if (Node->getNumOperands() == 3) {
1211 Tmp2 = LegalizeOp(Node->getOperand(2));
1212 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1213 } else {
1214 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1215 }
1216 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1217 }
1218 // Since CopyFromReg produces two values, make sure to remember that we
1219 // legalized both of them.
1220 AddLegalizedOperand(Op.getValue(0), Result);
1221 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001222 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001223 case ISD::UNDEF: {
Duncan Sands92c43912008-06-06 12:08:01 +00001224 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001225 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
1226 default: assert(0 && "This action is not supported yet!");
1227 case TargetLowering::Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00001228 if (VT.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001229 Result = DAG.getConstant(0, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00001230 else if (VT.isFloatingPoint())
1231 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesen20b76352007-09-26 17:26:49 +00001232 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001233 else
1234 assert(0 && "Unknown value type!");
1235 break;
1236 case TargetLowering::Legal:
1237 break;
1238 }
1239 break;
1240 }
1241
1242 case ISD::INTRINSIC_W_CHAIN:
1243 case ISD::INTRINSIC_WO_CHAIN:
1244 case ISD::INTRINSIC_VOID: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001245 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001246 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1247 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1248 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1249
1250 // Allow the target to custom lower its intrinsics if it wants to.
1251 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1252 TargetLowering::Custom) {
1253 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001254 if (Tmp3.getNode()) Result = Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001255 }
1256
Gabor Greif1c80d112008-08-28 21:40:38 +00001257 if (Result.getNode()->getNumValues() == 1) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001258
1259 // Must have return value and chain result.
Gabor Greif1c80d112008-08-28 21:40:38 +00001260 assert(Result.getNode()->getNumValues() == 2 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001261 "Cannot return more than two values!");
1262
1263 // Since loads produce two values, make sure to remember that we
1264 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001265 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1266 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001267 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001268 }
1269
Dan Gohman472d12c2008-06-30 20:59:49 +00001270 case ISD::DBG_STOPPOINT:
1271 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001272 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1273
Dan Gohman472d12c2008-06-30 20:59:49 +00001274 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001275 case TargetLowering::Promote:
1276 default: assert(0 && "This action is not supported yet!");
Bill Wendling2ca91b12009-02-13 02:01:04 +00001277 case TargetLowering::Expand:
1278 Result = Tmp1; // chain
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001279 break;
Evan Chengd6f57682008-07-08 20:06:39 +00001280 case TargetLowering::Legal: {
1281 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1282 if (Action == Legal && Tmp1 == Node->getOperand(0))
1283 break;
1284
Dan Gohman8181bd12008-07-27 21:46:04 +00001285 SmallVector<SDValue, 8> Ops;
Evan Chengd6f57682008-07-08 20:06:39 +00001286 Ops.push_back(Tmp1);
1287 if (Action == Legal) {
1288 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1289 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1290 } else {
1291 // Otherwise promote them.
1292 Ops.push_back(PromoteOp(Node->getOperand(1)));
1293 Ops.push_back(PromoteOp(Node->getOperand(2)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001294 }
Evan Chengd6f57682008-07-08 20:06:39 +00001295 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1296 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1297 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 break;
1299 }
Evan Chengd6f57682008-07-08 20:06:39 +00001300 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001301 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001302
1303 case ISD::DECLARE:
1304 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1305 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1306 default: assert(0 && "This action is not supported yet!");
1307 case TargetLowering::Legal:
1308 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1309 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1310 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1311 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1312 break;
Chris Lattner203cd052008-02-28 05:53:40 +00001313 case TargetLowering::Expand:
1314 Result = LegalizeOp(Node->getOperand(0));
1315 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001316 }
1317 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001318
1319 case ISD::DEBUG_LOC:
1320 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1321 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1322 default: assert(0 && "This action is not supported yet!");
Evan Chengd6f57682008-07-08 20:06:39 +00001323 case TargetLowering::Legal: {
1324 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001325 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Chengd6f57682008-07-08 20:06:39 +00001326 if (Action == Legal && Tmp1 == Node->getOperand(0))
1327 break;
1328 if (Action == Legal) {
1329 Tmp2 = Node->getOperand(1);
1330 Tmp3 = Node->getOperand(2);
1331 Tmp4 = Node->getOperand(3);
1332 } else {
1333 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1334 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1335 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1336 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001337 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1338 break;
1339 }
Evan Chengd6f57682008-07-08 20:06:39 +00001340 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001341 break;
1342
Dan Gohmanfa607c92008-07-01 00:05:16 +00001343 case ISD::DBG_LABEL:
1344 case ISD::EH_LABEL:
1345 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1346 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001347 default: assert(0 && "This action is not supported yet!");
1348 case TargetLowering::Legal:
1349 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohmanfa607c92008-07-01 00:05:16 +00001350 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001351 break;
1352 case TargetLowering::Expand:
1353 Result = LegalizeOp(Node->getOperand(0));
1354 break;
1355 }
1356 break;
1357
Evan Chengd1d68072008-03-08 00:58:38 +00001358 case ISD::PREFETCH:
1359 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1360 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1361 default: assert(0 && "This action is not supported yet!");
1362 case TargetLowering::Legal:
1363 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1364 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1365 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1366 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1367 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1368 break;
1369 case TargetLowering::Expand:
1370 // It's a noop.
1371 Result = LegalizeOp(Node->getOperand(0));
1372 break;
1373 }
1374 break;
1375
Andrew Lenharth785610d2008-02-16 01:24:58 +00001376 case ISD::MEMBARRIER: {
1377 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001378 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1379 default: assert(0 && "This action is not supported yet!");
1380 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001381 SDValue Ops[6];
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001382 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands3ee041a2008-02-27 08:53:44 +00001383 for (int x = 1; x < 6; ++x) {
1384 Ops[x] = Node->getOperand(x);
1385 if (!isTypeLegal(Ops[x].getValueType()))
1386 Ops[x] = PromoteOp(Ops[x]);
1387 }
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001388 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1389 break;
1390 }
1391 case TargetLowering::Expand:
1392 //There is no libgcc call for this op
1393 Result = Node->getOperand(0); // Noop
1394 break;
1395 }
Andrew Lenharth785610d2008-02-16 01:24:58 +00001396 break;
1397 }
1398
Dan Gohmanbebba8d2008-12-23 21:37:04 +00001399 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001400 unsigned int num_operands = 4;
1401 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001402 SDValue Ops[4];
Mon P Wang078a62d2008-05-05 19:05:59 +00001403 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001404 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang078a62d2008-05-05 19:05:59 +00001405 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1406
1407 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1408 default: assert(0 && "This action is not supported yet!");
1409 case TargetLowering::Custom:
1410 Result = TLI.LowerOperation(Result, DAG);
1411 break;
1412 case TargetLowering::Legal:
1413 break;
1414 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001415 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1416 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001417 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001418 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00001419 case ISD::ATOMIC_LOAD_ADD:
1420 case ISD::ATOMIC_LOAD_SUB:
1421 case ISD::ATOMIC_LOAD_AND:
1422 case ISD::ATOMIC_LOAD_OR:
1423 case ISD::ATOMIC_LOAD_XOR:
1424 case ISD::ATOMIC_LOAD_NAND:
1425 case ISD::ATOMIC_LOAD_MIN:
1426 case ISD::ATOMIC_LOAD_MAX:
1427 case ISD::ATOMIC_LOAD_UMIN:
1428 case ISD::ATOMIC_LOAD_UMAX:
1429 case ISD::ATOMIC_SWAP: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001430 unsigned int num_operands = 3;
1431 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001432 SDValue Ops[3];
Mon P Wang078a62d2008-05-05 19:05:59 +00001433 for (unsigned int x = 0; x < num_operands; ++x)
1434 Ops[x] = LegalizeOp(Node->getOperand(x));
1435 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sandsac496a12008-07-04 11:47:58 +00001436
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001437 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001438 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001439 case TargetLowering::Custom:
1440 Result = TLI.LowerOperation(Result, DAG);
1441 break;
1442 case TargetLowering::Legal:
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001443 break;
1444 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001445 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1446 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001447 return Result.getValue(Op.getResNo());
Duncan Sandsac496a12008-07-04 11:47:58 +00001448 }
Scott Michelf2e2b702007-08-08 23:23:31 +00001449 case ISD::Constant: {
1450 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1451 unsigned opAction =
1452 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1453
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001454 // We know we don't need to expand constants here, constants only have one
1455 // value and we check that it is fine above.
1456
Scott Michelf2e2b702007-08-08 23:23:31 +00001457 if (opAction == TargetLowering::Custom) {
1458 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001459 if (Tmp1.getNode())
Scott Michelf2e2b702007-08-08 23:23:31 +00001460 Result = Tmp1;
1461 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001462 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001463 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001464 case ISD::ConstantFP: {
1465 // Spill FP immediates to the constant pool if the target cannot directly
1466 // codegen them. Targets often have some immediate values that can be
1467 // efficiently generated into an FP register without a load. We explicitly
1468 // leave these constants as ConstantFP nodes for the target to deal with.
1469 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1470
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001471 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1472 default: assert(0 && "This action is not supported yet!");
Nate Begemane2ba64f2008-02-14 08:57:00 +00001473 case TargetLowering::Legal:
1474 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001475 case TargetLowering::Custom:
1476 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001477 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001478 Result = Tmp3;
1479 break;
1480 }
1481 // FALLTHROUGH
Nate Begemane2ba64f2008-02-14 08:57:00 +00001482 case TargetLowering::Expand: {
1483 // Check to see if this FP immediate is already legal.
1484 bool isLegal = false;
1485 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1486 E = TLI.legal_fpimm_end(); I != E; ++I) {
1487 if (CFP->isExactlyValue(*I)) {
1488 isLegal = true;
1489 break;
1490 }
1491 }
1492 // If this is a legal constant, turn it into a TargetConstantFP node.
1493 if (isLegal)
1494 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001495 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1496 }
Nate Begemane2ba64f2008-02-14 08:57:00 +00001497 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001498 break;
1499 }
1500 case ISD::TokenFactor:
1501 if (Node->getNumOperands() == 2) {
1502 Tmp1 = LegalizeOp(Node->getOperand(0));
1503 Tmp2 = LegalizeOp(Node->getOperand(1));
1504 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1505 } else if (Node->getNumOperands() == 3) {
1506 Tmp1 = LegalizeOp(Node->getOperand(0));
1507 Tmp2 = LegalizeOp(Node->getOperand(1));
1508 Tmp3 = LegalizeOp(Node->getOperand(2));
1509 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1510 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00001511 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001512 // Legalize the operands.
1513 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1514 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1515 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1516 }
1517 break;
1518
1519 case ISD::FORMAL_ARGUMENTS:
1520 case ISD::CALL:
1521 // The only option for this is to custom lower it.
1522 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001523 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesenac246272008-03-05 19:14:03 +00001524 // A call within a calling sequence must be legalized to something
1525 // other than the normal CALLSEQ_END. Violating this gets Legalize
1526 // into an infinite loop.
1527 assert ((!IsLegalizingCall ||
1528 Node->getOpcode() != ISD::CALL ||
Gabor Greif1c80d112008-08-28 21:40:38 +00001529 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesenac246272008-03-05 19:14:03 +00001530 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001531
1532 // The number of incoming and outgoing values should match; unless the final
1533 // outgoing value is a flag.
Gabor Greif1c80d112008-08-28 21:40:38 +00001534 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1535 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1536 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling22f8deb2007-11-13 00:44:25 +00001537 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001538 "Lowering call/formal_arguments produced unexpected # results!");
1539
1540 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1541 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greif1c80d112008-08-28 21:40:38 +00001542 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1543 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling22f8deb2007-11-13 00:44:25 +00001544 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00001546 if (Op.getResNo() == i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001547 Tmp2 = Tmp1;
Dan Gohman8181bd12008-07-27 21:46:04 +00001548 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001549 }
1550 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001551 case ISD::EXTRACT_SUBREG: {
1552 Tmp1 = LegalizeOp(Node->getOperand(0));
1553 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1554 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001555 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001556 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1557 }
1558 break;
1559 case ISD::INSERT_SUBREG: {
1560 Tmp1 = LegalizeOp(Node->getOperand(0));
1561 Tmp2 = LegalizeOp(Node->getOperand(1));
1562 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1563 assert(idx && "Operand must be a constant");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001564 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lambb768c2e2007-07-26 07:34:40 +00001565 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1566 }
1567 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001568 case ISD::BUILD_VECTOR:
1569 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1570 default: assert(0 && "This action is not supported yet!");
1571 case TargetLowering::Custom:
1572 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001573 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001574 Result = Tmp3;
1575 break;
1576 }
1577 // FALLTHROUGH
1578 case TargetLowering::Expand:
Gabor Greif1c80d112008-08-28 21:40:38 +00001579 Result = ExpandBUILD_VECTOR(Result.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001580 break;
1581 }
1582 break;
1583 case ISD::INSERT_VECTOR_ELT:
1584 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001585 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001586
1587 // The type of the value to insert may not be legal, even though the vector
1588 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1589 // here.
1590 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1591 default: assert(0 && "Cannot expand insert element operand");
1592 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1593 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang1448aad2008-10-30 08:01:45 +00001594 case Expand:
1595 // FIXME: An alternative would be to check to see if the target is not
1596 // going to custom lower this operation, we could bitcast to half elt
1597 // width and perform two inserts at that width, if that is legal.
1598 Tmp2 = Node->getOperand(1);
1599 break;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001600 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001601 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1602
1603 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1604 Node->getValueType(0))) {
1605 default: assert(0 && "This action is not supported yet!");
1606 case TargetLowering::Legal:
1607 break;
1608 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001609 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001610 if (Tmp4.getNode()) {
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001611 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001612 break;
1613 }
1614 // FALLTHROUGH
Mon P Wang1448aad2008-10-30 08:01:45 +00001615 case TargetLowering::Promote:
1616 // Fall thru for vector case
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001617 case TargetLowering::Expand: {
1618 // If the insert index is a constant, codegen this as a scalar_to_vector,
1619 // then a shuffle that inserts it into the right position in the vector.
1620 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001621 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1622 // match the element type of the vector being created.
1623 if (Tmp2.getValueType() ==
Duncan Sands92c43912008-06-06 12:08:01 +00001624 Op.getValueType().getVectorElementType()) {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001625 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001626 Tmp1.getValueType(), Tmp2);
1627
Duncan Sands92c43912008-06-06 12:08:01 +00001628 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1629 MVT ShufMaskVT =
1630 MVT::getIntVectorWithNumElements(NumElts);
1631 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001632
1633 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1634 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1635 // elt 0 of the RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00001636 SmallVector<SDValue, 8> ShufOps;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001637 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001638 if (i != InsertPos->getZExtValue())
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001639 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1640 else
1641 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1642 }
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001643 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, ShufMaskVT,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001644 &ShufOps[0], ShufOps.size());
1645
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001646 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, Tmp1.getValueType(),
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001647 Tmp1, ScVec, ShufMask);
1648 Result = LegalizeOp(Result);
1649 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001650 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001651 }
Dale Johannesen352c47e2009-02-02 20:41:04 +00001652 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001653 break;
1654 }
1655 }
1656 break;
1657 case ISD::SCALAR_TO_VECTOR:
1658 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1659 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1660 break;
1661 }
1662
1663 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1664 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1665 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1666 Node->getValueType(0))) {
1667 default: assert(0 && "This action is not supported yet!");
1668 case TargetLowering::Legal:
1669 break;
1670 case TargetLowering::Custom:
1671 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001672 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001673 Result = Tmp3;
1674 break;
1675 }
1676 // FALLTHROUGH
1677 case TargetLowering::Expand:
1678 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1679 break;
1680 }
1681 break;
1682 case ISD::VECTOR_SHUFFLE:
1683 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1684 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1685 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1686
1687 // Allow targets to custom lower the SHUFFLEs they support.
1688 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1689 default: assert(0 && "Unknown operation action!");
1690 case TargetLowering::Legal:
1691 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1692 "vector shuffle should not be created if not legal!");
1693 break;
1694 case TargetLowering::Custom:
1695 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001696 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001697 Result = Tmp3;
1698 break;
1699 }
1700 // FALLTHROUGH
1701 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00001702 MVT VT = Node->getValueType(0);
1703 MVT EltVT = VT.getVectorElementType();
1704 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00001705 SDValue Mask = Node->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001706 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00001707 SmallVector<SDValue,8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001708 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001709 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001710 if (Arg.getOpcode() == ISD::UNDEF) {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001711 Ops.push_back(DAG.getUNDEF(EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001712 } else {
1713 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001714 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001715 if (Idx < NumElems)
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001716 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001717 DAG.getConstant(Idx, PtrVT)));
1718 else
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001719 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001720 DAG.getConstant(Idx - NumElems, PtrVT)));
1721 }
1722 }
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001723 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001724 break;
1725 }
1726 case TargetLowering::Promote: {
1727 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00001728 MVT OVT = Node->getValueType(0);
1729 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001730
1731 // Cast the two input vectors.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001732 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
1733 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001734
1735 // Convert the shuffle mask to the right # elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00001736 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001737 assert(Tmp3.getNode() && "Shuffle not legal?");
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001738 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, NVT, Tmp1, Tmp2, Tmp3);
1739 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001740 break;
1741 }
1742 }
1743 break;
1744
1745 case ISD::EXTRACT_VECTOR_ELT:
1746 Tmp1 = Node->getOperand(0);
1747 Tmp2 = LegalizeOp(Node->getOperand(1));
1748 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1749 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1750 break;
1751
1752 case ISD::EXTRACT_SUBVECTOR:
1753 Tmp1 = Node->getOperand(0);
1754 Tmp2 = LegalizeOp(Node->getOperand(1));
1755 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1756 Result = ExpandEXTRACT_SUBVECTOR(Result);
1757 break;
1758
Mon P Wang1448aad2008-10-30 08:01:45 +00001759 case ISD::CONCAT_VECTORS: {
1760 // Use extract/insert/build vector for now. We might try to be
1761 // more clever later.
1762 MVT PtrVT = TLI.getPointerTy();
1763 SmallVector<SDValue, 8> Ops;
1764 unsigned NumOperands = Node->getNumOperands();
1765 for (unsigned i=0; i < NumOperands; ++i) {
1766 SDValue SubOp = Node->getOperand(i);
1767 MVT VVT = SubOp.getNode()->getValueType(0);
1768 MVT EltVT = VVT.getVectorElementType();
1769 unsigned NumSubElem = VVT.getVectorNumElements();
1770 for (unsigned j=0; j < NumSubElem; ++j) {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001771 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
Mon P Wang1448aad2008-10-30 08:01:45 +00001772 DAG.getConstant(j, PtrVT)));
1773 }
1774 }
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001775 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0),
Mon P Wang1448aad2008-10-30 08:01:45 +00001776 &Ops[0], Ops.size()));
1777 }
1778
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001779 case ISD::CALLSEQ_START: {
1780 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1781
1782 // Recursively Legalize all of the inputs of the call end that do not lead
1783 // to this call start. This ensures that any libcalls that need be inserted
1784 // are inserted *before* the CALLSEQ_START.
Mon P Wang7cba3942009-02-04 19:38:14 +00001785 IsLegalizingCallArgs = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001786 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1787 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greif1c80d112008-08-28 21:40:38 +00001788 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001789 NodesLeadingTo);
1790 }
Mon P Wang7cba3942009-02-04 19:38:14 +00001791 IsLegalizingCallArgs = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001792
1793 // Now that we legalized all of the inputs (which may have inserted
1794 // libcalls) create the new CALLSEQ_START node.
1795 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1796
1797 // Merge in the last call, to ensure that this call start after the last
1798 // call ended.
1799 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001800 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1801 Tmp1, LastCALLSEQ_END);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001802 Tmp1 = LegalizeOp(Tmp1);
1803 }
1804
1805 // Do not try to legalize the target-specific arguments (#1+).
1806 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001807 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001808 Ops[0] = Tmp1;
1809 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1810 }
1811
1812 // Remember that the CALLSEQ_START is legalized.
1813 AddLegalizedOperand(Op.getValue(0), Result);
1814 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1815 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1816
1817 // Now that the callseq_start and all of the non-call nodes above this call
1818 // sequence have been legalized, legalize the call itself. During this
1819 // process, no libcalls can/will be inserted, guaranteeing that no calls
1820 // can overlap.
1821 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001822 // Note that we are selecting this call!
Dan Gohman8181bd12008-07-27 21:46:04 +00001823 LastCALLSEQ_END = SDValue(CallEnd, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001824 IsLegalizingCall = true;
1825
1826 // Legalize the call, starting from the CALLSEQ_END.
1827 LegalizeOp(LastCALLSEQ_END);
1828 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1829 return Result;
1830 }
1831 case ISD::CALLSEQ_END:
1832 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1833 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greif1c80d112008-08-28 21:40:38 +00001834 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001835 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1836 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001837 assert(I != LegalizedNodes.end() &&
1838 "Legalizing the call start should have legalized this node!");
1839 return I->second;
1840 }
1841
1842 // Otherwise, the call start has been legalized and everything is going
1843 // according to plan. Just legalize ourselves normally here.
1844 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1845 // Do not try to legalize the target-specific arguments (#1+), except for
1846 // an optional flag input.
1847 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1848 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001849 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001850 Ops[0] = Tmp1;
1851 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1852 }
1853 } else {
1854 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1855 if (Tmp1 != Node->getOperand(0) ||
1856 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001857 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001858 Ops[0] = Tmp1;
1859 Ops.back() = Tmp2;
1860 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1861 }
1862 }
1863 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1864 // This finishes up call legalization.
1865 IsLegalizingCall = false;
1866
1867 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001868 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001869 if (Node->getNumValues() == 2)
Dan Gohman8181bd12008-07-27 21:46:04 +00001870 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001871 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001872 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands92c43912008-06-06 12:08:01 +00001873 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001874 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1875 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1876 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1877 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1878
1879 Tmp1 = Result.getValue(0);
1880 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001881 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001882 default: assert(0 && "This action is not supported yet!");
1883 case TargetLowering::Expand: {
1884 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1885 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1886 " not tell us which reg is the stack pointer!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001887 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001888
1889 // Chain the dynamic stack allocation so that it doesn't modify the stack
1890 // pointer when other instructions are using the stack.
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001891 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling22f8deb2007-11-13 00:44:25 +00001892
Dan Gohman8181bd12008-07-27 21:46:04 +00001893 SDValue Size = Tmp2.getOperand(1);
Dale Johannesen564036c2009-02-04 00:13:36 +00001894 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
Evan Chenga448bc42007-08-16 23:50:06 +00001895 Chain = SP.getValue(1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001896 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Chenga448bc42007-08-16 23:50:06 +00001897 unsigned StackAlign =
1898 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1899 if (Align > StackAlign)
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001900 SP = DAG.getNode(ISD::AND, dl, VT, SP,
Evan Cheng51ce0382007-08-17 18:02:22 +00001901 DAG.getConstant(-(uint64_t)Align, VT));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001902 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesen564036c2009-02-04 00:13:36 +00001903 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
Bill Wendling22f8deb2007-11-13 00:44:25 +00001904
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001905 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001906 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling22f8deb2007-11-13 00:44:25 +00001907
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001908 Tmp1 = LegalizeOp(Tmp1);
1909 Tmp2 = LegalizeOp(Tmp2);
1910 break;
1911 }
1912 case TargetLowering::Custom:
1913 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001914 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001915 Tmp1 = LegalizeOp(Tmp3);
1916 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1917 }
1918 break;
1919 case TargetLowering::Legal:
1920 break;
1921 }
1922 // Since this op produce two values, make sure to remember that we
1923 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001924 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1925 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00001926 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001927 }
1928 case ISD::INLINEASM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001929 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001930 bool Changed = false;
1931 // Legalize all of the operands of the inline asm, in case they are nodes
1932 // that need to be expanded or something. Note we skip the asm string and
1933 // all of the TargetConstant flags.
Dan Gohman8181bd12008-07-27 21:46:04 +00001934 SDValue Op = LegalizeOp(Ops[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001935 Changed = Op != Ops[0];
1936 Ops[0] = Op;
1937
1938 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1939 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001940 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001941 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001942 SDValue Op = LegalizeOp(Ops[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001943 if (Op != Ops[i]) {
1944 Changed = true;
1945 Ops[i] = Op;
1946 }
1947 }
1948 }
1949
1950 if (HasInFlag) {
1951 Op = LegalizeOp(Ops.back());
1952 Changed |= Op != Ops.back();
1953 Ops.back() = Op;
1954 }
1955
1956 if (Changed)
1957 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1958
1959 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman8181bd12008-07-27 21:46:04 +00001960 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1961 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif46bf5472008-08-26 22:36:50 +00001962 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001963 }
1964 case ISD::BR:
1965 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1966 // Ensure that libcalls are emitted before a branch.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001967 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001968 Tmp1 = LegalizeOp(Tmp1);
1969 LastCALLSEQ_END = DAG.getEntryNode();
1970
1971 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1972 break;
1973 case ISD::BRIND:
1974 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1975 // Ensure that libcalls are emitted before a branch.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001976 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001977 Tmp1 = LegalizeOp(Tmp1);
1978 LastCALLSEQ_END = DAG.getEntryNode();
1979
1980 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1981 default: assert(0 && "Indirect target must be legal type (pointer)!");
1982 case Legal:
1983 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1984 break;
1985 }
1986 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1987 break;
1988 case ISD::BR_JT:
1989 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1990 // Ensure that libcalls are emitted before a branch.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00001991 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001992 Tmp1 = LegalizeOp(Tmp1);
1993 LastCALLSEQ_END = DAG.getEntryNode();
1994
1995 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1996 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1997
1998 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1999 default: assert(0 && "This action is not supported yet!");
2000 case TargetLowering::Legal: break;
2001 case TargetLowering::Custom:
2002 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002003 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002004 break;
2005 case TargetLowering::Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002006 SDValue Chain = Result.getOperand(0);
2007 SDValue Table = Result.getOperand(1);
2008 SDValue Index = Result.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002009
Duncan Sands92c43912008-06-06 12:08:01 +00002010 MVT PTy = TLI.getPointerTy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002011 MachineFunction &MF = DAG.getMachineFunction();
2012 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002013 Index= DAG.getNode(ISD::MUL, dl, PTy,
2014 Index, DAG.getConstant(EntrySize, PTy));
2015 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002016
Duncan Sands12ddc802008-12-12 08:13:38 +00002017 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002018 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Duncan Sands12ddc802008-12-12 08:13:38 +00002019 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Cheng6fb06762007-11-09 01:32:10 +00002020 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002021 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2022 // For PIC, the sequence is:
2023 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00002024 // RelocBase can be JumpTable, GOT or some sort of global base.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002025 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
Evan Cheng6fb06762007-11-09 01:32:10 +00002026 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002027 }
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002028 Result = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002029 }
2030 }
2031 break;
2032 case ISD::BRCOND:
2033 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2034 // Ensure that libcalls are emitted before a return.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002035 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002036 Tmp1 = LegalizeOp(Tmp1);
2037 LastCALLSEQ_END = DAG.getEntryNode();
2038
2039 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2040 case Expand: assert(0 && "It's impossible to expand bools");
2041 case Legal:
2042 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2043 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002044 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002045 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
2046
2047 // The top bits of the promoted condition are not necessarily zero, ensure
2048 // that the value is properly zero extended.
Dan Gohman07961cd2008-02-25 21:11:39 +00002049 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002050 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman07961cd2008-02-25 21:11:39 +00002051 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002052 Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, MVT::i1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002053 break;
2054 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002055 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002056
2057 // Basic block destination (Op#2) is always legal.
2058 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2059
2060 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
2061 default: assert(0 && "This action is not supported yet!");
2062 case TargetLowering::Legal: break;
2063 case TargetLowering::Custom:
2064 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002065 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002066 break;
2067 case TargetLowering::Expand:
2068 // Expand brcond's setcc into its constituent parts and create a BR_CC
2069 // Node.
2070 if (Tmp2.getOpcode() == ISD::SETCC) {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002071 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
2072 Tmp1, Tmp2.getOperand(2),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002073 Tmp2.getOperand(0), Tmp2.getOperand(1),
2074 Node->getOperand(2));
2075 } else {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002076 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002077 DAG.getCondCode(ISD::SETNE), Tmp2,
2078 DAG.getConstant(0, Tmp2.getValueType()),
2079 Node->getOperand(2));
2080 }
2081 break;
2082 }
2083 break;
2084 case ISD::BR_CC:
2085 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2086 // Ensure that libcalls are emitted before a branch.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002087 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002088 Tmp1 = LegalizeOp(Tmp1);
2089 Tmp2 = Node->getOperand(2); // LHS
2090 Tmp3 = Node->getOperand(3); // RHS
2091 Tmp4 = Node->getOperand(1); // CC
2092
Dale Johannesen352c47e2009-02-02 20:41:04 +00002093 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()),
2094 Tmp2, Tmp3, Tmp4, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002095 LastCALLSEQ_END = DAG.getEntryNode();
2096
Evan Cheng71343822008-10-15 02:05:31 +00002097 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002098 // the LHS is a legal SETCC itself. In this case, we need to compare
2099 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00002100 if (Tmp3.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002101 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2102 Tmp4 = DAG.getCondCode(ISD::SETNE);
2103 }
2104
2105 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2106 Node->getOperand(4));
2107
2108 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2109 default: assert(0 && "Unexpected action for BR_CC!");
2110 case TargetLowering::Legal: break;
2111 case TargetLowering::Custom:
2112 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002113 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002114 break;
2115 }
2116 break;
2117 case ISD::LOAD: {
2118 LoadSDNode *LD = cast<LoadSDNode>(Node);
2119 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2120 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
2121
2122 ISD::LoadExtType ExtType = LD->getExtensionType();
2123 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands92c43912008-06-06 12:08:01 +00002124 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002125 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2126 Tmp3 = Result.getValue(0);
2127 Tmp4 = Result.getValue(1);
2128
2129 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2130 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002131 case TargetLowering::Legal:
2132 // If this is an unaligned load and the target doesn't support it,
2133 // expand it.
2134 if (!TLI.allowsUnalignedMemoryAccesses()) {
2135 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002136 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002137 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002138 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002139 TLI);
2140 Tmp3 = Result.getOperand(0);
2141 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00002142 Tmp3 = LegalizeOp(Tmp3);
2143 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002144 }
2145 }
2146 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002147 case TargetLowering::Custom:
2148 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002149 if (Tmp1.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002150 Tmp3 = LegalizeOp(Tmp1);
2151 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2152 }
2153 break;
2154 case TargetLowering::Promote: {
2155 // Only promote a load of vector type to another.
Duncan Sands92c43912008-06-06 12:08:01 +00002156 assert(VT.isVector() && "Cannot promote this load!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002157 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002158 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002159
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002160 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002161 LD->getSrcValueOffset(),
2162 LD->isVolatile(), LD->getAlignment());
Dale Johannesen24dd9a52009-02-07 00:55:49 +00002163 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002164 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2165 break;
2166 }
2167 }
2168 // Since loads produce two values, make sure to remember that we
2169 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002170 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2171 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif46bf5472008-08-26 22:36:50 +00002172 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002173 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00002174 MVT SrcVT = LD->getMemoryVT();
2175 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sands082524c2008-01-23 20:39:46 +00002176 int SVOffset = LD->getSrcValueOffset();
2177 unsigned Alignment = LD->getAlignment();
2178 bool isVolatile = LD->isVolatile();
2179
Duncan Sands92c43912008-06-06 12:08:01 +00002180 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002181 // Some targets pretend to have an i1 loading operation, and actually
2182 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2183 // bits are guaranteed to be zero; it helps the optimizers understand
2184 // that these bits are zero. It is also useful for EXTLOAD, since it
2185 // tells the optimizers that those bits are undefined. It would be
2186 // nice to have an effective generic way of getting these benefits...
2187 // Until such a way is found, don't insist on promoting i1 here.
2188 (SrcVT != MVT::i1 ||
Evan Cheng08c171a2008-10-14 21:26:46 +00002189 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002190 // Promote to a byte-sized load if not loading an integral number of
2191 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands92c43912008-06-06 12:08:01 +00002192 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2193 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002194 SDValue Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002195
2196 // The extra bits are guaranteed to be zero, since we stored them that
2197 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2198
2199 ISD::LoadExtType NewExtType =
2200 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2201
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002202 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Duncan Sands082524c2008-01-23 20:39:46 +00002203 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2204 NVT, isVolatile, Alignment);
2205
2206 Ch = Result.getValue(1); // The chain.
2207
2208 if (ExtType == ISD::SEXTLOAD)
2209 // Having the top bits zero doesn't help when sign extending.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002210 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
2211 Result.getValueType(),
Duncan Sands082524c2008-01-23 20:39:46 +00002212 Result, DAG.getValueType(SrcVT));
2213 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2214 // All the top bits are guaranteed to be zero - inform the optimizers.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002215 Result = DAG.getNode(ISD::AssertZext, dl,
2216 Result.getValueType(), Result,
Duncan Sands082524c2008-01-23 20:39:46 +00002217 DAG.getValueType(SrcVT));
2218
2219 Tmp1 = LegalizeOp(Result);
2220 Tmp2 = LegalizeOp(Ch);
2221 } else if (SrcWidth & (SrcWidth - 1)) {
2222 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands92c43912008-06-06 12:08:01 +00002223 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002224 "Unsupported extload!");
2225 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2226 assert(RoundWidth < SrcWidth);
2227 unsigned ExtraWidth = SrcWidth - RoundWidth;
2228 assert(ExtraWidth < RoundWidth);
2229 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2230 "Load size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002231 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2232 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002233 SDValue Lo, Hi, Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002234 unsigned IncrementSize;
2235
2236 if (TLI.isLittleEndian()) {
2237 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2238 // Load the bottom RoundWidth bits.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002239 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
2240 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sands082524c2008-01-23 20:39:46 +00002241 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2242 Alignment);
2243
2244 // Load the remaining ExtraWidth bits.
2245 IncrementSize = RoundWidth / 8;
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002246 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands082524c2008-01-23 20:39:46 +00002247 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002248 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sands082524c2008-01-23 20:39:46 +00002249 LD->getSrcValue(), SVOffset + IncrementSize,
2250 ExtraVT, isVolatile,
2251 MinAlign(Alignment, IncrementSize));
2252
2253 // Build a factor node to remember that this load is independent of the
2254 // other one.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002255 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sands082524c2008-01-23 20:39:46 +00002256 Hi.getValue(1));
2257
2258 // Move the top bits to the right place.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002259 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sands082524c2008-01-23 20:39:46 +00002260 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2261
2262 // Join the hi and lo parts.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002263 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002264 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00002265 // Big endian - avoid unaligned loads.
2266 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2267 // Load the top RoundWidth bits.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002268 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sands082524c2008-01-23 20:39:46 +00002269 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2270 Alignment);
2271
2272 // Load the remaining ExtraWidth bits.
2273 IncrementSize = RoundWidth / 8;
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002274 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands082524c2008-01-23 20:39:46 +00002275 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002276 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
2277 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sands082524c2008-01-23 20:39:46 +00002278 LD->getSrcValue(), SVOffset + IncrementSize,
2279 ExtraVT, isVolatile,
2280 MinAlign(Alignment, IncrementSize));
2281
2282 // Build a factor node to remember that this load is independent of the
2283 // other one.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002284 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sands082524c2008-01-23 20:39:46 +00002285 Hi.getValue(1));
2286
2287 // Move the top bits to the right place.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002288 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sands082524c2008-01-23 20:39:46 +00002289 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2290
2291 // Join the hi and lo parts.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002292 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sands082524c2008-01-23 20:39:46 +00002293 }
2294
2295 Tmp1 = LegalizeOp(Result);
2296 Tmp2 = LegalizeOp(Ch);
2297 } else {
Evan Cheng08c171a2008-10-14 21:26:46 +00002298 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sands082524c2008-01-23 20:39:46 +00002299 default: assert(0 && "This action is not supported yet!");
2300 case TargetLowering::Custom:
2301 isCustom = true;
2302 // FALLTHROUGH
2303 case TargetLowering::Legal:
2304 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2305 Tmp1 = Result.getValue(0);
2306 Tmp2 = Result.getValue(1);
2307
2308 if (isCustom) {
2309 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002310 if (Tmp3.getNode()) {
Duncan Sands082524c2008-01-23 20:39:46 +00002311 Tmp1 = LegalizeOp(Tmp3);
2312 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2313 }
2314 } else {
2315 // If this is an unaligned load and the target doesn't support it,
2316 // expand it.
2317 if (!TLI.allowsUnalignedMemoryAccesses()) {
2318 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002319 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sands082524c2008-01-23 20:39:46 +00002320 if (LD->getAlignment() < ABIAlignment){
Gabor Greif1c80d112008-08-28 21:40:38 +00002321 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sands082524c2008-01-23 20:39:46 +00002322 TLI);
2323 Tmp1 = Result.getOperand(0);
2324 Tmp2 = Result.getOperand(1);
2325 Tmp1 = LegalizeOp(Tmp1);
2326 Tmp2 = LegalizeOp(Tmp2);
2327 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002328 }
2329 }
Duncan Sands082524c2008-01-23 20:39:46 +00002330 break;
2331 case TargetLowering::Expand:
2332 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2333 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002334 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sands082524c2008-01-23 20:39:46 +00002335 LD->getSrcValueOffset(),
2336 LD->isVolatile(), LD->getAlignment());
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002337 Result = DAG.getNode(ISD::FP_EXTEND, dl,
2338 Node->getValueType(0), Load);
Duncan Sands082524c2008-01-23 20:39:46 +00002339 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2340 Tmp2 = LegalizeOp(Load.getValue(1));
2341 break;
2342 }
2343 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2344 // Turn the unsupported load into an EXTLOAD followed by an explicit
2345 // zero/sign extend inreg.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002346 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Duncan Sands082524c2008-01-23 20:39:46 +00002347 Tmp1, Tmp2, LD->getSrcValue(),
2348 LD->getSrcValueOffset(), SrcVT,
2349 LD->isVolatile(), LD->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00002350 SDValue ValRes;
Duncan Sands082524c2008-01-23 20:39:46 +00002351 if (ExtType == ISD::SEXTLOAD)
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002352 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
2353 Result.getValueType(),
Duncan Sands082524c2008-01-23 20:39:46 +00002354 Result, DAG.getValueType(SrcVT));
2355 else
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002356 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
Duncan Sands082524c2008-01-23 20:39:46 +00002357 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2358 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002359 break;
2360 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002361 }
Duncan Sands082524c2008-01-23 20:39:46 +00002362
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002363 // Since loads produce two values, make sure to remember that we legalized
2364 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002365 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2366 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002367 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002368 }
2369 }
2370 case ISD::EXTRACT_ELEMENT: {
Duncan Sands92c43912008-06-06 12:08:01 +00002371 MVT OpTy = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002372 switch (getTypeAction(OpTy)) {
2373 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2374 case Legal:
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002375 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002376 // 1 -> Hi
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002377 Result = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
Duncan Sands92c43912008-06-06 12:08:01 +00002378 DAG.getConstant(OpTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002379 TLI.getShiftAmountTy()));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002380 Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002381 } else {
2382 // 0 -> Lo
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002383 Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002384 Node->getOperand(0));
2385 }
2386 break;
2387 case Expand:
2388 // Get both the low and high parts.
2389 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002390 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002391 Result = Tmp2; // 1 -> Hi
2392 else
2393 Result = Tmp1; // 0 -> Lo
2394 break;
2395 }
2396 break;
2397 }
2398
2399 case ISD::CopyToReg:
2400 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2401
2402 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2403 "Register type must be legal!");
2404 // Legalize the incoming value (must be a legal type).
2405 Tmp2 = LegalizeOp(Node->getOperand(2));
2406 if (Node->getNumValues() == 1) {
2407 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2408 } else {
2409 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2410 if (Node->getNumOperands() == 4) {
2411 Tmp3 = LegalizeOp(Node->getOperand(3));
2412 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2413 Tmp3);
2414 } else {
2415 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2416 }
2417
2418 // Since this produces two values, make sure to remember that we legalized
2419 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002420 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2421 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002422 return Result;
2423 }
2424 break;
2425
2426 case ISD::RET:
2427 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2428
2429 // Ensure that libcalls are emitted before a return.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002430 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002431 Tmp1 = LegalizeOp(Tmp1);
2432 LastCALLSEQ_END = DAG.getEntryNode();
2433
2434 switch (Node->getNumOperands()) {
2435 case 3: // ret val
2436 Tmp2 = Node->getOperand(1);
2437 Tmp3 = Node->getOperand(2); // Signness
2438 switch (getTypeAction(Tmp2.getValueType())) {
2439 case Legal:
2440 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2441 break;
2442 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00002443 if (!Tmp2.getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002444 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002445 ExpandOp(Tmp2, Lo, Hi);
2446
2447 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002448 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002449 std::swap(Lo, Hi);
2450
Gabor Greif1c80d112008-08-28 21:40:38 +00002451 if (Hi.getNode())
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002452 Result = DAG.getNode(ISD::RET, dl, MVT::Other,
2453 Tmp1, Lo, Tmp3, Hi,Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002454 else
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002455 Result = DAG.getNode(ISD::RET, dl, MVT::Other, Tmp1, Lo, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002456 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);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002487 Result = DAG.getNode(ISD::RET, dl, MVT::Other,
2488 Tmp1, Lo, Tmp3, Hi,Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002489 Result = LegalizeOp(Result);
2490 }
2491 }
2492 break;
2493 case Promote:
2494 Tmp2 = PromoteOp(Node->getOperand(1));
2495 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2496 Result = LegalizeOp(Result);
2497 break;
2498 }
2499 break;
2500 case 1: // ret void
2501 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2502 break;
2503 default: { // ret <values>
Dan Gohman8181bd12008-07-27 21:46:04 +00002504 SmallVector<SDValue, 8> NewValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002505 NewValues.push_back(Tmp1);
2506 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2507 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2508 case Legal:
2509 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2510 NewValues.push_back(Node->getOperand(i+1));
2511 break;
2512 case Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002513 SDValue Lo, Hi;
Duncan Sands92c43912008-06-06 12:08:01 +00002514 assert(!Node->getOperand(i).getValueType().isExtended() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002515 "FIXME: TODO: implement returning non-legal vector types!");
2516 ExpandOp(Node->getOperand(i), Lo, Hi);
2517 NewValues.push_back(Lo);
2518 NewValues.push_back(Node->getOperand(i+1));
Gabor Greif1c80d112008-08-28 21:40:38 +00002519 if (Hi.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002520 NewValues.push_back(Hi);
2521 NewValues.push_back(Node->getOperand(i+1));
2522 }
2523 break;
2524 }
2525 case Promote:
2526 assert(0 && "Can't promote multiple return value yet!");
2527 }
2528
2529 if (NewValues.size() == Node->getNumOperands())
2530 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2531 else
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002532 Result = DAG.getNode(ISD::RET, dl, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002533 &NewValues[0], NewValues.size());
2534 break;
2535 }
2536 }
2537
2538 if (Result.getOpcode() == ISD::RET) {
2539 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2540 default: assert(0 && "This action is not supported yet!");
2541 case TargetLowering::Legal: break;
2542 case TargetLowering::Custom:
2543 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002544 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002545 break;
2546 }
2547 }
2548 break;
2549 case ISD::STORE: {
2550 StoreSDNode *ST = cast<StoreSDNode>(Node);
2551 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2552 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2553 int SVOffset = ST->getSrcValueOffset();
2554 unsigned Alignment = ST->getAlignment();
2555 bool isVolatile = ST->isVolatile();
2556
2557 if (!ST->isTruncatingStore()) {
2558 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2559 // FIXME: We shouldn't do this for TargetConstantFP's.
2560 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2561 // to phase ordering between legalized code and the dag combiner. This
2562 // probably means that we need to integrate dag combiner and legalizer
2563 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002564 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002565 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002566 if (CFP->getValueType(0) == MVT::f32 &&
2567 getTypeAction(MVT::i32) == Legal) {
Dan Gohman39509762008-03-11 00:11:06 +00002568 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002569 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen1616e902007-09-11 18:32:33 +00002570 MVT::i32);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002571 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dale Johannesen2fc20782007-09-14 22:26:36 +00002572 SVOffset, isVolatile, Alignment);
2573 break;
2574 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002575 // If this target supports 64-bit registers, do a single 64-bit store.
2576 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002577 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman39509762008-03-11 00:11:06 +00002578 zextOrTrunc(64), MVT::i64);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002579 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Chris Lattner19f229a2007-10-15 05:46:06 +00002580 SVOffset, isVolatile, Alignment);
2581 break;
Duncan Sands2418bec2008-06-13 19:07:40 +00002582 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002583 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2584 // stores. If the target supports neither 32- nor 64-bits, this
2585 // xform is certainly not worth it.
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00002586 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman8181bd12008-07-27 21:46:04 +00002587 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2588 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002589 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002590
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002591 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
Chris Lattner19f229a2007-10-15 05:46:06 +00002592 SVOffset, isVolatile, Alignment);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002593 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002594 DAG.getIntPtrConstant(4));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002595 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002596 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002597
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002598 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002599 break;
2600 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002601 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002602 }
2603
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002604 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002605 case Legal: {
2606 Tmp3 = LegalizeOp(ST->getValue());
2607 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2608 ST->getOffset());
2609
Duncan Sands92c43912008-06-06 12:08:01 +00002610 MVT VT = Tmp3.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002611 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2612 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002613 case TargetLowering::Legal:
2614 // If this is an unaligned store and the target doesn't support it,
2615 // expand it.
2616 if (!TLI.allowsUnalignedMemoryAccesses()) {
2617 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002618 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002619 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002620 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002621 TLI);
2622 }
2623 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002624 case TargetLowering::Custom:
2625 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002626 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002627 break;
2628 case TargetLowering::Promote:
Duncan Sands92c43912008-06-06 12:08:01 +00002629 assert(VT.isVector() && "Unknown legal promote case!");
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002630 Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002631 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002632 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002633 ST->getSrcValue(), SVOffset, isVolatile,
2634 Alignment);
2635 break;
2636 }
2637 break;
2638 }
2639 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002640 if (!ST->getMemoryVT().isVector()) {
2641 // Truncate the value and store the result.
2642 Tmp3 = PromoteOp(ST->getValue());
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002643 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Mon P Wang1448aad2008-10-30 08:01:45 +00002644 SVOffset, ST->getMemoryVT(),
2645 isVolatile, Alignment);
2646 break;
2647 }
2648 // Fall thru to expand for vector
2649 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002650 unsigned IncrementSize = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002651 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002652
2653 // If this is a vector type, then we have to calculate the increment as
2654 // the product of the element size in bytes, and the number of elements
2655 // in the high half of the vector.
Duncan Sands92c43912008-06-06 12:08:01 +00002656 if (ST->getValue().getValueType().isVector()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002657 SDNode *InVal = ST->getValue().getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00002658 int InIx = ST->getValue().getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00002659 MVT InVT = InVal->getValueType(InIx);
2660 unsigned NumElems = InVT.getVectorNumElements();
2661 MVT EVT = InVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002662
2663 // Figure out if there is a simple type corresponding to this Vector
2664 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002665 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002666 if (TLI.isTypeLegal(TVT)) {
2667 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002668 Tmp3 = LegalizeOp(ST->getValue());
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002669 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002670 SVOffset, isVolatile, Alignment);
2671 Result = LegalizeOp(Result);
2672 break;
2673 } else if (NumElems == 1) {
2674 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002675 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002676 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002677 SVOffset, isVolatile, Alignment);
2678 // The scalarized value type may not be legal, e.g. it might require
2679 // promotion or expansion. Relegalize the scalar store.
2680 Result = LegalizeOp(Result);
2681 break;
2682 } else {
Mon P Wang1448aad2008-10-30 08:01:45 +00002683 // Check if we have widen this node with another value
2684 std::map<SDValue, SDValue>::iterator I =
2685 WidenNodes.find(ST->getValue());
2686 if (I != WidenNodes.end()) {
2687 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2688 break;
2689 }
2690 else {
2691 SplitVectorOp(ST->getValue(), Lo, Hi);
2692 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2693 EVT.getSizeInBits()/8;
2694 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002695 }
2696 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002697 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greif1c80d112008-08-28 21:40:38 +00002698 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002699
Richard Pennington73ae9e42008-09-25 16:15:10 +00002700 if (Hi.getNode() && TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002701 std::swap(Lo, Hi);
2702 }
2703
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002704 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002705 SVOffset, isVolatile, Alignment);
2706
Gabor Greif1c80d112008-08-28 21:40:38 +00002707 if (Hi.getNode() == NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002708 // Must be int <-> float one-to-one expansion.
2709 Result = Lo;
2710 break;
2711 }
2712
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002713 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002714 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002715 assert(isTypeLegal(Tmp2.getValueType()) &&
2716 "Pointers must be legal!");
2717 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002718 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002719 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002720 SVOffset, isVolatile, Alignment);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002721 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002722 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00002723 } // case Expand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002724 }
2725 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002726 switch (getTypeAction(ST->getValue().getValueType())) {
2727 case Legal:
2728 Tmp3 = LegalizeOp(ST->getValue());
2729 break;
2730 case Promote:
Mon P Wang1448aad2008-10-30 08:01:45 +00002731 if (!ST->getValue().getValueType().isVector()) {
2732 // We can promote the value, the truncstore will still take care of it.
2733 Tmp3 = PromoteOp(ST->getValue());
2734 break;
2735 }
2736 // Vector case falls through to expand
Chris Lattner3bc08502008-01-17 19:59:44 +00002737 case Expand:
2738 // Just store the low part. This may become a non-trunc store, so make
2739 // sure to use getTruncStore, not UpdateNodeOperands below.
2740 ExpandOp(ST->getValue(), Tmp3, Tmp4);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002741 return DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Chris Lattner3bc08502008-01-17 19:59:44 +00002742 SVOffset, MVT::i8, isVolatile, Alignment);
2743 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002744
Duncan Sands92c43912008-06-06 12:08:01 +00002745 MVT StVT = ST->getMemoryVT();
2746 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands40676662008-01-22 07:17:34 +00002747
Duncan Sands92c43912008-06-06 12:08:01 +00002748 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands40676662008-01-22 07:17:34 +00002749 // Promote to a byte-sized store with upper bits zero if not
2750 // storing an integral number of bytes. For example, promote
2751 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands92c43912008-06-06 12:08:01 +00002752 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002753 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
2754 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands40676662008-01-22 07:17:34 +00002755 SVOffset, NVT, isVolatile, Alignment);
2756 } else if (StWidth & (StWidth - 1)) {
2757 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands92c43912008-06-06 12:08:01 +00002758 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands40676662008-01-22 07:17:34 +00002759 "Unsupported truncstore!");
2760 unsigned RoundWidth = 1 << Log2_32(StWidth);
2761 assert(RoundWidth < StWidth);
2762 unsigned ExtraWidth = StWidth - RoundWidth;
2763 assert(ExtraWidth < RoundWidth);
2764 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2765 "Store size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002766 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2767 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002768 SDValue Lo, Hi;
Duncan Sands40676662008-01-22 07:17:34 +00002769 unsigned IncrementSize;
2770
2771 if (TLI.isLittleEndian()) {
2772 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2773 // Store the bottom RoundWidth bits.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002774 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands40676662008-01-22 07:17:34 +00002775 SVOffset, RoundVT,
2776 isVolatile, Alignment);
2777
2778 // Store the remaining ExtraWidth bits.
2779 IncrementSize = RoundWidth / 8;
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002780 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands40676662008-01-22 07:17:34 +00002781 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002782 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands40676662008-01-22 07:17:34 +00002783 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002784 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
Duncan Sands40676662008-01-22 07:17:34 +00002785 SVOffset + IncrementSize, ExtraVT, isVolatile,
2786 MinAlign(Alignment, IncrementSize));
2787 } else {
2788 // Big endian - avoid unaligned stores.
2789 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2790 // Store the top RoundWidth bits.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002791 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands40676662008-01-22 07:17:34 +00002792 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002793 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
2794 SVOffset, RoundVT, isVolatile, Alignment);
Duncan Sands40676662008-01-22 07:17:34 +00002795
2796 // Store the remaining ExtraWidth bits.
2797 IncrementSize = RoundWidth / 8;
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002798 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands40676662008-01-22 07:17:34 +00002799 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002800 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands40676662008-01-22 07:17:34 +00002801 SVOffset + IncrementSize, ExtraVT, isVolatile,
2802 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002803 }
Duncan Sands40676662008-01-22 07:17:34 +00002804
2805 // The order of the stores doesn't matter.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002806 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands40676662008-01-22 07:17:34 +00002807 } else {
2808 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2809 Tmp2 != ST->getBasePtr())
2810 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2811 ST->getOffset());
2812
2813 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2814 default: assert(0 && "This action is not supported yet!");
2815 case TargetLowering::Legal:
2816 // If this is an unaligned store and the target doesn't support it,
2817 // expand it.
2818 if (!TLI.allowsUnalignedMemoryAccesses()) {
2819 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002820 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands40676662008-01-22 07:17:34 +00002821 if (ST->getAlignment() < ABIAlignment)
Gabor Greif1c80d112008-08-28 21:40:38 +00002822 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands40676662008-01-22 07:17:34 +00002823 TLI);
2824 }
2825 break;
2826 case TargetLowering::Custom:
2827 Result = TLI.LowerOperation(Result, DAG);
2828 break;
2829 case Expand:
2830 // TRUNCSTORE:i16 i32 -> STORE i16
2831 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002832 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
2833 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
2834 SVOffset, isVolatile, Alignment);
Duncan Sands40676662008-01-22 07:17:34 +00002835 break;
2836 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002837 }
2838 }
2839 break;
2840 }
2841 case ISD::PCMARKER:
2842 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2843 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2844 break;
2845 case ISD::STACKSAVE:
2846 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2847 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2848 Tmp1 = Result.getValue(0);
2849 Tmp2 = Result.getValue(1);
2850
2851 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2852 default: assert(0 && "This action is not supported yet!");
2853 case TargetLowering::Legal: break;
2854 case TargetLowering::Custom:
2855 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002856 if (Tmp3.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002857 Tmp1 = LegalizeOp(Tmp3);
2858 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2859 }
2860 break;
2861 case TargetLowering::Expand:
2862 // Expand to CopyFromReg if the target set
2863 // StackPointerRegisterToSaveRestore.
2864 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Dale Johannesen564036c2009-02-04 00:13:36 +00002865 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), dl, SP,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002866 Node->getValueType(0));
2867 Tmp2 = Tmp1.getValue(1);
2868 } else {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002869 Tmp1 = DAG.getUNDEF(Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002870 Tmp2 = Node->getOperand(0);
2871 }
2872 break;
2873 }
2874
2875 // Since stacksave produce two values, make sure to remember that we
2876 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002877 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2878 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00002879 return Op.getResNo() ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002880
2881 case ISD::STACKRESTORE:
2882 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2883 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2884 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2885
2886 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2887 default: assert(0 && "This action is not supported yet!");
2888 case TargetLowering::Legal: break;
2889 case TargetLowering::Custom:
2890 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002891 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002892 break;
2893 case TargetLowering::Expand:
2894 // Expand to CopyToReg if the target set
2895 // StackPointerRegisterToSaveRestore.
2896 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Dale Johannesen564036c2009-02-04 00:13:36 +00002897 Result = DAG.getCopyToReg(Tmp1, dl, SP, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002898 } else {
2899 Result = Tmp1;
2900 }
2901 break;
2902 }
2903 break;
2904
2905 case ISD::READCYCLECOUNTER:
2906 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2907 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2908 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2909 Node->getValueType(0))) {
2910 default: assert(0 && "This action is not supported yet!");
2911 case TargetLowering::Legal:
2912 Tmp1 = Result.getValue(0);
2913 Tmp2 = Result.getValue(1);
2914 break;
2915 case TargetLowering::Custom:
2916 Result = TLI.LowerOperation(Result, DAG);
2917 Tmp1 = LegalizeOp(Result.getValue(0));
2918 Tmp2 = LegalizeOp(Result.getValue(1));
2919 break;
2920 }
2921
2922 // Since rdcc produce two values, make sure to remember that we legalized
2923 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002924 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2925 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002926 return Result;
2927
2928 case ISD::SELECT:
2929 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2930 case Expand: assert(0 && "It's impossible to expand bools");
2931 case Legal:
2932 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2933 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002934 case Promote: {
Mon P Wang1448aad2008-10-30 08:01:45 +00002935 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002936 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2937 // Make sure the condition is either zero or one.
Dan Gohman07961cd2008-02-25 21:11:39 +00002938 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002939 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman07961cd2008-02-25 21:11:39 +00002940 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002941 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, MVT::i1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002942 break;
2943 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002944 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002945 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2946 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2947
2948 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2949
2950 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2951 default: assert(0 && "This action is not supported yet!");
2952 case TargetLowering::Legal: break;
2953 case TargetLowering::Custom: {
2954 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00002955 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002956 break;
2957 }
2958 case TargetLowering::Expand:
2959 if (Tmp1.getOpcode() == ISD::SETCC) {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002960 Result = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002961 Tmp2, Tmp3,
2962 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2963 } else {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002964 Result = DAG.getSelectCC(dl, Tmp1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002965 DAG.getConstant(0, Tmp1.getValueType()),
2966 Tmp2, Tmp3, ISD::SETNE);
2967 }
2968 break;
2969 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00002970 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002971 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2972 unsigned ExtOp, TruncOp;
Duncan Sands92c43912008-06-06 12:08:01 +00002973 if (Tmp2.getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002974 ExtOp = ISD::BIT_CONVERT;
2975 TruncOp = ISD::BIT_CONVERT;
Duncan Sands92c43912008-06-06 12:08:01 +00002976 } else if (Tmp2.getValueType().isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002977 ExtOp = ISD::ANY_EXTEND;
2978 TruncOp = ISD::TRUNCATE;
2979 } else {
2980 ExtOp = ISD::FP_EXTEND;
2981 TruncOp = ISD::FP_ROUND;
2982 }
2983 // Promote each of the values to the new type.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002984 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Tmp2);
2985 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002986 // Perform the larger operation, then round down.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002987 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00002988 if (TruncOp != ISD::FP_ROUND)
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002989 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result);
Chris Lattner5872a362008-01-17 07:00:52 +00002990 else
Dale Johannesenbbf56a22009-02-02 23:46:53 +00002991 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result,
Chris Lattner5872a362008-01-17 07:00:52 +00002992 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002993 break;
2994 }
2995 }
2996 break;
2997 case ISD::SELECT_CC: {
2998 Tmp1 = Node->getOperand(0); // LHS
2999 Tmp2 = Node->getOperand(1); // RHS
3000 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
3001 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman8181bd12008-07-27 21:46:04 +00003002 SDValue CC = Node->getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003003
Dale Johannesen352c47e2009-02-02 20:41:04 +00003004 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
3005 Tmp1, Tmp2, CC, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003006
Evan Cheng71343822008-10-15 02:05:31 +00003007 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003008 // the LHS is a legal SETCC itself. In this case, we need to compare
3009 // the result against zero to select between true and false values.
Gabor Greif1c80d112008-08-28 21:40:38 +00003010 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003011 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3012 CC = DAG.getCondCode(ISD::SETNE);
3013 }
3014 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
3015
3016 // Everything is legal, see if we should expand this op or something.
3017 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
3018 default: assert(0 && "This action is not supported yet!");
3019 case TargetLowering::Legal: break;
3020 case TargetLowering::Custom:
3021 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003022 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003023 break;
3024 }
3025 break;
3026 }
3027 case ISD::SETCC:
3028 Tmp1 = Node->getOperand(0);
3029 Tmp2 = Node->getOperand(1);
3030 Tmp3 = Node->getOperand(2);
Dale Johannesen352c47e2009-02-02 20:41:04 +00003031 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003032
3033 // If we had to Expand the SetCC operands into a SELECT node, then it may
3034 // not always be possible to return a true LHS & RHS. In this case, just
3035 // return the value we legalized, returned in the LHS
Gabor Greif1c80d112008-08-28 21:40:38 +00003036 if (Tmp2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003037 Result = Tmp1;
3038 break;
3039 }
3040
3041 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
3042 default: assert(0 && "Cannot handle this action for SETCC yet!");
3043 case TargetLowering::Custom:
3044 isCustom = true;
3045 // FALLTHROUGH.
3046 case TargetLowering::Legal:
3047 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3048 if (isCustom) {
3049 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003050 if (Tmp4.getNode()) Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003051 }
3052 break;
3053 case TargetLowering::Promote: {
3054 // First step, figure out the appropriate operation to use.
3055 // Allow SETCC to not be supported for all legal data types
3056 // Mostly this targets FP
Duncan Sands92c43912008-06-06 12:08:01 +00003057 MVT NewInTy = Node->getOperand(0).getValueType();
3058 MVT OldVT = NewInTy; OldVT = OldVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003059
3060 // Scan for the appropriate larger type to use.
3061 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00003062 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003063
Duncan Sands92c43912008-06-06 12:08:01 +00003064 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065 "Fell off of the edge of the integer world");
Duncan Sands92c43912008-06-06 12:08:01 +00003066 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003067 "Fell off of the edge of the floating point world");
3068
3069 // If the target supports SETCC of this type, use it.
Dan Gohman52c51aa2009-01-28 17:46:25 +00003070 if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003071 break;
3072 }
Duncan Sands92c43912008-06-06 12:08:01 +00003073 if (NewInTy.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003074 assert(0 && "Cannot promote Legal Integer SETCC yet");
3075 else {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003076 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp1);
3077 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003078 }
3079 Tmp1 = LegalizeOp(Tmp1);
3080 Tmp2 = LegalizeOp(Tmp2);
3081 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3082 Result = LegalizeOp(Result);
3083 break;
3084 }
3085 case TargetLowering::Expand:
3086 // Expand a setcc node into a select_cc of the same condition, lhs, and
3087 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands92c43912008-06-06 12:08:01 +00003088 MVT VT = Node->getValueType(0);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003089 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003090 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3091 Tmp3);
3092 break;
3093 }
3094 break;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003095 case ISD::VSETCC: {
3096 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3097 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman8181bd12008-07-27 21:46:04 +00003098 SDValue CC = Node->getOperand(2);
Nate Begeman9a1ce152008-05-12 19:40:03 +00003099
3100 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3101
3102 // Everything is legal, see if we should expand this op or something.
3103 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3104 default: assert(0 && "This action is not supported yet!");
3105 case TargetLowering::Legal: break;
3106 case TargetLowering::Custom:
3107 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003108 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9a1ce152008-05-12 19:40:03 +00003109 break;
Mon P Wangec428ad2008-12-13 08:15:14 +00003110 case TargetLowering::Expand: {
3111 // Unroll into a nasty set of scalar code for now.
3112 MVT VT = Node->getValueType(0);
3113 unsigned NumElems = VT.getVectorNumElements();
3114 MVT EltVT = VT.getVectorElementType();
3115 MVT TmpEltVT = Tmp1.getValueType().getVectorElementType();
3116 SmallVector<SDValue, 8> Ops(NumElems);
3117 for (unsigned i = 0; i < NumElems; ++i) {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003118 SDValue In1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT,
Mon P Wangec428ad2008-12-13 08:15:14 +00003119 Tmp1, DAG.getIntPtrConstant(i));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003120 Ops[i] = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(TmpEltVT),
3121 In1, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3122 TmpEltVT, Tmp2,
3123 DAG.getIntPtrConstant(i)),
Mon P Wang77bc9cd2008-12-17 08:49:47 +00003124 CC);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003125 Ops[i] = DAG.getNode(ISD::SELECT, dl, EltVT, Ops[i], DAG.getConstant(
Duncan Sands505ba942009-02-01 18:06:53 +00003126 APInt::getAllOnesValue(EltVT.getSizeInBits()),
3127 EltVT), DAG.getConstant(0, EltVT));
Mon P Wangec428ad2008-12-13 08:15:14 +00003128 }
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003129 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElems);
Mon P Wangec428ad2008-12-13 08:15:14 +00003130 break;
3131 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00003132 }
3133 break;
3134 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003135
3136 case ISD::SHL_PARTS:
3137 case ISD::SRA_PARTS:
3138 case ISD::SRL_PARTS: {
Dan Gohman8181bd12008-07-27 21:46:04 +00003139 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003140 bool Changed = false;
Duncan Sands7d9e3612009-01-31 15:50:11 +00003141 unsigned N = Node->getNumOperands();
3142 for (unsigned i = 0; i + 1 < N; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003143 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3144 Changed |= Ops.back() != Node->getOperand(i);
3145 }
Duncan Sands7d9e3612009-01-31 15:50:11 +00003146 Ops.push_back(LegalizeOp(DAG.getShiftAmountOperand(Node->getOperand(N-1))));
3147 Changed |= Ops.back() != Node->getOperand(N-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003148 if (Changed)
3149 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
3150
3151 switch (TLI.getOperationAction(Node->getOpcode(),
3152 Node->getValueType(0))) {
3153 default: assert(0 && "This action is not supported yet!");
3154 case TargetLowering::Legal: break;
3155 case TargetLowering::Custom:
3156 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003157 if (Tmp1.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003158 SDValue Tmp2, RetVal(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003159 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
3160 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman8181bd12008-07-27 21:46:04 +00003161 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif46bf5472008-08-26 22:36:50 +00003162 if (i == Op.getResNo())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003163 RetVal = Tmp2;
3164 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003165 assert(RetVal.getNode() && "Illegal result number");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003166 return RetVal;
3167 }
3168 break;
3169 }
3170
3171 // Since these produce multiple values, make sure to remember that we
3172 // legalized all of them.
3173 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman8181bd12008-07-27 21:46:04 +00003174 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif46bf5472008-08-26 22:36:50 +00003175 return Result.getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003176 }
3177
3178 // Binary operators
3179 case ISD::ADD:
3180 case ISD::SUB:
3181 case ISD::MUL:
3182 case ISD::MULHS:
3183 case ISD::MULHU:
3184 case ISD::UDIV:
3185 case ISD::SDIV:
3186 case ISD::AND:
3187 case ISD::OR:
3188 case ISD::XOR:
3189 case ISD::SHL:
3190 case ISD::SRL:
3191 case ISD::SRA:
3192 case ISD::FADD:
3193 case ISD::FSUB:
3194 case ISD::FMUL:
3195 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00003196 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003197 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands7d9e3612009-01-31 15:50:11 +00003198 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Nate Begemanbb1ce942008-07-29 15:49:41 +00003199
3200 if ((Node->getOpcode() == ISD::SHL ||
3201 Node->getOpcode() == ISD::SRL ||
3202 Node->getOpcode() == ISD::SRA) &&
Duncan Sands7d9e3612009-01-31 15:50:11 +00003203 !Node->getValueType(0).isVector())
3204 Tmp2 = DAG.getShiftAmountOperand(Tmp2);
3205
3206 switch (getTypeAction(Tmp2.getValueType())) {
3207 case Expand: assert(0 && "Not possible");
3208 case Legal:
3209 Tmp2 = LegalizeOp(Tmp2); // Legalize the RHS.
3210 break;
3211 case Promote:
3212 Tmp2 = PromoteOp(Tmp2); // Promote the RHS.
3213 break;
Mon P Wangec428ad2008-12-13 08:15:14 +00003214 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003215
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003216 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangbff5d9c2008-11-10 04:46:22 +00003217
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003218 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3219 default: assert(0 && "BinOp legalize operation not supported");
3220 case TargetLowering::Legal: break;
3221 case TargetLowering::Custom:
3222 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003223 if (Tmp1.getNode()) {
Nate Begemanbb1ce942008-07-29 15:49:41 +00003224 Result = Tmp1;
3225 break;
Nate Begeman7569e762008-07-29 19:07:27 +00003226 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00003227 // Fall through if the custom lower can't deal with the operation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003228 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00003229 MVT VT = Op.getValueType();
Mon P Wang1448aad2008-10-30 08:01:45 +00003230
Dan Gohman5a199552007-10-08 18:33:35 +00003231 // See if multiply or divide can be lowered using two-result operations.
3232 SDVTList VTs = DAG.getVTList(VT, VT);
3233 if (Node->getOpcode() == ISD::MUL) {
3234 // We just need the low half of the multiply; try both the signed
3235 // and unsigned forms. If the target supports both SMUL_LOHI and
3236 // UMUL_LOHI, form a preference by checking which forms of plain
3237 // MULH it supports.
Dan Gohman52c51aa2009-01-28 17:46:25 +00003238 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3239 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3240 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3241 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
Dan Gohman5a199552007-10-08 18:33:35 +00003242 unsigned OpToUse = 0;
3243 if (HasSMUL_LOHI && !HasMULHS) {
3244 OpToUse = ISD::SMUL_LOHI;
3245 } else if (HasUMUL_LOHI && !HasMULHU) {
3246 OpToUse = ISD::UMUL_LOHI;
3247 } else if (HasSMUL_LOHI) {
3248 OpToUse = ISD::SMUL_LOHI;
3249 } else if (HasUMUL_LOHI) {
3250 OpToUse = ISD::UMUL_LOHI;
3251 }
3252 if (OpToUse) {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003253 Result = SDValue(DAG.getNode(OpToUse, dl, VTs, Tmp1, Tmp2).getNode(),
3254 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003255 break;
3256 }
3257 }
3258 if (Node->getOpcode() == ISD::MULHS &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00003259 TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003260 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl,
3261 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner48188652008-10-04 21:27:46 +00003262 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003263 break;
3264 }
3265 if (Node->getOpcode() == ISD::MULHU &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00003266 TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003267 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl,
3268 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner48188652008-10-04 21:27:46 +00003269 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003270 break;
3271 }
3272 if (Node->getOpcode() == ISD::SDIV &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00003273 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003274 Result = SDValue(DAG.getNode(ISD::SDIVREM, dl,
3275 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner48188652008-10-04 21:27:46 +00003276 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003277 break;
3278 }
3279 if (Node->getOpcode() == ISD::UDIV &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00003280 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003281 Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
3282 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner48188652008-10-04 21:27:46 +00003283 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003284 break;
3285 }
Mon P Wang26342922008-12-18 20:03:17 +00003286
Dan Gohman6d05cac2007-10-11 23:57:53 +00003287 // Check to see if we have a libcall for this operator.
3288 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3289 bool isSigned = false;
3290 switch (Node->getOpcode()) {
3291 case ISD::UDIV:
3292 case ISD::SDIV:
3293 if (VT == MVT::i32) {
3294 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang1448aad2008-10-30 08:01:45 +00003295 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003296 isSigned = Node->getOpcode() == ISD::SDIV;
3297 }
3298 break;
Chris Lattner48188652008-10-04 21:27:46 +00003299 case ISD::MUL:
3300 if (VT == MVT::i32)
3301 LC = RTLIB::MUL_I32;
sampoa0d77372009-01-24 22:12:48 +00003302 else if (VT == MVT::i64)
Scott Michel81215042008-12-29 03:21:37 +00003303 LC = RTLIB::MUL_I64;
Chris Lattner48188652008-10-04 21:27:46 +00003304 break;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003305 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00003306 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3307 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003308 break;
Scott Michel8c67fa42009-01-21 04:58:48 +00003309 case ISD::FDIV:
3310 LC = GetFPLibCall(VT, RTLIB::DIV_F32, RTLIB::DIV_F64, RTLIB::DIV_F80,
3311 RTLIB::DIV_PPCF128);
3312 break;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003313 default: break;
3314 }
3315 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003316 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003317 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003318 break;
3319 }
Mon P Wang1448aad2008-10-30 08:01:45 +00003320
Duncan Sands92c43912008-06-06 12:08:01 +00003321 assert(Node->getValueType(0).isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003322 "Cannot expand this binary operator!");
3323 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003324 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003325 break;
3326 }
3327 case TargetLowering::Promote: {
3328 switch (Node->getOpcode()) {
3329 default: assert(0 && "Do not know how to promote this BinOp!");
3330 case ISD::AND:
3331 case ISD::OR:
3332 case ISD::XOR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003333 MVT OVT = Node->getValueType(0);
3334 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3335 assert(OVT.isVector() && "Cannot promote this BinOp!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003336 // Bit convert each of the values to the new type.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003337 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
3338 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
3339 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003340 // Bit convert the result back the original type.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003341 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003342 break;
3343 }
3344 }
3345 }
3346 }
3347 break;
3348
Dan Gohman475cd732007-10-05 14:17:22 +00003349 case ISD::SMUL_LOHI:
3350 case ISD::UMUL_LOHI:
3351 case ISD::SDIVREM:
3352 case ISD::UDIVREM:
3353 // These nodes will only be produced by target-specific lowering, so
3354 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003355 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003356 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003357
3358 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3359 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3360 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003361 break;
3362
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003363 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3364 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3365 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3366 case Expand: assert(0 && "Not possible");
3367 case Legal:
3368 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3369 break;
3370 case Promote:
3371 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3372 break;
3373 }
3374
3375 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3376
3377 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3378 default: assert(0 && "Operation not supported");
3379 case TargetLowering::Custom:
3380 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003381 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003382 break;
3383 case TargetLowering::Legal: break;
3384 case TargetLowering::Expand: {
3385 // If this target supports fabs/fneg natively and select is cheap,
3386 // do this efficiently.
3387 if (!TLI.isSelectExpensive() &&
3388 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3389 TargetLowering::Legal &&
3390 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3391 TargetLowering::Legal) {
3392 // Get the sign bit of the RHS.
Duncan Sands92c43912008-06-06 12:08:01 +00003393 MVT IVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003394 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003395 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2);
3396 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(IVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003397 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3398 // Get the absolute value of the result.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003399 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003400 // Select between the nabs and abs value based on the sign bit of
3401 // the input.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003402 Result = DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
Dale Johannesen24dd9a52009-02-07 00:55:49 +00003403 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003404 AbsVal),
3405 AbsVal);
3406 Result = LegalizeOp(Result);
3407 break;
3408 }
3409
3410 // Otherwise, do bitwise ops!
Duncan Sands92c43912008-06-06 12:08:01 +00003411 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003412 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3413 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003414 Result = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0), Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003415 Result = LegalizeOp(Result);
3416 break;
3417 }
3418 }
3419 break;
3420
3421 case ISD::ADDC:
3422 case ISD::SUBC:
3423 Tmp1 = LegalizeOp(Node->getOperand(0));
3424 Tmp2 = LegalizeOp(Node->getOperand(1));
3425 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003426 Tmp3 = Result.getValue(0);
3427 Tmp4 = Result.getValue(1);
3428
3429 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3430 default: assert(0 && "This action is not supported yet!");
3431 case TargetLowering::Legal:
3432 break;
3433 case TargetLowering::Custom:
3434 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3435 if (Tmp1.getNode() != NULL) {
Sanjiv Guptad57f2e12008-11-27 05:58:04 +00003436 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003437 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3438 }
3439 break;
3440 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003441 // Since this produces two values, make sure to remember that we legalized
3442 // both of them.
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003443 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3444 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3445 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003446
3447 case ISD::ADDE:
3448 case ISD::SUBE:
3449 Tmp1 = LegalizeOp(Node->getOperand(0));
3450 Tmp2 = LegalizeOp(Node->getOperand(1));
3451 Tmp3 = LegalizeOp(Node->getOperand(2));
3452 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003453 Tmp3 = Result.getValue(0);
3454 Tmp4 = Result.getValue(1);
3455
3456 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3457 default: assert(0 && "This action is not supported yet!");
3458 case TargetLowering::Legal:
3459 break;
3460 case TargetLowering::Custom:
3461 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3462 if (Tmp1.getNode() != NULL) {
Sanjiv Guptad57f2e12008-11-27 05:58:04 +00003463 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003464 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3465 }
3466 break;
3467 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003468 // Since this produces two values, make sure to remember that we legalized
3469 // both of them.
Sanjiv Gupta7a61e7f2008-11-26 11:19:00 +00003470 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3471 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3472 return Op.getResNo() ? Tmp4 : Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003473
3474 case ISD::BUILD_PAIR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003475 MVT PairTy = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003476 // TODO: handle the case where the Lo and Hi operands are not of legal type
3477 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3478 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3479 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3480 case TargetLowering::Promote:
3481 case TargetLowering::Custom:
3482 assert(0 && "Cannot promote/custom this yet!");
3483 case TargetLowering::Legal:
3484 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003485 Result = DAG.getNode(ISD::BUILD_PAIR, dl, PairTy, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003486 break;
3487 case TargetLowering::Expand:
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003488 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Tmp1);
3489 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Tmp2);
3490 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003491 DAG.getConstant(PairTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003492 TLI.getShiftAmountTy()));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003493 Result = DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003494 break;
3495 }
3496 break;
3497 }
3498
3499 case ISD::UREM:
3500 case ISD::SREM:
3501 case ISD::FREM:
3502 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3503 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3504
3505 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3506 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3507 case TargetLowering::Custom:
3508 isCustom = true;
3509 // FALLTHROUGH
3510 case TargetLowering::Legal:
3511 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3512 if (isCustom) {
3513 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003514 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003515 }
3516 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003517 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003518 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3519 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands92c43912008-06-06 12:08:01 +00003520 MVT VT = Node->getValueType(0);
Dan Gohman5a199552007-10-08 18:33:35 +00003521
3522 // See if remainder can be lowered using two-result operations.
3523 SDVTList VTs = DAG.getVTList(VT, VT);
3524 if (Node->getOpcode() == ISD::SREM &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00003525 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003526 Result = SDValue(DAG.getNode(ISD::SDIVREM, dl,
3527 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003528 break;
3529 }
3530 if (Node->getOpcode() == ISD::UREM &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00003531 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003532 Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
3533 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003534 break;
3535 }
3536
Duncan Sands92c43912008-06-06 12:08:01 +00003537 if (VT.isInteger()) {
Dan Gohman5a199552007-10-08 18:33:35 +00003538 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003539 TargetLowering::Legal) {
3540 // X % Y -> X-X/Y*Y
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003541 Result = DAG.getNode(DivOpc, dl, VT, Tmp1, Tmp2);
3542 Result = DAG.getNode(ISD::MUL, dl, VT, Result, Tmp2);
3543 Result = DAG.getNode(ISD::SUB, dl, VT, Tmp1, Result);
Duncan Sands92c43912008-06-06 12:08:01 +00003544 } else if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003545 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003546 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003547 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003548 "Cannot expand this binary operator!");
3549 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3550 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman8181bd12008-07-27 21:46:04 +00003551 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003552 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003553 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003554 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00003555 assert(VT.isFloatingPoint() &&
Dan Gohman59b4b102007-11-06 22:11:54 +00003556 "remainder op must have integer or floating-point type");
Duncan Sands92c43912008-06-06 12:08:01 +00003557 if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003558 Result = LegalizeOp(UnrollVectorOp(Op));
3559 } else {
3560 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003561 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3562 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003563 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003564 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003565 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003566 }
3567 break;
3568 }
Dan Gohman5a199552007-10-08 18:33:35 +00003569 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003570 break;
3571 case ISD::VAARG: {
3572 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3573 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3574
Duncan Sands92c43912008-06-06 12:08:01 +00003575 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003576 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3577 default: assert(0 && "This action is not supported yet!");
3578 case TargetLowering::Custom:
3579 isCustom = true;
3580 // FALLTHROUGH
3581 case TargetLowering::Legal:
3582 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3583 Result = Result.getValue(0);
3584 Tmp1 = Result.getValue(1);
3585
3586 if (isCustom) {
3587 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003588 if (Tmp2.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003589 Result = LegalizeOp(Tmp2);
3590 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3591 }
3592 }
3593 break;
3594 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003595 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003596 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003597 // Increment the pointer, VAList, to the next vaarg
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003598 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sandsd68f13b2009-01-12 20:38:59 +00003599 DAG.getConstant(TLI.getTargetData()->
3600 getTypePaddedSize(VT.getTypeForMVT()),
3601 TLI.getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003602 // Store the incremented VAList to the legalized pointer
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003603 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003604 // Load the actual argument out of the pointer VAList
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003605 Result = DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003606 Tmp1 = LegalizeOp(Result.getValue(1));
3607 Result = LegalizeOp(Result);
3608 break;
3609 }
3610 }
3611 // Since VAARG produces two values, make sure to remember that we
3612 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003613 AddLegalizedOperand(SDValue(Node, 0), Result);
3614 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00003615 return Op.getResNo() ? Tmp1 : Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003616 }
3617
3618 case ISD::VACOPY:
3619 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3620 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3621 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3622
3623 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3624 default: assert(0 && "This action is not supported yet!");
3625 case TargetLowering::Custom:
3626 isCustom = true;
3627 // FALLTHROUGH
3628 case TargetLowering::Legal:
3629 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3630 Node->getOperand(3), Node->getOperand(4));
3631 if (isCustom) {
3632 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003633 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003634 }
3635 break;
3636 case TargetLowering::Expand:
3637 // This defaults to loading a pointer from the input and storing it to the
3638 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003639 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3640 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003641 Tmp4 = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp3, VS, 0);
3642 Result = DAG.getStore(Tmp4.getValue(1), dl, Tmp4, Tmp2, VD, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003643 break;
3644 }
3645 break;
3646
3647 case ISD::VAEND:
3648 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3649 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3650
3651 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3652 default: assert(0 && "This action is not supported yet!");
3653 case TargetLowering::Custom:
3654 isCustom = true;
3655 // FALLTHROUGH
3656 case TargetLowering::Legal:
3657 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3658 if (isCustom) {
3659 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003660 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003661 }
3662 break;
3663 case TargetLowering::Expand:
3664 Result = Tmp1; // Default to a no-op, return the chain
3665 break;
3666 }
3667 break;
3668
3669 case ISD::VASTART:
3670 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3671 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3672
3673 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3674
3675 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3676 default: assert(0 && "This action is not supported yet!");
3677 case TargetLowering::Legal: 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 }
3683 break;
3684
3685 case ISD::ROTL:
3686 case ISD::ROTR:
3687 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands7d9e3612009-01-31 15:50:11 +00003688 Tmp2 = LegalizeOp(DAG.getShiftAmountOperand(Node->getOperand(1))); // RHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003689 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3690 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3691 default:
3692 assert(0 && "ROTL/ROTR legalize operation not supported");
3693 break;
3694 case TargetLowering::Legal:
3695 break;
3696 case TargetLowering::Custom:
3697 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003698 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003699 break;
3700 case TargetLowering::Promote:
3701 assert(0 && "Do not know how to promote ROTL/ROTR");
3702 break;
3703 case TargetLowering::Expand:
3704 assert(0 && "Do not know how to expand ROTL/ROTR");
3705 break;
3706 }
3707 break;
3708
3709 case ISD::BSWAP:
3710 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3711 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3712 case TargetLowering::Custom:
3713 assert(0 && "Cannot custom legalize this yet!");
3714 case TargetLowering::Legal:
3715 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3716 break;
3717 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003718 MVT OVT = Tmp1.getValueType();
3719 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3720 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003721
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003722 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
3723 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3724 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003725 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3726 break;
3727 }
3728 case TargetLowering::Expand:
Dale Johannesen82b5b722009-02-02 22:12:50 +00003729 Result = ExpandBSWAP(Tmp1, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003730 break;
3731 }
3732 break;
3733
3734 case ISD::CTPOP:
3735 case ISD::CTTZ:
3736 case ISD::CTLZ:
3737 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3738 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003739 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003740 case TargetLowering::Legal:
3741 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003742 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003743 TargetLowering::Custom) {
3744 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003745 if (Tmp1.getNode()) {
Scott Michelbc62b412007-08-02 02:22:46 +00003746 Result = Tmp1;
3747 }
Scott Michel48b63e62007-07-30 21:00:31 +00003748 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003749 break;
3750 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003751 MVT OVT = Tmp1.getValueType();
3752 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003753
3754 // Zero extend the argument.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003755 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003756 // Perform the larger operation, then subtract if needed.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003757 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003758 switch (Node->getOpcode()) {
3759 case ISD::CTPOP:
3760 Result = Tmp1;
3761 break;
3762 case ISD::CTTZ:
3763 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003764 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3765 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003766 ISD::SETEQ);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003767 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003768 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003769 break;
3770 case ISD::CTLZ:
3771 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003772 Result = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003773 DAG.getConstant(NVT.getSizeInBits() -
3774 OVT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003775 break;
3776 }
3777 break;
3778 }
3779 case TargetLowering::Expand:
Dale Johannesen82b5b722009-02-02 22:12:50 +00003780 Result = ExpandBitCount(Node->getOpcode(), Tmp1, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003781 break;
3782 }
3783 break;
3784
3785 // Unary operators
3786 case ISD::FABS:
3787 case ISD::FNEG:
3788 case ISD::FSQRT:
3789 case ISD::FSIN:
3790 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003791 case ISD::FLOG:
3792 case ISD::FLOG2:
3793 case ISD::FLOG10:
3794 case ISD::FEXP:
3795 case ISD::FEXP2:
Dan Gohmanc8b20e22008-08-21 17:55:02 +00003796 case ISD::FTRUNC:
3797 case ISD::FFLOOR:
3798 case ISD::FCEIL:
3799 case ISD::FRINT:
3800 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003801 Tmp1 = LegalizeOp(Node->getOperand(0));
3802 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3803 case TargetLowering::Promote:
3804 case TargetLowering::Custom:
3805 isCustom = true;
3806 // FALLTHROUGH
3807 case TargetLowering::Legal:
3808 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3809 if (isCustom) {
3810 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003811 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003812 }
3813 break;
3814 case TargetLowering::Expand:
3815 switch (Node->getOpcode()) {
3816 default: assert(0 && "Unreachable!");
3817 case ISD::FNEG:
3818 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3819 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003820 Result = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp2, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003821 break;
3822 case ISD::FABS: {
3823 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands92c43912008-06-06 12:08:01 +00003824 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003825 Tmp2 = DAG.getConstantFP(0.0, VT);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003826 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Duncan Sands4a361272009-01-01 15:52:00 +00003827 Tmp1, Tmp2, ISD::SETUGT);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003828 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
3829 Result = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003830 break;
3831 }
Evan Cheng1fac6952008-09-09 23:35:53 +00003832 case ISD::FSQRT:
3833 case ISD::FSIN:
3834 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00003835 case ISD::FLOG:
3836 case ISD::FLOG2:
3837 case ISD::FLOG10:
3838 case ISD::FEXP:
3839 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00003840 case ISD::FTRUNC:
3841 case ISD::FFLOOR:
3842 case ISD::FCEIL:
3843 case ISD::FRINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00003844 case ISD::FNEARBYINT: {
Duncan Sands92c43912008-06-06 12:08:01 +00003845 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003846
3847 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003848 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003849 Result = LegalizeOp(UnrollVectorOp(Op));
3850 break;
3851 }
3852
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003853 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3854 switch(Node->getOpcode()) {
3855 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003856 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3857 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003858 break;
3859 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003860 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3861 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003862 break;
3863 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003864 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3865 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003866 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00003867 case ISD::FLOG:
3868 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3869 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3870 break;
3871 case ISD::FLOG2:
3872 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3873 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3874 break;
3875 case ISD::FLOG10:
3876 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3877 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3878 break;
3879 case ISD::FEXP:
3880 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3881 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3882 break;
3883 case ISD::FEXP2:
3884 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3885 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3886 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00003887 case ISD::FTRUNC:
3888 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3889 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3890 break;
3891 case ISD::FFLOOR:
3892 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3893 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3894 break;
3895 case ISD::FCEIL:
3896 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3897 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3898 break;
3899 case ISD::FRINT:
3900 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3901 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3902 break;
3903 case ISD::FNEARBYINT:
3904 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3905 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3906 break;
Evan Cheng1fac6952008-09-09 23:35:53 +00003907 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003908 default: assert(0 && "Unreachable!");
3909 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003910 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003911 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003912 break;
3913 }
3914 }
3915 break;
3916 }
3917 break;
3918 case ISD::FPOWI: {
Duncan Sands92c43912008-06-06 12:08:01 +00003919 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003920
3921 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003922 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003923 Result = LegalizeOp(UnrollVectorOp(Op));
3924 break;
3925 }
3926
3927 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003928 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3929 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003930 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003931 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003932 break;
3933 }
3934 case ISD::BIT_CONVERT:
3935 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003936 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
Dale Johannesen82b5b722009-02-02 22:12:50 +00003937 Node->getValueType(0), dl);
Duncan Sands92c43912008-06-06 12:08:01 +00003938 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003939 // The input has to be a vector type, we have to either scalarize it, pack
3940 // it, or convert it based on whether the input vector type is legal.
Gabor Greif1c80d112008-08-28 21:40:38 +00003941 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00003942 int InIx = Node->getOperand(0).getResNo();
Duncan Sands92c43912008-06-06 12:08:01 +00003943 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3944 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003945
3946 // Figure out if there is a simple type corresponding to this Vector
3947 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00003948 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003949 if (TLI.isTypeLegal(TVT)) {
3950 // Turn this into a bit convert of the vector input.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003951 Result = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003952 LegalizeOp(Node->getOperand(0)));
3953 break;
3954 } else if (NumElems == 1) {
3955 // Turn this into a bit convert of the scalar input.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00003956 Result = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003957 ScalarizeVectorOp(Node->getOperand(0)));
3958 break;
3959 } else {
3960 // FIXME: UNIMP! Store then reload
3961 assert(0 && "Cast from unsupported vector type not implemented yet!");
3962 }
3963 } else {
3964 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3965 Node->getOperand(0).getValueType())) {
3966 default: assert(0 && "Unknown operation action!");
3967 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003968 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
Dale Johannesen82b5b722009-02-02 22:12:50 +00003969 Node->getValueType(0), dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003970 break;
3971 case TargetLowering::Legal:
3972 Tmp1 = LegalizeOp(Node->getOperand(0));
3973 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3974 break;
3975 }
3976 }
3977 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003978 case ISD::CONVERT_RNDSAT: {
3979 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3980 switch (CvtCode) {
3981 default: assert(0 && "Unknown cvt code!");
3982 case ISD::CVT_SF:
3983 case ISD::CVT_UF:
Mon P Wang73d31542008-11-10 20:54:11 +00003984 case ISD::CVT_FF:
Mon P Wang2fc3f9e2008-12-09 07:27:39 +00003985 break;
Mon P Wang73d31542008-11-10 20:54:11 +00003986 case ISD::CVT_FS:
3987 case ISD::CVT_FU:
3988 case ISD::CVT_SS:
3989 case ISD::CVT_SU:
3990 case ISD::CVT_US:
3991 case ISD::CVT_UU: {
3992 SDValue DTyOp = Node->getOperand(1);
3993 SDValue STyOp = Node->getOperand(2);
3994 SDValue RndOp = Node->getOperand(3);
3995 SDValue SatOp = Node->getOperand(4);
3996 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3997 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3998 case Legal:
3999 Tmp1 = LegalizeOp(Node->getOperand(0));
4000 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
4001 RndOp, SatOp);
4002 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4003 TargetLowering::Custom) {
4004 Tmp1 = TLI.LowerOperation(Result, DAG);
4005 if (Tmp1.getNode()) Result = Tmp1;
4006 }
4007 break;
4008 case Promote:
4009 Result = PromoteOp(Node->getOperand(0));
4010 // For FP, make Op1 a i32
4011
Dale Johannesen564036c2009-02-04 00:13:36 +00004012 Result = DAG.getConvertRndSat(Op.getValueType(), dl, Result,
Mon P Wang73d31542008-11-10 20:54:11 +00004013 DTyOp, STyOp, RndOp, SatOp, CvtCode);
4014 break;
4015 }
4016 break;
4017 }
4018 } // end switch CvtCode
4019 break;
4020 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004021 // Conversion operators. The source and destination have different types.
4022 case ISD::SINT_TO_FP:
4023 case ISD::UINT_TO_FP: {
4024 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman29c3cef2008-08-14 20:04:46 +00004025 Result = LegalizeINT_TO_FP(Result, isSigned,
Dale Johannesen9972b632009-02-02 19:03:57 +00004026 Node->getValueType(0), Node->getOperand(0), dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004027 break;
4028 }
4029 case ISD::TRUNCATE:
4030 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4031 case Legal:
4032 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michele9b8a402008-12-02 19:55:08 +00004033 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
4034 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
4035 case TargetLowering::Custom:
Mon P Wang72fe5462008-12-11 00:44:22 +00004036 isCustom = true;
4037 // FALLTHROUGH
Scott Michele9b8a402008-12-02 19:55:08 +00004038 case TargetLowering::Legal:
Mon P Wang72fe5462008-12-11 00:44:22 +00004039 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4040 if (isCustom) {
4041 Tmp1 = TLI.LowerOperation(Result, DAG);
4042 if (Tmp1.getNode()) Result = Tmp1;
4043 }
4044 break;
Mon P Wang83edba52008-12-12 01:25:51 +00004045 case TargetLowering::Expand:
4046 assert(Result.getValueType().isVector() && "must be vector type");
4047 // Unroll the truncate. We should do better.
4048 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Schellerbfc55ee2008-12-02 12:12:25 +00004049 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004050 break;
4051 case Expand:
4052 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4053
4054 // Since the result is legal, we should just be able to truncate the low
4055 // part of the source.
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004056 Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004057 break;
4058 case Promote:
4059 Result = PromoteOp(Node->getOperand(0));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004060 Result = DAG.getNode(ISD::TRUNCATE, dl, Op.getValueType(), Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004061 break;
4062 }
4063 break;
4064
4065 case ISD::FP_TO_SINT:
4066 case ISD::FP_TO_UINT:
4067 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4068 case Legal:
4069 Tmp1 = LegalizeOp(Node->getOperand(0));
4070
4071 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
4072 default: assert(0 && "Unknown operation action!");
4073 case TargetLowering::Custom:
4074 isCustom = true;
4075 // FALLTHROUGH
4076 case TargetLowering::Legal:
4077 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4078 if (isCustom) {
4079 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004080 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004081 }
4082 break;
4083 case TargetLowering::Promote:
4084 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Dale Johannesen9972b632009-02-02 19:03:57 +00004085 Node->getOpcode() == ISD::FP_TO_SINT,
4086 dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004087 break;
4088 case TargetLowering::Expand:
4089 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004090 SDValue True, False;
Duncan Sands92c43912008-06-06 12:08:01 +00004091 MVT VT = Node->getOperand(0).getValueType();
4092 MVT NVT = Node->getValueType(0);
Dale Johannesen958b08b2007-09-19 23:55:34 +00004093 const uint64_t zero[] = {0, 0};
Duncan Sands92c43912008-06-06 12:08:01 +00004094 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
4095 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohman88ae8c52008-02-29 01:44:25 +00004096 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00004097 Tmp2 = DAG.getConstantFP(apf, VT);
Dale Johannesen9972b632009-02-02 19:03:57 +00004098 Tmp3 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
4099 Node->getOperand(0),
Duncan Sands4a361272009-01-01 15:52:00 +00004100 Tmp2, ISD::SETLT);
Dale Johannesen9972b632009-02-02 19:03:57 +00004101 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
4102 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
4103 DAG.getNode(ISD::FSUB, dl, VT,
4104 Node->getOperand(0), Tmp2));
4105 False = DAG.getNode(ISD::XOR, dl, NVT, False,
Dan Gohman88ae8c52008-02-29 01:44:25 +00004106 DAG.getConstant(x, NVT));
Dale Johannesen9972b632009-02-02 19:03:57 +00004107 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp3, True, False);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004108 break;
4109 } else {
4110 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
4111 }
4112 break;
4113 }
4114 break;
4115 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00004116 MVT VT = Op.getValueType();
4117 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00004118 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004119 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00004120 if (Node->getOpcode() == ISD::FP_TO_SINT) {
Dale Johannesen9972b632009-02-02 19:03:57 +00004121 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
Chris Lattner5872a362008-01-17 07:00:52 +00004122 Node->getOperand(0), DAG.getValueType(MVT::f64));
Dale Johannesen9972b632009-02-02 19:03:57 +00004123 Result = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Result,
Chris Lattner5872a362008-01-17 07:00:52 +00004124 DAG.getIntPtrConstant(1));
Dale Johannesen9972b632009-02-02 19:03:57 +00004125 Result = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00004126 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00004127 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4128 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4129 Tmp2 = DAG.getConstantFP(apf, OVT);
4130 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4131 // FIXME: generated code sucks.
Dale Johannesen9972b632009-02-02 19:03:57 +00004132 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Node->getOperand(0),
4133 Tmp2,
4134 DAG.getNode(ISD::ADD, dl, MVT::i32,
4135 DAG.getNode(ISD::FP_TO_SINT, dl, VT,
4136 DAG.getNode(ISD::FSUB, dl, OVT,
Dale Johannesend3b6af32007-10-11 23:32:15 +00004137 Node->getOperand(0), Tmp2)),
4138 DAG.getConstant(0x80000000, MVT::i32)),
Dale Johannesen9972b632009-02-02 19:03:57 +00004139 DAG.getNode(ISD::FP_TO_SINT, dl, VT,
Dale Johannesend3b6af32007-10-11 23:32:15 +00004140 Node->getOperand(0)),
4141 DAG.getCondCode(ISD::SETGE));
4142 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004143 break;
4144 }
Dan Gohmanec51f642008-03-10 23:03:31 +00004145 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsf68dffb2008-07-17 02:36:29 +00004146 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4147 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4148 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman8181bd12008-07-27 21:46:04 +00004149 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00004150 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004151 break;
4152 }
4153 case Promote:
4154 Tmp1 = PromoteOp(Node->getOperand(0));
4155 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4156 Result = LegalizeOp(Result);
4157 break;
4158 }
4159 break;
4160
Chris Lattner56ecde32008-01-16 06:57:07 +00004161 case ISD::FP_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004162 MVT DstVT = Op.getValueType();
4163 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004164 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4165 // The only other way we can lower this is to turn it into a STORE,
4166 // LOAD pair, targetting a temporary location (a stack slot).
Dale Johannesen82b5b722009-02-02 22:12:50 +00004167 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT, dl);
Chris Lattner5872a362008-01-17 07:00:52 +00004168 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00004169 }
4170 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4171 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4172 case Legal:
4173 Tmp1 = LegalizeOp(Node->getOperand(0));
4174 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4175 break;
4176 case Promote:
4177 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004178 Result = DAG.getNode(ISD::FP_EXTEND, dl, Op.getValueType(), Tmp1);
Chris Lattner56ecde32008-01-16 06:57:07 +00004179 break;
4180 }
4181 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004182 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004183 case ISD::FP_ROUND: {
Duncan Sands92c43912008-06-06 12:08:01 +00004184 MVT DstVT = Op.getValueType();
4185 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00004186 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4187 if (SrcVT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004188 SDValue Lo;
Dale Johannesena0d36082008-01-20 01:18:38 +00004189 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00004190 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00004191 if (DstVT!=MVT::f64)
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004192 Result = DAG.getNode(ISD::FP_ROUND, dl,
4193 DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00004194 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00004195 }
Chris Lattner5872a362008-01-17 07:00:52 +00004196 // The only other way we can lower this is to turn it into a STORE,
4197 // LOAD pair, targetting a temporary location (a stack slot).
Dale Johannesen82b5b722009-02-02 22:12:50 +00004198 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT, dl);
Chris Lattner5872a362008-01-17 07:00:52 +00004199 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004200 }
Chris Lattner56ecde32008-01-16 06:57:07 +00004201 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4202 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4203 case Legal:
4204 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00004205 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004206 break;
4207 case Promote:
4208 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004209 Result = DAG.getNode(ISD::FP_ROUND, dl, Op.getValueType(), Tmp1,
Chris Lattner5872a362008-01-17 07:00:52 +00004210 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00004211 break;
4212 }
4213 break;
Chris Lattner5872a362008-01-17 07:00:52 +00004214 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004215 case ISD::ANY_EXTEND:
4216 case ISD::ZERO_EXTEND:
4217 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004218 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4219 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4220 case Legal:
4221 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac54d002008-04-30 00:26:38 +00004222 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michelac7091c2008-02-15 23:05:48 +00004223 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4224 TargetLowering::Custom) {
Scott Michelac54d002008-04-30 00:26:38 +00004225 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004226 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelac7091c2008-02-15 23:05:48 +00004227 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004228 break;
4229 case Promote:
4230 switch (Node->getOpcode()) {
4231 case ISD::ANY_EXTEND:
4232 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004233 Result = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004234 break;
4235 case ISD::ZERO_EXTEND:
4236 Result = PromoteOp(Node->getOperand(0));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004237 Result = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), Result);
4238 Result = DAG.getZeroExtendInReg(Result, dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004239 Node->getOperand(0).getValueType());
4240 break;
4241 case ISD::SIGN_EXTEND:
4242 Result = PromoteOp(Node->getOperand(0));
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004243 Result = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), Result);
4244 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Result.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004245 Result,
4246 DAG.getValueType(Node->getOperand(0).getValueType()));
4247 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004248 }
4249 }
4250 break;
4251 case ISD::FP_ROUND_INREG:
4252 case ISD::SIGN_EXTEND_INREG: {
4253 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004254 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004255
4256 // If this operation is not supported, convert it to a shl/shr or load/store
4257 // pair.
4258 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4259 default: assert(0 && "This action not supported for this op yet!");
4260 case TargetLowering::Legal:
4261 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
4262 break;
4263 case TargetLowering::Expand:
4264 // If this is an integer extend and shifts are supported, do that.
4265 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
4266 // NOTE: we could fall back on load/store here too for targets without
4267 // SAR. However, it is doubtful that any exist.
Duncan Sands92c43912008-06-06 12:08:01 +00004268 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4269 ExtraVT.getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00004270 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004271 Result = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004272 Node->getOperand(0), ShiftCst);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004273 Result = DAG.getNode(ISD::SRA, dl, Node->getValueType(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004274 Result, ShiftCst);
4275 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
4276 // The only way we can lower this is to turn it into a TRUNCSTORE,
4277 // EXTLOAD pair, targetting a temporary location (a stack slot).
4278
4279 // NOTE: there is a choice here between constantly creating new stack
4280 // slots and always reusing the same one. We currently always create
4281 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00004282 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
Dale Johannesen82b5b722009-02-02 22:12:50 +00004283 Node->getValueType(0), dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004284 } else {
4285 assert(0 && "Unknown op");
4286 }
4287 break;
4288 }
4289 break;
4290 }
Duncan Sands38947cd2007-07-27 12:58:54 +00004291 case ISD::TRAMPOLINE: {
Dan Gohman8181bd12008-07-27 21:46:04 +00004292 SDValue Ops[6];
Duncan Sands38947cd2007-07-27 12:58:54 +00004293 for (unsigned i = 0; i != 6; ++i)
4294 Ops[i] = LegalizeOp(Node->getOperand(i));
4295 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4296 // The only option for this node is to custom lower it.
4297 Result = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004298 assert(Result.getNode() && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00004299
4300 // Since trampoline produces two values, make sure to remember that we
4301 // legalized both of them.
4302 Tmp1 = LegalizeOp(Result.getValue(1));
4303 Result = LegalizeOp(Result);
Dan Gohman8181bd12008-07-27 21:46:04 +00004304 AddLegalizedOperand(SDValue(Node, 0), Result);
4305 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif46bf5472008-08-26 22:36:50 +00004306 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00004307 }
Dan Gohmane8e4a412008-05-14 00:43:10 +00004308 case ISD::FLT_ROUNDS_: {
Duncan Sands92c43912008-06-06 12:08:01 +00004309 MVT VT = Node->getValueType(0);
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004310 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4311 default: assert(0 && "This action not supported for this op yet!");
4312 case TargetLowering::Custom:
4313 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004314 if (Result.getNode()) break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004315 // Fall Thru
4316 case TargetLowering::Legal:
4317 // If this operation is not supported, lower it to constant 1
4318 Result = DAG.getConstant(1, VT);
4319 break;
4320 }
Dan Gohmane09dc8c2008-05-12 16:07:15 +00004321 break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004322 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004323 case ISD::TRAP: {
Duncan Sands92c43912008-06-06 12:08:01 +00004324 MVT VT = Node->getValueType(0);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004325 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4326 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00004327 case TargetLowering::Legal:
4328 Tmp1 = LegalizeOp(Node->getOperand(0));
4329 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4330 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004331 case TargetLowering::Custom:
4332 Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004333 if (Result.getNode()) break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004334 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00004335 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004336 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00004337 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004338 TargetLowering::ArgListTy Args;
Dan Gohman8181bd12008-07-27 21:46:04 +00004339 std::pair<SDValue,SDValue> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004340 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00004341 false, false, false, false, CallingConv::C, false,
Bill Wendlingfef06052008-09-16 21:48:12 +00004342 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Dale Johannesenca6237b2009-01-30 23:10:59 +00004343 Args, DAG, dl);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004344 Result = CallResult.second;
4345 break;
4346 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004347 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004348 }
Bill Wendling913dcf32008-11-22 00:22:52 +00004349
Bill Wendling7e04be62008-12-09 22:08:41 +00004350 case ISD::SADDO:
4351 case ISD::SSUBO: {
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004352 MVT VT = Node->getValueType(0);
4353 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4354 default: assert(0 && "This action not supported for this op yet!");
4355 case TargetLowering::Custom:
4356 Result = TLI.LowerOperation(Op, DAG);
4357 if (Result.getNode()) break;
4358 // FALLTHROUGH
4359 case TargetLowering::Legal: {
4360 SDValue LHS = LegalizeOp(Node->getOperand(0));
4361 SDValue RHS = LegalizeOp(Node->getOperand(1));
4362
Bill Wendling7e04be62008-12-09 22:08:41 +00004363 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004364 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling7e04be62008-12-09 22:08:41 +00004365 LHS, RHS);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004366 MVT OType = Node->getValueType(1);
4367
Bill Wendlingc65e6e42008-11-25 08:19:22 +00004368 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004369
Bill Wendlingcf4de122008-11-25 19:40:17 +00004370 // LHSSign -> LHS >= 0
4371 // RHSSign -> RHS >= 0
4372 // SumSign -> Sum >= 0
4373 //
Bill Wendling7e04be62008-12-09 22:08:41 +00004374 // Add:
Bill Wendlingcf4de122008-11-25 19:40:17 +00004375 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling7e04be62008-12-09 22:08:41 +00004376 // Sub:
4377 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendlingcf4de122008-11-25 19:40:17 +00004378 //
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004379 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
4380 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
4381 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
Bill Wendling7e04be62008-12-09 22:08:41 +00004382 Node->getOpcode() == ISD::SADDO ?
4383 ISD::SETEQ : ISD::SETNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004384
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004385 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
4386 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004387
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004388 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004389
4390 MVT ValueVTs[] = { LHS.getValueType(), OType };
4391 SDValue Ops[] = { Sum, Cmp };
4392
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004393 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
4394 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sands42d7bb82008-12-01 11:41:29 +00004395 &Ops[0], 2);
Bill Wendling6c4e3e02008-11-25 08:12:19 +00004396 SDNode *RNode = Result.getNode();
4397 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4398 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4399 break;
4400 }
4401 }
4402
4403 break;
4404 }
Bill Wendling7e04be62008-12-09 22:08:41 +00004405 case ISD::UADDO:
4406 case ISD::USUBO: {
Bill Wendling4c134df2008-11-24 19:21:46 +00004407 MVT VT = Node->getValueType(0);
4408 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4409 default: assert(0 && "This action not supported for this op yet!");
4410 case TargetLowering::Custom:
4411 Result = TLI.LowerOperation(Op, DAG);
4412 if (Result.getNode()) break;
4413 // FALLTHROUGH
4414 case TargetLowering::Legal: {
4415 SDValue LHS = LegalizeOp(Node->getOperand(0));
4416 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling913dcf32008-11-22 00:22:52 +00004417
Bill Wendling7e04be62008-12-09 22:08:41 +00004418 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004419 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling7e04be62008-12-09 22:08:41 +00004420 LHS, RHS);
Bill Wendling4c134df2008-11-24 19:21:46 +00004421 MVT OType = Node->getValueType(1);
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004422 SDValue Cmp = DAG.getSetCC(dl, OType, Sum, LHS,
Bill Wendling7e04be62008-12-09 22:08:41 +00004423 Node->getOpcode () == ISD::UADDO ?
4424 ISD::SETULT : ISD::SETUGT);
Bill Wendling913dcf32008-11-22 00:22:52 +00004425
Bill Wendling4c134df2008-11-24 19:21:46 +00004426 MVT ValueVTs[] = { LHS.getValueType(), OType };
4427 SDValue Ops[] = { Sum, Cmp };
Bill Wendling913dcf32008-11-22 00:22:52 +00004428
Dale Johannesenbbf56a22009-02-02 23:46:53 +00004429 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
4430 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sands42d7bb82008-12-01 11:41:29 +00004431 &Ops[0], 2);
Bill Wendling4c134df2008-11-24 19:21:46 +00004432 SDNode *RNode = Result.getNode();
4433 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4434 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4435 break;
4436 }
4437 }
4438
Bill Wendling913dcf32008-11-22 00:22:52 +00004439 break;
4440 }
Bill Wendling7e04be62008-12-09 22:08:41 +00004441 case ISD::SMULO:
4442 case ISD::UMULO: {
4443 MVT VT = Node->getValueType(0);
4444 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4445 default: assert(0 && "This action is not supported at all!");
4446 case TargetLowering::Custom:
4447 Result = TLI.LowerOperation(Op, DAG);
4448 if (Result.getNode()) break;
4449 // Fall Thru
4450 case TargetLowering::Legal:
4451 // FIXME: According to Hacker's Delight, this can be implemented in
4452 // target independent lowering, but it would be inefficient, since it
Bill Wendling35f1a9d2008-12-10 02:01:32 +00004453 // requires a division + a branch.
Bill Wendling7e04be62008-12-09 22:08:41 +00004454 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4455 break;
4456 }
4457 break;
4458 }
4459
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004460 }
4461
4462 assert(Result.getValueType() == Op.getValueType() &&
4463 "Bad legalization!");
4464
4465 // Make sure that the generated code is itself legal.
4466 if (Result != Op)
4467 Result = LegalizeOp(Result);
4468
4469 // Note that LegalizeOp may be reentered even from single-use nodes, which
4470 // means that we always must cache transformed nodes.
4471 AddLegalizedOperand(Op, Result);
4472 return Result;
4473}
4474
4475/// PromoteOp - Given an operation that produces a value in an invalid type,
4476/// promote it to compute the value into a larger type. The produced value will
4477/// have the correct bits for the low portion of the register, but no guarantee
4478/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +00004479SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00004480 MVT VT = Op.getValueType();
4481 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004482 assert(getTypeAction(VT) == Promote &&
4483 "Caller should expand or legalize operands that are not promotable!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004484 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485 "Cannot promote to smaller type!");
4486
Dan Gohman8181bd12008-07-27 21:46:04 +00004487 SDValue Tmp1, Tmp2, Tmp3;
4488 SDValue Result;
Gabor Greif1c80d112008-08-28 21:40:38 +00004489 SDNode *Node = Op.getNode();
Dale Johannesen82b5b722009-02-02 22:12:50 +00004490 DebugLoc dl = Node->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004491
Dan Gohman8181bd12008-07-27 21:46:04 +00004492 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004493 if (I != PromotedNodes.end()) return I->second;
4494
4495 switch (Node->getOpcode()) {
4496 case ISD::CopyFromReg:
4497 assert(0 && "CopyFromReg must be legal!");
4498 default:
4499#ifndef NDEBUG
4500 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4501#endif
4502 assert(0 && "Do not know how to promote this operator!");
4503 abort();
4504 case ISD::UNDEF:
Dale Johannesen9bfc0172009-02-06 23:05:02 +00004505 Result = DAG.getUNDEF(NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004506 break;
4507 case ISD::Constant:
4508 if (VT != MVT::i1)
Dale Johannesen82b5b722009-02-02 22:12:50 +00004509 Result = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004510 else
Dale Johannesen82b5b722009-02-02 22:12:50 +00004511 Result = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004512 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4513 break;
4514 case ISD::ConstantFP:
Dale Johannesen82b5b722009-02-02 22:12:50 +00004515 Result = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004516 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4517 break;
4518
Duncan Sands4a361272009-01-01 15:52:00 +00004519 case ISD::SETCC: {
4520 MVT VT0 = Node->getOperand(0).getValueType();
4521 assert(isTypeLegal(TLI.getSetCCResultType(VT0))
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004522 && "SetCC type is not legal??");
Dale Johannesen82b5b722009-02-02 22:12:50 +00004523 Result = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(VT0),
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004524 Node->getOperand(0), Node->getOperand(1),
4525 Node->getOperand(2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004526 break;
Duncan Sands4a361272009-01-01 15:52:00 +00004527 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004528 case ISD::TRUNCATE:
4529 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4530 case Legal:
4531 Result = LegalizeOp(Node->getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00004532 assert(Result.getValueType().bitsGE(NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004533 "This truncation doesn't make sense!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004534 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Dale Johannesen82b5b722009-02-02 22:12:50 +00004535 Result = DAG.getNode(ISD::TRUNCATE, dl, NVT, Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004536 break;
4537 case Promote:
4538 // The truncation is not required, because we don't guarantee anything
4539 // about high bits anyway.
4540 Result = PromoteOp(Node->getOperand(0));
4541 break;
4542 case Expand:
4543 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4544 // Truncate the low part of the expanded value to the result type
Dale Johannesen82b5b722009-02-02 22:12:50 +00004545 Result = DAG.getNode(ISD::TRUNCATE, dl, NVT, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004546 }
4547 break;
4548 case ISD::SIGN_EXTEND:
4549 case ISD::ZERO_EXTEND:
4550 case ISD::ANY_EXTEND:
4551 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4552 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4553 case Legal:
4554 // Input is legal? Just do extend all the way to the larger type.
Dale Johannesen82b5b722009-02-02 22:12:50 +00004555 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004556 break;
4557 case Promote:
4558 // Promote the reg if it's smaller.
4559 Result = PromoteOp(Node->getOperand(0));
4560 // The high bits are not guaranteed to be anything. Insert an extend.
4561 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Dale Johannesen82b5b722009-02-02 22:12:50 +00004562 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Result,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004563 DAG.getValueType(Node->getOperand(0).getValueType()));
4564 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Dale Johannesen82b5b722009-02-02 22:12:50 +00004565 Result = DAG.getZeroExtendInReg(Result, dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004566 Node->getOperand(0).getValueType());
4567 break;
4568 }
4569 break;
Mon P Wang73d31542008-11-10 20:54:11 +00004570 case ISD::CONVERT_RNDSAT: {
4571 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4572 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4573 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4574 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4575 "can only promote integers");
Dale Johannesen564036c2009-02-04 00:13:36 +00004576 Result = DAG.getConvertRndSat(NVT, dl, Node->getOperand(0),
Mon P Wang73d31542008-11-10 20:54:11 +00004577 Node->getOperand(1), Node->getOperand(2),
4578 Node->getOperand(3), Node->getOperand(4),
4579 CvtCode);
4580 break;
4581
4582 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004583 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004584 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
Dale Johannesen82b5b722009-02-02 22:12:50 +00004585 Node->getValueType(0), dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004586 Result = PromoteOp(Result);
4587 break;
4588
4589 case ISD::FP_EXTEND:
4590 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4591 case ISD::FP_ROUND:
4592 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4593 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4594 case Promote: assert(0 && "Unreachable with 2 FP types!");
4595 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004596 if (Node->getConstantOperandVal(1) == 0) {
4597 // Input is legal? Do an FP_ROUND_INREG.
Dale Johannesen82b5b722009-02-02 22:12:50 +00004598 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Node->getOperand(0),
Chris Lattner5872a362008-01-17 07:00:52 +00004599 DAG.getValueType(VT));
4600 } else {
4601 // Just remove the truncate, it isn't affecting the value.
Dale Johannesen82b5b722009-02-02 22:12:50 +00004602 Result = DAG.getNode(ISD::FP_ROUND, dl, NVT, Node->getOperand(0),
Chris Lattner5872a362008-01-17 07:00:52 +00004603 Node->getOperand(1));
4604 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004605 break;
4606 }
4607 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004608 case ISD::SINT_TO_FP:
4609 case ISD::UINT_TO_FP:
4610 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4611 case Legal:
4612 // No extra round required here.
Dale Johannesen82b5b722009-02-02 22:12:50 +00004613 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004614 break;
4615
4616 case Promote:
4617 Result = PromoteOp(Node->getOperand(0));
4618 if (Node->getOpcode() == ISD::SINT_TO_FP)
Dale Johannesen82b5b722009-02-02 22:12:50 +00004619 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Result.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004620 Result,
4621 DAG.getValueType(Node->getOperand(0).getValueType()));
4622 else
Dale Johannesen82b5b722009-02-02 22:12:50 +00004623 Result = DAG.getZeroExtendInReg(Result, dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004624 Node->getOperand(0).getValueType());
4625 // No extra round required here.
Dale Johannesen82b5b722009-02-02 22:12:50 +00004626 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004627 break;
4628 case Expand:
4629 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
Dale Johannesen82b5b722009-02-02 22:12:50 +00004630 Node->getOperand(0), dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004631 // Round if we cannot tolerate excess precision.
4632 if (NoExcessFPPrecision)
Dale Johannesen82b5b722009-02-02 22:12:50 +00004633 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004634 DAG.getValueType(VT));
4635 break;
4636 }
4637 break;
4638
4639 case ISD::SIGN_EXTEND_INREG:
4640 Result = PromoteOp(Node->getOperand(0));
Dale Johannesen82b5b722009-02-02 22:12:50 +00004641 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Result,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004642 Node->getOperand(1));
4643 break;
4644 case ISD::FP_TO_SINT:
4645 case ISD::FP_TO_UINT:
4646 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4647 case Legal:
4648 case Expand:
4649 Tmp1 = Node->getOperand(0);
4650 break;
4651 case Promote:
4652 // The input result is prerounded, so we don't have to do anything
4653 // special.
4654 Tmp1 = PromoteOp(Node->getOperand(0));
4655 break;
4656 }
4657 // If we're promoting a UINT to a larger size, check to see if the new node
4658 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4659 // we can use that instead. This allows us to generate better code for
4660 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4661 // legal, such as PowerPC.
4662 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00004663 !TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NVT) &&
4664 (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004665 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Dale Johannesen82b5b722009-02-02 22:12:50 +00004666 Result = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004667 } else {
Dale Johannesen82b5b722009-02-02 22:12:50 +00004668 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004669 }
4670 break;
4671
4672 case ISD::FABS:
4673 case ISD::FNEG:
4674 Tmp1 = PromoteOp(Node->getOperand(0));
4675 assert(Tmp1.getValueType() == NVT);
Dale Johannesen82b5b722009-02-02 22:12:50 +00004676 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004677 // NOTE: we do not have to do any extra rounding here for
4678 // NoExcessFPPrecision, because we know the input will have the appropriate
4679 // precision, and these operations don't modify precision at all.
4680 break;
4681
Dale Johannesen92b33082008-09-04 00:47:13 +00004682 case ISD::FLOG:
4683 case ISD::FLOG2:
4684 case ISD::FLOG10:
4685 case ISD::FEXP:
4686 case ISD::FEXP2:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004687 case ISD::FSQRT:
4688 case ISD::FSIN:
4689 case ISD::FCOS:
Dan Gohmanb2158232008-08-21 18:38:14 +00004690 case ISD::FTRUNC:
4691 case ISD::FFLOOR:
4692 case ISD::FCEIL:
4693 case ISD::FRINT:
4694 case ISD::FNEARBYINT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004695 Tmp1 = PromoteOp(Node->getOperand(0));
4696 assert(Tmp1.getValueType() == NVT);
Dale Johannesen82b5b722009-02-02 22:12:50 +00004697 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004698 if (NoExcessFPPrecision)
Dale Johannesen82b5b722009-02-02 22:12:50 +00004699 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004700 DAG.getValueType(VT));
4701 break;
4702
Evan Cheng1fac6952008-09-09 23:35:53 +00004703 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004704 case ISD::FPOWI: {
Evan Cheng1fac6952008-09-09 23:35:53 +00004705 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004706 // directly as well, which may be better.
4707 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng1fac6952008-09-09 23:35:53 +00004708 Tmp2 = Node->getOperand(1);
4709 if (Node->getOpcode() == ISD::FPOW)
4710 Tmp2 = PromoteOp(Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004711 assert(Tmp1.getValueType() == NVT);
Dale Johannesen82b5b722009-02-02 22:12:50 +00004712 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004713 if (NoExcessFPPrecision)
Dale Johannesen82b5b722009-02-02 22:12:50 +00004714 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004715 DAG.getValueType(VT));
4716 break;
4717 }
4718
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004719 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004720 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004721 Tmp2 = PromoteOp(Node->getOperand(2));
4722 Tmp3 = PromoteOp(Node->getOperand(3));
Dale Johannesen82b5b722009-02-02 22:12:50 +00004723 Result = DAG.getAtomic(Node->getOpcode(), dl, AtomNode->getMemoryVT(),
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004724 AtomNode->getChain(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004725 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004726 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004727 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004728 // Remember that we legalized the chain.
4729 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4730 break;
4731 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004732 case ISD::ATOMIC_LOAD_ADD:
4733 case ISD::ATOMIC_LOAD_SUB:
4734 case ISD::ATOMIC_LOAD_AND:
4735 case ISD::ATOMIC_LOAD_OR:
4736 case ISD::ATOMIC_LOAD_XOR:
4737 case ISD::ATOMIC_LOAD_NAND:
4738 case ISD::ATOMIC_LOAD_MIN:
4739 case ISD::ATOMIC_LOAD_MAX:
4740 case ISD::ATOMIC_LOAD_UMIN:
4741 case ISD::ATOMIC_LOAD_UMAX:
4742 case ISD::ATOMIC_SWAP: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004743 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004744 Tmp2 = PromoteOp(Node->getOperand(2));
Dale Johannesen82b5b722009-02-02 22:12:50 +00004745 Result = DAG.getAtomic(Node->getOpcode(), dl, AtomNode->getMemoryVT(),
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004746 AtomNode->getChain(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004747 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004748 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004749 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004750 // Remember that we legalized the chain.
4751 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4752 break;
4753 }
4754
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004755 case ISD::AND:
4756 case ISD::OR:
4757 case ISD::XOR:
4758 case ISD::ADD:
4759 case ISD::SUB:
4760 case ISD::MUL:
4761 // The input may have strange things in the top bits of the registers, but
4762 // these operations don't care. They may have weird bits going out, but
4763 // that too is okay if they are integer operations.
4764 Tmp1 = PromoteOp(Node->getOperand(0));
4765 Tmp2 = PromoteOp(Node->getOperand(1));
4766 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
Dale Johannesen82b5b722009-02-02 22:12:50 +00004767 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004768 break;
4769 case ISD::FADD:
4770 case ISD::FSUB:
4771 case ISD::FMUL:
4772 Tmp1 = PromoteOp(Node->getOperand(0));
4773 Tmp2 = PromoteOp(Node->getOperand(1));
4774 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
Dale Johannesen82b5b722009-02-02 22:12:50 +00004775 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004776
4777 // Floating point operations will give excess precision that we may not be
4778 // able to tolerate. If we DO allow excess precision, just leave it,
4779 // otherwise excise it.
4780 // FIXME: Why would we need to round FP ops more than integer ones?
4781 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4782 if (NoExcessFPPrecision)
Dale Johannesen82b5b722009-02-02 22:12:50 +00004783 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004784 DAG.getValueType(VT));
4785 break;
4786
4787 case ISD::SDIV:
4788 case ISD::SREM:
4789 // These operators require that their input be sign extended.
4790 Tmp1 = PromoteOp(Node->getOperand(0));
4791 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004792 if (NVT.isInteger()) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00004793 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004794 DAG.getValueType(VT));
Dale Johannesen82b5b722009-02-02 22:12:50 +00004795 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004796 DAG.getValueType(VT));
4797 }
Dale Johannesen82b5b722009-02-02 22:12:50 +00004798 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004799
4800 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands92c43912008-06-06 12:08:01 +00004801 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Dale Johannesen82b5b722009-02-02 22:12:50 +00004802 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004803 DAG.getValueType(VT));
4804 break;
4805 case ISD::FDIV:
4806 case ISD::FREM:
4807 case ISD::FCOPYSIGN:
4808 // These operators require that their input be fp extended.
4809 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004810 case Expand: assert(0 && "not implemented");
4811 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4812 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004813 }
4814 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004815 case Expand: assert(0 && "not implemented");
4816 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4817 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004818 }
Dale Johannesen82b5b722009-02-02 22:12:50 +00004819 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004820
4821 // Perform FP_ROUND: this is probably overly pessimistic.
4822 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Dale Johannesen82b5b722009-02-02 22:12:50 +00004823 Result = DAG.getNode(ISD::FP_ROUND_INREG, dl, NVT, Result,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004824 DAG.getValueType(VT));
4825 break;
4826
4827 case ISD::UDIV:
4828 case ISD::UREM:
4829 // These operators require that their input be zero extended.
4830 Tmp1 = PromoteOp(Node->getOperand(0));
4831 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004832 assert(NVT.isInteger() && "Operators don't apply to FP!");
Dale Johannesen82b5b722009-02-02 22:12:50 +00004833 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
4834 Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, VT);
4835 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004836 break;
4837
4838 case ISD::SHL:
4839 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesen82b5b722009-02-02 22:12:50 +00004840 Result = DAG.getNode(ISD::SHL, dl, NVT, Tmp1, Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004841 break;
4842 case ISD::SRA:
4843 // The input value must be properly sign extended.
4844 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesen82b5b722009-02-02 22:12:50 +00004845 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004846 DAG.getValueType(VT));
Dale Johannesen82b5b722009-02-02 22:12:50 +00004847 Result = DAG.getNode(ISD::SRA, dl, NVT, Tmp1, Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004848 break;
4849 case ISD::SRL:
4850 // The input value must be properly zero extended.
4851 Tmp1 = PromoteOp(Node->getOperand(0));
Dale Johannesen82b5b722009-02-02 22:12:50 +00004852 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
4853 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1, Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004854 break;
4855
4856 case ISD::VAARG:
4857 Tmp1 = Node->getOperand(0); // Get the chain.
4858 Tmp2 = Node->getOperand(1); // Get the pointer.
4859 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
Dale Johannesen564036c2009-02-04 00:13:36 +00004860 Tmp3 = DAG.getVAArg(VT, dl, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sandsac496a12008-07-04 11:47:58 +00004861 Result = TLI.LowerOperation(Tmp3, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004862 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004863 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesen564036c2009-02-04 00:13:36 +00004864 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004865 // Increment the pointer, VAList, to the next vaarg
Dale Johannesen82b5b722009-02-02 22:12:50 +00004866 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sands92c43912008-06-06 12:08:01 +00004867 DAG.getConstant(VT.getSizeInBits()/8,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004868 TLI.getPointerTy()));
4869 // Store the incremented VAList to the legalized pointer
Dale Johannesen82b5b722009-02-02 22:12:50 +00004870 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004871 // Load the actual argument out of the pointer VAList
Dale Johannesen82b5b722009-02-02 22:12:50 +00004872 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Tmp3, VAList, NULL, 0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004873 }
4874 // Remember that we legalized the chain.
4875 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4876 break;
4877
4878 case ISD::LOAD: {
4879 LoadSDNode *LD = cast<LoadSDNode>(Node);
4880 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4881 ? ISD::EXTLOAD : LD->getExtensionType();
Dale Johannesen82b5b722009-02-02 22:12:50 +00004882 Result = DAG.getExtLoad(ExtType, dl, NVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004883 LD->getChain(), LD->getBasePtr(),
4884 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004885 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004886 LD->isVolatile(),
4887 LD->getAlignment());
4888 // Remember that we legalized the chain.
4889 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4890 break;
4891 }
Scott Michel67224b22008-06-02 22:18:03 +00004892 case ISD::SELECT: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004893 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4894 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel67224b22008-06-02 22:18:03 +00004895
Duncan Sands92c43912008-06-06 12:08:01 +00004896 MVT VT2 = Tmp2.getValueType();
Scott Michel67224b22008-06-02 22:18:03 +00004897 assert(VT2 == Tmp3.getValueType()
Scott Michel7b54de02008-06-03 19:13:20 +00004898 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4899 // Ensure that the resulting node is at least the same size as the operands'
4900 // value types, because we cannot assume that TLI.getSetCCValueType() is
4901 // constant.
Dale Johannesen82b5b722009-02-02 22:12:50 +00004902 Result = DAG.getNode(ISD::SELECT, dl, VT2, Node->getOperand(0), Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004903 break;
Scott Michel67224b22008-06-02 22:18:03 +00004904 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004905 case ISD::SELECT_CC:
4906 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4907 Tmp3 = PromoteOp(Node->getOperand(3)); // False
Dale Johannesen82b5b722009-02-02 22:12:50 +00004908 Result = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004909 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4910 break;
4911 case ISD::BSWAP:
4912 Tmp1 = Node->getOperand(0);
Dale Johannesen82b5b722009-02-02 22:12:50 +00004913 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
4914 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
4915 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004916 DAG.getConstant(NVT.getSizeInBits() -
4917 VT.getSizeInBits(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004918 TLI.getShiftAmountTy()));
4919 break;
4920 case ISD::CTPOP:
4921 case ISD::CTTZ:
4922 case ISD::CTLZ:
4923 // Zero extend the argument
Dale Johannesen82b5b722009-02-02 22:12:50 +00004924 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004925 // Perform the larger operation, then subtract if needed.
Dale Johannesen82b5b722009-02-02 22:12:50 +00004926 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004927 switch(Node->getOpcode()) {
4928 case ISD::CTPOP:
4929 Result = Tmp1;
4930 break;
4931 case ISD::CTTZ:
4932 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Dale Johannesen82b5b722009-02-02 22:12:50 +00004933 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004934 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004935 ISD::SETEQ);
Dale Johannesen82b5b722009-02-02 22:12:50 +00004936 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00004937 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004938 break;
4939 case ISD::CTLZ:
4940 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Dale Johannesen82b5b722009-02-02 22:12:50 +00004941 Result = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004942 DAG.getConstant(NVT.getSizeInBits() -
4943 VT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004944 break;
4945 }
4946 break;
4947 case ISD::EXTRACT_SUBVECTOR:
4948 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4949 break;
4950 case ISD::EXTRACT_VECTOR_ELT:
4951 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4952 break;
4953 }
4954
Gabor Greif1c80d112008-08-28 21:40:38 +00004955 assert(Result.getNode() && "Didn't set a result!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004956
4957 // Make sure the result is itself legal.
4958 Result = LegalizeOp(Result);
4959
4960 // Remember that we promoted this!
4961 AddPromotedOperand(Op, Result);
4962 return Result;
4963}
4964
4965/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4966/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4967/// based on the vector type. The return type of this matches the element type
4968/// of the vector, which may not be legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00004969SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004970 // We know that operand #0 is the Vec vector. If the index is a constant
4971 // or if the invec is a supported hardware type, we can use it. Otherwise,
4972 // lower to a store then an indexed load.
Dan Gohman8181bd12008-07-27 21:46:04 +00004973 SDValue Vec = Op.getOperand(0);
4974 SDValue Idx = Op.getOperand(1);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004975 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004976
Duncan Sands92c43912008-06-06 12:08:01 +00004977 MVT TVT = Vec.getValueType();
4978 unsigned NumElems = TVT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004979
4980 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4981 default: assert(0 && "This action is not supported yet!");
4982 case TargetLowering::Custom: {
4983 Vec = LegalizeOp(Vec);
4984 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004985 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004986 if (Tmp3.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004987 return Tmp3;
4988 break;
4989 }
4990 case TargetLowering::Legal:
4991 if (isTypeLegal(TVT)) {
4992 Vec = LegalizeOp(Vec);
4993 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004994 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004995 }
4996 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00004997 case TargetLowering::Promote:
4998 assert(TVT.isVector() && "not vector type");
4999 // fall thru to expand since vectors are by default are promote
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005000 case TargetLowering::Expand:
5001 break;
5002 }
5003
5004 if (NumElems == 1) {
5005 // This must be an access of the only element. Return it.
5006 Op = ScalarizeVectorOp(Vec);
5007 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00005008 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005009 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00005010 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005011 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005012 if (CIdx->getZExtValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005013 Vec = Lo;
5014 } else {
5015 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005016 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005017 Idx.getValueType());
5018 }
5019
5020 // It's now an extract from the appropriate high or low part. Recurse.
5021 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
5022 Op = ExpandEXTRACT_VECTOR_ELT(Op);
5023 } else {
5024 // Store the value to a temporary stack slot, then LOAD the scalar
5025 // element back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00005026 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dale Johannesen352c47e2009-02-02 20:41:04 +00005027 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005028
5029 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +00005030 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dale Johannesen352c47e2009-02-02 20:41:04 +00005031 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005032 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00005033
Duncan Sandsec142ee2008-06-08 20:54:56 +00005034 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Dale Johannesen352c47e2009-02-02 20:41:04 +00005035 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00005036 else
Dale Johannesen352c47e2009-02-02 20:41:04 +00005037 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00005038
Dale Johannesen352c47e2009-02-02 20:41:04 +00005039 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005040
Dale Johannesen352c47e2009-02-02 20:41:04 +00005041 Op = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005042 }
5043 return Op;
5044}
5045
5046/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
5047/// we assume the operation can be split if it is not already legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00005048SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005049 // We know that operand #0 is the Vec vector. For now we assume the index
5050 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman8181bd12008-07-27 21:46:04 +00005051 SDValue Vec = Op.getOperand(0);
5052 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005053
Duncan Sands92c43912008-06-06 12:08:01 +00005054 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005055
Duncan Sands92c43912008-06-06 12:08:01 +00005056 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005057 // This must be an access of the desired vector length. Return it.
5058 return Vec;
5059 }
5060
5061 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00005062 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005063 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005064 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005065 Vec = Lo;
5066 } else {
5067 Vec = Hi;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005068 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
5069 Idx.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005070 }
5071
5072 // It's now an extract from the appropriate high or low part. Recurse.
5073 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
5074 return ExpandEXTRACT_SUBVECTOR(Op);
5075}
5076
5077/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
5078/// with condition CC on the current target. This usually involves legalizing
5079/// or promoting the arguments. In the case where LHS and RHS must be expanded,
5080/// there may be no choice but to create a new SetCC node to represent the
5081/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman8181bd12008-07-27 21:46:04 +00005082/// LHS, and the SDValue returned in RHS has a nil SDNode value.
5083void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
5084 SDValue &RHS,
Dale Johannesen352c47e2009-02-02 20:41:04 +00005085 SDValue &CC,
5086 DebugLoc dl) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005087 SDValue Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005088
5089 switch (getTypeAction(LHS.getValueType())) {
5090 case Legal:
5091 Tmp1 = LegalizeOp(LHS); // LHS
5092 Tmp2 = LegalizeOp(RHS); // RHS
5093 break;
5094 case Promote:
5095 Tmp1 = PromoteOp(LHS); // LHS
5096 Tmp2 = PromoteOp(RHS); // RHS
5097
5098 // If this is an FP compare, the operands have already been extended.
Duncan Sands92c43912008-06-06 12:08:01 +00005099 if (LHS.getValueType().isInteger()) {
5100 MVT VT = LHS.getValueType();
5101 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005102
5103 // Otherwise, we have to insert explicit sign or zero extends. Note
5104 // that we could insert sign extends for ALL conditions, but zero extend
5105 // is cheaper on many machines (an AND instead of two shifts), so prefer
5106 // it.
5107 switch (cast<CondCodeSDNode>(CC)->get()) {
5108 default: assert(0 && "Unknown integer comparison!");
5109 case ISD::SETEQ:
5110 case ISD::SETNE:
5111 case ISD::SETUGE:
5112 case ISD::SETUGT:
5113 case ISD::SETULE:
5114 case ISD::SETULT:
5115 // ALL of these operations will work if we either sign or zero extend
5116 // the operands (including the unsigned comparisons!). Zero extend is
5117 // usually a simpler/cheaper operation, so prefer it.
Dale Johannesen352c47e2009-02-02 20:41:04 +00005118 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl, VT);
5119 Tmp2 = DAG.getZeroExtendInReg(Tmp2, dl, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005120 break;
5121 case ISD::SETGE:
5122 case ISD::SETGT:
5123 case ISD::SETLT:
5124 case ISD::SETLE:
Dale Johannesen352c47e2009-02-02 20:41:04 +00005125 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005126 DAG.getValueType(VT));
Dale Johannesen352c47e2009-02-02 20:41:04 +00005127 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005128 DAG.getValueType(VT));
Evan Chengd901b662008-10-13 18:46:18 +00005129 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5130 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005131 break;
5132 }
5133 }
5134 break;
5135 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00005136 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005137 if (VT == MVT::f32 || VT == MVT::f64) {
5138 // Expand into one or more soft-fp libcall(s).
Evan Cheng24108632008-07-01 21:35:46 +00005139 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005140 switch (cast<CondCodeSDNode>(CC)->get()) {
5141 case ISD::SETEQ:
5142 case ISD::SETOEQ:
5143 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5144 break;
5145 case ISD::SETNE:
5146 case ISD::SETUNE:
5147 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
5148 break;
5149 case ISD::SETGE:
5150 case ISD::SETOGE:
5151 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5152 break;
5153 case ISD::SETLT:
5154 case ISD::SETOLT:
5155 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5156 break;
5157 case ISD::SETLE:
5158 case ISD::SETOLE:
5159 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5160 break;
5161 case ISD::SETGT:
5162 case ISD::SETOGT:
5163 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
5164 break;
5165 case ISD::SETUO:
5166 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5167 break;
5168 case ISD::SETO:
5169 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
5170 break;
5171 default:
5172 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5173 switch (cast<CondCodeSDNode>(CC)->get()) {
5174 case ISD::SETONE:
5175 // SETONE = SETOLT | SETOGT
5176 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5177 // Fallthrough
5178 case ISD::SETUGT:
5179 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
5180 break;
5181 case ISD::SETUGE:
5182 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
5183 break;
5184 case ISD::SETULT:
5185 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
5186 break;
5187 case ISD::SETULE:
5188 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
5189 break;
5190 case ISD::SETUEQ:
5191 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
5192 break;
5193 default: assert(0 && "Unsupported FP setcc!");
5194 }
5195 }
Duncan Sandsf19591c2008-06-30 10:19:09 +00005196
Dan Gohman8181bd12008-07-27 21:46:04 +00005197 SDValue Dummy;
5198 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen352c47e2009-02-02 20:41:04 +00005199 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2, dl).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005200 false /*sign irrelevant*/, Dummy);
5201 Tmp2 = DAG.getConstant(0, MVT::i32);
5202 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
5203 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Dale Johannesen352c47e2009-02-02 20:41:04 +00005204 Tmp1 = DAG.getNode(ISD::SETCC, dl,
Duncan Sands4a361272009-01-01 15:52:00 +00005205 TLI.getSetCCResultType(Tmp1.getValueType()),
5206 Tmp1, Tmp2, CC);
Dale Johannesen352c47e2009-02-02 20:41:04 +00005207 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2, dl).getNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005208 false /*sign irrelevant*/, Dummy);
Dale Johannesen352c47e2009-02-02 20:41:04 +00005209 Tmp2 = DAG.getNode(ISD::SETCC, dl,
Duncan Sands4a361272009-01-01 15:52:00 +00005210 TLI.getSetCCResultType(LHS.getValueType()), LHS,
5211 Tmp2, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Dale Johannesen352c47e2009-02-02 20:41:04 +00005212 Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005213 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005214 }
Evan Cheng18a1ab12008-07-07 07:18:09 +00005215 LHS = LegalizeOp(Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005216 RHS = Tmp2;
5217 return;
5218 }
5219
Dan Gohman8181bd12008-07-27 21:46:04 +00005220 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005221 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00005222 ExpandOp(RHS, RHSLo, RHSHi);
5223 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5224
5225 if (VT==MVT::ppcf128) {
5226 // FIXME: This generated code sucks. We want to generate
Dale Johannesen26317b62008-09-12 00:30:56 +00005227 // FCMPU crN, hi1, hi2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005228 // BNE crN, L:
Dale Johannesen26317b62008-09-12 00:30:56 +00005229 // FCMPU crN, lo1, lo2
Dale Johannesen472d15d2007-10-06 01:24:11 +00005230 // The following can be improved, but not that much.
Dale Johannesen352c47e2009-02-02 20:41:04 +00005231 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands4a361272009-01-01 15:52:00 +00005232 LHSHi, RHSHi, ISD::SETOEQ);
Dale Johannesen352c47e2009-02-02 20:41:04 +00005233 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
Duncan Sands4a361272009-01-01 15:52:00 +00005234 LHSLo, RHSLo, CCCode);
Dale Johannesen352c47e2009-02-02 20:41:04 +00005235 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
5236 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands4a361272009-01-01 15:52:00 +00005237 LHSHi, RHSHi, ISD::SETUNE);
Dale Johannesen352c47e2009-02-02 20:41:04 +00005238 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands4a361272009-01-01 15:52:00 +00005239 LHSHi, RHSHi, CCCode);
Dale Johannesen352c47e2009-02-02 20:41:04 +00005240 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
5241 Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00005242 Tmp2 = SDValue();
Dale Johannesen472d15d2007-10-06 01:24:11 +00005243 break;
5244 }
5245
5246 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005247 case ISD::SETEQ:
5248 case ISD::SETNE:
5249 if (RHSLo == RHSHi)
5250 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5251 if (RHSCST->isAllOnesValue()) {
5252 // Comparison to -1.
Dale Johannesen352c47e2009-02-02 20:41:04 +00005253 Tmp1 = DAG.getNode(ISD::AND, dl,LHSLo.getValueType(), LHSLo, LHSHi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005254 Tmp2 = RHSLo;
5255 break;
5256 }
5257
Dale Johannesen352c47e2009-02-02 20:41:04 +00005258 Tmp1 = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
5259 Tmp2 = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
5260 Tmp1 = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005261 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5262 break;
5263 default:
5264 // If this is a comparison of the sign bit, just look at the top part.
5265 // X > -1, x < 0
5266 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5267 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00005268 CST->isNullValue()) || // X < 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005269 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5270 CST->isAllOnesValue())) { // X > -1
5271 Tmp1 = LHSHi;
5272 Tmp2 = RHSHi;
5273 break;
5274 }
5275
5276 // FIXME: This generated code sucks.
5277 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005278 switch (CCCode) {
5279 default: assert(0 && "Unknown integer setcc!");
5280 case ISD::SETLT:
5281 case ISD::SETULT: LowCC = ISD::SETULT; break;
5282 case ISD::SETGT:
5283 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5284 case ISD::SETLE:
5285 case ISD::SETULE: LowCC = ISD::SETULE; break;
5286 case ISD::SETGE:
5287 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5288 }
5289
5290 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5291 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5292 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5293
5294 // NOTE: on targets without efficient SELECT of bools, we can always use
5295 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
5296 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Duncan Sands4a361272009-01-01 15:52:00 +00005297 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
Dale Johannesen38496eb2009-02-03 00:47:48 +00005298 LHSLo, RHSLo, LowCC, false, DagCombineInfo, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00005299 if (!Tmp1.getNode())
Dale Johannesen352c47e2009-02-02 20:41:04 +00005300 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
Duncan Sands4a361272009-01-01 15:52:00 +00005301 LHSLo, RHSLo, LowCC);
5302 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
Dale Johannesen38496eb2009-02-03 00:47:48 +00005303 LHSHi, RHSHi, CCCode, false, DagCombineInfo, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00005304 if (!Tmp2.getNode())
Dale Johannesen352c47e2009-02-02 20:41:04 +00005305 Tmp2 = DAG.getNode(ISD::SETCC, dl,
Duncan Sands4a361272009-01-01 15:52:00 +00005306 TLI.getSetCCResultType(LHSHi.getValueType()),
5307 LHSHi, RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005308
Gabor Greif1c80d112008-08-28 21:40:38 +00005309 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5310 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman9d24dc72008-03-13 22:13:53 +00005311 if ((Tmp1C && Tmp1C->isNullValue()) ||
5312 (Tmp2C && Tmp2C->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005313 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5314 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman9d24dc72008-03-13 22:13:53 +00005315 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005316 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5317 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5318 // low part is known false, returns high part.
5319 // For LE / GE, if high part is known false, ignore the low part.
5320 // For LT / GT, if high part is known true, ignore the low part.
5321 Tmp1 = Tmp2;
Dan Gohman8181bd12008-07-27 21:46:04 +00005322 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005323 } else {
Duncan Sands4a361272009-01-01 15:52:00 +00005324 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5325 LHSHi, RHSHi, ISD::SETEQ, false,
Dale Johannesen38496eb2009-02-03 00:47:48 +00005326 DagCombineInfo, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00005327 if (!Result.getNode())
Dale Johannesen352c47e2009-02-02 20:41:04 +00005328 Result=DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
Duncan Sands4a361272009-01-01 15:52:00 +00005329 LHSHi, RHSHi, ISD::SETEQ);
Dale Johannesen352c47e2009-02-02 20:41:04 +00005330 Result = LegalizeOp(DAG.getNode(ISD::SELECT, dl, Tmp1.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005331 Result, Tmp1, Tmp2));
5332 Tmp1 = Result;
Dan Gohman8181bd12008-07-27 21:46:04 +00005333 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005334 }
5335 }
5336 }
5337 }
5338 LHS = Tmp1;
5339 RHS = Tmp2;
5340}
5341
Evan Cheng71343822008-10-15 02:05:31 +00005342/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5343/// condition code CC on the current target. This routine assumes LHS and rHS
5344/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5345/// illegal condition code into AND / OR of multiple SETCC values.
5346void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5347 SDValue &LHS, SDValue &RHS,
Dale Johannesen352c47e2009-02-02 20:41:04 +00005348 SDValue &CC,
5349 DebugLoc dl) {
Evan Cheng71343822008-10-15 02:05:31 +00005350 MVT OpVT = LHS.getValueType();
5351 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5352 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5353 default: assert(0 && "Unknown condition code action!");
5354 case TargetLowering::Legal:
5355 // Nothing to do.
5356 break;
5357 case TargetLowering::Expand: {
5358 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5359 unsigned Opc = 0;
5360 switch (CCCode) {
5361 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohman2b5b9ca2008-10-21 03:12:54 +00005362 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5363 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5364 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5365 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5366 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5367 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5368 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5369 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5370 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5371 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5372 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5373 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng71343822008-10-15 02:05:31 +00005374 // FIXME: Implement more expansions.
5375 }
5376
Dale Johannesen352c47e2009-02-02 20:41:04 +00005377 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
5378 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
5379 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng71343822008-10-15 02:05:31 +00005380 RHS = SDValue();
5381 CC = SDValue();
5382 break;
5383 }
5384 }
5385}
5386
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005387/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5388/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5389/// a load from the stack slot to DestVT, extending it if needed.
5390/// The resultant code need not be legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00005391SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5392 MVT SlotVT,
Dale Johannesen82b5b722009-02-02 22:12:50 +00005393 MVT DestVT,
5394 DebugLoc dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005395 // Create the stack frame object.
Mon P Wang55854cc2008-07-05 20:40:31 +00005396 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5397 SrcOp.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00005398 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang55854cc2008-07-05 20:40:31 +00005399
Dan Gohman20e37962008-02-11 18:58:42 +00005400 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005401 int SPFI = StackPtrFI->getIndex();
Dan Gohman8a8251a2009-01-29 21:02:43 +00005402 const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
5403
Duncan Sands92c43912008-06-06 12:08:01 +00005404 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5405 unsigned SlotSize = SlotVT.getSizeInBits();
5406 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang55854cc2008-07-05 20:40:31 +00005407 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5408 DestVT.getTypeForMVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005409
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005410 // Emit a store to the stack slot. Use a truncstore if the input value is
5411 // later than DestVT.
Dan Gohman8181bd12008-07-27 21:46:04 +00005412 SDValue Store;
Mon P Wang55854cc2008-07-05 20:40:31 +00005413
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005414 if (SrcSize > SlotSize)
Dale Johannesen82b5b722009-02-02 22:12:50 +00005415 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman8a8251a2009-01-29 21:02:43 +00005416 SV, 0, SlotVT, false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005417 else {
5418 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen82b5b722009-02-02 22:12:50 +00005419 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman8a8251a2009-01-29 21:02:43 +00005420 SV, 0, false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005421 }
5422
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005423 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005424 if (SlotSize == DestSize)
Dale Johannesen82b5b722009-02-02 22:12:50 +00005425 return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, DestAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005426
5427 assert(SlotSize < DestSize && "Unknown extension!");
Dale Johannesen82b5b722009-02-02 22:12:50 +00005428 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT,
Mon P Wang55854cc2008-07-05 20:40:31 +00005429 false, DestAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005430}
5431
Dan Gohman8181bd12008-07-27 21:46:04 +00005432SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00005433 DebugLoc dl = Node->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005434 // Create a vector sized/aligned stack slot, store the value to element #0,
5435 // then load the whole vector back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00005436 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman12a9c082008-02-06 22:27:42 +00005437
Dan Gohman20e37962008-02-11 18:58:42 +00005438 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00005439 int SPFI = StackPtrFI->getIndex();
5440
Dale Johannesen82b5b722009-02-02 22:12:50 +00005441 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(0),
5442 StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005443 PseudoSourceValue::getFixedStack(SPFI), 0);
Dale Johannesen82b5b722009-02-02 22:12:50 +00005444 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005445 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005446}
5447
5448
5449/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
5450/// support the operation, but do support the resultant vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00005451SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005452
5453 // If the only non-undef value is the low element, turn this into a
5454 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
5455 unsigned NumElems = Node->getNumOperands();
5456 bool isOnlyLowElement = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005457 SDValue SplatValue = Node->getOperand(0);
Dale Johannesen82b5b722009-02-02 22:12:50 +00005458 DebugLoc dl = Node->getDebugLoc();
Chris Lattnerd8cee732008-03-09 00:29:42 +00005459
Dan Gohman8181bd12008-07-27 21:46:04 +00005460 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerd8cee732008-03-09 00:29:42 +00005461 // and use a bitmask instead of a list of elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00005462 std::map<SDValue, std::vector<unsigned> > Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005463 Values[SplatValue].push_back(0);
5464 bool isConstant = true;
5465 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5466 SplatValue.getOpcode() != ISD::UNDEF)
5467 isConstant = false;
5468
5469 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005470 SDValue V = Node->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005471 Values[V].push_back(i);
5472 if (V.getOpcode() != ISD::UNDEF)
5473 isOnlyLowElement = false;
5474 if (SplatValue != V)
Dan Gohman8181bd12008-07-27 21:46:04 +00005475 SplatValue = SDValue(0,0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005476
5477 // If this isn't a constant element or an undef, we can't use a constant
5478 // pool load.
5479 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5480 V.getOpcode() != ISD::UNDEF)
5481 isConstant = false;
5482 }
5483
5484 if (isOnlyLowElement) {
5485 // If the low element is an undef too, then this whole things is an undef.
5486 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00005487 return DAG.getUNDEF(Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005488 // Otherwise, turn this into a scalar_to_vector node.
Dale Johannesen82b5b722009-02-02 22:12:50 +00005489 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, Node->getValueType(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005490 Node->getOperand(0));
5491 }
5492
5493 // If all elements are constants, create a load from the constant pool.
5494 if (isConstant) {
Duncan Sands92c43912008-06-06 12:08:01 +00005495 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005496 std::vector<Constant*> CV;
5497 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5498 if (ConstantFPSDNode *V =
5499 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005500 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005501 } else if (ConstantSDNode *V =
Chris Lattner5e0610f2008-04-20 00:41:09 +00005502 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00005503 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005504 } else {
5505 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner5e0610f2008-04-20 00:41:09 +00005506 const Type *OpNTy =
Duncan Sands92c43912008-06-06 12:08:01 +00005507 Node->getOperand(0).getValueType().getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005508 CV.push_back(UndefValue::get(OpNTy));
5509 }
5510 }
5511 Constant *CP = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005512 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005513 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen82b5b722009-02-02 22:12:50 +00005514 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005515 PseudoSourceValue::getConstantPool(), 0,
5516 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005517 }
5518
Gabor Greif1c80d112008-08-28 21:40:38 +00005519 if (SplatValue.getNode()) { // Splat of one value?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005520 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands92c43912008-06-06 12:08:01 +00005521 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman8181bd12008-07-27 21:46:04 +00005522 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5523 std::vector<SDValue> ZeroVec(NumElems, Zero);
Dale Johannesen82b5b722009-02-02 22:12:50 +00005524 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005525 &ZeroVec[0], ZeroVec.size());
5526
5527 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
5528 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
5529 // Get the splatted value into the low element of a vector register.
Dan Gohman8181bd12008-07-27 21:46:04 +00005530 SDValue LowValVec =
Dale Johannesen82b5b722009-02-02 22:12:50 +00005531 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5532 Node->getValueType(0), SplatValue);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005533
5534 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Dale Johannesen82b5b722009-02-02 22:12:50 +00005535 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl,
5536 Node->getValueType(0), LowValVec,
Dale Johannesen9bfc0172009-02-06 23:05:02 +00005537 DAG.getUNDEF(Node->getValueType(0)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005538 SplatMask);
5539 }
5540 }
5541
5542 // If there are only two unique elements, we may be able to turn this into a
5543 // vector shuffle.
5544 if (Values.size() == 2) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00005545 // Get the two values in deterministic order.
Dan Gohman8181bd12008-07-27 21:46:04 +00005546 SDValue Val1 = Node->getOperand(1);
5547 SDValue Val2;
5548 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerd8cee732008-03-09 00:29:42 +00005549 if (MI->first != Val1)
5550 Val2 = MI->first;
5551 else
5552 Val2 = (++MI)->first;
5553
5554 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5555 // vector shuffle has the undef vector on the RHS.
5556 if (Val1.getOpcode() == ISD::UNDEF)
5557 std::swap(Val1, Val2);
5558
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005559 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands92c43912008-06-06 12:08:01 +00005560 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5561 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005562 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005563
5564 // Set elements of the shuffle mask for Val1.
5565 std::vector<unsigned> &Val1Elts = Values[Val1];
5566 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5567 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5568
5569 // Set elements of the shuffle mask for Val2.
5570 std::vector<unsigned> &Val2Elts = Values[Val2];
5571 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5572 if (Val2.getOpcode() != ISD::UNDEF)
5573 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5574 else
Dale Johannesen9bfc0172009-02-06 23:05:02 +00005575 MaskVec[Val2Elts[i]] = DAG.getUNDEF(MaskEltVT);
Chris Lattnerd8cee732008-03-09 00:29:42 +00005576
Dale Johannesen82b5b722009-02-02 22:12:50 +00005577 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005578 &MaskVec[0], MaskVec.size());
5579
Chris Lattnerd8cee732008-03-09 00:29:42 +00005580 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohman52c51aa2009-01-28 17:46:25 +00005581 if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR,
5582 Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005583 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00005584 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,Node->getValueType(0), Val1);
5585 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,Node->getValueType(0), Val2);
Dan Gohman8181bd12008-07-27 21:46:04 +00005586 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005587
5588 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Dale Johannesen82b5b722009-02-02 22:12:50 +00005589 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl,Node->getValueType(0), Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005590 }
5591 }
5592
5593 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5594 // aligned object on the stack, store each element into it, then load
5595 // the result as a vector.
Duncan Sands92c43912008-06-06 12:08:01 +00005596 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005597 // Create the stack frame object.
Dan Gohman8181bd12008-07-27 21:46:04 +00005598 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohman8a8251a2009-01-29 21:02:43 +00005599 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
5600 const Value *SV = PseudoSourceValue::getFixedStack(FI);
5601
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005602 // Emit a store of each element to the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00005603 SmallVector<SDValue, 8> Stores;
Duncan Sands92c43912008-06-06 12:08:01 +00005604 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005605 // Store (in the right endianness) the elements to memory.
5606 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5607 // Ignore undef elements.
5608 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5609
5610 unsigned Offset = TypeByteSize*i;
5611
Dan Gohman8181bd12008-07-27 21:46:04 +00005612 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dale Johannesen82b5b722009-02-02 22:12:50 +00005613 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005614
Dale Johannesen82b5b722009-02-02 22:12:50 +00005615 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
5616 Idx, SV, Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005617 }
5618
Dan Gohman8181bd12008-07-27 21:46:04 +00005619 SDValue StoreChain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005620 if (!Stores.empty()) // Not all undef elements?
Dale Johannesen82b5b722009-02-02 22:12:50 +00005621 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005622 &Stores[0], Stores.size());
5623 else
5624 StoreChain = DAG.getEntryNode();
5625
5626 // Result is a load from the stack slot.
Dale Johannesen82b5b722009-02-02 22:12:50 +00005627 return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005628}
5629
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005630void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman8181bd12008-07-27 21:46:04 +00005631 SDValue Op, SDValue Amt,
Dale Johannesen82b5b722009-02-02 22:12:50 +00005632 SDValue &Lo, SDValue &Hi,
5633 DebugLoc dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005634 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00005635 SDValue LHSL, LHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005636 ExpandOp(Op, LHSL, LHSH);
5637
Dan Gohman8181bd12008-07-27 21:46:04 +00005638 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands92c43912008-06-06 12:08:01 +00005639 MVT VT = LHSL.getValueType();
Dale Johannesen82b5b722009-02-02 22:12:50 +00005640 Lo = DAG.getNode(NodeOp, dl, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005641 Hi = Lo.getValue(1);
5642}
5643
5644
5645/// ExpandShift - Try to find a clever way to expand this shift operation out to
5646/// smaller elements. If we can't find a way that is more efficient than a
5647/// libcall on this target, return false. Otherwise, return true with the
5648/// low-parts expanded into Lo and Hi.
Dan Gohman8181bd12008-07-27 21:46:04 +00005649bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
Dale Johannesen82b5b722009-02-02 22:12:50 +00005650 SDValue &Lo, SDValue &Hi,
5651 DebugLoc dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005652 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5653 "This is not a shift!");
5654
Duncan Sands92c43912008-06-06 12:08:01 +00005655 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman8181bd12008-07-27 21:46:04 +00005656 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands92c43912008-06-06 12:08:01 +00005657 MVT ShTy = ShAmt.getValueType();
5658 unsigned ShBits = ShTy.getSizeInBits();
5659 unsigned VTBits = Op.getValueType().getSizeInBits();
5660 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005661
Chris Lattner8c931452007-10-14 20:35:12 +00005662 // Handle the case when Amt is an immediate.
Gabor Greif1c80d112008-08-28 21:40:38 +00005663 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005664 unsigned Cst = CN->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005665 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005666 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005667 ExpandOp(Op, InL, InH);
5668 switch(Opc) {
5669 case ISD::SHL:
5670 if (Cst > VTBits) {
5671 Lo = DAG.getConstant(0, NVT);
5672 Hi = DAG.getConstant(0, NVT);
5673 } else if (Cst > NVTBits) {
5674 Lo = DAG.getConstant(0, NVT);
Dale Johannesen82b5b722009-02-02 22:12:50 +00005675 Hi = DAG.getNode(ISD::SHL, dl,
5676 NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005677 } else if (Cst == NVTBits) {
5678 Lo = DAG.getConstant(0, NVT);
5679 Hi = InL;
5680 } else {
Dale Johannesen82b5b722009-02-02 22:12:50 +00005681 Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, DAG.getConstant(Cst, ShTy));
5682 Hi = DAG.getNode(ISD::OR, dl, NVT,
5683 DAG.getNode(ISD::SHL, dl, NVT, InH, DAG.getConstant(Cst, ShTy)),
5684 DAG.getNode(ISD::SRL, dl, NVT, InL,
5685 DAG.getConstant(NVTBits-Cst, ShTy)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005686 }
5687 return true;
5688 case ISD::SRL:
5689 if (Cst > VTBits) {
5690 Lo = DAG.getConstant(0, NVT);
5691 Hi = DAG.getConstant(0, NVT);
5692 } else if (Cst > NVTBits) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00005693 Lo = DAG.getNode(ISD::SRL, dl, NVT,
5694 InH, DAG.getConstant(Cst-NVTBits,ShTy));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005695 Hi = DAG.getConstant(0, NVT);
5696 } else if (Cst == NVTBits) {
5697 Lo = InH;
5698 Hi = DAG.getConstant(0, NVT);
5699 } else {
Dale Johannesen82b5b722009-02-02 22:12:50 +00005700 Lo = DAG.getNode(ISD::OR, dl, NVT,
5701 DAG.getNode(ISD::SRL, dl, NVT, InL, DAG.getConstant(Cst, ShTy)),
5702 DAG.getNode(ISD::SHL, dl, NVT, InH,
5703 DAG.getConstant(NVTBits-Cst, ShTy)));
5704 Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, DAG.getConstant(Cst, ShTy));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005705 }
5706 return true;
5707 case ISD::SRA:
5708 if (Cst > VTBits) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00005709 Hi = Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005710 DAG.getConstant(NVTBits-1, ShTy));
5711 } else if (Cst > NVTBits) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00005712 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005713 DAG.getConstant(Cst-NVTBits, ShTy));
Dale Johannesen82b5b722009-02-02 22:12:50 +00005714 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005715 DAG.getConstant(NVTBits-1, ShTy));
5716 } else if (Cst == NVTBits) {
5717 Lo = InH;
Dale Johannesen82b5b722009-02-02 22:12:50 +00005718 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005719 DAG.getConstant(NVTBits-1, ShTy));
5720 } else {
Dale Johannesen82b5b722009-02-02 22:12:50 +00005721 Lo = DAG.getNode(ISD::OR, dl, NVT,
5722 DAG.getNode(ISD::SRL, dl, NVT, InL, DAG.getConstant(Cst, ShTy)),
5723 DAG.getNode(ISD::SHL, dl,
5724 NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5725 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, DAG.getConstant(Cst, ShTy));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005726 }
5727 return true;
5728 }
5729 }
5730
5731 // Okay, the shift amount isn't constant. However, if we can tell that it is
5732 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005733 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5734 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005735 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5736
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005737 // If we know that if any of the high bits of the shift amount are one, then
5738 // we can do this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005739 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005740 // Mask out the high bit, which we know is set.
Dale Johannesen82b5b722009-02-02 22:12:50 +00005741 Amt = DAG.getNode(ISD::AND, dl, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005742 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005743
5744 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005745 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005746 ExpandOp(Op, InL, InH);
5747 switch(Opc) {
5748 case ISD::SHL:
5749 Lo = DAG.getConstant(0, NVT); // Low part is zero.
Dale Johannesen82b5b722009-02-02 22:12:50 +00005750 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005751 return true;
5752 case ISD::SRL:
5753 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
Dale Johannesen82b5b722009-02-02 22:12:50 +00005754 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005755 return true;
5756 case ISD::SRA:
Dale Johannesen82b5b722009-02-02 22:12:50 +00005757 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005758 DAG.getConstant(NVTBits-1, Amt.getValueType()));
Dale Johannesen82b5b722009-02-02 22:12:50 +00005759 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005760 return true;
5761 }
5762 }
5763
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005764 // If we know that the high bits of the shift amount are all zero, then we can
5765 // do this as a couple of simple shifts.
5766 if ((KnownZero & Mask) == Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005767 // Compute 32-amt.
Dale Johannesen82b5b722009-02-02 22:12:50 +00005768 SDValue Amt2 = DAG.getNode(ISD::SUB, dl, Amt.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005769 DAG.getConstant(NVTBits, Amt.getValueType()),
5770 Amt);
5771
5772 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005773 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005774 ExpandOp(Op, InL, InH);
5775 switch(Opc) {
5776 case ISD::SHL:
Dale Johannesen82b5b722009-02-02 22:12:50 +00005777 Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
5778 Hi = DAG.getNode(ISD::OR, dl, NVT,
5779 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
5780 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005781 return true;
5782 case ISD::SRL:
Dale Johannesen82b5b722009-02-02 22:12:50 +00005783 Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
5784 Lo = DAG.getNode(ISD::OR, dl, NVT,
5785 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
5786 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005787 return true;
5788 case ISD::SRA:
Dale Johannesen82b5b722009-02-02 22:12:50 +00005789 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
5790 Lo = DAG.getNode(ISD::OR, dl, NVT,
5791 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
5792 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005793 return true;
5794 }
5795 }
5796
5797 return false;
5798}
5799
5800
5801// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5802// does not fit into a register, return the lo part and set the hi part to the
5803// by-reg argument. If it does fit into a single register, return the result
5804// and leave the Hi part unset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005805SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5806 bool isSigned, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005807 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5808 // The input chain to this libcall is the entry node of the function.
5809 // Legalizing the call will automatically add the previous call to the
5810 // dependence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005811 SDValue InChain = DAG.getEntryNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005812
5813 TargetLowering::ArgListTy Args;
5814 TargetLowering::ArgListEntry Entry;
5815 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00005816 MVT ArgVT = Node->getOperand(i).getValueType();
5817 const Type *ArgTy = ArgVT.getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005818 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5819 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005820 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005821 Args.push_back(Entry);
5822 }
Bill Wendlingfef06052008-09-16 21:48:12 +00005823 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang1448aad2008-10-30 08:01:45 +00005824 TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005825
5826 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands92c43912008-06-06 12:08:01 +00005827 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman8181bd12008-07-27 21:46:04 +00005828 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen67cc9b62008-09-26 19:31:26 +00005829 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Dale Johannesenca6237b2009-01-30 23:10:59 +00005830 CallingConv::C, false, Callee, Args, DAG,
5831 Node->getDebugLoc());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005832
5833 // Legalize the call sequence, starting with the chain. This will advance
5834 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5835 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5836 LegalizeOp(CallInfo.second);
Dan Gohman8181bd12008-07-27 21:46:04 +00005837 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005838 switch (getTypeAction(CallInfo.first.getValueType())) {
5839 default: assert(0 && "Unknown thing");
5840 case Legal:
5841 Result = CallInfo.first;
5842 break;
5843 case Expand:
5844 ExpandOp(CallInfo.first, Result, Hi);
5845 break;
5846 }
5847 return Result;
5848}
5849
Dan Gohman29c3cef2008-08-14 20:04:46 +00005850/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5851///
5852SDValue SelectionDAGLegalize::
Dale Johannesen9972b632009-02-02 19:03:57 +00005853LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op,
5854 DebugLoc dl) {
Dan Gohman29c3cef2008-08-14 20:04:46 +00005855 bool isCustom = false;
5856 SDValue Tmp1;
5857 switch (getTypeAction(Op.getValueType())) {
5858 case Legal:
5859 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5860 Op.getValueType())) {
5861 default: assert(0 && "Unknown operation action!");
5862 case TargetLowering::Custom:
5863 isCustom = true;
5864 // FALLTHROUGH
5865 case TargetLowering::Legal:
5866 Tmp1 = LegalizeOp(Op);
Gabor Greif1c80d112008-08-28 21:40:38 +00005867 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005868 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5869 else
Dale Johannesen9972b632009-02-02 19:03:57 +00005870 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, dl,
Dan Gohman29c3cef2008-08-14 20:04:46 +00005871 DestTy, Tmp1);
5872 if (isCustom) {
5873 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00005874 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman29c3cef2008-08-14 20:04:46 +00005875 }
5876 break;
5877 case TargetLowering::Expand:
Dale Johannesen9972b632009-02-02 19:03:57 +00005878 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy, dl);
Dan Gohman29c3cef2008-08-14 20:04:46 +00005879 break;
5880 case TargetLowering::Promote:
Dale Johannesen9972b632009-02-02 19:03:57 +00005881 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned, dl);
Dan Gohman29c3cef2008-08-14 20:04:46 +00005882 break;
5883 }
5884 break;
5885 case Expand:
Dale Johannesen9972b632009-02-02 19:03:57 +00005886 Result = ExpandIntToFP(isSigned, DestTy, Op, dl) ;
Dan Gohman29c3cef2008-08-14 20:04:46 +00005887 break;
5888 case Promote:
5889 Tmp1 = PromoteOp(Op);
5890 if (isSigned) {
Dale Johannesen9972b632009-02-02 19:03:57 +00005891 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Tmp1.getValueType(),
Dan Gohman29c3cef2008-08-14 20:04:46 +00005892 Tmp1, DAG.getValueType(Op.getValueType()));
5893 } else {
Dale Johannesen9972b632009-02-02 19:03:57 +00005894 Tmp1 = DAG.getZeroExtendInReg(Tmp1, dl,
Dan Gohman29c3cef2008-08-14 20:04:46 +00005895 Op.getValueType());
5896 }
Gabor Greif1c80d112008-08-28 21:40:38 +00005897 if (Result.getNode())
Dan Gohman29c3cef2008-08-14 20:04:46 +00005898 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5899 else
Dale Johannesen9972b632009-02-02 19:03:57 +00005900 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, dl,
Dan Gohman29c3cef2008-08-14 20:04:46 +00005901 DestTy, Tmp1);
5902 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5903 break;
5904 }
5905 return Result;
5906}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005907
5908/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5909///
Dan Gohman8181bd12008-07-27 21:46:04 +00005910SDValue SelectionDAGLegalize::
Dale Johannesen9972b632009-02-02 19:03:57 +00005911ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00005912 MVT SourceVT = Source.getValueType();
Dan Gohman8b232ff2008-03-11 01:59:03 +00005913 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005914
Dan Gohman29c3cef2008-08-14 20:04:46 +00005915 // Expand unsupported int-to-fp vector casts by unrolling them.
5916 if (DestTy.isVector()) {
5917 if (!ExpandSource)
5918 return LegalizeOp(UnrollVectorOp(Source));
5919 MVT DestEltTy = DestTy.getVectorElementType();
5920 if (DestTy.getVectorNumElements() == 1) {
5921 SDValue Scalar = ScalarizeVectorOp(Source);
5922 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
Dale Johannesen9972b632009-02-02 19:03:57 +00005923 DestEltTy, Scalar, dl);
5924 return DAG.getNode(ISD::BUILD_VECTOR, dl, DestTy, Result);
Dan Gohman29c3cef2008-08-14 20:04:46 +00005925 }
5926 SDValue Lo, Hi;
5927 SplitVectorOp(Source, Lo, Hi);
5928 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5929 DestTy.getVectorNumElements() / 2);
Dale Johannesen9972b632009-02-02 19:03:57 +00005930 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy,
5931 Lo, dl);
5932 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy,
5933 Hi, dl);
5934 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, DestTy, LoResult,
Evan Chengd901b662008-10-13 18:46:18 +00005935 HiResult));
Dan Gohman29c3cef2008-08-14 20:04:46 +00005936 }
5937
Evan Chengf99a7752008-04-01 02:18:22 +00005938 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5939 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmana193dba2008-03-05 02:07:31 +00005940 // The integer value loaded will be incorrectly if the 'sign bit' of the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005941 // incoming integer is set. To handle this, we dynamically test to see if
5942 // it is set, and, if so, add a fudge factor.
Dan Gohman8181bd12008-07-27 21:46:04 +00005943 SDValue Hi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005944 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005945 SDValue Lo;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005946 ExpandOp(Source, Lo, Hi);
Dale Johannesen9972b632009-02-02 19:03:57 +00005947 Source = DAG.getNode(ISD::BUILD_PAIR, dl, SourceVT, Lo, Hi);
Dan Gohman8b232ff2008-03-11 01:59:03 +00005948 } else {
5949 // The comparison for the sign bit will use the entire operand.
5950 Hi = Source;
5951 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005952
Dale Johannesen96db7962008-11-04 20:52:49 +00005953 // Check to see if the target has a custom way to lower this. If so, use
5954 // it. (Note we've already expanded the operand in this case.)
Dale Johannesena359b8b2008-10-21 20:50:01 +00005955 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5956 default: assert(0 && "This action not implemented for this operation!");
5957 case TargetLowering::Legal:
5958 case TargetLowering::Expand:
5959 break; // This case is handled below.
5960 case TargetLowering::Custom: {
Dale Johannesen24dd9a52009-02-07 00:55:49 +00005961 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, dl, DestTy,
Dale Johannesena359b8b2008-10-21 20:50:01 +00005962 Source), DAG);
5963 if (NV.getNode())
5964 return LegalizeOp(NV);
5965 break; // The target decided this was legal after all
5966 }
5967 }
5968
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005969 // If this is unsigned, and not supported, first perform the conversion to
5970 // signed, then adjust the result if the sign bit is set.
Dale Johannesen9972b632009-02-02 19:03:57 +00005971 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005972
Dale Johannesen9972b632009-02-02 19:03:57 +00005973 SDValue SignSet = DAG.getSetCC(dl,
5974 TLI.getSetCCResultType(Hi.getValueType()),
Duncan Sands4a361272009-01-01 15:52:00 +00005975 Hi, DAG.getConstant(0, Hi.getValueType()),
5976 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005977 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesen9972b632009-02-02 19:03:57 +00005978 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005979 SignSet, Four, Zero);
5980 uint64_t FF = 0x5f800000ULL;
5981 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmana193dba2008-03-05 02:07:31 +00005982 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005983
Dan Gohman8181bd12008-07-27 21:46:04 +00005984 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00005985 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen9972b632009-02-02 19:03:57 +00005986 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00005987 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00005988 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005989 if (DestTy == MVT::f32)
Dale Johannesen9972b632009-02-02 19:03:57 +00005990 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00005991 PseudoSourceValue::getConstantPool(), 0,
5992 false, Alignment);
Duncan Sandsec142ee2008-06-08 20:54:56 +00005993 else if (DestTy.bitsGT(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005994 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen9972b632009-02-02 19:03:57 +00005995 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, dl, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00005996 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005997 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00005998 MVT::f32, false, Alignment);
Dale Johannesen2fc20782007-09-14 22:26:36 +00005999 else
6000 assert(0 && "Unexpected conversion");
6001
Duncan Sands92c43912008-06-06 12:08:01 +00006002 MVT SCVT = SignedConv.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006003 if (SCVT != DestTy) {
6004 // Destination type needs to be expanded as well. The FADD now we are
6005 // constructing will be expanded into a libcall.
Duncan Sands92c43912008-06-06 12:08:01 +00006006 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
6007 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dale Johannesen9972b632009-02-02 19:03:57 +00006008 SignedConv = DAG.getNode(ISD::BUILD_PAIR, dl, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006009 SignedConv, SignedConv.getValue(1));
6010 }
Dale Johannesen9972b632009-02-02 19:03:57 +00006011 SignedConv = DAG.getNode(ISD::BIT_CONVERT, dl, DestTy, SignedConv);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006012 }
Dale Johannesen9972b632009-02-02 19:03:57 +00006013 return DAG.getNode(ISD::FADD, dl, DestTy, SignedConv, FudgeInReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006014 }
6015
6016 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmanc98645c2008-03-05 01:08:17 +00006017 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006018 default: assert(0 && "This action not implemented for this operation!");
6019 case TargetLowering::Legal:
6020 case TargetLowering::Expand:
6021 break; // This case is handled below.
6022 case TargetLowering::Custom: {
Dale Johannesen9972b632009-02-02 19:03:57 +00006023 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, dl, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006024 Source), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006025 if (NV.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006026 return LegalizeOp(NV);
6027 break; // The target decided this was legal after all
6028 }
6029 }
6030
6031 // Expand the source, then glue it back together for the call. We must expand
6032 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman8b232ff2008-03-11 01:59:03 +00006033 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006034 SDValue SrcLo, SrcHi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00006035 ExpandOp(Source, SrcLo, SrcHi);
Dale Johannesen9972b632009-02-02 19:03:57 +00006036 Source = DAG.getNode(ISD::BUILD_PAIR, dl, SourceVT, SrcLo, SrcHi);
Dan Gohman8b232ff2008-03-11 01:59:03 +00006037 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006038
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006039 RTLIB::Libcall LC = isSigned ?
6040 RTLIB::getSINTTOFP(SourceVT, DestTy) :
6041 RTLIB::getUINTTOFP(SourceVT, DestTy);
6042 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
6043
Dale Johannesen9972b632009-02-02 19:03:57 +00006044 Source = DAG.getNode(ISD::SINT_TO_FP, dl, DestTy, Source);
Dan Gohman8181bd12008-07-27 21:46:04 +00006045 SDValue HiPart;
Gabor Greif1c80d112008-08-28 21:40:38 +00006046 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
6047 if (Result.getValueType() != DestTy && HiPart.getNode())
Dale Johannesen9972b632009-02-02 19:03:57 +00006048 Result = DAG.getNode(ISD::BUILD_PAIR, dl, DestTy, Result, HiPart);
Dan Gohmanec51f642008-03-10 23:03:31 +00006049 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006050}
6051
6052/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
6053/// INT_TO_FP operation of the specified operand when the target requests that
6054/// we expand it. At this point, we know that the result and operand types are
6055/// legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00006056SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
6057 SDValue Op0,
Dale Johannesen9972b632009-02-02 19:03:57 +00006058 MVT DestVT,
6059 DebugLoc dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006060 if (Op0.getValueType() == MVT::i32) {
6061 // simple 32-bit [signed|unsigned] integer to float/double expansion
6062
Chris Lattner0aeb1d02008-01-16 07:03:22 +00006063 // Get the stack frame index of a 8 byte buffer.
Dan Gohman8181bd12008-07-27 21:46:04 +00006064 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner0aeb1d02008-01-16 07:03:22 +00006065
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006066 // word offset constant for Hi/Lo address computation
Dan Gohman8181bd12008-07-27 21:46:04 +00006067 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006068 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman8181bd12008-07-27 21:46:04 +00006069 SDValue Hi = StackSlot;
Dale Johannesen9972b632009-02-02 19:03:57 +00006070 SDValue Lo = DAG.getNode(ISD::ADD, dl,
6071 TLI.getPointerTy(), StackSlot,WordOff);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006072 if (TLI.isLittleEndian())
6073 std::swap(Hi, Lo);
6074
6075 // if signed map to unsigned space
Dan Gohman8181bd12008-07-27 21:46:04 +00006076 SDValue Op0Mapped;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006077 if (isSigned) {
6078 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman8181bd12008-07-27 21:46:04 +00006079 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dale Johannesen9972b632009-02-02 19:03:57 +00006080 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006081 } else {
6082 Op0Mapped = Op0;
6083 }
6084 // store the lo of the constructed double - based on integer input
Dale Johannesen9972b632009-02-02 19:03:57 +00006085 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006086 Op0Mapped, Lo, NULL, 0);
6087 // initial hi portion of constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00006088 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006089 // store the hi of the constructed double - biased exponent
Dale Johannesen9972b632009-02-02 19:03:57 +00006090 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006091 // load the constructed double
Dale Johannesen9972b632009-02-02 19:03:57 +00006092 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006093 // FP constant to bias correct the final result
Dan Gohman8181bd12008-07-27 21:46:04 +00006094 SDValue Bias = DAG.getConstantFP(isSigned ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006095 BitsToDouble(0x4330000080000000ULL)
6096 : BitsToDouble(0x4330000000000000ULL),
6097 MVT::f64);
6098 // subtract the bias
Dale Johannesen9972b632009-02-02 19:03:57 +00006099 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006100 // final result
Dan Gohman8181bd12008-07-27 21:46:04 +00006101 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006102 // handle final rounding
6103 if (DestVT == MVT::f64) {
6104 // do nothing
6105 Result = Sub;
Duncan Sandsec142ee2008-06-08 20:54:56 +00006106 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesen9972b632009-02-02 19:03:57 +00006107 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner5872a362008-01-17 07:00:52 +00006108 DAG.getIntPtrConstant(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00006109 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesen9972b632009-02-02 19:03:57 +00006110 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006111 }
6112 return Result;
6113 }
6114 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesen9972b632009-02-02 19:03:57 +00006115 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006116
Dale Johannesen9972b632009-02-02 19:03:57 +00006117 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands4a361272009-01-01 15:52:00 +00006118 Op0, DAG.getConstant(0, Op0.getValueType()),
6119 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00006120 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesen9972b632009-02-02 19:03:57 +00006121 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006122 SignSet, Four, Zero);
6123
6124 // If the sign bit of the integer is set, the large number will be treated
6125 // as a negative number. To counteract this, the dynamic code adds an
6126 // offset depending on the data type.
6127 uint64_t FF;
Duncan Sands92c43912008-06-06 12:08:01 +00006128 switch (Op0.getValueType().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006129 default: assert(0 && "Unsupported integer type!");
6130 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6131 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6132 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6133 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6134 }
6135 if (TLI.isLittleEndian()) FF <<= 32;
6136 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
6137
Dan Gohman8181bd12008-07-27 21:46:04 +00006138 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman04637d12008-09-16 22:05:41 +00006139 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen9972b632009-02-02 19:03:57 +00006140 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohmandc5901a2008-09-22 22:40:08 +00006141 Alignment = std::min(Alignment, 4u);
Dan Gohman8181bd12008-07-27 21:46:04 +00006142 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006143 if (DestVT == MVT::f32)
Dale Johannesen9972b632009-02-02 19:03:57 +00006144 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman04637d12008-09-16 22:05:41 +00006145 PseudoSourceValue::getConstantPool(), 0,
6146 false, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006147 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00006148 FudgeInReg =
Dale Johannesen9972b632009-02-02 19:03:57 +00006149 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohman12a9c082008-02-06 22:27:42 +00006150 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006151 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman04637d12008-09-16 22:05:41 +00006152 MVT::f32, false, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006153 }
6154
Dale Johannesen9972b632009-02-02 19:03:57 +00006155 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006156}
6157
6158/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6159/// *INT_TO_FP operation of the specified operand when the target requests that
6160/// we promote it. At this point, we know that the result and operand types are
6161/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6162/// operation that takes a larger input.
Dan Gohman8181bd12008-07-27 21:46:04 +00006163SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6164 MVT DestVT,
Dale Johannesen9972b632009-02-02 19:03:57 +00006165 bool isSigned,
6166 DebugLoc dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006167 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00006168 MVT NewInTy = LegalOp.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006169
6170 unsigned OpToUse = 0;
6171
6172 // Scan for the appropriate larger type to use.
6173 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00006174 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6175 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006176
6177 // If the target supports SINT_TO_FP of this type, use it.
6178 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6179 default: break;
6180 case TargetLowering::Legal:
6181 if (!TLI.isTypeLegal(NewInTy))
6182 break; // Can't use this datatype.
6183 // FALL THROUGH.
6184 case TargetLowering::Custom:
6185 OpToUse = ISD::SINT_TO_FP;
6186 break;
6187 }
6188 if (OpToUse) break;
6189 if (isSigned) continue;
6190
6191 // If the target supports UINT_TO_FP of this type, use it.
6192 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6193 default: break;
6194 case TargetLowering::Legal:
6195 if (!TLI.isTypeLegal(NewInTy))
6196 break; // Can't use this datatype.
6197 // FALL THROUGH.
6198 case TargetLowering::Custom:
6199 OpToUse = ISD::UINT_TO_FP;
6200 break;
6201 }
6202 if (OpToUse) break;
6203
6204 // Otherwise, try a larger type.
6205 }
6206
6207 // Okay, we found the operation and type to use. Zero extend our input to the
6208 // desired type then run the operation on it.
Dale Johannesen9972b632009-02-02 19:03:57 +00006209 return DAG.getNode(OpToUse, dl, DestVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006210 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesen9972b632009-02-02 19:03:57 +00006211 dl, NewInTy, LegalOp));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006212}
6213
6214/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6215/// FP_TO_*INT operation of the specified operand when the target requests that
6216/// we promote it. At this point, we know that the result and operand types are
6217/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6218/// operation that returns a larger result.
Dan Gohman8181bd12008-07-27 21:46:04 +00006219SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6220 MVT DestVT,
Dale Johannesen9972b632009-02-02 19:03:57 +00006221 bool isSigned,
6222 DebugLoc dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006223 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00006224 MVT NewOutTy = DestVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006225
6226 unsigned OpToUse = 0;
6227
6228 // Scan for the appropriate larger type to use.
6229 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00006230 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6231 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006232
6233 // If the target supports FP_TO_SINT returning this type, use it.
6234 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6235 default: break;
6236 case TargetLowering::Legal:
6237 if (!TLI.isTypeLegal(NewOutTy))
6238 break; // Can't use this datatype.
6239 // FALL THROUGH.
6240 case TargetLowering::Custom:
6241 OpToUse = ISD::FP_TO_SINT;
6242 break;
6243 }
6244 if (OpToUse) break;
6245
6246 // If the target supports FP_TO_UINT of this type, use it.
6247 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6248 default: break;
6249 case TargetLowering::Legal:
6250 if (!TLI.isTypeLegal(NewOutTy))
6251 break; // Can't use this datatype.
6252 // FALL THROUGH.
6253 case TargetLowering::Custom:
6254 OpToUse = ISD::FP_TO_UINT;
6255 break;
6256 }
6257 if (OpToUse) break;
6258
6259 // Otherwise, try a larger type.
6260 }
6261
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006262
6263 // Okay, we found the operation and type to use.
Dale Johannesen9972b632009-02-02 19:03:57 +00006264 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sandsac496a12008-07-04 11:47:58 +00006265
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006266 // If the operation produces an invalid type, it must be custom lowered. Use
6267 // the target lowering hooks to expand it. Just keep the low part of the
6268 // expanded operation, we know that we're truncating anyway.
6269 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006270 SmallVector<SDValue, 2> Results;
6271 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6272 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6273 Operation = Results[0];
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006274 }
Duncan Sandsac496a12008-07-04 11:47:58 +00006275
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006276 // Truncate the result of the extended FP_TO_*INT operation to the desired
6277 // size.
Dale Johannesen9972b632009-02-02 19:03:57 +00006278 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006279}
6280
6281/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6282///
Dale Johannesen82b5b722009-02-02 22:12:50 +00006283SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00006284 MVT VT = Op.getValueType();
6285 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00006286 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands92c43912008-06-06 12:08:01 +00006287 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006288 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6289 case MVT::i16:
Dale Johannesen82b5b722009-02-02 22:12:50 +00006290 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
6291 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
6292 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006293 case MVT::i32:
Dale Johannesen82b5b722009-02-02 22:12:50 +00006294 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
6295 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
6296 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
6297 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
6298 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6299 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6300 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
6301 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
6302 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006303 case MVT::i64:
Dale Johannesen82b5b722009-02-02 22:12:50 +00006304 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
6305 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
6306 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
6307 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
6308 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
6309 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
6310 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
6311 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
6312 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6313 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6314 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6315 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6316 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6317 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6318 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
6319 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
6320 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
6321 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
6322 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
6323 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
6324 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006325 }
6326}
6327
6328/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6329///
Dale Johannesen82b5b722009-02-02 22:12:50 +00006330SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
6331 DebugLoc dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006332 switch (Opc) {
6333 default: assert(0 && "Cannot expand this yet!");
6334 case ISD::CTPOP: {
6335 static const uint64_t mask[6] = {
6336 0x5555555555555555ULL, 0x3333333333333333ULL,
6337 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6338 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6339 };
Duncan Sands92c43912008-06-06 12:08:01 +00006340 MVT VT = Op.getValueType();
6341 MVT ShVT = TLI.getShiftAmountTy();
6342 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006343 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6344 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Duncan Sands505ba942009-02-01 18:06:53 +00006345 unsigned EltSize = VT.isVector() ?
6346 VT.getVectorElementType().getSizeInBits() : len;
6347 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00006348 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dale Johannesen18ae8af2009-02-06 21:55:48 +00006349 Op = DAG.getNode(ISD::ADD, dl, VT,
6350 DAG.getNode(ISD::AND, dl, VT, Op, Tmp2),
Dale Johannesen82b5b722009-02-02 22:12:50 +00006351 DAG.getNode(ISD::AND, dl, VT,
6352 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3),
6353 Tmp2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006354 }
6355 return Op;
6356 }
6357 case ISD::CTLZ: {
6358 // for now, we do this:
6359 // x = x | (x >> 1);
6360 // x = x | (x >> 2);
6361 // ...
6362 // x = x | (x >>16);
6363 // x = x | (x >>32); // for 64-bit input
6364 // return popcount(~x);
6365 //
6366 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006367 MVT VT = Op.getValueType();
6368 MVT ShVT = TLI.getShiftAmountTy();
6369 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006370 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006371 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dale Johannesen82b5b722009-02-02 22:12:50 +00006372 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesen18ae8af2009-02-06 21:55:48 +00006373 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006374 }
Dale Johannesen82b5b722009-02-02 22:12:50 +00006375 Op = DAG.getNOT(dl, Op, VT);
6376 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006377 }
6378 case ISD::CTTZ: {
6379 // for now, we use: { return popcount(~x & (x - 1)); }
6380 // unless the target has ctlz but not ctpop, in which case we use:
6381 // { return 32 - nlz(~x & (x-1)); }
6382 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00006383 MVT VT = Op.getValueType();
Dale Johannesen82b5b722009-02-02 22:12:50 +00006384 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
6385 DAG.getNOT(dl, Op, VT),
6386 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendlingfcfb47d2009-01-30 23:03:19 +00006387 DAG.getConstant(1, VT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006388 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohman52c51aa2009-01-28 17:46:25 +00006389 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
6390 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen82b5b722009-02-02 22:12:50 +00006391 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands92c43912008-06-06 12:08:01 +00006392 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen82b5b722009-02-02 22:12:50 +00006393 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
6394 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006395 }
6396 }
6397}
6398
Dan Gohman8181bd12008-07-27 21:46:04 +00006399/// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006400/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman4fc03742008-10-01 15:07:49 +00006401/// LegalizedNodes map is filled in for any results that are not expanded, the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006402/// ExpandedNodes map is filled in for any results that are expanded, and the
6403/// Lo/Hi values are returned.
Dan Gohman8181bd12008-07-27 21:46:04 +00006404void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands92c43912008-06-06 12:08:01 +00006405 MVT VT = Op.getValueType();
6406 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greif1c80d112008-08-28 21:40:38 +00006407 SDNode *Node = Op.getNode();
Dale Johannesen82b5b722009-02-02 22:12:50 +00006408 DebugLoc dl = Node->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006409 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00006410 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands92c43912008-06-06 12:08:01 +00006411 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006412
6413 // See if we already expanded it.
Dan Gohman8181bd12008-07-27 21:46:04 +00006414 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006415 = ExpandedNodes.find(Op);
6416 if (I != ExpandedNodes.end()) {
6417 Lo = I->second.first;
6418 Hi = I->second.second;
6419 return;
6420 }
6421
6422 switch (Node->getOpcode()) {
6423 case ISD::CopyFromReg:
6424 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006425 case ISD::FP_ROUND_INREG:
6426 if (VT == MVT::ppcf128 &&
6427 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6428 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006429 SDValue SrcLo, SrcHi, Src;
Dale Johannesend3b6af32007-10-11 23:32:15 +00006430 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
Dale Johannesen82b5b722009-02-02 22:12:50 +00006431 Src = DAG.getNode(ISD::BUILD_PAIR, dl, VT, SrcLo, SrcHi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006432 SDValue Result = TLI.LowerOperation(
Dale Johannesen82b5b722009-02-02 22:12:50 +00006433 DAG.getNode(ISD::FP_ROUND_INREG, dl, VT, Src, Op.getOperand(1)), DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006434 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6435 Lo = Result.getNode()->getOperand(0);
6436 Hi = Result.getNode()->getOperand(1);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00006437 break;
6438 }
6439 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006440 default:
6441#ifndef NDEBUG
6442 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
6443#endif
6444 assert(0 && "Do not know how to expand this operator!");
6445 abort();
Dan Gohman550c8462008-02-27 01:52:30 +00006446 case ISD::EXTRACT_ELEMENT:
6447 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006448 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman550c8462008-02-27 01:52:30 +00006449 return ExpandOp(Hi, Lo, Hi);
Dan Gohman7e7aa2c2008-02-27 19:44:57 +00006450 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006451 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen2ff963d2007-10-31 00:32:36 +00006452 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6453 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6454 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006455 case ISD::UNDEF:
Dale Johannesen9bfc0172009-02-06 23:05:02 +00006456 Lo = DAG.getUNDEF(NVT);
6457 Hi = DAG.getUNDEF(NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006458 break;
6459 case ISD::Constant: {
Duncan Sands92c43912008-06-06 12:08:01 +00006460 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman97f1f8e2008-03-03 22:20:46 +00006461 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6462 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6463 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006464 break;
6465 }
6466 case ISD::ConstantFP: {
6467 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00006468 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00006469 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen2aef5692007-10-11 18:07:22 +00006470 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6471 MVT::f64);
6472 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6473 MVT::f64);
6474 break;
6475 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006476 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
6477 if (getTypeAction(Lo.getValueType()) == Expand)
6478 ExpandOp(Lo, Lo, Hi);
6479 break;
6480 }
6481 case ISD::BUILD_PAIR:
6482 // Return the operands.
6483 Lo = Node->getOperand(0);
6484 Hi = Node->getOperand(1);
6485 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006486
6487 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00006488 if (Node->getNumValues() == 1) {
6489 ExpandOp(Op.getOperand(0), Lo, Hi);
6490 break;
6491 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006492 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif46bf5472008-08-26 22:36:50 +00006493 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006494 Op.getValue(1).getValueType() == MVT::Other &&
6495 "unhandled MERGE_VALUES");
6496 ExpandOp(Op.getOperand(0), Lo, Hi);
6497 // Remember that we legalized the chain.
6498 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6499 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006500
6501 case ISD::SIGN_EXTEND_INREG:
6502 ExpandOp(Node->getOperand(0), Lo, Hi);
6503 // sext_inreg the low part if needed.
Dale Johannesen82b5b722009-02-02 22:12:50 +00006504 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Lo, Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006505
6506 // The high part gets the sign extension from the lo-part. This handles
6507 // things like sextinreg V:i64 from i8.
Dale Johannesen82b5b722009-02-02 22:12:50 +00006508 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
Duncan Sands92c43912008-06-06 12:08:01 +00006509 DAG.getConstant(NVT.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006510 TLI.getShiftAmountTy()));
6511 break;
6512
6513 case ISD::BSWAP: {
6514 ExpandOp(Node->getOperand(0), Lo, Hi);
Dale Johannesen82b5b722009-02-02 22:12:50 +00006515 SDValue TempLo = DAG.getNode(ISD::BSWAP, dl, NVT, Hi);
6516 Hi = DAG.getNode(ISD::BSWAP, dl, NVT, Lo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006517 Lo = TempLo;
6518 break;
6519 }
6520
6521 case ISD::CTPOP:
6522 ExpandOp(Node->getOperand(0), Lo, Hi);
Dale Johannesen82b5b722009-02-02 22:12:50 +00006523 Lo = DAG.getNode(ISD::ADD, dl, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6524 DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
6525 DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006526 Hi = DAG.getConstant(0, NVT);
6527 break;
6528
6529 case ISD::CTLZ: {
6530 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
6531 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006532 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
Dale Johannesen82b5b722009-02-02 22:12:50 +00006533 SDValue HLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Hi);
6534 SDValue TopNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), HLZ,
6535 BitsC, ISD::SETNE);
6536 SDValue LowPart = DAG.getNode(ISD::CTLZ, dl, NVT, Lo);
6537 LowPart = DAG.getNode(ISD::ADD, dl, NVT, LowPart, BitsC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006538
Dale Johannesen82b5b722009-02-02 22:12:50 +00006539 Lo = DAG.getNode(ISD::SELECT, dl, NVT, TopNotZero, HLZ, LowPart);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006540 Hi = DAG.getConstant(0, NVT);
6541 break;
6542 }
6543
6544 case ISD::CTTZ: {
6545 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
6546 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006547 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
Dale Johannesen82b5b722009-02-02 22:12:50 +00006548 SDValue LTZ = DAG.getNode(ISD::CTTZ, dl, NVT, Lo);
6549 SDValue BotNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), LTZ,
6550 BitsC, ISD::SETNE);
6551 SDValue HiPart = DAG.getNode(ISD::CTTZ, dl, NVT, Hi);
6552 HiPart = DAG.getNode(ISD::ADD, dl, NVT, HiPart, BitsC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006553
Dale Johannesen82b5b722009-02-02 22:12:50 +00006554 Lo = DAG.getNode(ISD::SELECT, dl, NVT, BotNotZero, LTZ, HiPart);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006555 Hi = DAG.getConstant(0, NVT);
6556 break;
6557 }
6558
6559 case ISD::VAARG: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006560 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6561 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Dale Johannesen564036c2009-02-04 00:13:36 +00006562 Lo = DAG.getVAArg(NVT, dl, Ch, Ptr, Node->getOperand(2));
6563 Hi = DAG.getVAArg(NVT, dl, Lo.getValue(1), Ptr, Node->getOperand(2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006564
6565 // Remember that we legalized the chain.
6566 Hi = LegalizeOp(Hi);
6567 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006568 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006569 std::swap(Lo, Hi);
6570 break;
6571 }
6572
6573 case ISD::LOAD: {
6574 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00006575 SDValue Ch = LD->getChain(); // Legalize the chain.
6576 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006577 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman29c3cef2008-08-14 20:04:46 +00006578 const Value *SV = LD->getSrcValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006579 int SVOffset = LD->getSrcValueOffset();
6580 unsigned Alignment = LD->getAlignment();
6581 bool isVolatile = LD->isVolatile();
6582
6583 if (ExtType == ISD::NON_EXTLOAD) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00006584 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006585 isVolatile, Alignment);
6586 if (VT == MVT::f32 || VT == MVT::f64) {
6587 // f32->i32 or f64->i64 one to one expansion.
6588 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006589 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006590 // Recursively expand the new load.
6591 if (getTypeAction(NVT) == Expand)
6592 ExpandOp(Lo, Lo, Hi);
6593 break;
6594 }
6595
6596 // Increment the pointer to the other half.
Duncan Sands92c43912008-06-06 12:08:01 +00006597 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Dale Johannesen82b5b722009-02-02 22:12:50 +00006598 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006599 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006600 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006601 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesen82b5b722009-02-02 22:12:50 +00006602 Hi = DAG.getLoad(NVT, dl, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006603 isVolatile, Alignment);
6604
6605 // Build a factor node to remember that this load is independent of the
6606 // other one.
Dale Johannesen82b5b722009-02-02 22:12:50 +00006607 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006608 Hi.getValue(1));
6609
6610 // Remember that we legalized the chain.
6611 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00006612 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006613 std::swap(Lo, Hi);
6614 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00006615 MVT EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006616
Dale Johannesen2550e3a2007-10-19 20:29:00 +00006617 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6618 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006619 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dale Johannesen82b5b722009-02-02 22:12:50 +00006620 SDValue Load = DAG.getLoad(EVT, dl, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006621 SVOffset, isVolatile, Alignment);
6622 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006623 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Dale Johannesen82b5b722009-02-02 22:12:50 +00006624 ExpandOp(DAG.getNode(ISD::FP_EXTEND, dl, VT, Load), Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006625 break;
6626 }
6627
6628 if (EVT == NVT)
Dale Johannesen82b5b722009-02-02 22:12:50 +00006629 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006630 SVOffset, isVolatile, Alignment);
6631 else
Dale Johannesen82b5b722009-02-02 22:12:50 +00006632 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006633 SVOffset, EVT, isVolatile,
6634 Alignment);
6635
6636 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006637 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006638
6639 if (ExtType == ISD::SEXTLOAD) {
6640 // The high part is obtained by SRA'ing all but one of the bits of the
6641 // lo part.
Duncan Sands92c43912008-06-06 12:08:01 +00006642 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dale Johannesen82b5b722009-02-02 22:12:50 +00006643 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006644 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6645 } else if (ExtType == ISD::ZEXTLOAD) {
6646 // The high part is just a zero.
6647 Hi = DAG.getConstant(0, NVT);
6648 } else /* if (ExtType == ISD::EXTLOAD) */ {
6649 // The high part is undefined.
Dale Johannesen9bfc0172009-02-06 23:05:02 +00006650 Hi = DAG.getUNDEF(NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006651 }
6652 }
6653 break;
6654 }
6655 case ISD::AND:
6656 case ISD::OR:
6657 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +00006658 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006659 ExpandOp(Node->getOperand(0), LL, LH);
6660 ExpandOp(Node->getOperand(1), RL, RH);
Dale Johannesen82b5b722009-02-02 22:12:50 +00006661 Lo = DAG.getNode(Node->getOpcode(), dl, NVT, LL, RL);
6662 Hi = DAG.getNode(Node->getOpcode(), dl, NVT, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006663 break;
6664 }
6665 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006666 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006667 ExpandOp(Node->getOperand(1), LL, LH);
6668 ExpandOp(Node->getOperand(2), RL, RH);
6669 if (getTypeAction(NVT) == Expand)
6670 NVT = TLI.getTypeToExpandTo(NVT);
Dale Johannesen82b5b722009-02-02 22:12:50 +00006671 Lo = DAG.getNode(ISD::SELECT, dl, NVT, Node->getOperand(0), LL, RL);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006672 if (VT != MVT::f32)
Dale Johannesen82b5b722009-02-02 22:12:50 +00006673 Hi = DAG.getNode(ISD::SELECT, dl, NVT, Node->getOperand(0), LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006674 break;
6675 }
6676 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006677 SDValue TL, TH, FL, FH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006678 ExpandOp(Node->getOperand(2), TL, TH);
6679 ExpandOp(Node->getOperand(3), FL, FH);
6680 if (getTypeAction(NVT) == Expand)
6681 NVT = TLI.getTypeToExpandTo(NVT);
Dale Johannesen82b5b722009-02-02 22:12:50 +00006682 Lo = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006683 Node->getOperand(1), TL, FL, Node->getOperand(4));
6684 if (VT != MVT::f32)
Dale Johannesen82b5b722009-02-02 22:12:50 +00006685 Hi = DAG.getNode(ISD::SELECT_CC, dl, NVT, Node->getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006686 Node->getOperand(1), TH, FH, Node->getOperand(4));
6687 break;
6688 }
6689 case ISD::ANY_EXTEND:
6690 // The low part is any extension of the input (which degenerates to a copy).
Dale Johannesen82b5b722009-02-02 22:12:50 +00006691 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006692 // The high part is undefined.
Dale Johannesen9bfc0172009-02-06 23:05:02 +00006693 Hi = DAG.getUNDEF(NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006694 break;
6695 case ISD::SIGN_EXTEND: {
6696 // The low part is just a sign extension of the input (which degenerates to
6697 // a copy).
Dale Johannesen82b5b722009-02-02 22:12:50 +00006698 Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006699
6700 // The high part is obtained by SRA'ing all but one of the bits of the lo
6701 // part.
Duncan Sands92c43912008-06-06 12:08:01 +00006702 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dale Johannesen82b5b722009-02-02 22:12:50 +00006703 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006704 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6705 break;
6706 }
6707 case ISD::ZERO_EXTEND:
6708 // The low part is just a zero extension of the input (which degenerates to
6709 // a copy).
Dale Johannesen82b5b722009-02-02 22:12:50 +00006710 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006711
6712 // The high part is just a zero.
6713 Hi = DAG.getConstant(0, NVT);
6714 break;
6715
6716 case ISD::TRUNCATE: {
6717 // The input value must be larger than this value. Expand *it*.
Dan Gohman8181bd12008-07-27 21:46:04 +00006718 SDValue NewLo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006719 ExpandOp(Node->getOperand(0), NewLo, Hi);
6720
6721 // The low part is now either the right size, or it is closer. If not the
6722 // right size, make an illegal truncate so we recursively expand it.
6723 if (NewLo.getValueType() != Node->getValueType(0))
Dale Johannesen82b5b722009-02-02 22:12:50 +00006724 NewLo = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), NewLo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006725 ExpandOp(NewLo, Lo, Hi);
6726 break;
6727 }
6728
6729 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006730 SDValue Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006731 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6732 // If the target wants to, allow it to lower this itself.
6733 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6734 case Expand: assert(0 && "cannot expand FP!");
6735 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6736 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6737 }
Dale Johannesen82b5b722009-02-02 22:12:50 +00006738 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006739 }
6740
6741 // f32 / f64 must be expanded to i32 / i64.
6742 if (VT == MVT::f32 || VT == MVT::f64) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00006743 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006744 if (getTypeAction(NVT) == Expand)
6745 ExpandOp(Lo, Lo, Hi);
6746 break;
6747 }
6748
6749 // If source operand will be expanded to the same type as VT, i.e.
6750 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands92c43912008-06-06 12:08:01 +00006751 MVT VT0 = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006752 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6753 ExpandOp(Node->getOperand(0), Lo, Hi);
6754 break;
6755 }
6756
6757 // Turn this into a load/store pair by default.
Gabor Greif1c80d112008-08-28 21:40:38 +00006758 if (Tmp.getNode() == 0)
Dale Johannesen82b5b722009-02-02 22:12:50 +00006759 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006760
6761 ExpandOp(Tmp, Lo, Hi);
6762 break;
6763 }
6764
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006765 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006766 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6767 TargetLowering::Custom &&
6768 "Must custom expand ReadCycleCounter");
Dan Gohman8181bd12008-07-27 21:46:04 +00006769 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006770 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006771 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006772 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006773 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006774 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006775 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006776
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006777 case ISD::ATOMIC_CMP_SWAP: {
Dale Johannesen44eb5372008-10-03 19:41:08 +00006778 // This operation does not need a loop.
6779 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6780 assert(Tmp.getNode() && "Node must be custom expanded!");
6781 ExpandOp(Tmp.getValue(0), Lo, Hi);
6782 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6783 LegalizeOp(Tmp.getValue(1)));
6784 break;
6785 }
6786
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006787 case ISD::ATOMIC_LOAD_ADD:
6788 case ISD::ATOMIC_LOAD_SUB:
6789 case ISD::ATOMIC_LOAD_AND:
6790 case ISD::ATOMIC_LOAD_OR:
6791 case ISD::ATOMIC_LOAD_XOR:
6792 case ISD::ATOMIC_LOAD_NAND:
6793 case ISD::ATOMIC_SWAP: {
Dale Johannesen44eb5372008-10-03 19:41:08 +00006794 // These operations require a loop to be generated. We can't do that yet,
6795 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesenf160d802008-10-02 18:53:47 +00006796 SDValue In2Lo, In2Hi, In2;
6797 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
Dale Johannesen82b5b722009-02-02 22:12:50 +00006798 In2 = DAG.getNode(ISD::BUILD_PAIR, dl, VT, In2Lo, In2Hi);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006799 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6800 SDValue Replace =
Dale Johannesen82b5b722009-02-02 22:12:50 +00006801 DAG.getAtomic(Op.getOpcode(), dl, Anode->getMemoryVT(),
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006802 Op.getOperand(0), Op.getOperand(1), In2,
Dale Johannesen44eb5372008-10-03 19:41:08 +00006803 Anode->getSrcValue(), Anode->getAlignment());
6804 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006805 ExpandOp(Result.getValue(0), Lo, Hi);
6806 // Remember that we legalized the chain.
6807 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharth81580822008-03-05 01:15:49 +00006808 break;
6809 }
6810
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006811 // These operators cannot be expanded directly, emit them as calls to
6812 // library functions.
6813 case ISD::FP_TO_SINT: {
6814 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006815 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006816 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6817 case Expand: assert(0 && "cannot expand FP!");
6818 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6819 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6820 }
6821
Dale Johannesen82b5b722009-02-02 22:12:50 +00006822 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006823
6824 // Now that the custom expander is done, expand the result, which is still
6825 // VT.
Gabor Greif1c80d112008-08-28 21:40:38 +00006826 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006827 ExpandOp(Op, Lo, Hi);
6828 break;
6829 }
6830 }
6831
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006832 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6833 VT);
6834 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6835 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006836 break;
6837 }
6838
6839 case ISD::FP_TO_UINT: {
6840 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006841 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006842 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6843 case Expand: assert(0 && "cannot expand FP!");
6844 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6845 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6846 }
6847
Dale Johannesen82b5b722009-02-02 22:12:50 +00006848 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, dl, VT, Op), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006849
6850 // Now that the custom expander is done, expand the result.
Gabor Greif1c80d112008-08-28 21:40:38 +00006851 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006852 ExpandOp(Op, Lo, Hi);
6853 break;
6854 }
6855 }
6856
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006857 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6858 VT);
6859 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6860 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006861 break;
6862 }
6863
6864 case ISD::SHL: {
6865 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006866 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006867 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00006868 SDValue Op = DAG.getNode(ISD::SHL, dl, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006869 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006870 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006871 // Now that the custom expander is done, expand the result, which is
6872 // still VT.
6873 ExpandOp(Op, Lo, Hi);
6874 break;
6875 }
6876 }
6877
6878 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6879 // this X << 1 as X+X.
6880 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman52c51aa2009-01-28 17:46:25 +00006881 if (ShAmt->getAPIntValue() == 1 &&
6882 TLI.isOperationLegalOrCustom(ISD::ADDC, NVT) &&
6883 TLI.isOperationLegalOrCustom(ISD::ADDE, NVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006884 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006885 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6886 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6887 LoOps[1] = LoOps[0];
Dale Johannesen82b5b722009-02-02 22:12:50 +00006888 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006889
6890 HiOps[1] = HiOps[0];
6891 HiOps[2] = Lo.getValue(1);
Dale Johannesen82b5b722009-02-02 22:12:50 +00006892 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006893 break;
6894 }
6895 }
6896
6897 // If we can emit an efficient shift operation, do so now.
Dale Johannesen82b5b722009-02-02 22:12:50 +00006898 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006899 break;
6900
6901 // If this target supports SHL_PARTS, use it.
6902 TargetLowering::LegalizeAction Action =
6903 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6904 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6905 Action == TargetLowering::Custom) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00006906 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0),
6907 ShiftAmt, Lo, Hi, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006908 break;
6909 }
6910
6911 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006912 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006913 break;
6914 }
6915
6916 case ISD::SRA: {
6917 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006918 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006919 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dale Johannesen18ae8af2009-02-06 21:55:48 +00006920 SDValue Op = DAG.getNode(ISD::SRA, dl, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006921 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006922 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006923 // Now that the custom expander is done, expand the result, which is
6924 // still VT.
6925 ExpandOp(Op, Lo, Hi);
6926 break;
6927 }
6928 }
6929
6930 // If we can emit an efficient shift operation, do so now.
Dale Johannesen82b5b722009-02-02 22:12:50 +00006931 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006932 break;
6933
6934 // If this target supports SRA_PARTS, use it.
6935 TargetLowering::LegalizeAction Action =
6936 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6937 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6938 Action == TargetLowering::Custom) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00006939 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0),
6940 ShiftAmt, Lo, Hi, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006941 break;
6942 }
6943
6944 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006945 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006946 break;
6947 }
6948
6949 case ISD::SRL: {
6950 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006951 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006952 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00006953 SDValue Op = DAG.getNode(ISD::SRL, dl, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006954 Op = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006955 if (Op.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006956 // Now that the custom expander is done, expand the result, which is
6957 // still VT.
6958 ExpandOp(Op, Lo, Hi);
6959 break;
6960 }
6961 }
6962
6963 // If we can emit an efficient shift operation, do so now.
Dale Johannesen82b5b722009-02-02 22:12:50 +00006964 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi, dl))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006965 break;
6966
6967 // If this target supports SRL_PARTS, use it.
6968 TargetLowering::LegalizeAction Action =
6969 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6970 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6971 Action == TargetLowering::Custom) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00006972 ExpandShiftParts(ISD::SRL_PARTS,
6973 Node->getOperand(0), ShiftAmt, Lo, Hi, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006974 break;
6975 }
6976
6977 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006978 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006979 break;
6980 }
6981
6982 case ISD::ADD:
6983 case ISD::SUB: {
6984 // If the target wants to custom expand this, let them.
6985 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6986 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006987 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00006988 if (Result.getNode()) {
Duncan Sands4c3885b2008-06-22 09:42:16 +00006989 ExpandOp(Result, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006990 break;
6991 }
6992 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006993 // 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);
Dan Gohman8181bd12008-07-27 21:46:04 +00006997 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006998 LoOps[0] = LHSL;
6999 LoOps[1] = RHSL;
7000 HiOps[0] = LHSH;
7001 HiOps[1] = RHSH;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00007002
Andrew Lenhartha23d6992008-10-07 17:03:15 +00007003 //cascaded check to see if any smaller size has a a carry flag.
7004 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
7005 bool hasCarry = false;
Andrew Lenharth97c315a2008-10-07 18:27:23 +00007006 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
7007 MVT AVT = MVT::getIntegerVT(BitSize);
Dan Gohman52c51aa2009-01-28 17:46:25 +00007008 if (TLI.isOperationLegalOrCustom(OpV, AVT)) {
Andrew Lenharth97c315a2008-10-07 18:27:23 +00007009 hasCarry = true;
7010 break;
7011 }
7012 }
7013
Andrew Lenhartha23d6992008-10-07 17:03:15 +00007014 if(hasCarry) {
Evan Cheng2bdd3d92008-12-12 18:49:09 +00007015 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth5e814462008-10-07 14:15:42 +00007016 if (Node->getOpcode() == ISD::ADD) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00007017 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
Andrew Lenharth5e814462008-10-07 14:15:42 +00007018 HiOps[2] = Lo.getValue(1);
Dale Johannesen82b5b722009-02-02 22:12:50 +00007019 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
Andrew Lenharth5e814462008-10-07 14:15:42 +00007020 } else {
Dale Johannesen82b5b722009-02-02 22:12:50 +00007021 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
Andrew Lenharth5e814462008-10-07 14:15:42 +00007022 HiOps[2] = Lo.getValue(1);
Dale Johannesen82b5b722009-02-02 22:12:50 +00007023 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
Andrew Lenharth5e814462008-10-07 14:15:42 +00007024 }
7025 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007026 } else {
Andrew Lenharth5e814462008-10-07 14:15:42 +00007027 if (Node->getOpcode() == ISD::ADD) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00007028 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps, 2);
7029 Hi = DAG.getNode(ISD::ADD, dl, NVT, HiOps, 2);
7030 SDValue Cmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Andrew Lenhartha23d6992008-10-07 17:03:15 +00007031 Lo, LoOps[0], ISD::SETULT);
Dale Johannesen82b5b722009-02-02 22:12:50 +00007032 SDValue Carry1 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp1,
Andrew Lenharth5e814462008-10-07 14:15:42 +00007033 DAG.getConstant(1, NVT),
7034 DAG.getConstant(0, NVT));
Dale Johannesen82b5b722009-02-02 22:12:50 +00007035 SDValue Cmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Andrew Lenhartha23d6992008-10-07 17:03:15 +00007036 Lo, LoOps[1], ISD::SETULT);
Dale Johannesen82b5b722009-02-02 22:12:50 +00007037 SDValue Carry2 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp2,
Andrew Lenharth5e814462008-10-07 14:15:42 +00007038 DAG.getConstant(1, NVT),
7039 Carry1);
Dale Johannesen82b5b722009-02-02 22:12:50 +00007040 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
Andrew Lenharth5e814462008-10-07 14:15:42 +00007041 } else {
Dale Johannesen82b5b722009-02-02 22:12:50 +00007042 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps, 2);
7043 Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps, 2);
7044 SDValue Cmp = DAG.getSetCC(dl, NVT, LoOps[0], LoOps[1], ISD::SETULT);
7045 SDValue Borrow = DAG.getNode(ISD::SELECT, dl, NVT, Cmp,
Andrew Lenharth5e814462008-10-07 14:15:42 +00007046 DAG.getConstant(1, NVT),
7047 DAG.getConstant(0, NVT));
Dale Johannesen82b5b722009-02-02 22:12:50 +00007048 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
Andrew Lenharth5e814462008-10-07 14:15:42 +00007049 }
7050 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007051 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007052 }
7053
7054 case ISD::ADDC:
7055 case ISD::SUBC: {
7056 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00007057 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007058 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7059 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7060 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00007061 SDValue LoOps[2] = { LHSL, RHSL };
7062 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007063
7064 if (Node->getOpcode() == ISD::ADDC) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00007065 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007066 HiOps[2] = Lo.getValue(1);
Dale Johannesen82b5b722009-02-02 22:12:50 +00007067 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007068 } else {
Dale Johannesen82b5b722009-02-02 22:12:50 +00007069 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007070 HiOps[2] = Lo.getValue(1);
Dale Johannesen82b5b722009-02-02 22:12:50 +00007071 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007072 }
7073 // Remember that we legalized the flag.
7074 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7075 break;
7076 }
7077 case ISD::ADDE:
7078 case ISD::SUBE: {
7079 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00007080 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007081 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7082 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7083 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00007084 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
7085 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007086
Dale Johannesen82b5b722009-02-02 22:12:50 +00007087 Lo = DAG.getNode(Node->getOpcode(), dl, VTList, LoOps, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007088 HiOps[2] = Lo.getValue(1);
Dale Johannesen82b5b722009-02-02 22:12:50 +00007089 Hi = DAG.getNode(Node->getOpcode(), dl, VTList, HiOps, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007090
7091 // Remember that we legalized the flag.
7092 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7093 break;
7094 }
7095 case ISD::MUL: {
7096 // If the target wants to custom expand this, let them.
7097 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007098 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00007099 if (New.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007100 ExpandOp(New, Lo, Hi);
7101 break;
7102 }
7103 }
7104
Dan Gohman52c51aa2009-01-28 17:46:25 +00007105 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, NVT);
7106 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, NVT);
7107 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, NVT);
7108 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00007109 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007110 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007111 ExpandOp(Node->getOperand(0), LL, LH);
7112 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman07961cd2008-02-25 21:11:39 +00007113 unsigned OuterBitSize = Op.getValueSizeInBits();
7114 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman5a199552007-10-08 18:33:35 +00007115 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
7116 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman2594d942008-03-10 20:42:19 +00007117 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
7118 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
7119 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman5a199552007-10-08 18:33:35 +00007120 // The inputs are both zero-extended.
7121 if (HasUMUL_LOHI) {
7122 // We can emit a umul_lohi.
Dale Johannesen82b5b722009-02-02 22:12:50 +00007123 Lo = DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00007124 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00007125 break;
7126 }
7127 if (HasMULHU) {
7128 // We can emit a mulhu+mul.
Dale Johannesen82b5b722009-02-02 22:12:50 +00007129 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
7130 Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
Dan Gohman5a199552007-10-08 18:33:35 +00007131 break;
7132 }
Dan Gohman5a199552007-10-08 18:33:35 +00007133 }
Dan Gohman07961cd2008-02-25 21:11:39 +00007134 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman5a199552007-10-08 18:33:35 +00007135 // The input values are both sign-extended.
7136 if (HasSMUL_LOHI) {
7137 // We can emit a smul_lohi.
Dale Johannesen82b5b722009-02-02 22:12:50 +00007138 Lo = DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00007139 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman5a199552007-10-08 18:33:35 +00007140 break;
7141 }
7142 if (HasMULHS) {
7143 // We can emit a mulhs+mul.
Dale Johannesen82b5b722009-02-02 22:12:50 +00007144 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
7145 Hi = DAG.getNode(ISD::MULHS, dl, NVT, LL, RL);
Dan Gohman5a199552007-10-08 18:33:35 +00007146 break;
7147 }
7148 }
7149 if (HasUMUL_LOHI) {
7150 // Lo,Hi = umul LHS, RHS.
Dale Johannesen82b5b722009-02-02 22:12:50 +00007151 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, dl,
Dan Gohman5a199552007-10-08 18:33:35 +00007152 DAG.getVTList(NVT, NVT), LL, RL);
7153 Lo = UMulLOHI;
7154 Hi = UMulLOHI.getValue(1);
Dale Johannesen82b5b722009-02-02 22:12:50 +00007155 RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
7156 LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
7157 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
7158 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007159 break;
7160 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00007161 if (HasMULHU) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00007162 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
7163 Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
7164 RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
7165 LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
7166 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
7167 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
Dale Johannesen612c88b2007-10-24 22:26:08 +00007168 break;
7169 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007170 }
7171
Dan Gohman5a199552007-10-08 18:33:35 +00007172 // If nothing else, we can make a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007173 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007174 break;
7175 }
7176 case ISD::SDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007177 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007178 break;
7179 case ISD::UDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007180 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007181 break;
7182 case ISD::SREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007183 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007184 break;
7185 case ISD::UREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007186 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007187 break;
7188
7189 case ISD::FADD:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007190 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7191 RTLIB::ADD_F64,
7192 RTLIB::ADD_F80,
7193 RTLIB::ADD_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007194 Node, false, Hi);
7195 break;
7196 case ISD::FSUB:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007197 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7198 RTLIB::SUB_F64,
7199 RTLIB::SUB_F80,
7200 RTLIB::SUB_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007201 Node, false, Hi);
7202 break;
7203 case ISD::FMUL:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007204 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7205 RTLIB::MUL_F64,
7206 RTLIB::MUL_F80,
7207 RTLIB::MUL_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007208 Node, false, Hi);
7209 break;
7210 case ISD::FDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007211 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7212 RTLIB::DIV_F64,
7213 RTLIB::DIV_F80,
7214 RTLIB::DIV_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007215 Node, false, Hi);
7216 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007217 case ISD::FP_EXTEND: {
Dale Johannesen4c14d512007-10-12 01:37:08 +00007218 if (VT == MVT::ppcf128) {
7219 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7220 Node->getOperand(0).getValueType()==MVT::f64);
7221 const uint64_t zero = 0;
7222 if (Node->getOperand(0).getValueType()==MVT::f32)
Dale Johannesen82b5b722009-02-02 22:12:50 +00007223 Hi = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Node->getOperand(0));
Dale Johannesen4c14d512007-10-12 01:37:08 +00007224 else
7225 Hi = Node->getOperand(0);
7226 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7227 break;
7228 }
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007229 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7230 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7231 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007232 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007233 }
7234 case ISD::FP_ROUND: {
7235 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7236 VT);
7237 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7238 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007239 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00007240 }
Evan Cheng5316b392008-09-09 23:02:14 +00007241 case ISD::FSQRT:
7242 case ISD::FSIN:
7243 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007244 case ISD::FLOG:
7245 case ISD::FLOG2:
7246 case ISD::FLOG10:
7247 case ISD::FEXP:
7248 case ISD::FEXP2:
Dan Gohmanb2158232008-08-21 18:38:14 +00007249 case ISD::FTRUNC:
7250 case ISD::FFLOOR:
7251 case ISD::FCEIL:
7252 case ISD::FRINT:
7253 case ISD::FNEARBYINT:
Evan Cheng1fac6952008-09-09 23:35:53 +00007254 case ISD::FPOW:
7255 case ISD::FPOWI: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007256 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
7257 switch(Node->getOpcode()) {
7258 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00007259 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7260 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007261 break;
7262 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00007263 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7264 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007265 break;
7266 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00007267 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7268 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007269 break;
Dale Johannesen92b33082008-09-04 00:47:13 +00007270 case ISD::FLOG:
7271 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7272 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7273 break;
7274 case ISD::FLOG2:
7275 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7276 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7277 break;
7278 case ISD::FLOG10:
7279 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7280 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7281 break;
7282 case ISD::FEXP:
7283 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7284 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7285 break;
7286 case ISD::FEXP2:
7287 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7288 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7289 break;
Dan Gohmanb2158232008-08-21 18:38:14 +00007290 case ISD::FTRUNC:
7291 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7292 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7293 break;
7294 case ISD::FFLOOR:
7295 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7296 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7297 break;
7298 case ISD::FCEIL:
7299 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7300 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7301 break;
7302 case ISD::FRINT:
7303 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7304 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7305 break;
7306 case ISD::FNEARBYINT:
7307 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7308 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7309 break;
Evan Cheng5316b392008-09-09 23:02:14 +00007310 case ISD::FPOW:
7311 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7312 RTLIB::POW_PPCF128);
7313 break;
7314 case ISD::FPOWI:
7315 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7316 RTLIB::POWI_PPCF128);
7317 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007318 default: assert(0 && "Unreachable!");
7319 }
Duncan Sandsf1db7c82008-04-12 17:14:18 +00007320 Lo = ExpandLibCall(LC, Node, false, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007321 break;
7322 }
7323 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007324 if (VT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007325 SDValue Tmp;
Dale Johannesen5707ef82007-10-12 19:02:17 +00007326 ExpandOp(Node->getOperand(0), Lo, Tmp);
Dale Johannesen82b5b722009-02-02 22:12:50 +00007327 Hi = DAG.getNode(ISD::FABS, dl, NVT, Tmp);
Dale Johannesen5707ef82007-10-12 19:02:17 +00007328 // lo = hi==fabs(hi) ? lo : -lo;
Dale Johannesen82b5b722009-02-02 22:12:50 +00007329 Lo = DAG.getNode(ISD::SELECT_CC, dl, NVT, Hi, Tmp,
7330 Lo, DAG.getNode(ISD::FNEG, dl, NVT, Lo),
Dale Johannesen5707ef82007-10-12 19:02:17 +00007331 DAG.getCondCode(ISD::SETEQ));
7332 break;
7333 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007334 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007335 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7336 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
Dale Johannesen82b5b722009-02-02 22:12:50 +00007337 Mask = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask);
7338 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
7339 Lo = DAG.getNode(ISD::AND, dl, NVT, Lo, Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007340 if (getTypeAction(NVT) == Expand)
7341 ExpandOp(Lo, Lo, Hi);
7342 break;
7343 }
7344 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00007345 if (VT == MVT::ppcf128) {
7346 ExpandOp(Node->getOperand(0), Lo, Hi);
Dale Johannesen82b5b722009-02-02 22:12:50 +00007347 Lo = DAG.getNode(ISD::FNEG, dl, MVT::f64, Lo);
7348 Hi = DAG.getNode(ISD::FNEG, dl, MVT::f64, Hi);
Dale Johannesen5707ef82007-10-12 19:02:17 +00007349 break;
7350 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007351 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007352 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7353 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
Dale Johannesen82b5b722009-02-02 22:12:50 +00007354 Mask = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Mask);
7355 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
7356 Lo = DAG.getNode(ISD::XOR, dl, NVT, Lo, Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007357 if (getTypeAction(NVT) == Expand)
7358 ExpandOp(Lo, Lo, Hi);
7359 break;
7360 }
7361 case ISD::FCOPYSIGN: {
7362 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7363 if (getTypeAction(NVT) == Expand)
7364 ExpandOp(Lo, Lo, Hi);
7365 break;
7366 }
7367 case ISD::SINT_TO_FP:
7368 case ISD::UINT_TO_FP: {
7369 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands92c43912008-06-06 12:08:01 +00007370 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007371
7372 // Promote the operand if needed. Do this before checking for
7373 // ppcf128 so conversions of i16 and i8 work.
7374 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007375 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesen6a779c82008-03-18 17:28:38 +00007376 Tmp = isSigned
Dale Johannesen82b5b722009-02-02 22:12:50 +00007377 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Tmp.getValueType(), Tmp,
Dale Johannesen6a779c82008-03-18 17:28:38 +00007378 DAG.getValueType(SrcVT))
Dale Johannesen82b5b722009-02-02 22:12:50 +00007379 : DAG.getZeroExtendInReg(Tmp, dl, SrcVT);
Gabor Greif1c80d112008-08-28 21:40:38 +00007380 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesen6a779c82008-03-18 17:28:38 +00007381 SrcVT = Node->getOperand(0).getValueType();
7382 }
7383
Dan Gohmanec51f642008-03-10 23:03:31 +00007384 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman84d00962008-02-25 21:39:34 +00007385 static const uint64_t zero = 0;
Dale Johannesen4c14d512007-10-12 01:37:08 +00007386 if (isSigned) {
Dale Johannesen82b5b722009-02-02 22:12:50 +00007387 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f64,
Dale Johannesen4c14d512007-10-12 01:37:08 +00007388 Node->getOperand(0)));
7389 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7390 } else {
Dan Gohman84d00962008-02-25 21:39:34 +00007391 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen82b5b722009-02-02 22:12:50 +00007392 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f64,
Dale Johannesen4c14d512007-10-12 01:37:08 +00007393 Node->getOperand(0)));
7394 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
Dale Johannesen82b5b722009-02-02 22:12:50 +00007395 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007396 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen82b5b722009-02-02 22:12:50 +00007397 ExpandOp(DAG.getNode(ISD::SELECT_CC, dl,
7398 MVT::ppcf128, Node->getOperand(0),
Dale Johannesen4c14d512007-10-12 01:37:08 +00007399 DAG.getConstant(0, MVT::i32),
Dale Johannesen82b5b722009-02-02 22:12:50 +00007400 DAG.getNode(ISD::FADD, dl, MVT::ppcf128, Hi,
Dale Johannesen4c14d512007-10-12 01:37:08 +00007401 DAG.getConstantFP(
7402 APFloat(APInt(128, 2, TwoE32)),
7403 MVT::ppcf128)),
7404 Hi,
7405 DAG.getCondCode(ISD::SETLT)),
7406 Lo, Hi);
7407 }
7408 break;
7409 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007410 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7411 // si64->ppcf128 done by libcall, below
Dan Gohman84d00962008-02-25 21:39:34 +00007412 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen82b5b722009-02-02 22:12:50 +00007413 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, dl, MVT::ppcf128,
7414 Node->getOperand(0)), Lo, Hi);
7415 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007416 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
Dale Johannesen82b5b722009-02-02 22:12:50 +00007417 ExpandOp(DAG.getNode(ISD::SELECT_CC, dl, MVT::ppcf128,
7418 Node->getOperand(0),
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007419 DAG.getConstant(0, MVT::i64),
Dale Johannesen82b5b722009-02-02 22:12:50 +00007420 DAG.getNode(ISD::FADD, dl, MVT::ppcf128, Hi,
Dale Johannesen9aec5b22007-10-12 17:52:03 +00007421 DAG.getConstantFP(
7422 APFloat(APInt(128, 2, TwoE64)),
7423 MVT::ppcf128)),
7424 Hi,
7425 DAG.getCondCode(ISD::SETLT)),
7426 Lo, Hi);
7427 break;
7428 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007429
Dan Gohmanec51f642008-03-10 23:03:31 +00007430 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
Dale Johannesen82b5b722009-02-02 22:12:50 +00007431 Node->getOperand(0), dl);
Evan Chenga8740032008-04-01 01:50:16 +00007432 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng4a2f6df2008-04-01 01:51:26 +00007433 // float to i32 etc. can be 'expanded' to a single node.
Evan Chenga8740032008-04-01 01:50:16 +00007434 ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007435 break;
7436 }
7437 }
7438
7439 // Make sure the resultant values have been legalized themselves, unless this
7440 // is a type that requires multi-step expansion.
7441 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7442 Lo = LegalizeOp(Lo);
Gabor Greif1c80d112008-08-28 21:40:38 +00007443 if (Hi.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007444 // Don't legalize the high part if it is expanded to a single node.
7445 Hi = LegalizeOp(Hi);
7446 }
7447
7448 // Remember in a map if the values will be reused later.
Dan Gohman55d19662008-07-07 17:46:23 +00007449 bool isNew =
7450 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007451 assert(isNew && "Value already expanded?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007452 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007453}
7454
7455/// SplitVectorOp - Given an operand of vector type, break it down into
7456/// two smaller values, still of vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00007457void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7458 SDValue &Hi) {
Duncan Sands92c43912008-06-06 12:08:01 +00007459 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007460 SDNode *Node = Op.getNode();
Dale Johannesen352c47e2009-02-02 20:41:04 +00007461 DebugLoc dl = Node->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00007462 unsigned NumElements = Op.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007463 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00007464
Duncan Sands92c43912008-06-06 12:08:01 +00007465 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman4a365ad2007-11-15 21:15:26 +00007466
7467 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7468 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7469
Duncan Sands92c43912008-06-06 12:08:01 +00007470 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7471 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00007472
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007473 // See if we already split it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007474 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007475 = SplitNodes.find(Op);
7476 if (I != SplitNodes.end()) {
7477 Lo = I->second.first;
7478 Hi = I->second.second;
7479 return;
7480 }
7481
7482 switch (Node->getOpcode()) {
7483 default:
7484#ifndef NDEBUG
7485 Node->dump(&DAG);
7486#endif
7487 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00007488 case ISD::UNDEF:
Dale Johannesen9bfc0172009-02-06 23:05:02 +00007489 Lo = DAG.getUNDEF(NewVT_Lo);
7490 Hi = DAG.getUNDEF(NewVT_Hi);
Chris Lattner3dec33a2007-11-19 20:21:32 +00007491 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007492 case ISD::BUILD_PAIR:
7493 Lo = Node->getOperand(0);
7494 Hi = Node->getOperand(1);
7495 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007496 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007497 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7498 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007499 unsigned Index = Idx->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007500 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007501 if (Index < NewNumElts_Lo)
Dale Johannesend8fd5342009-02-02 22:49:46 +00007502 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVT_Lo, Lo, ScalarOp,
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007503 DAG.getIntPtrConstant(Index));
7504 else
Dale Johannesend8fd5342009-02-02 22:49:46 +00007505 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVT_Hi, Hi, ScalarOp,
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007506 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7507 break;
7508 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007509 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007510 Node->getOperand(1),
Dale Johannesen352c47e2009-02-02 20:41:04 +00007511 Node->getOperand(2), dl);
Nate Begeman7c9e4b72008-04-25 18:07:40 +00007512 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohmanb3228dc2007-09-28 23:53:40 +00007513 break;
7514 }
Chris Lattner587c46d2007-11-19 21:16:54 +00007515 case ISD::VECTOR_SHUFFLE: {
7516 // Build the low part.
Dan Gohman8181bd12008-07-27 21:46:04 +00007517 SDValue Mask = Node->getOperand(2);
7518 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +00007519 MVT PtrVT = TLI.getPointerTy();
Chris Lattner587c46d2007-11-19 21:16:54 +00007520
7521 // Insert all of the elements from the input that are needed. We use
7522 // buildvector of extractelement here because the input vectors will have
7523 // to be legalized, so this makes the code simpler.
7524 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007525 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007526 if (IdxNode.getOpcode() == ISD::UNDEF) {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00007527 Ops.push_back(DAG.getUNDEF(NewEltVT));
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007528 continue;
7529 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007530 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007531 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007532 if (Idx >= NumElements) {
7533 InVec = Node->getOperand(1);
7534 Idx -= NumElements;
7535 }
Dale Johannesend8fd5342009-02-02 22:49:46 +00007536 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewEltVT, InVec,
Chris Lattner587c46d2007-11-19 21:16:54 +00007537 DAG.getConstant(Idx, PtrVT)));
7538 }
Dale Johannesend8fd5342009-02-02 22:49:46 +00007539 Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Lo, &Ops[0], Ops.size());
Chris Lattner587c46d2007-11-19 21:16:54 +00007540 Ops.clear();
7541
7542 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007543 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007544 if (IdxNode.getOpcode() == ISD::UNDEF) {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00007545 Ops.push_back(DAG.getUNDEF(NewEltVT));
Nate Begeman8bb3cb32008-03-14 00:53:31 +00007546 continue;
7547 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007548 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00007549 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00007550 if (Idx >= NumElements) {
7551 InVec = Node->getOperand(1);
7552 Idx -= NumElements;
7553 }
Dale Johannesend8fd5342009-02-02 22:49:46 +00007554 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewEltVT, InVec,
Chris Lattner587c46d2007-11-19 21:16:54 +00007555 DAG.getConstant(Idx, PtrVT)));
7556 }
Dale Johannesend8fd5342009-02-02 22:49:46 +00007557 Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner587c46d2007-11-19 21:16:54 +00007558 break;
7559 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007560 case ISD::BUILD_VECTOR: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007561 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00007562 Node->op_begin()+NewNumElts_Lo);
Dale Johannesend8fd5342009-02-02 22:49:46 +00007563 Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007564
Dan Gohman8181bd12008-07-27 21:46:04 +00007565 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007566 Node->op_end());
Dale Johannesend8fd5342009-02-02 22:49:46 +00007567 Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007568 break;
7569 }
7570 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00007571 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007572 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7573 if (NewNumSubvectors == 1) {
7574 Lo = Node->getOperand(0);
7575 Hi = Node->getOperand(1);
7576 } else {
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007577 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7578 Node->op_begin()+NewNumSubvectors);
Dale Johannesend8fd5342009-02-02 22:49:46 +00007579 Lo = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewVT_Lo,
7580 &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007581
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007582 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007583 Node->op_end());
Dale Johannesend8fd5342009-02-02 22:49:46 +00007584 Hi = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewVT_Hi,
7585 &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007586 }
7587 break;
7588 }
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007589 case ISD::EXTRACT_SUBVECTOR: {
7590 SDValue Vec = Op.getOperand(0);
7591 SDValue Idx = Op.getOperand(1);
7592 MVT IdxVT = Idx.getValueType();
7593
Dale Johannesend8fd5342009-02-02 22:49:46 +00007594 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Lo, Vec, Idx);
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007595 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7596 if (CIdx) {
Dale Johannesend8fd5342009-02-02 22:49:46 +00007597 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Hi, Vec,
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007598 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7599 IdxVT));
7600 } else {
Dale Johannesend8fd5342009-02-02 22:49:46 +00007601 Idx = DAG.getNode(ISD::ADD, dl, IdxVT, Idx,
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007602 DAG.getConstant(NewNumElts_Lo, IdxVT));
Dale Johannesend8fd5342009-02-02 22:49:46 +00007603 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT_Hi, Vec, Idx);
Mon P Wangbff5d9c2008-11-10 04:46:22 +00007604 }
7605 break;
7606 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00007607 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007608 SDValue Cond = Node->getOperand(0);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007609
Dan Gohman8181bd12008-07-27 21:46:04 +00007610 SDValue LL, LH, RL, RH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007611 SplitVectorOp(Node->getOperand(1), LL, LH);
7612 SplitVectorOp(Node->getOperand(2), RL, RH);
7613
Duncan Sands92c43912008-06-06 12:08:01 +00007614 if (Cond.getValueType().isVector()) {
Dan Gohmand5d4c872007-10-17 14:48:28 +00007615 // Handle a vector merge.
Dan Gohman8181bd12008-07-27 21:46:04 +00007616 SDValue CL, CH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00007617 SplitVectorOp(Cond, CL, CH);
Dale Johannesend8fd5342009-02-02 22:49:46 +00007618 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, CL, LL, RL);
7619 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007620 } else {
7621 // Handle a simple select with vector operands.
Dale Johannesend8fd5342009-02-02 22:49:46 +00007622 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, Cond, LL, RL);
7623 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00007624 }
7625 break;
7626 }
Chris Lattnerc7471452008-06-30 02:43:01 +00007627 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007628 SDValue CondLHS = Node->getOperand(0);
7629 SDValue CondRHS = Node->getOperand(1);
7630 SDValue CondCode = Node->getOperand(4);
Chris Lattnerc7471452008-06-30 02:43:01 +00007631
Dan Gohman8181bd12008-07-27 21:46:04 +00007632 SDValue LL, LH, RL, RH;
Chris Lattnerc7471452008-06-30 02:43:01 +00007633 SplitVectorOp(Node->getOperand(2), LL, LH);
7634 SplitVectorOp(Node->getOperand(3), RL, RH);
7635
7636 // Handle a simple select with vector operands.
Dale Johannesend8fd5342009-02-02 22:49:46 +00007637 Lo = DAG.getNode(ISD::SELECT_CC, dl, NewVT_Lo, CondLHS, CondRHS,
Chris Lattnerc7471452008-06-30 02:43:01 +00007638 LL, RL, CondCode);
Dale Johannesend8fd5342009-02-02 22:49:46 +00007639 Hi = DAG.getNode(ISD::SELECT_CC, dl, NewVT_Hi, CondLHS, CondRHS,
Chris Lattnerc7471452008-06-30 02:43:01 +00007640 LH, RH, CondCode);
7641 break;
7642 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00007643 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007644 SDValue LL, LH, RL, RH;
Nate Begeman9a1ce152008-05-12 19:40:03 +00007645 SplitVectorOp(Node->getOperand(0), LL, LH);
7646 SplitVectorOp(Node->getOperand(1), RL, RH);
Dale Johannesend8fd5342009-02-02 22:49:46 +00007647 Lo = DAG.getNode(ISD::VSETCC, dl, NewVT_Lo, LL, RL, Node->getOperand(2));
7648 Hi = DAG.getNode(ISD::VSETCC, dl, NewVT_Hi, LH, RH, Node->getOperand(2));
Nate Begeman9a1ce152008-05-12 19:40:03 +00007649 break;
7650 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007651 case ISD::ADD:
7652 case ISD::SUB:
7653 case ISD::MUL:
7654 case ISD::FADD:
7655 case ISD::FSUB:
7656 case ISD::FMUL:
7657 case ISD::SDIV:
7658 case ISD::UDIV:
7659 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007660 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007661 case ISD::AND:
7662 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00007663 case ISD::XOR:
7664 case ISD::UREM:
7665 case ISD::SREM:
Mon P Wang26342922008-12-18 20:03:17 +00007666 case ISD::FREM:
7667 case ISD::SHL:
7668 case ISD::SRA:
7669 case ISD::SRL: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007670 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007671 SplitVectorOp(Node->getOperand(0), LL, LH);
7672 SplitVectorOp(Node->getOperand(1), RL, RH);
7673
Dale Johannesend8fd5342009-02-02 22:49:46 +00007674 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, LL, RL);
7675 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007676 break;
7677 }
Dan Gohman29c3cef2008-08-14 20:04:46 +00007678 case ISD::FP_ROUND:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007679 case ISD::FPOWI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007680 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007681 SplitVectorOp(Node->getOperand(0), L, H);
7682
Dale Johannesend8fd5342009-02-02 22:49:46 +00007683 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, L, Node->getOperand(1));
7684 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00007685 break;
7686 }
7687 case ISD::CTTZ:
7688 case ISD::CTLZ:
7689 case ISD::CTPOP:
7690 case ISD::FNEG:
7691 case ISD::FABS:
7692 case ISD::FSQRT:
7693 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00007694 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007695 case ISD::FLOG:
7696 case ISD::FLOG2:
7697 case ISD::FLOG10:
7698 case ISD::FEXP:
7699 case ISD::FEXP2:
Nate Begeman78246ca2007-11-17 03:58:34 +00007700 case ISD::FP_TO_SINT:
7701 case ISD::FP_TO_UINT:
7702 case ISD::SINT_TO_FP:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007703 case ISD::UINT_TO_FP:
7704 case ISD::TRUNCATE:
7705 case ISD::ANY_EXTEND:
7706 case ISD::SIGN_EXTEND:
7707 case ISD::ZERO_EXTEND:
7708 case ISD::FP_EXTEND: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007709 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00007710 SplitVectorOp(Node->getOperand(0), L, H);
7711
Dale Johannesend8fd5342009-02-02 22:49:46 +00007712 Lo = DAG.getNode(Node->getOpcode(), dl, NewVT_Lo, L);
7713 Hi = DAG.getNode(Node->getOpcode(), dl, NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00007714 break;
7715 }
Mon P Wang73d31542008-11-10 20:54:11 +00007716 case ISD::CONVERT_RNDSAT: {
7717 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7718 SDValue L, H;
7719 SplitVectorOp(Node->getOperand(0), L, H);
7720 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7721 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7722 SDValue STyOpL = DAG.getValueType(L.getValueType());
7723 SDValue STyOpH = DAG.getValueType(H.getValueType());
7724
7725 SDValue RndOp = Node->getOperand(3);
7726 SDValue SatOp = Node->getOperand(4);
7727
Dale Johannesen564036c2009-02-04 00:13:36 +00007728 Lo = DAG.getConvertRndSat(NewVT_Lo, dl, L, DTyOpL, STyOpL,
Mon P Wang73d31542008-11-10 20:54:11 +00007729 RndOp, SatOp, CvtCode);
Dale Johannesen564036c2009-02-04 00:13:36 +00007730 Hi = DAG.getConvertRndSat(NewVT_Hi, dl, H, DTyOpH, STyOpH,
Mon P Wang73d31542008-11-10 20:54:11 +00007731 RndOp, SatOp, CvtCode);
7732 break;
7733 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007734 case ISD::LOAD: {
7735 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007736 SDValue Ch = LD->getChain();
7737 SDValue Ptr = LD->getBasePtr();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007738 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007739 const Value *SV = LD->getSrcValue();
7740 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007741 MVT MemoryVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007742 unsigned Alignment = LD->getAlignment();
7743 bool isVolatile = LD->isVolatile();
7744
Dan Gohman29c3cef2008-08-14 20:04:46 +00007745 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
Dale Johannesen9bfc0172009-02-06 23:05:02 +00007746 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
Dan Gohman29c3cef2008-08-14 20:04:46 +00007747
7748 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7749 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7750 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7751
Dale Johannesend8fd5342009-02-02 22:49:46 +00007752 Lo = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
Dan Gohman29c3cef2008-08-14 20:04:46 +00007753 NewVT_Lo, Ch, Ptr, Offset,
7754 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7755 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Dale Johannesend8fd5342009-02-02 22:49:46 +00007756 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00007757 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007758 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00007759 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesend8fd5342009-02-02 22:49:46 +00007760 Hi = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
Dan Gohman29c3cef2008-08-14 20:04:46 +00007761 NewVT_Hi, Ch, Ptr, Offset,
7762 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007763
7764 // Build a factor node to remember that this load is independent of the
7765 // other one.
Dale Johannesend8fd5342009-02-02 22:49:46 +00007766 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007767 Hi.getValue(1));
7768
7769 // Remember that we legalized the chain.
7770 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
7771 break;
7772 }
7773 case ISD::BIT_CONVERT: {
7774 // We know the result is a vector. The input may be either a vector or a
7775 // scalar value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007776 SDValue InOp = Node->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007777 if (!InOp.getValueType().isVector() ||
7778 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007779 // The input is a scalar or single-element vector.
7780 // Lower to a store/load so that it can be split.
7781 // FIXME: this could be improved probably.
Mon P Wang36b59ac2008-07-15 05:28:34 +00007782 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7783 Op.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00007784 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greif1c80d112008-08-28 21:40:38 +00007785 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007786
Dale Johannesend8fd5342009-02-02 22:49:46 +00007787 SDValue St = DAG.getStore(DAG.getEntryNode(), dl,
Dan Gohman12a9c082008-02-06 22:27:42 +00007788 InOp, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007789 PseudoSourceValue::getFixedStack(FI), 0);
Dale Johannesend8fd5342009-02-02 22:49:46 +00007790 InOp = DAG.getLoad(Op.getValueType(), dl, St, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00007791 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007792 }
7793 // Split the vector and convert each of the pieces now.
7794 SplitVectorOp(InOp, Lo, Hi);
Dale Johannesend8fd5342009-02-02 22:49:46 +00007795 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT_Lo, Lo);
7796 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007797 break;
7798 }
7799 }
7800
7801 // Remember in a map if the values will be reused later.
7802 bool isNew =
7803 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
7804 assert(isNew && "Value already split?!?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007805 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007806}
7807
7808
7809/// ScalarizeVectorOp - Given an operand of single-element vector type
7810/// (e.g. v1f32), convert it into the equivalent operation that returns a
7811/// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +00007812SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00007813 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greif1c80d112008-08-28 21:40:38 +00007814 SDNode *Node = Op.getNode();
Dale Johannesend8fd5342009-02-02 22:49:46 +00007815 DebugLoc dl = Node->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00007816 MVT NewVT = Op.getValueType().getVectorElementType();
7817 assert(Op.getValueType().getVectorNumElements() == 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007818
7819 // See if we already scalarized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007820 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007821 if (I != ScalarizedNodes.end()) return I->second;
7822
Dan Gohman8181bd12008-07-27 21:46:04 +00007823 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007824 switch (Node->getOpcode()) {
7825 default:
7826#ifndef NDEBUG
7827 Node->dump(&DAG); cerr << "\n";
7828#endif
7829 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7830 case ISD::ADD:
7831 case ISD::FADD:
7832 case ISD::SUB:
7833 case ISD::FSUB:
7834 case ISD::MUL:
7835 case ISD::FMUL:
7836 case ISD::SDIV:
7837 case ISD::UDIV:
7838 case ISD::FDIV:
7839 case ISD::SREM:
7840 case ISD::UREM:
7841 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00007842 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007843 case ISD::AND:
7844 case ISD::OR:
7845 case ISD::XOR:
Dale Johannesend8fd5342009-02-02 22:49:46 +00007846 Result = DAG.getNode(Node->getOpcode(), dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007847 NewVT,
7848 ScalarizeVectorOp(Node->getOperand(0)),
7849 ScalarizeVectorOp(Node->getOperand(1)));
7850 break;
7851 case ISD::FNEG:
7852 case ISD::FABS:
7853 case ISD::FSQRT:
7854 case ISD::FSIN:
7855 case ISD::FCOS:
Dale Johannesen92b33082008-09-04 00:47:13 +00007856 case ISD::FLOG:
7857 case ISD::FLOG2:
7858 case ISD::FLOG10:
7859 case ISD::FEXP:
7860 case ISD::FEXP2:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007861 case ISD::FP_TO_SINT:
7862 case ISD::FP_TO_UINT:
7863 case ISD::SINT_TO_FP:
7864 case ISD::UINT_TO_FP:
7865 case ISD::SIGN_EXTEND:
7866 case ISD::ZERO_EXTEND:
7867 case ISD::ANY_EXTEND:
7868 case ISD::TRUNCATE:
7869 case ISD::FP_EXTEND:
Dale Johannesend8fd5342009-02-02 22:49:46 +00007870 Result = DAG.getNode(Node->getOpcode(), dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007871 NewVT,
7872 ScalarizeVectorOp(Node->getOperand(0)));
7873 break;
Mon P Wang73d31542008-11-10 20:54:11 +00007874 case ISD::CONVERT_RNDSAT: {
7875 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
Dale Johannesen564036c2009-02-04 00:13:36 +00007876 Result = DAG.getConvertRndSat(NewVT, dl, Op0,
Mon P Wang73d31542008-11-10 20:54:11 +00007877 DAG.getValueType(NewVT),
7878 DAG.getValueType(Op0.getValueType()),
7879 Node->getOperand(3),
7880 Node->getOperand(4),
7881 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7882 break;
7883 }
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007884 case ISD::FPOWI:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007885 case ISD::FP_ROUND:
Dale Johannesend8fd5342009-02-02 22:49:46 +00007886 Result = DAG.getNode(Node->getOpcode(), dl,
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007887 NewVT,
7888 ScalarizeVectorOp(Node->getOperand(0)),
7889 Node->getOperand(1));
7890 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007891 case ISD::LOAD: {
7892 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007893 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7894 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman29c3cef2008-08-14 20:04:46 +00007895 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007896 const Value *SV = LD->getSrcValue();
7897 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007898 MVT MemoryVT = LD->getMemoryVT();
7899 unsigned Alignment = LD->getAlignment();
7900 bool isVolatile = LD->isVolatile();
7901
7902 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
Dale Johannesen9bfc0172009-02-06 23:05:02 +00007903 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
Dan Gohman29c3cef2008-08-14 20:04:46 +00007904
Dale Johannesend8fd5342009-02-02 22:49:46 +00007905 Result = DAG.getLoad(ISD::UNINDEXED, dl, ExtType,
Dan Gohman29c3cef2008-08-14 20:04:46 +00007906 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7907 MemoryVT.getVectorElementType(),
7908 isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007909
7910 // Remember that we legalized the chain.
7911 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7912 break;
7913 }
7914 case ISD::BUILD_VECTOR:
7915 Result = Node->getOperand(0);
7916 break;
7917 case ISD::INSERT_VECTOR_ELT:
7918 // Returning the inserted scalar element.
7919 Result = Node->getOperand(1);
7920 break;
7921 case ISD::CONCAT_VECTORS:
7922 assert(Node->getOperand(0).getValueType() == NewVT &&
7923 "Concat of non-legal vectors not yet supported!");
7924 Result = Node->getOperand(0);
7925 break;
7926 case ISD::VECTOR_SHUFFLE: {
7927 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007928 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007929 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007930 Result = ScalarizeVectorOp(Node->getOperand(1));
7931 else
7932 Result = ScalarizeVectorOp(Node->getOperand(0));
7933 break;
7934 }
7935 case ISD::EXTRACT_SUBVECTOR:
Dale Johannesend8fd5342009-02-02 22:49:46 +00007936 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT,
7937 Node->getOperand(0), Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007938 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007939 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007940 SDValue Op0 = Op.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007941 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng2cc16e72008-05-16 17:19:05 +00007942 Op0 = ScalarizeVectorOp(Op0);
Dale Johannesend8fd5342009-02-02 22:49:46 +00007943 Result = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007944 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007945 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007946 case ISD::SELECT:
Dale Johannesend8fd5342009-02-02 22:49:46 +00007947 Result = DAG.getNode(ISD::SELECT, dl, NewVT, Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007948 ScalarizeVectorOp(Op.getOperand(1)),
7949 ScalarizeVectorOp(Op.getOperand(2)));
7950 break;
Chris Lattnerc7471452008-06-30 02:43:01 +00007951 case ISD::SELECT_CC:
Dale Johannesend8fd5342009-02-02 22:49:46 +00007952 Result = DAG.getNode(ISD::SELECT_CC, dl, NewVT, Node->getOperand(0),
Chris Lattnerc7471452008-06-30 02:43:01 +00007953 Node->getOperand(1),
7954 ScalarizeVectorOp(Op.getOperand(2)),
7955 ScalarizeVectorOp(Op.getOperand(3)),
7956 Node->getOperand(4));
7957 break;
Nate Begeman78ca4f92008-05-12 23:09:43 +00007958 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007959 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7960 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Dale Johannesend8fd5342009-02-02 22:49:46 +00007961 Result = DAG.getNode(ISD::SETCC, dl,
7962 TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands4a361272009-01-01 15:52:00 +00007963 Op0, Op1, Op.getOperand(2));
Dale Johannesend8fd5342009-02-02 22:49:46 +00007964 Result = DAG.getNode(ISD::SELECT, dl, NewVT, Result,
Nate Begeman78ca4f92008-05-12 23:09:43 +00007965 DAG.getConstant(-1ULL, NewVT),
7966 DAG.getConstant(0ULL, NewVT));
7967 break;
7968 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007969 }
7970
7971 if (TLI.isTypeLegal(NewVT))
7972 Result = LegalizeOp(Result);
7973 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7974 assert(isNew && "Value already scalarized?");
Evan Chengcf576fd2008-11-24 07:09:49 +00007975 isNew = isNew;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007976 return Result;
7977}
7978
7979
Mon P Wang1448aad2008-10-30 08:01:45 +00007980SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7981 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7982 if (I != WidenNodes.end()) return I->second;
7983
7984 MVT VT = Op.getValueType();
7985 assert(VT.isVector() && "Cannot widen non-vector type!");
7986
7987 SDValue Result;
7988 SDNode *Node = Op.getNode();
Dale Johannesend8fd5342009-02-02 22:49:46 +00007989 DebugLoc dl = Node->getDebugLoc();
Mon P Wang1448aad2008-10-30 08:01:45 +00007990 MVT EVT = VT.getVectorElementType();
7991
7992 unsigned NumElts = VT.getVectorNumElements();
7993 unsigned NewNumElts = WidenVT.getVectorNumElements();
7994 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7995 assert(NewNumElts < 17);
7996
7997 // When widen is called, it is assumed that it is more efficient to use a
7998 // wide type. The default action is to widen to operation to a wider legal
7999 // vector type and then do the operation if it is legal by calling LegalizeOp
8000 // again. If there is no vector equivalent, we will unroll the operation, do
8001 // it, and rebuild the vector. If most of the operations are vectorizible to
8002 // the legal type, the resulting code will be more efficient. If this is not
8003 // the case, the resulting code will preform badly as we end up generating
8004 // code to pack/unpack the results. It is the function that calls widen
Mon P Wanga5a239f2008-11-06 05:31:54 +00008005 // that is responsible for seeing this doesn't happen.
Mon P Wang1448aad2008-10-30 08:01:45 +00008006 switch (Node->getOpcode()) {
8007 default:
8008#ifndef NDEBUG
8009 Node->dump(&DAG);
8010#endif
8011 assert(0 && "Unexpected operation in WidenVectorOp!");
8012 break;
8013 case ISD::CopyFromReg:
Mon P Wang257e1c72008-11-15 06:05:52 +00008014 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang1448aad2008-10-30 08:01:45 +00008015 case ISD::Constant:
8016 case ISD::ConstantFP:
8017 // To build a vector of these elements, clients should call BuildVector
8018 // and with each element instead of creating a node with a vector type
8019 assert(0 && "Unexpected operation in WidenVectorOp!");
8020 case ISD::VAARG:
8021 // Variable Arguments with vector types doesn't make any sense to me
8022 assert(0 && "Unexpected operation in WidenVectorOp!");
8023 break;
Mon P Wang257e1c72008-11-15 06:05:52 +00008024 case ISD::UNDEF:
Dale Johannesen9bfc0172009-02-06 23:05:02 +00008025 Result = DAG.getUNDEF(WidenVT);
Mon P Wang257e1c72008-11-15 06:05:52 +00008026 break;
Mon P Wang1448aad2008-10-30 08:01:45 +00008027 case ISD::BUILD_VECTOR: {
8028 // Build a vector with undefined for the new nodes
8029 SDValueVector NewOps(Node->op_begin(), Node->op_end());
8030 for (unsigned i = NumElts; i < NewNumElts; ++i) {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00008031 NewOps.push_back(DAG.getUNDEF(EVT));
Mon P Wang1448aad2008-10-30 08:01:45 +00008032 }
Dale Johannesend8fd5342009-02-02 22:49:46 +00008033 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT,
8034 &NewOps[0], NewOps.size());
Mon P Wang1448aad2008-10-30 08:01:45 +00008035 break;
8036 }
8037 case ISD::INSERT_VECTOR_ELT: {
8038 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
Dale Johannesend8fd5342009-02-02 22:49:46 +00008039 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, WidenVT, Tmp1,
Mon P Wang1448aad2008-10-30 08:01:45 +00008040 Node->getOperand(1), Node->getOperand(2));
8041 break;
8042 }
8043 case ISD::VECTOR_SHUFFLE: {
8044 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8045 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8046 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
8047 // used as permutation array. We build the vector here instead of widening
8048 // because we don't want to legalize and have it turned to something else.
8049 SDValue PermOp = Node->getOperand(2);
8050 SDValueVector NewOps;
8051 MVT PVT = PermOp.getValueType().getVectorElementType();
8052 for (unsigned i = 0; i < NumElts; ++i) {
8053 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
8054 NewOps.push_back(PermOp.getOperand(i));
8055 } else {
8056 unsigned Idx =
Mon P Wangec428ad2008-12-13 08:15:14 +00008057 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
Mon P Wang1448aad2008-10-30 08:01:45 +00008058 if (Idx < NumElts) {
8059 NewOps.push_back(PermOp.getOperand(i));
8060 }
8061 else {
8062 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
8063 PermOp.getOperand(i).getValueType()));
8064 }
8065 }
8066 }
8067 for (unsigned i = NumElts; i < NewNumElts; ++i) {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00008068 NewOps.push_back(DAG.getUNDEF(PVT));
Mon P Wang1448aad2008-10-30 08:01:45 +00008069 }
8070
Dale Johannesend8fd5342009-02-02 22:49:46 +00008071 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR, dl,
Mon P Wang1448aad2008-10-30 08:01:45 +00008072 MVT::getVectorVT(PVT, NewOps.size()),
8073 &NewOps[0], NewOps.size());
8074
Dale Johannesend8fd5342009-02-02 22:49:46 +00008075 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, WidenVT, Tmp1, Tmp2, Tmp3);
Mon P Wang1448aad2008-10-30 08:01:45 +00008076 break;
8077 }
8078 case ISD::LOAD: {
8079 // If the load widen returns true, we can use a single load for the
8080 // vector. Otherwise, it is returning a token factor for multiple
8081 // loads.
8082 SDValue TFOp;
8083 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
8084 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
8085 else
8086 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
8087 break;
8088 }
8089
8090 case ISD::BIT_CONVERT: {
8091 SDValue Tmp1 = Node->getOperand(0);
8092 // Converts between two different types so we need to determine
8093 // the correct widen type for the input operand.
Mon P Wang26342922008-12-18 20:03:17 +00008094 MVT InVT = Tmp1.getValueType();
8095 unsigned WidenSize = WidenVT.getSizeInBits();
8096 if (InVT.isVector()) {
8097 MVT InEltVT = InVT.getVectorElementType();
8098 unsigned InEltSize = InEltVT.getSizeInBits();
8099 assert(WidenSize % InEltSize == 0 &&
8100 "can not widen bit convert that are not multiple of element type");
8101 MVT NewInWidenVT = MVT::getVectorVT(InEltVT, WidenSize / InEltSize);
8102 Tmp1 = WidenVectorOp(Tmp1, NewInWidenVT);
8103 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
Dale Johannesend8fd5342009-02-02 22:49:46 +00008104 Result = DAG.getNode(ISD::BIT_CONVERT, dl, WidenVT, Tmp1);
Mon P Wang26342922008-12-18 20:03:17 +00008105 } else {
8106 // If the result size is a multiple of the input size, widen the input
8107 // and then convert.
8108 unsigned InSize = InVT.getSizeInBits();
8109 assert(WidenSize % InSize == 0 &&
8110 "can not widen bit convert that are not multiple of element type");
8111 unsigned NewNumElts = WidenSize / InSize;
8112 SmallVector<SDValue, 16> Ops(NewNumElts);
Dale Johannesen9bfc0172009-02-06 23:05:02 +00008113 SDValue UndefVal = DAG.getUNDEF(InVT);
Mon P Wang26342922008-12-18 20:03:17 +00008114 Ops[0] = Tmp1;
8115 for (unsigned i = 1; i < NewNumElts; ++i)
8116 Ops[i] = UndefVal;
Mon P Wang1448aad2008-10-30 08:01:45 +00008117
Mon P Wang26342922008-12-18 20:03:17 +00008118 MVT NewInVT = MVT::getVectorVT(InVT, NewNumElts);
Dale Johannesend8fd5342009-02-02 22:49:46 +00008119 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, NewInVT, &Ops[0], NewNumElts);
8120 Result = DAG.getNode(ISD::BIT_CONVERT, dl, WidenVT, Result);
Mon P Wang1448aad2008-10-30 08:01:45 +00008121 }
8122 break;
8123 }
8124
8125 case ISD::SINT_TO_FP:
8126 case ISD::UINT_TO_FP:
8127 case ISD::FP_TO_SINT:
Mon P Wang26342922008-12-18 20:03:17 +00008128 case ISD::FP_TO_UINT:
8129 case ISD::FP_ROUND: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008130 SDValue Tmp1 = Node->getOperand(0);
8131 // Converts between two different types so we need to determine
8132 // the correct widen type for the input operand.
8133 MVT TVT = Tmp1.getValueType();
8134 assert(TVT.isVector() && "can not widen non vector type");
8135 MVT TEVT = TVT.getVectorElementType();
8136 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
8137 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
8138 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
Dale Johannesend8fd5342009-02-02 22:49:46 +00008139 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1);
Mon P Wang1448aad2008-10-30 08:01:45 +00008140 break;
8141 }
8142
8143 case ISD::FP_EXTEND:
8144 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
8145 case ISD::TRUNCATE:
8146 case ISD::SIGN_EXTEND:
8147 case ISD::ZERO_EXTEND:
8148 case ISD::ANY_EXTEND:
Mon P Wang1448aad2008-10-30 08:01:45 +00008149 case ISD::SIGN_EXTEND_INREG:
8150 case ISD::FABS:
8151 case ISD::FNEG:
8152 case ISD::FSQRT:
8153 case ISD::FSIN:
Mon P Wang257e1c72008-11-15 06:05:52 +00008154 case ISD::FCOS:
8155 case ISD::CTPOP:
8156 case ISD::CTTZ:
8157 case ISD::CTLZ: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008158 // Unary op widening
Mon P Wang26342922008-12-18 20:03:17 +00008159 SDValue Tmp1;
Mon P Wang1448aad2008-10-30 08:01:45 +00008160 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8161 assert(Tmp1.getValueType() == WidenVT);
Dale Johannesend8fd5342009-02-02 22:49:46 +00008162 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1);
Mon P Wang1448aad2008-10-30 08:01:45 +00008163 break;
8164 }
Mon P Wang73d31542008-11-10 20:54:11 +00008165 case ISD::CONVERT_RNDSAT: {
8166 SDValue RndOp = Node->getOperand(3);
8167 SDValue SatOp = Node->getOperand(4);
Mon P Wang73d31542008-11-10 20:54:11 +00008168 SDValue SrcOp = Node->getOperand(0);
8169
8170 // Converts between two different types so we need to determine
8171 // the correct widen type for the input operand.
8172 MVT SVT = SrcOp.getValueType();
8173 assert(SVT.isVector() && "can not widen non vector type");
8174 MVT SEVT = SVT.getVectorElementType();
8175 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8176
8177 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8178 assert(SrcOp.getValueType() == WidenVT);
8179 SDValue DTyOp = DAG.getValueType(WidenVT);
8180 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8181 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8182
Dale Johannesen564036c2009-02-04 00:13:36 +00008183 Result = DAG.getConvertRndSat(WidenVT, dl, SrcOp, DTyOp, STyOp,
Mon P Wang73d31542008-11-10 20:54:11 +00008184 RndOp, SatOp, CvtCode);
Mon P Wang73d31542008-11-10 20:54:11 +00008185 break;
8186 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008187 case ISD::FPOW:
8188 case ISD::FPOWI:
8189 case ISD::ADD:
8190 case ISD::SUB:
8191 case ISD::MUL:
8192 case ISD::MULHS:
8193 case ISD::MULHU:
8194 case ISD::AND:
8195 case ISD::OR:
8196 case ISD::XOR:
8197 case ISD::FADD:
8198 case ISD::FSUB:
8199 case ISD::FMUL:
8200 case ISD::SDIV:
8201 case ISD::SREM:
8202 case ISD::FDIV:
8203 case ISD::FREM:
8204 case ISD::FCOPYSIGN:
8205 case ISD::UDIV:
8206 case ISD::UREM:
8207 case ISD::BSWAP: {
8208 // Binary op widening
Mon P Wang1448aad2008-10-30 08:01:45 +00008209 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8210 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8211 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
Dale Johannesend8fd5342009-02-02 22:49:46 +00008212 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, Tmp2);
Mon P Wang1448aad2008-10-30 08:01:45 +00008213 break;
8214 }
8215
8216 case ISD::SHL:
8217 case ISD::SRA:
8218 case ISD::SRL: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008219 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8220 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangd5638262008-12-02 07:35:08 +00008221 SDValue ShOp = Node->getOperand(1);
8222 MVT ShVT = ShOp.getValueType();
8223 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8224 WidenVT.getVectorNumElements());
8225 ShOp = WidenVectorOp(ShOp, NewShVT);
8226 assert(ShOp.getValueType() == NewShVT);
Dale Johannesend8fd5342009-02-02 22:49:46 +00008227 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, ShOp);
Mon P Wang1448aad2008-10-30 08:01:45 +00008228 break;
8229 }
Mon P Wangd5638262008-12-02 07:35:08 +00008230
Mon P Wang1448aad2008-10-30 08:01:45 +00008231 case ISD::EXTRACT_VECTOR_ELT: {
8232 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8233 assert(Tmp1.getValueType() == WidenVT);
Dale Johannesend8fd5342009-02-02 22:49:46 +00008234 Result = DAG.getNode(Node->getOpcode(), dl, EVT, Tmp1, Node->getOperand(1));
Mon P Wang1448aad2008-10-30 08:01:45 +00008235 break;
8236 }
8237 case ISD::CONCAT_VECTORS: {
8238 // We concurrently support only widen on a multiple of the incoming vector.
8239 // We could widen on a multiple of the incoming operand if necessary.
8240 unsigned NumConcat = NewNumElts / NumElts;
8241 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Dale Johannesen9bfc0172009-02-06 23:05:02 +00008242 SDValue UndefVal = DAG.getUNDEF(VT);
Mon P Wang1448aad2008-10-30 08:01:45 +00008243 SmallVector<SDValue, 8> MOps;
8244 MOps.push_back(Op);
8245 for (unsigned i = 1; i != NumConcat; ++i) {
8246 MOps.push_back(UndefVal);
8247 }
Dale Johannesend8fd5342009-02-02 22:49:46 +00008248 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
Mon P Wang1448aad2008-10-30 08:01:45 +00008249 &MOps[0], MOps.size()));
8250 break;
8251 }
8252 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang257e1c72008-11-15 06:05:52 +00008253 SDValue Tmp1 = Node->getOperand(0);
8254 SDValue Idx = Node->getOperand(1);
8255 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8256 if (CIdx && CIdx->getZExtValue() == 0) {
8257 // Since we are access the start of the vector, the incoming
8258 // vector type might be the proper.
8259 MVT Tmp1VT = Tmp1.getValueType();
8260 if (Tmp1VT == WidenVT)
8261 return Tmp1;
8262 else {
8263 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8264 if (Tmp1VTNumElts < NewNumElts)
8265 Result = WidenVectorOp(Tmp1, WidenVT);
8266 else
Dale Johannesend8fd5342009-02-02 22:49:46 +00008267 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, WidenVT, Tmp1, Idx);
Mon P Wang257e1c72008-11-15 06:05:52 +00008268 }
8269 } else if (NewNumElts % NumElts == 0) {
8270 // Widen the extracted subvector.
8271 unsigned NumConcat = NewNumElts / NumElts;
Dale Johannesen9bfc0172009-02-06 23:05:02 +00008272 SDValue UndefVal = DAG.getUNDEF(VT);
Mon P Wang257e1c72008-11-15 06:05:52 +00008273 SmallVector<SDValue, 8> MOps;
8274 MOps.push_back(Op);
8275 for (unsigned i = 1; i != NumConcat; ++i) {
8276 MOps.push_back(UndefVal);
8277 }
Dale Johannesend8fd5342009-02-02 22:49:46 +00008278 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
Mon P Wang257e1c72008-11-15 06:05:52 +00008279 &MOps[0], MOps.size()));
8280 } else {
8281 assert(0 && "can not widen extract subvector");
8282 // This could be implemented using insert and build vector but I would
8283 // like to see when this happens.
8284 }
Mon P Wang1448aad2008-10-30 08:01:45 +00008285 break;
8286 }
8287
8288 case ISD::SELECT: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008289 // Determine new condition widen type and widen
8290 SDValue Cond1 = Node->getOperand(0);
8291 MVT CondVT = Cond1.getValueType();
8292 assert(CondVT.isVector() && "can not widen non vector type");
8293 MVT CondEVT = CondVT.getVectorElementType();
8294 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8295 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8296 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8297
8298 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8299 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8300 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
Dale Johannesend8fd5342009-02-02 22:49:46 +00008301 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang1448aad2008-10-30 08:01:45 +00008302 break;
8303 }
8304
8305 case ISD::SELECT_CC: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008306 // Determine new condition widen type and widen
8307 SDValue Cond1 = Node->getOperand(0);
8308 SDValue Cond2 = Node->getOperand(1);
8309 MVT CondVT = Cond1.getValueType();
8310 assert(CondVT.isVector() && "can not widen non vector type");
8311 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8312 MVT CondEVT = CondVT.getVectorElementType();
8313 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8314 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8315 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8316 assert(Cond1.getValueType() == CondWidenVT &&
8317 Cond2.getValueType() == CondWidenVT && "condition not widen");
8318
8319 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8320 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8321 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8322 "operands not widen");
Dale Johannesend8fd5342009-02-02 22:49:46 +00008323 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Cond1, Cond2, Tmp1,
Mon P Wang1448aad2008-10-30 08:01:45 +00008324 Tmp2, Node->getOperand(4));
Mon P Wang1448aad2008-10-30 08:01:45 +00008325 break;
Mon P Wang42ac14e2008-10-30 18:21:52 +00008326 }
8327 case ISD::VSETCC: {
8328 // Determine widen for the operand
8329 SDValue Tmp1 = Node->getOperand(0);
8330 MVT TmpVT = Tmp1.getValueType();
8331 assert(TmpVT.isVector() && "can not widen non vector type");
8332 MVT TmpEVT = TmpVT.getVectorElementType();
8333 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8334 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8335 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
Dale Johannesend8fd5342009-02-02 22:49:46 +00008336 Result = DAG.getNode(Node->getOpcode(), dl, WidenVT, Tmp1, Tmp2,
Mon P Wang42ac14e2008-10-30 18:21:52 +00008337 Node->getOperand(2));
Mon P Wang1448aad2008-10-30 08:01:45 +00008338 break;
8339 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00008340 case ISD::ATOMIC_CMP_SWAP:
8341 case ISD::ATOMIC_LOAD_ADD:
8342 case ISD::ATOMIC_LOAD_SUB:
8343 case ISD::ATOMIC_LOAD_AND:
8344 case ISD::ATOMIC_LOAD_OR:
8345 case ISD::ATOMIC_LOAD_XOR:
8346 case ISD::ATOMIC_LOAD_NAND:
8347 case ISD::ATOMIC_LOAD_MIN:
8348 case ISD::ATOMIC_LOAD_MAX:
8349 case ISD::ATOMIC_LOAD_UMIN:
8350 case ISD::ATOMIC_LOAD_UMAX:
8351 case ISD::ATOMIC_SWAP: {
Mon P Wang1448aad2008-10-30 08:01:45 +00008352 // For now, we assume that using vectors for these operations don't make
8353 // much sense so we just split it. We return an empty result
8354 SDValue X, Y;
8355 SplitVectorOp(Op, X, Y);
8356 return Result;
8357 break;
8358 }
8359
8360 } // end switch (Node->getOpcode())
8361
8362 assert(Result.getNode() && "Didn't set a result!");
8363 if (Result != Op)
8364 Result = LegalizeOp(Result);
8365
Mon P Wanga5a239f2008-11-06 05:31:54 +00008366 AddWidenedOperand(Op, Result);
Mon P Wang1448aad2008-10-30 08:01:45 +00008367 return Result;
8368}
8369
8370// Utility function to find a legal vector type and its associated element
8371// type from a preferred width and whose vector type must be the same size
8372// as the VVT.
8373// TLI: Target lowering used to determine legal types
8374// Width: Preferred width of element type
8375// VVT: Vector value type whose size we must match.
8376// Returns VecEVT and EVT - the vector type and its associated element type
Dan Gohman0275b132009-01-15 16:43:02 +00008377static void FindWidenVecType(const TargetLowering &TLI, unsigned Width, MVT VVT,
Mon P Wang1448aad2008-10-30 08:01:45 +00008378 MVT& EVT, MVT& VecEVT) {
8379 // We start with the preferred width, make it a power of 2 and see if
8380 // we can find a vector type of that width. If not, we reduce it by
8381 // another power of 2. If we have widen the type, a vector of bytes should
8382 // always be legal.
8383 assert(TLI.isTypeLegal(VVT));
8384 unsigned EWidth = Width + 1;
8385 do {
8386 assert(EWidth > 0);
8387 EWidth = (1 << Log2_32(EWidth-1));
8388 EVT = MVT::getIntegerVT(EWidth);
8389 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8390 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8391 } while (!TLI.isTypeLegal(VecEVT) ||
8392 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8393}
8394
8395SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8396 SDValue Chain,
8397 SDValue BasePtr,
8398 const Value *SV,
8399 int SVOffset,
8400 unsigned Alignment,
8401 bool isVolatile,
8402 unsigned LdWidth,
Dale Johannesend8fd5342009-02-02 22:49:46 +00008403 MVT ResType,
8404 DebugLoc dl) {
Mon P Wang1448aad2008-10-30 08:01:45 +00008405 // We assume that we have good rules to handle loading power of two loads so
8406 // we break down the operations to power of 2 loads. The strategy is to
8407 // load the largest power of 2 that we can easily transform to a legal vector
8408 // and then insert into that vector, and the cast the result into the legal
8409 // vector that we want. This avoids unnecessary stack converts.
8410 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8411 // the load is nonvolatile, we an use a wider load for the value.
8412 // Find a vector length we can load a large chunk
8413 MVT EVT, VecEVT;
8414 unsigned EVTWidth;
8415 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8416 EVTWidth = EVT.getSizeInBits();
8417
Dale Johannesend8fd5342009-02-02 22:49:46 +00008418 SDValue LdOp = DAG.getLoad(EVT, dl, Chain, BasePtr, SV, SVOffset,
Mon P Wang1448aad2008-10-30 08:01:45 +00008419 isVolatile, Alignment);
Dale Johannesend8fd5342009-02-02 22:49:46 +00008420 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecEVT, LdOp);
Mon P Wang1448aad2008-10-30 08:01:45 +00008421 LdChain.push_back(LdOp.getValue(1));
8422
8423 // Check if we can load the element with one instruction
8424 if (LdWidth == EVTWidth) {
Dale Johannesend8fd5342009-02-02 22:49:46 +00008425 return DAG.getNode(ISD::BIT_CONVERT, dl, ResType, VecOp);
Mon P Wang1448aad2008-10-30 08:01:45 +00008426 }
8427
8428 // The vector element order is endianness dependent.
8429 unsigned Idx = 1;
8430 LdWidth -= EVTWidth;
8431 unsigned Offset = 0;
8432
8433 while (LdWidth > 0) {
8434 unsigned Increment = EVTWidth / 8;
8435 Offset += Increment;
Dale Johannesend8fd5342009-02-02 22:49:46 +00008436 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Mon P Wang1448aad2008-10-30 08:01:45 +00008437 DAG.getIntPtrConstant(Increment));
8438
8439 if (LdWidth < EVTWidth) {
8440 // Our current type we are using is too large, use a smaller size by
8441 // using a smaller power of 2
8442 unsigned oEVTWidth = EVTWidth;
8443 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8444 EVTWidth = EVT.getSizeInBits();
8445 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008446 Idx = Idx * (oEVTWidth/EVTWidth);
Dale Johannesend8fd5342009-02-02 22:49:46 +00008447 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, VecOp);
Mon P Wang1448aad2008-10-30 08:01:45 +00008448 }
8449
Dale Johannesend8fd5342009-02-02 22:49:46 +00008450 SDValue LdOp = DAG.getLoad(EVT, dl, Chain, BasePtr, SV,
Mon P Wang1448aad2008-10-30 08:01:45 +00008451 SVOffset+Offset, isVolatile,
8452 MinAlign(Alignment, Offset));
8453 LdChain.push_back(LdOp.getValue(1));
Dale Johannesend8fd5342009-02-02 22:49:46 +00008454 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecEVT, VecOp, LdOp,
Mon P Wang1448aad2008-10-30 08:01:45 +00008455 DAG.getIntPtrConstant(Idx++));
8456
8457 LdWidth -= EVTWidth;
8458 }
8459
Dale Johannesend8fd5342009-02-02 22:49:46 +00008460 return DAG.getNode(ISD::BIT_CONVERT, dl, ResType, VecOp);
Mon P Wang1448aad2008-10-30 08:01:45 +00008461}
8462
8463bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8464 SDValue& TFOp,
8465 SDValue Op,
8466 MVT NVT) {
8467 // TODO: Add support for ConcatVec and the ability to load many vector
8468 // types (e.g., v4i8). This will not work when a vector register
8469 // to memory mapping is strange (e.g., vector elements are not
8470 // stored in some sequential order).
8471
8472 // It must be true that the widen vector type is bigger than where
8473 // we need to load from.
8474 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8475 MVT LdVT = LD->getMemoryVT();
Dale Johannesend8fd5342009-02-02 22:49:46 +00008476 DebugLoc dl = LD->getDebugLoc();
Mon P Wang1448aad2008-10-30 08:01:45 +00008477 assert(LdVT.isVector() && NVT.isVector());
8478 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8479
8480 // Load information
8481 SDValue Chain = LD->getChain();
8482 SDValue BasePtr = LD->getBasePtr();
8483 int SVOffset = LD->getSrcValueOffset();
8484 unsigned Alignment = LD->getAlignment();
8485 bool isVolatile = LD->isVolatile();
8486 const Value *SV = LD->getSrcValue();
8487 unsigned int LdWidth = LdVT.getSizeInBits();
8488
8489 // Load value as a large register
8490 SDValueVector LdChain;
8491 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
Dale Johannesend8fd5342009-02-02 22:49:46 +00008492 Alignment, isVolatile, LdWidth, NVT, dl);
Mon P Wang1448aad2008-10-30 08:01:45 +00008493
8494 if (LdChain.size() == 1) {
8495 TFOp = LdChain[0];
8496 return true;
8497 }
8498 else {
Dale Johannesend8fd5342009-02-02 22:49:46 +00008499 TFOp=DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8500 &LdChain[0], LdChain.size());
Mon P Wang1448aad2008-10-30 08:01:45 +00008501 return false;
8502 }
8503}
8504
8505
8506void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8507 SDValue Chain,
8508 SDValue BasePtr,
8509 const Value *SV,
8510 int SVOffset,
8511 unsigned Alignment,
8512 bool isVolatile,
Mon P Wang257e1c72008-11-15 06:05:52 +00008513 SDValue ValOp,
Dale Johannesend8fd5342009-02-02 22:49:46 +00008514 unsigned StWidth,
8515 DebugLoc dl) {
Mon P Wang1448aad2008-10-30 08:01:45 +00008516 // Breaks the stores into a series of power of 2 width stores. For any
8517 // width, we convert the vector to the vector of element size that we
8518 // want to store. This avoids requiring a stack convert.
8519
8520 // Find a width of the element type we can store with
8521 MVT VVT = ValOp.getValueType();
8522 MVT EVT, VecEVT;
8523 unsigned EVTWidth;
8524 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8525 EVTWidth = EVT.getSizeInBits();
8526
Dale Johannesend8fd5342009-02-02 22:49:46 +00008527 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, ValOp);
8528 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT, VecOp,
Mon P Wang927daf52008-11-06 22:52:21 +00008529 DAG.getIntPtrConstant(0));
Dale Johannesend8fd5342009-02-02 22:49:46 +00008530 SDValue StOp = DAG.getStore(Chain, dl, EOp, BasePtr, SV, SVOffset,
Mon P Wang1448aad2008-10-30 08:01:45 +00008531 isVolatile, Alignment);
8532 StChain.push_back(StOp);
8533
8534 // Check if we are done
8535 if (StWidth == EVTWidth) {
8536 return;
8537 }
8538
8539 unsigned Idx = 1;
8540 StWidth -= EVTWidth;
8541 unsigned Offset = 0;
8542
8543 while (StWidth > 0) {
8544 unsigned Increment = EVTWidth / 8;
8545 Offset += Increment;
Dale Johannesend8fd5342009-02-02 22:49:46 +00008546 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
Mon P Wang1448aad2008-10-30 08:01:45 +00008547 DAG.getIntPtrConstant(Increment));
8548
8549 if (StWidth < EVTWidth) {
8550 // Our current type we are using is too large, use a smaller size by
8551 // using a smaller power of 2
8552 unsigned oEVTWidth = EVTWidth;
8553 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8554 EVTWidth = EVT.getSizeInBits();
8555 // Readjust position and vector position based on new load type
Mon P Wang257e1c72008-11-15 06:05:52 +00008556 Idx = Idx * (oEVTWidth/EVTWidth);
Dale Johannesend8fd5342009-02-02 22:49:46 +00008557 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, VecEVT, VecOp);
Mon P Wang1448aad2008-10-30 08:01:45 +00008558 }
8559
Dale Johannesend8fd5342009-02-02 22:49:46 +00008560 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT, VecOp,
Mon P Wang257e1c72008-11-15 06:05:52 +00008561 DAG.getIntPtrConstant(Idx++));
Dale Johannesend8fd5342009-02-02 22:49:46 +00008562 StChain.push_back(DAG.getStore(Chain, dl, EOp, BasePtr, SV,
Mon P Wang1448aad2008-10-30 08:01:45 +00008563 SVOffset + Offset, isVolatile,
8564 MinAlign(Alignment, Offset)));
8565 StWidth -= EVTWidth;
8566 }
8567}
8568
8569
8570SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8571 SDValue Chain,
8572 SDValue BasePtr) {
8573 // TODO: It might be cleaner if we can use SplitVector and have more legal
8574 // vector types that can be stored into memory (e.g., v4xi8 can
8575 // be stored as a word). This will not work when a vector register
8576 // to memory mapping is strange (e.g., vector elements are not
8577 // stored in some sequential order).
8578
8579 MVT StVT = ST->getMemoryVT();
8580 SDValue ValOp = ST->getValue();
Dale Johannesend8fd5342009-02-02 22:49:46 +00008581 DebugLoc dl = ST->getDebugLoc();
Mon P Wang1448aad2008-10-30 08:01:45 +00008582
8583 // Check if we have widen this node with another value
8584 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8585 if (I != WidenNodes.end())
8586 ValOp = I->second;
8587
8588 MVT VVT = ValOp.getValueType();
8589
8590 // It must be true that we the widen vector type is bigger than where
8591 // we need to store.
8592 assert(StVT.isVector() && VVT.isVector());
Dan Gohman783a32c2009-01-28 03:10:52 +00008593 assert(StVT.bitsLT(VVT));
Mon P Wang1448aad2008-10-30 08:01:45 +00008594 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8595
8596 // Store value
8597 SDValueVector StChain;
8598 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8599 ST->getSrcValueOffset(), ST->getAlignment(),
Dale Johannesend8fd5342009-02-02 22:49:46 +00008600 ST->isVolatile(), ValOp, StVT.getSizeInBits(), dl);
Mon P Wang1448aad2008-10-30 08:01:45 +00008601 if (StChain.size() == 1)
8602 return StChain[0];
8603 else
Dale Johannesend8fd5342009-02-02 22:49:46 +00008604 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8605 &StChain[0], StChain.size());
Mon P Wang1448aad2008-10-30 08:01:45 +00008606}
8607
8608
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008609// SelectionDAG::Legalize - This is the entry point for the file.
8610//
Duncan Sandse016a2e2008-12-14 09:43:15 +00008611void SelectionDAG::Legalize(bool TypesNeedLegalizing) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008612 /// run - This is the main entry point to this class.
8613 ///
Duncan Sandse016a2e2008-12-14 09:43:15 +00008614 SelectionDAGLegalize(*this, TypesNeedLegalizing).LegalizeDAG();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008615}
8616